From 84a2768c25d5e4ab78f5c8eabfb23cc957892118 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 30 Apr 2019 11:46:56 -0400 Subject: [PATCH] tls: support enableTrace in TLSSocket() This commit adds the enableTrace option to the TLSSocket constructor. It also plumbs the option through other relevant APIs. PR-URL: https://github.com/nodejs/node/pull/27497 Reviewed-By: Anna Henningsen Reviewed-By: Richard Lau Reviewed-By: Rich Trott --- doc/api/tls.md | 9 +++++++++ lib/_tls_wrap.js | 25 ++++++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/doc/api/tls.md b/doc/api/tls.md index 228de3979474e3..3d2a9534f1ab13 100644 --- a/doc/api/tls.md +++ b/doc/api/tls.md @@ -586,6 +586,9 @@ connection is open. * `options` {Object} + * `enableTrace`: See [`tls.createServer()`][] * `host` {string} Host the client should connect to. **Default:** `'localhost'`. * `port` {number} Port the client should connect to. @@ -1647,6 +1655,7 @@ changes: * `rejectUnauthorized` {boolean} If not `false` a server automatically reject clients with invalid certificates. Only applies when `isServer` is `true`. * `options` + * `enableTrace`: See [`tls.createServer()`][] * `secureContext`: A TLS context object from [`tls.createSecureContext()`][] * `isServer`: If `true` the TLS socket will be instantiated in server-mode. **Default:** `false`. diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 64add601e74f84..0d4f5550ed2579 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -343,6 +343,12 @@ function initRead(tlsSocket, socket) { function TLSSocket(socket, opts) { const tlsOptions = { ...opts }; + const enableTrace = tlsOptions.enableTrace; + + if (typeof enableTrace !== 'boolean' && enableTrace != null) { + throw new ERR_INVALID_ARG_TYPE( + 'options.enableTrace', 'boolean', enableTrace); + } if (tlsOptions.ALPNProtocols) tls.convertALPNProtocols(tlsOptions.ALPNProtocols, tlsOptions); @@ -397,6 +403,9 @@ function TLSSocket(socket, opts) { this.readable = true; this.writable = true; + if (enableTrace && this._handle) + this._handle.enableTrace(); + // Read on next tick so the caller has a chance to setup listeners process.nextTick(initRead, this, socket); } @@ -872,10 +881,9 @@ function tlsConnectionListener(rawSocket) { rejectUnauthorized: this.rejectUnauthorized, handshakeTimeout: this[kHandshakeTimeout], ALPNProtocols: this.ALPNProtocols, - SNICallback: this[kSNICallback] || SNICallback + SNICallback: this[kSNICallback] || SNICallback, + enableTrace: this[kEnableTrace] }); - if (this[kEnableTrace] && socket._handle) - socket._handle.enableTrace(); socket.on('secure', onServerSocketSecure); @@ -997,13 +1005,7 @@ function Server(options, listener) { this.on('secureConnection', listener); } - const enableTrace = options.enableTrace; - if (typeof enableTrace === 'boolean') { - this[kEnableTrace] = enableTrace; - } else if (enableTrace != null) { - throw new ERR_INVALID_ARG_TYPE( - 'options.enableTrace', 'boolean', enableTrace); - } + this[kEnableTrace] = options.enableTrace; } Object.setPrototypeOf(Server.prototype, net.Server.prototype); @@ -1364,7 +1366,8 @@ exports.connect = function connect(...args) { rejectUnauthorized: options.rejectUnauthorized !== false, session: options.session, ALPNProtocols: options.ALPNProtocols, - requestOCSP: options.requestOCSP + requestOCSP: options.requestOCSP, + enableTrace: options.enableTrace }); tlssock[kConnectOptions] = options;