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;
|
2020-07-19 13:49:44 -04:00
|
|
|
const { Listener, Conn } = window.__bootstrap.net;
|
|
|
|
|
|
|
|
function opConnectTls(
|
|
|
|
args,
|
|
|
|
) {
|
2021-10-30 12:51:42 -04:00
|
|
|
return core.opAsync("op_tls_connect", args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opAcceptTLS(rid) {
|
2021-10-30 12:51:42 -04:00
|
|
|
return core.opAsync("op_tls_accept", rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opListenTls(args) {
|
2021-10-30 12:51:42 -04:00
|
|
|
return core.opSync("op_tls_listen", args);
|
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
|
|
|
}) {
|
|
|
|
const res = await opConnectTls({
|
|
|
|
port,
|
|
|
|
hostname,
|
|
|
|
transport,
|
|
|
|
certFile,
|
2021-09-30 03:26:15 -04:00
|
|
|
caCerts,
|
2021-08-09 09:55:00 -04:00
|
|
|
certChain,
|
|
|
|
privateKey,
|
2021-11-26 13:59:53 -05:00
|
|
|
alpnProtocols,
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
2021-10-26 16:27:47 -04:00
|
|
|
return new TlsConn(res.rid, res.remoteAddr, res.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() {
|
|
|
|
const res = await opAcceptTLS(this.rid);
|
2021-10-26 16:27:47 -04:00
|
|
|
return new TlsConn(res.rid, res.remoteAddr, res.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
|
|
|
}) {
|
|
|
|
const res = opListenTls({
|
|
|
|
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,
|
|
|
|
transport,
|
2021-04-10 16:04:44 -04:00
|
|
|
alpnProtocols,
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
2021-10-26 16:27:47 -04:00
|
|
|
return new TlsListener(res.rid, res.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
|
|
|
) {
|
|
|
|
const res = await opStartTls({
|
|
|
|
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
|
|
|
});
|
2021-10-26 16:27:47 -04:00
|
|
|
return new TlsConn(res.rid, res.remoteAddr, res.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);
|