2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2020-07-19 13:49:44 -04:00
|
|
|
const { Listener, Conn } = window.__bootstrap.net;
|
2022-10-25 16:50:55 -04:00
|
|
|
const { TypeError } = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
function opStartTls(args) {
|
2021-10-30 12:51:42 -04:00
|
|
|
return core.opAsync("op_tls_start", args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-10-26 16:27:47 -04:00
|
|
|
function opTlsHandshake(rid) {
|
|
|
|
return core.opAsync("op_tls_handshake", rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
class TlsConn extends Conn {
|
|
|
|
handshake() {
|
|
|
|
return opTlsHandshake(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
async function connectTls({
|
|
|
|
port,
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
transport = "tcp",
|
|
|
|
certFile = undefined,
|
2021-09-30 03:26:15 -04:00
|
|
|
caCerts = [],
|
2021-08-09 09:55:00 -04:00
|
|
|
certChain = undefined,
|
|
|
|
privateKey = undefined,
|
2021-11-26 13:59:53 -05:00
|
|
|
alpnProtocols = undefined,
|
2020-07-19 13:49:44 -04:00
|
|
|
}) {
|
2022-10-25 16:50:55 -04:00
|
|
|
if (transport !== "tcp") {
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
|
|
|
}
|
|
|
|
const [rid, localAddr, 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);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-10-26 16:27:47 -04:00
|
|
|
class TlsListener extends Listener {
|
2020-07-19 13:49:44 -04:00
|
|
|
async accept() {
|
2022-10-25 16:50:55 -04:00
|
|
|
const [rid, localAddr, remoteAddr] = await core.opAsync(
|
|
|
|
"op_net_accept_tls",
|
|
|
|
this.rid,
|
|
|
|
);
|
|
|
|
localAddr.transport = "tcp";
|
|
|
|
remoteAddr.transport = "tcp";
|
|
|
|
return new TlsConn(rid, remoteAddr, localAddr);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function listenTls({
|
|
|
|
port,
|
2022-02-23 23:16:56 -05:00
|
|
|
cert,
|
2020-07-19 13:49:44 -04:00
|
|
|
certFile,
|
2022-02-23 23:16:56 -05:00
|
|
|
key,
|
2020-07-19 13:49:44 -04:00
|
|
|
keyFile,
|
|
|
|
hostname = "0.0.0.0",
|
|
|
|
transport = "tcp",
|
2021-11-26 13:59:53 -05:00
|
|
|
alpnProtocols = undefined,
|
2020-07-19 13:49:44 -04:00
|
|
|
}) {
|
2022-10-25 16:50:55 -04:00
|
|
|
if (transport !== "tcp") {
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
|
|
|
}
|
|
|
|
const [rid, localAddr] = ops.op_net_listen_tls(
|
|
|
|
{ hostname, port },
|
|
|
|
{ cert, certFile, key, keyFile, alpnProtocols },
|
|
|
|
);
|
|
|
|
return new TlsListener(rid, localAddr);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function startTls(
|
|
|
|
conn,
|
2021-11-26 13:59:53 -05:00
|
|
|
{
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
certFile = undefined,
|
|
|
|
caCerts = [],
|
|
|
|
alpnProtocols = undefined,
|
|
|
|
} = {},
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2022-10-25 16:50:55 -04:00
|
|
|
const [rid, localAddr, remoteAddr] = await opStartTls({
|
2020-07-19 13:49:44 -04:00
|
|
|
rid: conn.rid,
|
|
|
|
hostname,
|
|
|
|
certFile,
|
2021-09-30 03:26:15 -04:00
|
|
|
caCerts,
|
2021-11-26 13:59:53 -05:00
|
|
|
alpnProtocols,
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
2022-10-25 16:50:55 -04:00
|
|
|
return new TlsConn(rid, remoteAddr, localAddr);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.tls = {
|
|
|
|
startTls,
|
|
|
|
listenTls,
|
|
|
|
connectTls,
|
2021-10-26 16:27:47 -04:00
|
|
|
TlsConn,
|
|
|
|
TlsListener,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|