summaryrefslogtreecommitdiff
path: root/cli/js/tls.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-10-21 20:38:28 +0200
committerRy Dahl <ry@tinyclouds.org>2019-10-21 14:38:28 -0400
commit6c5a981fd2afad21af73a1345c4e30fb6b30b09a (patch)
treec6065fe502cc99f29d7f5554257729552920f7f4 /cli/js/tls.ts
parent1f52c66ced9bed0cae6bff065dfa7563cbfaee29 (diff)
feat: Deno.listenTLS (#3152)
Diffstat (limited to 'cli/js/tls.ts')
-rw-r--r--cli/js/tls.ts46
1 files changed, 44 insertions, 2 deletions
diff --git a/cli/js/tls.ts b/cli/js/tls.ts
index ec24b458b..3e38c7854 100644
--- a/cli/js/tls.ts
+++ b/cli/js/tls.ts
@@ -1,13 +1,14 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { sendAsync } from "./dispatch_json.ts";
+import { sendAsync, sendSync } from "./dispatch_json.ts";
import * as dispatch from "./dispatch.ts";
-import { Conn, ConnImpl } from "./net.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 DialTLSOptions {
port: number;
hostname?: string;
+ certFile?: string;
}
const dialTLSDefaults = { hostname: "127.0.0.1", transport: "tcp" };
@@ -19,3 +20,44 @@ export async function dialTLS(options: DialTLSOptions): Promise<Conn> {
const res = await sendAsync(dispatch.OP_DIAL_TLS, options);
return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!);
}
+
+class TLSListenerImpl extends ListenerImpl {
+ async accept(): Promise<Conn> {
+ const res = await sendAsync(dispatch.OP_ACCEPT_TLS, { rid: this.rid });
+ 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";
+ const res = sendSync(dispatch.OP_LISTEN_TLS, {
+ hostname,
+ port: options.port,
+ transport,
+ certFile: options.certFile,
+ keyFile: options.keyFile
+ });
+ return new TLSListenerImpl(res.rid, transport, res.localAddr);
+}