v1.4.1
This commit is contained in:
parent
74e7cc0fc7
commit
6ae1cddcfc
91
index.js
91
index.js
|
@ -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;
|
||||||
|
|
||||||
|
@ -15,6 +32,8 @@ Packer.create = function (opts) {
|
||||||
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('tunnelEnd', msg);
|
machine.emit(serviceEvents[msg.service] || serviceEvents.default);
|
||||||
}
|
} else {
|
||||||
else {
|
(machine[serviceFuncs[msg.service]] || machine[serviceFuncs.default])(msg);
|
||||||
(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,12 +251,6 @@ Packer.socketToId = function (socket) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* Tunnel Packer
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
var addressNames = [
|
var addressNames = [
|
||||||
'remoteAddress'
|
'remoteAddress'
|
||||||
, 'remotePort'
|
, 'remotePort'
|
||||||
|
@ -275,8 +258,18 @@ Packer.socketToId = function (socket) {
|
||||||
, 'localAddress'
|
, 'localAddress'
|
||||||
, 'localPort'
|
, 'localPort'
|
||||||
];
|
];
|
||||||
// Imporoved workaround for https://github.com/nodejs/node/issues/8854
|
var sockFuncs = [
|
||||||
// Unlike Duplex this should handle all of the events needed to make everything work.
|
'address'
|
||||||
|
, 'destroy'
|
||||||
|
, 'ref'
|
||||||
|
, 'unref'
|
||||||
|
, 'setEncoding'
|
||||||
|
, 'setKeepAlive'
|
||||||
|
, '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;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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": {
|
||||||
|
|
Loading…
Reference in New Issue