mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
2.8 KiB
129 lines
2.8 KiB
14 years ago
|
var common = require('../common');
|
||
14 years ago
|
var assert = require('assert');
|
||
14 years ago
|
|
||
|
var join = require('path').join;
|
||
|
var net = require('net');
|
||
|
var fs = require('fs');
|
||
|
var crypto = require('crypto');
|
||
14 years ago
|
var tls = require('tls');
|
||
14 years ago
|
var spawn = require('child_process').spawn;
|
||
|
|
||
|
var connections = 0;
|
||
14 years ago
|
var key = fs.readFileSync(join(common.fixturesDir, 'agent.key')).toString();
|
||
|
var cert = fs.readFileSync(join(common.fixturesDir, 'agent.crt')).toString();
|
||
14 years ago
|
|
||
14 years ago
|
function log(a) {
|
||
14 years ago
|
console.error('***server*** ' + a);
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
var server = net.createServer(function(socket) {
|
||
14 years ago
|
connections++;
|
||
14 years ago
|
log('connection fd=' + socket.fd);
|
||
14 years ago
|
var sslcontext = crypto.createCredentials({key: key, cert: cert});
|
||
|
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
|
||
|
|
||
14 years ago
|
var pair = tls.createSecurePair(sslcontext, true);
|
||
14 years ago
|
|
||
14 years ago
|
assert.ok(pair.encrypted.writable);
|
||
|
assert.ok(pair.cleartext.writable);
|
||
14 years ago
|
|
||
14 years ago
|
pair.encrypted.pipe(socket);
|
||
|
socket.pipe(pair.encrypted);
|
||
14 years ago
|
|
||
|
log('i set it secure');
|
||
|
|
||
14 years ago
|
pair.on('secure', function() {
|
||
14 years ago
|
log('connected+secure!');
|
||
14 years ago
|
pair.cleartext.write('hello\r\n');
|
||
14 years ago
|
log(pair.cleartext.getPeerCertificate());
|
||
|
log(pair.cleartext.getCipher());
|
||
14 years ago
|
});
|
||
|
|
||
14 years ago
|
pair.cleartext.on('data', function(data) {
|
||
14 years ago
|
log('read bytes ' + data.length);
|
||
|
pair.cleartext.write(data);
|
||
14 years ago
|
});
|
||
|
|
||
14 years ago
|
socket.on('end', function() {
|
||
14 years ago
|
log('socket end');
|
||
|
pair.cleartext.write('goodbye\r\n');
|
||
|
pair.cleartext.end();
|
||
14 years ago
|
});
|
||
|
|
||
14 years ago
|
pair.cleartext.on('error', function(err) {
|
||
14 years ago
|
log('got error: ');
|
||
|
log(err);
|
||
|
log(err.stack);
|
||
|
socket.destroy();
|
||
|
});
|
||
|
|
||
14 years ago
|
pair.encrypted.on('error', function(err) {
|
||
14 years ago
|
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();
|
||
|
});
|
||
|
|
||
14 years ago
|
socket.on('close', function(err) {
|
||
|
log('socket closed');
|
||
|
});
|
||
|
|
||
|
pair.on('error', function(err) {
|
||
14 years ago
|
log('secure error: ');
|
||
|
log(err);
|
||
|
log(err.stack);
|
||
|
socket.destroy();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
var gotHello = false;
|
||
|
var sentWorld = false;
|
||
|
var gotWorld = false;
|
||
|
var opensslExitCode = -1;
|
||
|
|
||
14 years ago
|
server.listen(8000, function() {
|
||
14 years ago
|
// 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');
|
||
14 years ago
|
client.stdout.on('data', function(d) {
|
||
14 years ago
|
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);
|
||
|
|
||
14 years ago
|
client.on('exit', function(code) {
|
||
14 years ago
|
opensslExitCode = code;
|
||
|
server.close();
|
||
|
});
|
||
|
});
|
||
|
|
||
14 years ago
|
process.on('exit', function() {
|
||
14 years ago
|
assert.equal(1, connections);
|
||
|
assert.ok(gotHello);
|
||
|
assert.ok(sentWorld);
|
||
|
assert.ok(gotWorld);
|
||
|
assert.equal(0, opensslExitCode);
|
||
|
});
|