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