2010-11-19 22:28:04 +01:00
|
|
|
var common = require('../common');
|
2010-12-05 20:15:30 +01:00
|
|
|
var assert = require('assert');
|
2010-11-19 22:28:04 +01:00
|
|
|
|
|
|
|
var join = require('path').join;
|
|
|
|
var net = require('net');
|
|
|
|
var fs = require('fs');
|
|
|
|
var crypto = require('crypto');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
var connections = 0;
|
2010-12-04 23:45:52 +01:00
|
|
|
var key = fs.readFileSync(join(common.fixturesDir, 'agent.key')).toString();
|
|
|
|
var cert = fs.readFileSync(join(common.fixturesDir, 'agent.crt')).toString();
|
2010-11-19 22:28:04 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
function log(a) {
|
2010-11-24 03:30:52 +01:00
|
|
|
console.error('***server*** ' + a);
|
2010-11-19 22:28:04 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
var server = net.createServer(function(socket) {
|
2010-11-19 22:28:04 +01:00
|
|
|
connections++;
|
2010-11-24 03:30:52 +01:00
|
|
|
log('connection fd=' + socket.fd);
|
2010-11-19 22:28:04 +01:00
|
|
|
var sslcontext = crypto.createCredentials({key: key, cert: cert});
|
|
|
|
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
|
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
var pair = crypto.createPair(sslcontext, true);
|
2010-11-19 22:28:04 +01:00
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
assert.ok(pair.encrypted.writable);
|
|
|
|
assert.ok(pair.cleartext.writable);
|
2010-11-19 22:28:04 +01:00
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
pair.encrypted.pipe(socket);
|
|
|
|
socket.pipe(pair.encrypted);
|
2010-11-19 22:28:04 +01:00
|
|
|
|
|
|
|
log('i set it secure');
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
pair.on('secure', function() {
|
2010-11-19 22:28:04 +01:00
|
|
|
log('connected+secure!');
|
2010-11-24 03:30:52 +01:00
|
|
|
pair.cleartext.write('hello\r\n');
|
|
|
|
log(pair.getPeerCertificate());
|
|
|
|
log(pair.getCipher());
|
2010-11-19 22:28:04 +01:00
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
pair.cleartext.on('data', function(data) {
|
2010-11-24 03:30:52 +01:00
|
|
|
log('read bytes ' + data.length);
|
|
|
|
pair.cleartext.write(data);
|
2010-11-19 22:28:04 +01:00
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
socket.on('end', function() {
|
2010-11-24 03:30:52 +01:00
|
|
|
log('socket end');
|
|
|
|
pair.cleartext.write('goodbye\r\n');
|
|
|
|
pair.cleartext.end();
|
2010-11-19 22:28:04 +01:00
|
|
|
});
|
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
pair.cleartext.on('error', function(err) {
|
2010-11-19 22:28:04 +01:00
|
|
|
log('got error: ');
|
|
|
|
log(err);
|
|
|
|
log(err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
pair.encrypted.on('error', function(err) {
|
2010-11-19 22:28:04 +01:00
|
|
|
log('encrypted error: ');
|
|
|
|
log(err);
|
|
|
|
log(err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('error', function(err) {
|
|
|
|
log('socket error: ');
|
|
|
|
log(err);
|
|
|
|
log(err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
socket.on('close', function(err) {
|
|
|
|
log('socket closed');
|
|
|
|
});
|
|
|
|
|
|
|
|
pair.on('error', function(err) {
|
2010-11-19 22:28:04 +01:00
|
|
|
log('secure error: ');
|
|
|
|
log(err);
|
|
|
|
log(err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var gotHello = false;
|
|
|
|
var sentWorld = false;
|
|
|
|
var gotWorld = false;
|
|
|
|
var opensslExitCode = -1;
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
server.listen(8000, function() {
|
2010-11-19 22:28:04 +01:00
|
|
|
// To test use: openssl s_client -connect localhost:8000
|
|
|
|
var client = spawn('openssl', ['s_client', '-connect', '127.0.0.1:8000']);
|
|
|
|
|
|
|
|
|
|
|
|
var out = '';
|
|
|
|
|
|
|
|
client.stdout.setEncoding('utf8');
|
2010-12-04 23:45:52 +01:00
|
|
|
client.stdout.on('data', function(d) {
|
2010-11-19 22:28:04 +01:00
|
|
|
out += d;
|
|
|
|
|
|
|
|
if (!gotHello && /hello/.test(out)) {
|
|
|
|
gotHello = true;
|
|
|
|
client.stdin.write('world\r\n');
|
|
|
|
sentWorld = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gotWorld && /world/.test(out)) {
|
|
|
|
gotWorld = true;
|
|
|
|
client.stdin.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
client.stdout.pipe(process.stdout);
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
client.on('exit', function(code) {
|
2010-11-19 22:28:04 +01:00
|
|
|
opensslExitCode = code;
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
process.on('exit', function() {
|
2010-11-19 22:28:04 +01:00
|
|
|
assert.equal(1, connections);
|
|
|
|
assert.ok(gotHello);
|
|
|
|
assert.ok(sentWorld);
|
|
|
|
assert.ok(gotWorld);
|
|
|
|
assert.equal(0, opensslExitCode);
|
|
|
|
});
|