changed how TLS sockets are wrapped

This commit is contained in:
tigerbot 2017-05-17 17:12:04 -06:00
parent febe106a81
commit d25ceadf4a
1 changed files with 47 additions and 24 deletions

View File

@ -9,38 +9,61 @@ module.exports.create = function (deps, config, netHandler) {
function extractSocketProp(socket, propName) { function extractSocketProp(socket, propName) {
// remoteAddress, remotePort... ugh... https://github.com/nodejs/node/issues/8854 // remoteAddress, remotePort... ugh... https://github.com/nodejs/node/issues/8854
return socket[propName] var value = socket[propName] || socket['_' + propName];
|| socket['_' + propName] try {
|| socket._handle._parent.owner.stream[propName] value = value || socket._handle._parent.owner.stream[propName];
; } catch (e) {}
try {
value = value || socket._handle._parentWrap[propName];
value = value || socket._handle._parentWrap._handle.owner.stream[propName];
} catch (e) {}
return value || '';
} }
var addressNames = [
'remoteAddress'
, 'remotePort'
, 'remoteFamily'
, 'localAddress'
, 'localPort'
];
function wrapSocket(socket, opts) { function wrapSocket(socket, opts) {
var myDuplex = require('tunnel-packer').Stream.create(socket); var reader = require('socket-pair').create(function (err, writer) {
myDuplex.remoteFamily = opts.remoteFamily || myDuplex.remoteFamily; if (err) {
myDuplex.remoteAddress = opts.remoteAddress || myDuplex.remoteAddress; reader.emit('error', err);
myDuplex.remotePort = opts.remotePort || myDuplex.remotePort; return;
}
socket.on('data', function (chunk) { process.nextTick(function () {
console.log('[' + Date.now() + '] tls socket data', chunk.byteLength); socket.unshift(opts.firstChunk);
myDuplex.push(chunk); });
});
socket.on('error', function (err) { socket.pipe(writer);
console.error('[error] httpsTunnel (Admin) TODO close'); writer.pipe(socket);
console.error(err);
myDuplex.emit('error', err); socket.on('error', function (err) {
}); console.log('wrapped TLS socket error', err);
socket.on('close', function () { reader.emit('error', err);
myDuplex.end(); });
writer.on('error', function (err) {
console.error('socket-pair writer error', err);
// If the writer had an error the reader probably did too, and I don't think we'll
// get much out of emitting this on the original socket, so logging is enough.
});
}); });
process.nextTick(function () { // We can't set these properties the normal way because there is a getter without a setter,
// this must happen after the socket is emitted to the next in the chain, // but we can use defineProperty. We reuse the descriptor even though we will be manipulating
// but before any more data comes in via the network // it because we will only ever set the value and we set it every time.
socket.unshift(opts.firstChunk); var descriptor = {enumerable: true, configurable: true, writable: true};
addressNames.forEach(function (name) {
descriptor.value = opts[name] || extractSocketProp(socket, name);
Object.defineProperty(reader, name, descriptor);
}); });
return myDuplex; return reader;
} }
var le = greenlock.create({ var le = greenlock.create({