1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-30 11:16:38 -05:00
denoland-deno/cli/js/tls.ts

64 lines
2.1 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.
2019-10-21 14:38:28 -04:00
import { sendAsync, sendSync } from "./dispatch_json.ts";
import { Listener, Transport, 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?: Transport;
port: number;
hostname?: string;
2019-10-21 14:38:28 -04:00
certFile?: string;
}
const connectTLSDefaults = { hostname: "127.0.0.1", transport: "tcp" };
/**
* Establishes a secure connection over TLS (transport layer security).
*/
export async function connectTLS(options: ConnectTLSOptions): Promise<Conn> {
options = Object.assign(connectTLSDefaults, options);
2020-02-25 09:14:27 -05:00
const res = await sendAsync("op_connect_tls", options);
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-02-25 09:14:27 -05:00
const res = await sendAsync("op_accept_tls", { rid: 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;
transport?: Transport;
certFile: string;
keyFile: string;
}
/** Listen announces on the local transport address over TLS (transport layer security).
*
* @param options
* @param options.port The port to connect to. (Required.)
* @param options.hostname A literal IP address or host name that can be
* resolved to an IP address. If not specified, defaults to 0.0.0.0
* @param options.certFile Server certificate file
* @param options.keyFile Server public key file
*
* Examples:
*
* Deno.listenTLS({ port: 443, certFile: "./my_server.crt", keyFile: "./my_server.key" })
*/
export function listenTLS(options: ListenTLSOptions): Listener {
const hostname = options.hostname || "0.0.0.0";
const transport = options.transport || "tcp";
2020-02-25 09:14:27 -05:00
const res = sendSync("op_listen_tls", {
2019-10-21 14:38:28 -04:00
hostname,
port: options.port,
transport,
certFile: options.certFile,
keyFile: options.keyFile
});
return new TLSListenerImpl(res.rid, res.localAddr);
2019-10-21 14:38:28 -04:00
}