2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
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-01-18 12:35:12 -05: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-03-10 12:08:58 -04:00
|
|
|
export async function connectTLS({
|
|
|
|
port,
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
transport = "tcp",
|
2020-03-28 13:03:49 -04:00
|
|
|
certFile = undefined,
|
2020-03-10 12:08:58 -04:00
|
|
|
}: ConnectTLSOptions): Promise<Conn> {
|
|
|
|
const res = await tlsOps.connectTLS({
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ListenTLSOptions {
|
|
|
|
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-03-10 12:08:58 -04:00
|
|
|
export function listenTLS({
|
|
|
|
port,
|
|
|
|
certFile,
|
|
|
|
keyFile,
|
|
|
|
hostname = "0.0.0.0",
|
2020-03-28 13:03:49 -04:00
|
|
|
transport = "tcp",
|
2020-03-10 12:08:58 -04:00
|
|
|
}: ListenTLSOptions): Listener {
|
2020-03-09 19:22:15 -04:00
|
|
|
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
|
|
|
}
|