2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const ops = core.ops;
|
2023-03-08 06:44:54 -05:00
|
|
|
import { Conn, Listener } from "ext:deno_net/01_net.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const { TypeError } = primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function opStartTls(args) {
|
|
|
|
return core.opAsync("op_tls_start", args);
|
|
|
|
}
|
|
|
|
|
|
|
|
function opTlsHandshake(rid) {
|
|
|
|
return core.opAsync("op_tls_handshake", rid);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class TlsConn extends Conn {
|
|
|
|
handshake() {
|
|
|
|
return opTlsHandshake(this.rid);
|
2021-10-26 16:27:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-10-26 16:27:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function connectTls({
|
|
|
|
port,
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
transport = "tcp",
|
|
|
|
certFile = undefined,
|
|
|
|
caCerts = [],
|
|
|
|
certChain = undefined,
|
|
|
|
privateKey = undefined,
|
|
|
|
alpnProtocols = undefined,
|
|
|
|
}) {
|
|
|
|
if (transport !== "tcp") {
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
2021-10-26 16:27:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await core.opAsync(
|
|
|
|
"op_net_connect_tls",
|
|
|
|
{ hostname, port },
|
|
|
|
{ certFile, caCerts, certChain, privateKey, alpnProtocols },
|
|
|
|
);
|
|
|
|
localAddr.transport = "tcp";
|
|
|
|
remoteAddr.transport = "tcp";
|
|
|
|
return new TlsConn(rid, remoteAddr, localAddr);
|
|
|
|
}
|
2021-10-26 16:27:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class TlsListener extends Listener {
|
|
|
|
async accept() {
|
2023-01-16 11:17:18 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await core.opAsync(
|
2023-02-07 14:22:46 -05:00
|
|
|
"op_net_accept_tls",
|
|
|
|
this.rid,
|
2022-10-25 16:50:55 -04:00
|
|
|
);
|
|
|
|
localAddr.transport = "tcp";
|
|
|
|
remoteAddr.transport = "tcp";
|
|
|
|
return new TlsConn(rid, remoteAddr, localAddr);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function listenTls({
|
|
|
|
port,
|
|
|
|
cert,
|
|
|
|
certFile,
|
|
|
|
key,
|
|
|
|
keyFile,
|
|
|
|
hostname = "0.0.0.0",
|
|
|
|
transport = "tcp",
|
|
|
|
alpnProtocols = undefined,
|
|
|
|
reusePort = false,
|
|
|
|
}) {
|
|
|
|
if (transport !== "tcp") {
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const { 0: rid, 1: localAddr } = ops.op_net_listen_tls(
|
|
|
|
{ hostname, port },
|
|
|
|
{ cert, certFile, key, keyFile, alpnProtocols, reusePort },
|
|
|
|
);
|
|
|
|
return new TlsListener(rid, localAddr);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function startTls(
|
|
|
|
conn,
|
|
|
|
{
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
certFile = undefined,
|
|
|
|
caCerts = [],
|
2021-11-26 13:59:53 -05:00
|
|
|
alpnProtocols = undefined,
|
2023-02-07 14:22:46 -05:00
|
|
|
} = {},
|
|
|
|
) {
|
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await opStartTls({
|
|
|
|
rid: conn.rid,
|
|
|
|
hostname,
|
|
|
|
certFile,
|
|
|
|
caCerts,
|
|
|
|
alpnProtocols,
|
|
|
|
});
|
|
|
|
return new TlsConn(rid, remoteAddr, localAddr);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { connectTls, listenTls, startTls, TlsConn, TlsListener };
|