From c937e38d2a4e4dfd2da1ab6eac3cdfb6238e9ef7 Mon Sep 17 00:00:00 2001 From: leeight Date: Wed, 21 Nov 2018 23:12:17 +0800 Subject: [PATCH] https: add missing localPort while create socket In `_tls_wrap.js` while calling `socket.connect` the `localPort` was missing, restore it. Fix: https://github.com/nodejs/node/issues/24543 --- lib/_tls_wrap.js | 1 + test/parallel/test-https-connect-localport.js | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/parallel/test-https-connect-localport.js diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 1d6ccbb8a9cbaf..1971339e7e0dc7 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -1248,6 +1248,7 @@ exports.connect = function connect(...args) { host: options.host, family: options.family, localAddress: options.localAddress, + localPort: options.localPort, lookup: options.lookup }; socket.connect(connectOpt, socket._start); diff --git a/test/parallel/test-https-connect-localport.js b/test/parallel/test-https-connect-localport.js new file mode 100644 index 00000000000000..fc9e11f27e7188 --- /dev/null +++ b/test/parallel/test-https-connect-localport.js @@ -0,0 +1,32 @@ +'use strict'; +const common = require('../common'); +const fixtures = require('../common/fixtures'); + +if (!common.hasCrypto) + common.skip('missing crypto'); + +const https = require('https'); +const assert = require('assert'); + +{ + https.createServer({ + cert: fixtures.readKey('agent1-cert.pem'), + key: fixtures.readKey('agent1-key.pem'), + }, common.mustCall(function(req, res) { + this.close(); + res.end(); + })).listen(0, common.localhostIPv4, common.mustCall(function() { + const port = this.address().port; + const req = https.get({ + host: common.localhostIPv4, + pathname: '/', + port, + family: 4, + localPort: 34567, + rejectUnauthorized: false + }, common.mustCall(() => { + assert.strictEqual(req.socket.localPort, 34567); + assert.strictEqual(req.socket.remotePort, port); + })); + })); +}