tls: reuse hostname from underlying net.Socket
When `tls.connect()` is called with `socket` option, it should try to reuse hostname previously passed to `net.connect()` and only after that fall back to `'localhost'`. fix #6409
This commit is contained in:
parent
691b9ebc8c
commit
5977cba985
@ -683,7 +683,10 @@ exports.connect = function(/* [port, host], options, cb */) {
|
|||||||
};
|
};
|
||||||
options = util._extend(defaults, options || {});
|
options = util._extend(defaults, options || {});
|
||||||
|
|
||||||
var hostname = options.servername || options.host || 'localhost',
|
var hostname = options.servername ||
|
||||||
|
options.host ||
|
||||||
|
options.socket && options.socket._host ||
|
||||||
|
'localhost',
|
||||||
NPN = {},
|
NPN = {},
|
||||||
credentials = crypto.createCredentials(options);
|
credentials = crypto.createCredentials(options);
|
||||||
tls.convertNPNProtocols(options.NPNProtocols, NPN);
|
tls.convertNPNProtocols(options.NPNProtocols, NPN);
|
||||||
|
@ -134,6 +134,7 @@ function Socket(options) {
|
|||||||
this._connecting = false;
|
this._connecting = false;
|
||||||
this._hadError = false;
|
this._hadError = false;
|
||||||
this._handle = null;
|
this._handle = null;
|
||||||
|
this._host = null;
|
||||||
|
|
||||||
if (util.isNumber(options))
|
if (util.isNumber(options))
|
||||||
options = { fd: options }; // Legacy interface.
|
options = { fd: options }; // Legacy interface.
|
||||||
@ -859,12 +860,14 @@ Socket.prototype.connect = function(options, cb) {
|
|||||||
|
|
||||||
} else if (!options.host) {
|
} else if (!options.host) {
|
||||||
debug('connect: missing host');
|
debug('connect: missing host');
|
||||||
connect(self, '127.0.0.1', options.port, 4);
|
self._host = '127.0.0.1';
|
||||||
|
connect(self, self._host, options.port, 4);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
var host = options.host;
|
var host = options.host;
|
||||||
var family = options.family || 4;
|
var family = options.family || 4;
|
||||||
debug('connect: find host ' + host);
|
debug('connect: find host ' + host);
|
||||||
|
self._host = host;
|
||||||
require('dns').lookup(host, family, function(err, ip, addressType) {
|
require('dns').lookup(host, family, function(err, ip, addressType) {
|
||||||
self.emit('lookup', err, ip, addressType);
|
self.emit('lookup', err, ip, addressType);
|
||||||
|
|
||||||
|
42
test/internet/test-tls-reuse-host-from-socket.js
Normal file
42
test/internet/test-tls-reuse-host-from-socket.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// Copyright Joyent, Inc. and other Node contributors.
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
// copy of this software and associated documentation files (the
|
||||||
|
// "Software"), to deal in the Software without restriction, including
|
||||||
|
// without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
// persons to whom the Software is furnished to do so, subject to the
|
||||||
|
// following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included
|
||||||
|
// in all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
var common = require('../common');
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
|
var tls = require('tls');
|
||||||
|
var net = require('net');
|
||||||
|
var connected = false;
|
||||||
|
var secure = false;
|
||||||
|
|
||||||
|
process.on('exit', function() {
|
||||||
|
assert(connected);
|
||||||
|
assert(secure);
|
||||||
|
console.log('ok');
|
||||||
|
});
|
||||||
|
|
||||||
|
var socket = net.connect(443, 'www.google.com', function() {
|
||||||
|
connected = true;
|
||||||
|
var secureSocket = tls.connect({ socket: socket }, function() {
|
||||||
|
secure = true;
|
||||||
|
secureSocket.destroy();
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user