1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00
denoland-deno/cli/js/tls.ts

77 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as tlsOps from "./ops/tls.ts";
import { Listener, Conn, ConnImpl, ListenerImpl } from "./net.ts";
// TODO(ry) There are many configuration options to add...
// https://docs.rs/rustls/0.16.0/rustls/struct.ClientConfig.html
interface ConnectTlsOptions {
transport?: "tcp";
port: number;
hostname?: string;
2019-10-21 14:38:28 -04:00
certFile?: string;
}
export async function connectTls({
port,
hostname = "127.0.0.1",
transport = "tcp",
certFile = undefined,
}: ConnectTlsOptions): Promise<Conn> {
const res = await tlsOps.connectTls({
port,
hostname,
transport,
certFile,
});
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> {
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 {
2019-10-21 14:38:28 -04:00
port: number;
hostname?: string;
transport?: "tcp";
2019-10-21 14:38:28 -04:00
certFile: string;
keyFile: string;
}
export function listenTls({
port,
certFile,
keyFile,
hostname = "0.0.0.0",
transport = "tcp",
}: ListenTlsOptions): Listener {
const res = tlsOps.listenTls({
port,
certFile,
keyFile,
2019-10-21 14:38:28 -04:00
hostname,
transport,
2019-10-21 14:38:28 -04:00
});
return new TLSListenerImpl(res.rid, res.localAddr);
2019-10-21 14:38:28 -04:00
}
2020-04-18 11:21:20 -04:00
interface StartTlsOptions {
2020-04-18 11:21:20 -04:00
hostname?: string;
certFile?: string;
}
export async function startTls(
2020-04-18 11:21:20 -04:00
conn: Conn,
{ hostname = "127.0.0.1", certFile = undefined }: StartTlsOptions = {}
2020-04-18 11:21:20 -04:00
): Promise<Conn> {
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!);
}