Skip to content

Commit b19b883

Browse files
koichikIgor Zinkovsky
authored andcommitted
tls: Allow establishing secure connection on the existing socket
1 parent 1ce14ec commit b19b883

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

doc/api/tls.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ defaults to `localhost`.) `options` should be an object which specifies
130130

131131
- `servername`: Servername for SNI (Server Name Indication) TLS extension.
132132

133+
- `socket`: Establish secure connection on a given socket rather than
134+
creating a new socket. If this option is specified, `host` and `port`
135+
are ignored. This is intended FOR INTERNAL USE ONLY. As with all
136+
undocumented APIs in Node, they should not be used.
137+
133138
The `secureConnectListener` parameter will be added as a listener for the
134139
['secureConnect'](#event_secureConnect_) event.
135140

lib/tls.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ exports.connect = function(port /* host, options, cb */) {
10301030
}
10311031
}
10321032

1033-
var socket = new net.Stream();
1033+
var socket = options.socket ? options.socket : new net.Stream();
10341034

10351035
var sslcontext = crypto.createCredentials(options);
10361036

@@ -1050,7 +1050,9 @@ exports.connect = function(port /* host, options, cb */) {
10501050
cleartext.on('secureConnect', cb);
10511051
}
10521052

1053-
socket.connect(port, host);
1053+
if (!options.socket) {
1054+
socket.connect(port, host);
1055+
}
10541056

10551057
pair.on('secure', function() {
10561058
var verifyError = pair.ssl.verifyError();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var tls = require('tls');
25+
var net = require('net');
26+
var fs = require('fs');
27+
var path = require('path');
28+
29+
var serverConnected = false;
30+
var clientConnected = false;
31+
32+
var options = {
33+
key: fs.readFileSync(path.join(common.fixturesDir, 'test_key.pem')),
34+
cert: fs.readFileSync(path.join(common.fixturesDir, 'test_cert.pem'))
35+
};
36+
37+
var server = tls.createServer(options, function(socket) {
38+
serverConnected = true;
39+
socket.end('Hello');
40+
}).listen(common.PORT, function() {
41+
var socket = net.connect(common.PORT, function() {
42+
var client = tls.connect(0, {socket: socket}, function() {
43+
clientConnected = true;
44+
var data = '';
45+
client.on('data', function(chunk) {
46+
data += chunk.toString();
47+
});
48+
client.on('end', function() {
49+
assert.equal(data, 'Hello');
50+
server.close();
51+
});
52+
});
53+
});
54+
});
55+
56+
process.on('exit', function() {
57+
assert(serverConnected);
58+
assert(clientConnected);
59+
});

0 commit comments

Comments
 (0)