This commit is contained in:
AJ ONeal 2018-04-28 19:28:54 -06:00
parent 74e7cc0fc7
commit 6ae1cddcfc
2 changed files with 56 additions and 57 deletions

111
index.js
View File

@ -2,6 +2,23 @@
var Packer = module.exports; var Packer = module.exports;
var serviceEvents = {
default: 'tunnelData'
, control: 'tunnelControl'
, error: 'tunnelError'
, end: 'tunnelEnd'
, pause: 'tunnelPause'
, resume: 'tunnelResume'
};
var serviceFuncs = {
default: 'onmessage'
, control: 'oncontrol'
, error: 'onerror'
, end: 'onend'
, pause: 'onpause'
, resume: 'onresume'
};
Packer.create = function (opts) { Packer.create = function (opts) {
var machine; var machine;
@ -13,8 +30,10 @@ Packer.create = function (opts) {
machine.onmessage = opts.onmessage || opts.onMessage; machine.onmessage = opts.onmessage || opts.onMessage;
machine.oncontrol = opts.oncontrol || opts.onControl; machine.oncontrol = opts.oncontrol || opts.onControl;
machine.onerror = opts.onerror || opts.onError; machine.onerror = opts.onerror || opts.onError;
machine.onend = opts.onend || opts.onEnd; machine.onend = opts.onend || opts.onEnd;
machine.onpause = opts.onpause || opts.onPause;
machine.onresume = opts.onresume || opts.onResume;
machine._version = 1; machine._version = 1;
machine.fns = {}; machine.fns = {};
@ -135,37 +154,10 @@ Packer.create = function (opts) {
msg.service = machine.service; msg.service = machine.service;
msg.data = data; msg.data = data;
if ('end' === machine.service) { if (machine.emit) {
if (machine.emit) { machine.emit(serviceEvents[msg.service] || serviceEvents.default);
machine.emit('tunnelEnd', msg); } else {
} (machine[serviceFuncs[msg.service]] || machine[serviceFuncs.default])(msg);
else {
(machine.onend||machine.onmessage)(msg);
}
}
else if ('error' === machine.service) {
if (machine.emit) {
machine.emit('tunnelError', msg);
}
else {
(machine.onerror||machine.onmessage)(msg);
}
}
else if ('control' === machine.service) {
if (machine.emit) {
machine.emit('tunnelControl', msg);
}
else {
(machine.oncontrol||machine.onmessage)(msg);
}
}
else {
if (machine.emit) {
machine.emit('tunnelData', msg);
}
else {
machine.onmessage(msg);
}
} }
return true; return true;
@ -191,19 +183,16 @@ Packer.create = function (opts) {
}; };
Packer.pack = function (address, data, service) { Packer.pack = function (address, data, service) {
data = data || Buffer.alloc(1); data = data || Buffer.from(' ');
if (!Buffer.isBuffer(data)) { if (!Buffer.isBuffer(data)) {
data = new Buffer(JSON.stringify(data)); data = new Buffer(JSON.stringify(data));
} }
if (!data.byteLength) { if (!data.byteLength) {
data = Buffer.alloc(1); data = Buffer.from(' ');
} }
if ('error' === service) { if (service && service !== 'control') {
address.service = 'error'; address.service = service;
}
else if ('end' === service) {
address.service = 'end';
} }
var version = 1; var version = 1;
@ -262,21 +251,25 @@ Packer.socketToId = function (socket) {
}; };
/* var addressNames = [
* 'remoteAddress'
* Tunnel Packer , 'remotePort'
* , 'remoteFamily'
*/ , 'localAddress'
, 'localPort'
var addressNames = [ ];
'remoteAddress' var sockFuncs = [
, 'remotePort' 'address'
, 'remoteFamily' , 'destroy'
, 'localAddress' , 'ref'
, 'localPort' , 'unref'
]; , 'setEncoding'
// Imporoved workaround for https://github.com/nodejs/node/issues/8854 , 'setKeepAlive'
// Unlike Duplex this should handle all of the events needed to make everything work. , 'setNoDelay'
, 'setTimeout'
];
// Improved workaround for https://github.com/nodejs/node/issues/8854
// Unlike Packer.Stream.create this should handle all of the events needed to make everything work.
Packer.wrapSocket = function (socket) { Packer.wrapSocket = function (socket) {
var myDuplex = new require('stream').Duplex(); var myDuplex = new require('stream').Duplex();
addressNames.forEach(function (name) { addressNames.forEach(function (name) {
@ -310,7 +303,13 @@ Packer.wrapSocket = function (socket) {
socket.on('close', function () { socket.on('close', function () {
myDuplex.emit('close'); myDuplex.emit('close');
}); });
myDuplex.destroy = socket.destroy.bind(socket); 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);
}
});
return myDuplex; return myDuplex;
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "tunnel-packer", "name": "tunnel-packer",
"version": "1.3.1", "version": "1.4.1",
"description": "A strategy for packing and unpacking a proxy stream (i.e. packets through a tunnel). Handles multiplexed and tls connections. Used by telebit and telebitd.", "description": "A strategy for packing and unpacking a proxy stream (i.e. packets through a tunnel). Handles multiplexed and tls connections. Used by telebit and telebitd.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {