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) {
console.log('[' + Date.now() + '] tls socket data', chunk.byteLength);
myDuplex.push(chunk);
});
socket.on('error', function (err) {
console.error('[error] httpsTunnel (Admin) TODO close');
console.error(err);
myDuplex.emit('error', err);
});
socket.on('close', function () {
myDuplex.end();
});
process.nextTick(function () { process.nextTick(function () {
// this must happen after the socket is emitted to the next in the chain,
// but before any more data comes in via the network
socket.unshift(opts.firstChunk); socket.unshift(opts.firstChunk);
}); });
return myDuplex; socket.pipe(writer);
writer.pipe(socket);
socket.on('error', function (err) {
console.log('wrapped TLS socket error', err);
reader.emit('error', err);
});
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.
});
});
// We can't set these properties the normal way because there is a getter without a setter,
// but we can use defineProperty. We reuse the descriptor even though we will be manipulating
// it because we will only ever set the value and we set it every time.
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 reader;
} }
var le = greenlock.create({ var le = greenlock.create({