changed how TLS sockets are wrapped
This commit is contained in:
parent
febe106a81
commit
d25ceadf4a
|
@ -9,38 +9,61 @@ module.exports.create = function (deps, config, netHandler) {
|
|||
|
||||
function extractSocketProp(socket, propName) {
|
||||
// remoteAddress, remotePort... ugh... https://github.com/nodejs/node/issues/8854
|
||||
return socket[propName]
|
||||
|| socket['_' + propName]
|
||||
|| socket._handle._parent.owner.stream[propName]
|
||||
;
|
||||
var value = socket[propName] || socket['_' + propName];
|
||||
try {
|
||||
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) {
|
||||
var myDuplex = require('tunnel-packer').Stream.create(socket);
|
||||
myDuplex.remoteFamily = opts.remoteFamily || myDuplex.remoteFamily;
|
||||
myDuplex.remoteAddress = opts.remoteAddress || myDuplex.remoteAddress;
|
||||
myDuplex.remotePort = opts.remotePort || myDuplex.remotePort;
|
||||
|
||||
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();
|
||||
});
|
||||
var reader = require('socket-pair').create(function (err, writer) {
|
||||
if (err) {
|
||||
reader.emit('error', err);
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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({
|
||||
|
|
Loading…
Reference in New Issue