2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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-04-12 15:55:05 -04:00
|
|
|
return core.opAsync("op_connect_tls", args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opAcceptTLS(rid) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opAsync("op_accept_tls", rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opListenTls(args) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_listen_tls", args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opStartTls(args) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opAsync("op_start_tls", 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,
|
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,
|
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,
|
|
|
|
certFile,
|
|
|
|
keyFile,
|
|
|
|
hostname = "0.0.0.0",
|
|
|
|
transport = "tcp",
|
2021-04-10 16:04:44 -04:00
|
|
|
alpnProtocols,
|
2020-07-19 13:49:44 -04:00
|
|
|
}) {
|
|
|
|
const res = opListenTls({
|
|
|
|
port,
|
|
|
|
certFile,
|
|
|
|
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-09-30 03:26:15 -04:00
|
|
|
{ hostname = "127.0.0.1", certFile = undefined, caCerts = [] } = {},
|
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,
|
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);
|