Instantly share code, notes, and snippets.

Embed
What would you like to do?
var net = require('net');
var fs = require('fs');
var crypto = require('crypto');
var s = net.createServer(function (stream) {
console.log('got stream');
var key = fs.readFileSync("/opt/cast/agent.key").toString();
var cert = fs.readFileSync("/opt/cast/agent.crt").toString();
var sslcontext = crypto.createCredentials({key: key, cert: cert});
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
var spair = crypto.createPair(sslcontext, true);
spair.encrypted.pipe(stream);
stream.pipe(spair.encrypted);
var clear = spair.cleartext;
//stream.setSecure(sslcontext);
console.log('i set it secure');
spair.on('secure', function () {
console.log('connected+secure!');
clear.write(new Buffer('hello\r\n'));
console.log(spair.getPeerCertificate());
console.log(spair.getCipher());
});
clear.on('data', function (data) {
console.log('echoing data');
clear.write(data);
});
stream.on('end', function (err) {
console.log('all done: '+ err);
clear.write(new Buffer('goodbye\r\n'));
clear.end();
});
clear.on('error', function(err) {
console.log('got error: ');
console.log(err);
console.log(err.stack);
stream.destroy();
});
spair.encrypted.on('error', function(err) {
console.log('encrypted error: ');
console.log(err);
console.log(err.stack);
stream.destroy();
});
stream.on('error', function(err) {
console.log('stream error: ');
console.log(err);
console.log(err.stack);
stream.destroy();
});
spair.on('error', function(err) {
console.log('secure error: ');
console.log(err);
console.log(err.stack);
stream.destroy();
});
});
s.listen(8000, 'localhost');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment