Initial implementation of tls.connect()

Seems to work checkout test/disabled/tls-client.js
Type "GET /" after connected.
This commit is contained in:
Ryan Dahl 2010-12-09 00:35:16 -08:00
parent 137c361517
commit 2ca63c8f79
2 changed files with 89 additions and 0 deletions

View File

@ -547,4 +547,75 @@ Server.prototype.setOptions = function(options) {
};
// Target API:
//
// var s = tls.connect(8000, "google.com", options, function() {
// if (!s.authorized) {
// s.destroy();
// return;
// }
//
// // s.socket;
//
// s.end("hello world\n");
// });
//
exports.connect = function(port /* host, options, cb */) {
// parse args
var host, options = {}, cb;
switch (typeof arguments[1]) {
case 'string':
host = arguments[1];
if (typeof arguments[2] == 'object') {
options = arguments[2];
if (typeof arguments[3] == 'function') cb = arguments[3];
} else if (typeof arguments[2] == 'function') {
cb = arguments[2];
}
break;
case 'object':
options = arguments[1];
if (typeof arguments[2] == 'function') cb = arguments[2];
break;
case 'function':
cb = arguments[1];
break;
default:
break;
}
var socket = new net.Stream();
var sslcontext = crypto.createCredentials(options);
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
var pair = new SecurePair(sslcontext, false);
pair.encrypted.pipe(socket);
socket.pipe(pair.encrypted);
var cleartext = pair.cleartext;
cleartext.socket = socket;
cleartext.encrypted = pair.encrypted;
cleartext.authorized = false;
socket.connect(port, host);
pair.on('secure', function() {
console.log('client: connected+secure!');
console.log('client pair.getPeerCertificate(): %j',
pair.getPeerCertificate());
console.log('client pair.getCipher(): %j',
pair.getCipher());
if (cb) {
cb(cleartext);
}
});
return cleartext;
};

View File

@ -0,0 +1,18 @@
var common = require('../common');
var tls = require('tls');
var fs = require('fs');
// most servers don't require certificates
var options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
};
var s = tls.connect(443, "google.com", options, function() {
console.error("CONNECTED");
s.pipe(process.stdout);
process.openStdin().pipe(s);
});