proxy-packer.js/index.js

497 lines
14 KiB
JavaScript
Raw Normal View History

2016-09-22 19:11:54 +00:00
'use strict';
2016-10-01 02:07:01 +00:00
var Packer = module.exports;
2018-04-29 01:28:54 +00:00
var serviceEvents = {
default: 'tunnelData'
, connection: 'tunnelConnection'
2018-04-29 01:28:54 +00:00
, control: 'tunnelControl'
, error: 'tunnelError'
, end: 'tunnelEnd'
, pause: 'tunnelPause'
, resume: 'tunnelResume'
};
var serviceFuncs = {
default: 'onmessage'
, connection: 'onconnection'
2018-04-29 01:28:54 +00:00
, control: 'oncontrol'
, error: 'onerror'
, end: 'onend'
, pause: 'onpause'
, resume: 'onresume'
};
2016-10-01 02:07:01 +00:00
Packer.create = function (opts) {
2016-09-30 04:25:54 +00:00
var machine;
if (!opts.onMessage && !opts.onmessage) {
machine = new (require('events').EventEmitter)();
2016-09-30 04:51:40 +00:00
} else {
machine = {};
2016-09-30 04:25:54 +00:00
}
machine.onmessage = opts.onmessage || opts.onMessage;
machine.oncontrol = opts.oncontrol || opts.onControl;
machine.onconnection = opts.onconnection || opts.onConnection || function () {};
machine.onerror = opts.onerror || opts.onError;
machine.onend = opts.onend || opts.onEnd;
machine.onpause = opts.onpause || opts.onPause;
machine.onresume = opts.onresume || opts.onResume;
2016-09-22 19:11:54 +00:00
machine._version = 1;
machine.fns = {};
2018-04-29 01:24:13 +00:00
machine.chunkIndex = 0;
machine.buf = null;
machine.bufIndex = 0;
machine.fns.collectData = function (chunk, size) {
var chunkLeft = chunk.length - machine.chunkIndex;
var hasLen = (size > 0);
2018-04-29 01:24:13 +00:00
if (!hasLen) {
2018-04-29 01:24:13 +00:00
return Buffer.alloc(0);
}
// First handle case where we don't have all the data we need yet. We need to save
// what we have in a buffer, and increment the index for both the buffer and the chunk.
if (machine.bufIndex + chunkLeft < size) {
if (!machine.buf) {
machine.buf = Buffer.alloc(size);
}
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex);
machine.bufIndex += chunkLeft;
machine.chunkIndex += chunkLeft;
return null;
}
// Read and mark as read however much data we need from the chunk to complete our buffer.
var partLen = size - machine.bufIndex;
var part = chunk.slice(machine.chunkIndex, machine.chunkIndex+partLen);
machine.chunkIndex += partLen;
// If we had nothing buffered than the part of the chunk we just read is all we need.
if (!machine.buf) {
return part;
}
// Otherwise we need to copy the new data into the buffer.
part.copy(machine.buf, machine.bufIndex);
// Before returning the buffer we need to clear our reference to it.
var buf = machine.buf;
machine.buf = null;
machine.bufIndex = 0;
return buf;
};
2016-09-22 19:11:54 +00:00
machine.fns.version = function (chunk) {
//console.log('');
//console.log('[version]');
if ((255 - machine._version) !== chunk[machine.chunkIndex]) {
console.error("not v" + machine._version + " (or data is corrupt)");
// no idea how to fix this yet
}
machine.chunkIndex += 1;
return true;
};
machine.headerLen = 0;
machine.fns.headerLength = function (chunk) {
//console.log('');
//console.log('[headerLength]');
machine.headerLen = chunk[machine.chunkIndex];
machine.chunkIndex += 1;
return true;
};
machine.fns.header = function (chunk) {
//console.log('');
//console.log('[header]');
2018-04-29 01:24:13 +00:00
var header = machine.fns.collectData(chunk, machine.headerLen);
2016-09-22 19:11:54 +00:00
2018-04-29 01:24:13 +00:00
// We don't have the entire header yet so return false.
if (!header) {
2016-09-22 19:11:54 +00:00
return false;
}
2018-04-29 01:24:13 +00:00
machine._headers = header.toString().split(/,/g);
2016-09-22 19:11:54 +00:00
machine.family = machine._headers[0];
machine.address = machine._headers[1];
machine.port = machine._headers[2];
machine.bodyLen = parseInt(machine._headers[3], 10) || 0;
machine.service = machine._headers[4];
2018-05-31 04:48:32 +00:00
machine.serviceport = machine._headers[5];
machine.name = machine._headers[6];
machine.servicename = machine._headers[7];
2018-04-29 01:24:13 +00:00
//console.log('machine.service', machine.service);
2016-09-22 19:11:54 +00:00
2018-04-29 01:24:13 +00:00
return true;
2016-09-22 19:11:54 +00:00
};
machine.fns.data = function (chunk) {
//console.log('');
//console.log('[data]');
var data;
// The 'connection' event may not have a body
// Other events may not have a body either
if (machine.bodyLen) {
data = machine.fns.collectData(chunk, machine.bodyLen);
// We don't have the entire body yet so return false.
if (!data) {
return false;
}
2016-09-22 19:11:54 +00:00
}
2016-09-30 04:25:54 +00:00
//
// data, end, error
//
2018-04-29 01:24:13 +00:00
var msg = {};
if ('error' === machine.service) {
try {
msg = JSON.parse(data.toString());
} catch(e) {
msg.message = 'e:' + JSON.stringify(data);
2018-04-29 01:24:13 +00:00
msg.code = 'E_UNKNOWN_ERR';
}
}
2016-09-30 04:25:54 +00:00
2018-05-31 04:48:32 +00:00
msg.family = machine.family;
msg.address = machine.address;
msg.port = machine.port;
msg.service = machine.service;
msg.serviceport = machine.serviceport;
msg.name = machine.name;
msg.data = data;
2016-09-30 04:25:54 +00:00
if ('connection' === machine.service) {
msg.service = machine.servicename;
}
//console.log('msn', machine.service);
2018-04-29 01:28:54 +00:00
if (machine.emit) {
machine.emit(serviceEvents[machine.service] || serviceEvents[msg.service] || serviceEvents.default);
2018-04-29 01:28:54 +00:00
} else {
(machine[serviceFuncs[machine.service]] || machine[serviceFuncs[msg.service]] || machine[serviceFuncs.default])(msg);
2016-09-30 04:25:54 +00:00
}
2016-09-22 19:11:54 +00:00
return true;
};
2018-04-29 01:24:13 +00:00
machine.state = 0;
machine.states = ['version', 'headerLength', 'header', 'data'];
2016-09-22 19:11:54 +00:00
machine.fns.addChunk = function (chunk) {
//console.log('');
//console.log('[addChunk]');
machine.chunkIndex = 0;
while (machine.chunkIndex < chunk.length) {
//console.log('chunkIndex:', machine.chunkIndex, 'state:', machine.state);
if (true === machine.fns[machine.states[machine.state]](chunk)) {
machine.state += 1;
2018-04-29 01:24:13 +00:00
machine.state %= machine.states.length;
2016-09-22 19:11:54 +00:00
}
}
if ('data' === machine.states[machine.state] && 0 === machine.bodyLen) {
machine.fns[machine.states[machine.state]](chunk)
machine.state += 1;
machine.state %= machine.states.length;
}
2016-09-22 19:11:54 +00:00
};
return machine;
};
Packer.packHeader = function (meta, data, service, andBody, oldways) {
if (oldways && !data) {
data = Buffer.from(' ');
}
if (data && !Buffer.isBuffer(data)) {
data = Buffer.from(JSON.stringify(data));
2018-04-29 01:24:13 +00:00
}
if (oldways && !data.byteLength) {
2018-04-29 01:28:54 +00:00
data = Buffer.from(' ');
2016-09-30 06:44:36 +00:00
}
2016-09-30 07:03:26 +00:00
if (service && -1 === ['control','connection'].indexOf(service)) {
//console.log('end?', service);
2018-05-31 04:48:32 +00:00
meta.service = service;
2016-09-30 03:41:42 +00:00
}
var size = data && data.byteLength || 0;
var sizeReserve = andBody ? size : 0;
2016-09-22 19:11:54 +00:00
var version = 1;
2018-04-29 01:24:13 +00:00
var header;
if (service === 'control') {
header = Buffer.from(['', '', '', size, service].join(','));
2018-04-29 01:24:13 +00:00
}
else if (service === 'connection') {
header = Buffer.from([
meta.family, meta.address, meta.port, size,
'connection', (meta.serviceport || ''), (meta.name || ''),
(meta.service || '')
].join(','));
}
2018-04-29 01:24:13 +00:00
else {
header = Buffer.from([
meta.family, meta.address, meta.port, size,
2018-05-31 04:48:32 +00:00
(meta.service || ''), (meta.serviceport || ''), (meta.name || '')
2018-04-29 01:24:13 +00:00
].join(','));
}
2018-05-31 04:48:32 +00:00
var metaBuf = Buffer.from([ 255 - version, header.length ]);
var buf = Buffer.alloc(metaBuf.byteLength + header.byteLength + sizeReserve);
2016-09-22 19:31:31 +00:00
2018-05-31 04:48:32 +00:00
metaBuf.copy(buf, 0);
2018-04-29 01:24:13 +00:00
header.copy(buf, 2);
if (sizeReserve) { data.copy(buf, 2 + header.byteLength); }
2016-09-22 19:11:54 +00:00
return buf;
};
Packer.pack = function (meta, data, service) {
return Packer.packHeader(meta, data, service, true, true);
};
2016-10-01 02:07:01 +00:00
2018-05-31 11:09:48 +00:00
function extractSocketProps(socket, propNames) {
var props = {};
if (socket.remotePort) {
propNames.forEach(function (propName) {
props[propName] = socket[propName];
});
} else if (socket._remotePort) {
propNames.forEach(function (propName) {
props[propName] = socket['_' + propName];
});
2019-09-03 15:14:47 +00:00
} else if (socket._handle) {
if (
socket._handle._parent
&& socket._handle._parent.owner
&& socket._handle._parent.owner.stream
&& socket._handle._parent.owner.stream.remotePort
) {
propNames.forEach(function (propName) {
props[propName] = socket._handle._parent.owner.stream[propName];
});
} else if (
socket._handle._parentWrap
&& socket._handle._parentWrap.remotePort
) {
propNames.forEach(function (propName) {
props[propName] = socket._handle._parentWrap[propName];
});
} else if (
socket._handle._parentWrap
&& socket._handle._parentWrap._handle
&& socket._handle._parentWrap._handle.owner
&& socket._handle._parentWrap._handle.owner.stream
&& socket._handle._parentWrap._handle.owner.stream.remotePort
) {
propNames.forEach(function (propName) {
props[propName] = socket._handle._parentWrap._handle.owner.stream[propName];
});
}
2018-05-31 11:09:48 +00:00
}
return props;
}
2018-04-29 01:25:46 +00:00
function extractSocketProp(socket, propName) {
// remoteAddress, remotePort... ugh... https://github.com/nodejs/node/issues/8854
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 || '';
}
2016-10-01 02:07:01 +00:00
Packer.socketToAddr = function (socket) {
// TODO BUG XXX
// https://github.com/nodejs/node/issues/8854
// tlsSocket.remoteAddress = remoteAddress; // causes core dump
// console.log(tlsSocket.remoteAddress);
2018-05-31 11:13:27 +00:00
var props = extractSocketProps(socket, [ 'remoteFamily', 'remoteAddress', 'remotePort', 'localPort' ]);
2016-10-01 02:07:01 +00:00
return {
2018-05-31 11:09:48 +00:00
family: props.remoteFamily
, address: props.remoteAddress
, port: props.remotePort
2018-05-31 11:13:27 +00:00
, serviceport: props.localPort
2016-10-01 02:07:01 +00:00
};
};
Packer.addrToId = function (address) {
return address.family + ',' + address.address + ',' + address.port;
};
Packer.socketToId = function (socket) {
return Packer.addrToId(Packer.socketToAddr(socket));
};
2018-04-29 01:28:54 +00:00
var addressNames = [
'remoteAddress'
, 'remotePort'
, 'remoteFamily'
, 'localAddress'
, 'localPort'
];
/*
2018-04-29 01:28:54 +00:00
var sockFuncs = [
'address'
, 'destroy'
, 'ref'
, 'unref'
, 'setEncoding'
, 'setKeepAlive'
, 'setNoDelay'
, 'setTimeout'
];
*/
2018-04-29 01:28:54 +00:00
// Unlike Packer.Stream.create this should handle all of the events needed to make everything work.
2018-04-29 01:25:46 +00:00
Packer.wrapSocket = function (socket) {
2018-05-31 11:35:49 +00:00
// node v10.2+ doesn't need a workaround for https://github.com/nodejs/node/issues/8854
addressNames.forEach(function (name) {
Object.defineProperty(socket, name, {
enumerable: false,
configurable: true,
get: function() {
return extractSocketProp(socket, name);
}
});
});
return socket;
// Improved workaround for https://github.com/nodejs/node/issues/8854
/*
2018-05-31 11:09:48 +00:00
// TODO use defineProperty to override remotePort, etc
2018-04-29 01:25:46 +00:00
var myDuplex = new require('stream').Duplex();
addressNames.forEach(function (name) {
myDuplex[name] = extractSocketProp(socket, name);
});
// Handle everything needed for the write part of the Duplex. We need to overwrite the
// `end` function because there is no other way to know when the other side calls it.
myDuplex._write = socket.write.bind(socket);
myDuplex.end = socket.end.bind(socket);
// Handle everything needed for the read part of the Duplex. See the example under
// https://nodejs.org/api/stream.html#stream_readable_push_chunk_encoding.
myDuplex._read = function () {
socket.resume();
};
socket.on('data', function (chunk) {
if (!myDuplex.push(chunk)) {
socket.pause();
}
});
socket.on('end', function () {
myDuplex.push(null);
});
// Handle the the things not directly related to reading or writing
socket.on('error', function (err) {
console.error('[error] wrapped socket errored - ' + err.toString());
myDuplex.emit('error', err);
});
socket.on('close', function () {
myDuplex.emit('close');
});
2018-04-29 01:28:54 +00:00
sockFuncs.forEach(function (name) {
if (typeof socket[name] !== 'function') {
console.warn('expected `'+name+'` to be a function on wrapped socket');
} else {
myDuplex[name] = socket[name].bind(socket);
}
});
2018-04-29 01:25:46 +00:00
return myDuplex;
2018-05-31 11:35:49 +00:00
*/
2018-04-29 01:25:46 +00:00
};
2016-10-01 02:07:01 +00:00
var Transform = require('stream').Transform;
var util = require('util');
function MyTransform(options) {
if (!(this instanceof MyTransform)) {
return new MyTransform(options);
}
this.__my_addr = options.address;
this.__my_service = options.service;
2018-05-31 04:48:32 +00:00
this.__my_serviceport = options.serviceport;
this.__my_name = options.name;
2016-10-01 02:07:01 +00:00
Transform.call(this, options);
}
util.inherits(MyTransform, Transform);
MyTransform.prototype._transform = function (data, encoding, callback) {
2018-04-29 01:25:46 +00:00
var address = this.__my_addr;
address.service = address.service || this.__my_service;
2018-05-31 04:48:32 +00:00
address.serviceport = address.serviceport || this.__my_serviceport;
address.name = address.name || this.__my_name;
2018-04-29 01:25:46 +00:00
this.push(Packer.pack(address, data));
callback();
2016-10-01 02:07:01 +00:00
};
Packer.Stream = {};
var Dup = {
write: function (chunk, encoding, cb) {
//console.log('_write', chunk.byteLength);
2018-04-29 01:25:46 +00:00
this.__my_socket.write(chunk, encoding, cb);
2016-10-01 02:07:01 +00:00
}
, read: function (size) {
//console.log('_read');
var x = this.__my_socket.read(size);
if (x) {
console.log('_read', size);
this.push(x);
}
}
};
Packer.Stream.create = function (socket) {
2018-04-29 01:25:46 +00:00
if (!Packer.Stream.warned) {
console.warn('`Stream.create` deprecated, use `wrapSocket` instead');
Packer.Stream.warned = true;
}
2016-10-01 02:07:01 +00:00
// Workaround for
// https://github.com/nodejs/node/issues/8854
// https://www.google.com/#q=get+socket+address+from+file+descriptor
// TODO try creating a new net.Socket({ handle: socket._handle, fd: socket._handle.fd })
// from the old one and then adding back the data with
// sock.push(firstChunk)
var Duplex = require('stream').Duplex;
var myDuplex = new Duplex();
myDuplex.__my_socket = socket;
myDuplex._write = Dup.write;
myDuplex._read = Dup.read;
//console.log('plainSocket.*Address');
//console.log('remote:', socket.remoteAddress);
//console.log('local:', socket.localAddress);
//console.log('address():', socket.address());
myDuplex.remoteFamily = socket.remoteFamily;
myDuplex.remoteAddress = socket.remoteAddress;
myDuplex.remotePort = socket.remotePort;
myDuplex.localFamily = socket.localFamily;
myDuplex.localAddress = socket.localAddress;
myDuplex.localPort = socket.localPort;
return myDuplex;
};
Packer.Transform = {};
Packer.Transform.create = function (opts) {
// Note: service refers to the port that the incoming request was from,
// if known (smtps, smtp, https, http, etc)
// { address: '127.0.0.1', service: 'https' }
return new MyTransform(opts);
};