2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-03-09 19:22:15 -04:00
|
|
|
import * as tlsOps from "./ops/tls.ts";
|
2020-03-23 18:02:51 -04:00
|
|
|
import { Listener, Conn, ConnImpl, ListenerImpl } from "./net.ts";
|
2019-09-23 14:40:38 -04:00
|
|
|
|
|
|
|
// TODO(ry) There are many configuration options to add...
|
|
|
|
// https://docs.rs/rustls/0.16.0/rustls/struct.ClientConfig.html
|
2020-04-24 17:29:14 -04:00
|
|
|
interface ConnectTlsOptions {
|
2020-03-23 18:02:51 -04:00
|
|
|
transport?: "tcp";
|
2019-09-23 14:40:38 -04:00
|
|
|
port: number;
|
|
|
|
hostname?: string;
|
2019-10-21 14:38:28 -04:00
|
|
|
certFile?: string;
|
2019-09-23 14:40:38 -04:00
|
|
|
}
|
|
|
|
|
2020-04-24 17:29:14 -04:00
|
|
|
export async function connectTls({
|
2020-03-10 12:08:58 -04:00
|
|
|
port,
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
transport = "tcp",
|
2020-03-28 13:03:49 -04:00
|
|
|
certFile = undefined,
|
2020-04-24 17:29:14 -04:00
|
|
|
}: ConnectTlsOptions): Promise<Conn> {
|
|
|
|
const res = await tlsOps.connectTls({
|
2020-03-10 12:08:58 -04:00
|
|
|
port,
|
|
|
|
hostname,
|
|
|
|
transport,
|
2020-03-28 13:03:49 -04:00
|
|
|
certFile,
|
2020-03-10 12:08:58 -04:00
|
|
|
});
|
2019-09-23 14:40:38 -04:00
|
|
|
return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!);
|
|
|
|
}
|
2019-10-21 14:38:28 -04:00
|
|
|
|
|
|
|
class TLSListenerImpl extends ListenerImpl {
|
|
|
|
async accept(): Promise<Conn> {
|
2020-03-09 19:22:15 -04:00
|
|
|
const res = await tlsOps.acceptTLS(this.rid);
|
2019-10-21 14:38:28 -04:00
|
|
|
return new ConnImpl(res.rid, res.remoteAddr, res.localAddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:29:14 -04:00
|
|
|
export interface ListenTlsOptions {
|
2019-10-21 14:38:28 -04:00
|
|
|
port: number;
|
|
|
|
hostname?: string;
|
2020-03-23 18:02:51 -04:00
|
|
|
transport?: "tcp";
|
2019-10-21 14:38:28 -04:00
|
|
|
certFile: string;
|
|
|
|
keyFile: string;
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:29:14 -04:00
|
|
|
export function listenTls({
|
2020-03-10 12:08:58 -04:00
|
|
|
port,
|
|
|
|
certFile,
|
|
|
|
keyFile,
|
|
|
|
hostname = "0.0.0.0",
|
2020-03-28 13:03:49 -04:00
|
|
|
transport = "tcp",
|
2020-04-24 17:29:14 -04:00
|
|
|
}: ListenTlsOptions): Listener {
|
|
|
|
const res = tlsOps.listenTls({
|
2020-03-10 12:08:58 -04:00
|
|
|
port,
|
|
|
|
certFile,
|
|
|
|
keyFile,
|
2019-10-21 14:38:28 -04:00
|
|
|
hostname,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport,
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
2020-01-18 15:49:55 -05:00
|
|
|
return new TLSListenerImpl(res.rid, res.localAddr);
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
2020-04-18 11:21:20 -04:00
|
|
|
|
2020-04-24 17:29:14 -04:00
|
|
|
interface StartTlsOptions {
|
2020-04-18 11:21:20 -04:00
|
|
|
hostname?: string;
|
|
|
|
certFile?: string;
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:29:14 -04:00
|
|
|
export async function startTls(
|
2020-04-18 11:21:20 -04:00
|
|
|
conn: Conn,
|
2020-07-06 21:45:39 -04:00
|
|
|
{ hostname = "127.0.0.1", certFile }: StartTlsOptions = {}
|
2020-04-18 11:21:20 -04:00
|
|
|
): Promise<Conn> {
|
2020-04-24 17:29:14 -04:00
|
|
|
const res = await tlsOps.startTls({
|
2020-04-18 11:21:20 -04:00
|
|
|
rid: conn.rid,
|
|
|
|
hostname,
|
|
|
|
certFile,
|
|
|
|
});
|
|
|
|
return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!);
|
|
|
|
}
|