2016-09-30 01:10:40 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var WebSocket = require('ws');
|
|
|
|
var sni = require('sni');
|
2016-10-06 05:55:48 +00:00
|
|
|
var Packer = require('tunnel-packer');
|
2016-09-30 19:04:27 +00:00
|
|
|
var authenticated = false;
|
2016-09-30 01:10:40 +00:00
|
|
|
|
2016-09-30 19:04:27 +00:00
|
|
|
function run(copts) {
|
2016-10-11 23:43:24 +00:00
|
|
|
var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + copts.token;
|
2016-09-30 19:04:27 +00:00
|
|
|
var wstunneler;
|
|
|
|
var localclients = {};
|
|
|
|
// BaaS / Backendless / noBackend / horizon.io
|
|
|
|
// user authentication
|
|
|
|
// a place to store data
|
|
|
|
// file management
|
|
|
|
// Synergy Teamwork Paradigm = Jabberwocky
|
|
|
|
var handlers = {
|
|
|
|
onmessage: function (opts) {
|
2016-10-06 05:02:28 +00:00
|
|
|
var net = copts.net || require('net');
|
2016-10-06 05:55:48 +00:00
|
|
|
var cid = Packer.addrToId(opts);
|
2017-04-03 21:11:59 +00:00
|
|
|
var service = opts.service.toLowerCase();
|
|
|
|
var portList = copts.services[service];
|
2016-09-30 19:04:27 +00:00
|
|
|
var servername;
|
2017-04-03 21:11:59 +00:00
|
|
|
var port;
|
2016-09-30 19:04:27 +00:00
|
|
|
var str;
|
|
|
|
var m;
|
|
|
|
|
|
|
|
authenticated = true;
|
|
|
|
|
|
|
|
if (localclients[cid]) {
|
|
|
|
//console.log("[=>] received data from '" + cid + "' =>", opts.data.byteLength);
|
|
|
|
localclients[cid].write(opts.data);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-03 21:11:59 +00:00
|
|
|
if (!portList) {
|
|
|
|
handlers._onLocalError(cid, opts, new Error("unsupported service '" + service + "'"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('http' === service) {
|
2016-09-30 19:04:27 +00:00
|
|
|
str = opts.data.toString();
|
|
|
|
m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im);
|
|
|
|
servername = (m && m[1].toLowerCase() || '').split(':')[0];
|
|
|
|
}
|
|
|
|
else if ('https' === service) {
|
|
|
|
servername = sni(opts.data);
|
|
|
|
}
|
|
|
|
else {
|
2017-04-03 21:11:59 +00:00
|
|
|
servername = '*';
|
2016-09-30 19:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!servername) {
|
|
|
|
//console.warn(opts.data.toString());
|
2017-04-03 21:11:59 +00:00
|
|
|
handlers._onLocalError(cid, opts, new Error("missing servername for '" + cid + "' " + opts.data.byteLength));
|
2016-09-30 19:04:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-03 21:11:59 +00:00
|
|
|
port = portList[servername] || portList['*'];
|
2016-09-30 19:04:27 +00:00
|
|
|
|
|
|
|
console.info("[connect] new client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() + 1) + " clients)");
|
|
|
|
|
2017-04-03 21:11:59 +00:00
|
|
|
console.log('port', port, opts.port, service, portList);
|
2016-10-06 05:02:28 +00:00
|
|
|
localclients[cid] = net.createConnection({
|
2016-10-11 23:43:24 +00:00
|
|
|
port: port
|
2016-10-06 05:02:28 +00:00
|
|
|
, host: '127.0.0.1'
|
2016-10-11 23:43:24 +00:00
|
|
|
|
|
|
|
, servername: servername
|
2016-10-06 05:10:16 +00:00
|
|
|
, data: opts.data
|
2016-10-06 21:01:58 +00:00
|
|
|
, remoteFamily: opts.family
|
|
|
|
, remoteAddress: opts.address
|
|
|
|
, remotePort: opts.port
|
2016-10-06 05:02:28 +00:00
|
|
|
}, function () {
|
2016-09-30 19:04:27 +00:00
|
|
|
//console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength);
|
2016-10-12 19:14:16 +00:00
|
|
|
// this will happen before 'data' is triggered
|
2016-10-12 18:59:13 +00:00
|
|
|
localclients[cid].write(opts.data);
|
2016-09-30 19:04:27 +00:00
|
|
|
});
|
2016-10-06 05:02:28 +00:00
|
|
|
// 'data'
|
2016-10-12 19:11:21 +00:00
|
|
|
/*
|
2016-10-12 18:59:13 +00:00
|
|
|
localclients[cid].on('data', function (chunk) {
|
|
|
|
//console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
|
|
|
|
//console.log(JSON.stringify(chunk.toString()));
|
|
|
|
wstunneler.send(Packer.pack(opts, chunk), { binary: true });
|
|
|
|
});
|
2016-10-12 19:11:21 +00:00
|
|
|
//*/
|
|
|
|
///*
|
2016-10-06 05:02:28 +00:00
|
|
|
localclients[cid].on('readable', function (size) {
|
|
|
|
var chunk;
|
|
|
|
|
2016-10-11 23:43:24 +00:00
|
|
|
if (!localclients[cid]) {
|
|
|
|
console.error("[error] localclients[cid]", cid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!localclients[cid].read) {
|
|
|
|
console.error("[error] localclients[cid].read", cid);
|
|
|
|
console.log(localclients[cid]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-06 05:02:28 +00:00
|
|
|
do {
|
|
|
|
chunk = localclients[cid].read(size);
|
|
|
|
//console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
|
|
|
|
//console.log(JSON.stringify(chunk.toString()));
|
2016-10-12 19:11:21 +00:00
|
|
|
if (chunk) {
|
|
|
|
wstunneler.send(Packer.pack(opts, chunk), { binary: true });
|
|
|
|
}
|
2016-10-06 05:02:28 +00:00
|
|
|
} while (chunk);
|
2016-09-30 19:04:27 +00:00
|
|
|
});
|
2016-10-12 19:11:21 +00:00
|
|
|
//*/
|
2016-09-30 19:04:27 +00:00
|
|
|
localclients[cid].on('error', function (err) {
|
|
|
|
handlers._onLocalError(cid, opts, err);
|
|
|
|
});
|
|
|
|
localclients[cid].on('end', function () {
|
|
|
|
console.info("[end] closing client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() - 1) + " clients)");
|
|
|
|
handlers._onLocalClose(cid, opts);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
, onend: function (opts) {
|
2016-10-06 05:55:48 +00:00
|
|
|
var cid = Packer.addrToId(opts);
|
2016-09-30 19:04:27 +00:00
|
|
|
//console.log("[end] '" + cid + "'");
|
|
|
|
handlers._onend(cid);
|
|
|
|
}
|
|
|
|
, onerror: function (opts) {
|
2016-10-06 05:55:48 +00:00
|
|
|
var cid = Packer.addrToId(opts);
|
2016-09-30 19:04:27 +00:00
|
|
|
//console.log("[error] '" + cid + "'", opts.code || '', opts.message);
|
|
|
|
handlers._onend(cid);
|
|
|
|
}
|
|
|
|
, _onend: function (cid) {
|
2016-10-11 23:43:24 +00:00
|
|
|
console.log('[_onend]');
|
2016-09-30 19:04:27 +00:00
|
|
|
if (localclients[cid]) {
|
|
|
|
try {
|
|
|
|
localclients[cid].end();
|
|
|
|
} catch(e) {
|
|
|
|
// ignore
|
2016-09-30 01:10:40 +00:00
|
|
|
}
|
2016-09-30 19:04:27 +00:00
|
|
|
}
|
|
|
|
delete localclients[cid];
|
|
|
|
}
|
|
|
|
, _onLocalClose: function (cid, opts, err) {
|
2016-10-11 23:43:24 +00:00
|
|
|
console.log('[_onLocalClose]');
|
2016-09-30 19:04:27 +00:00
|
|
|
try {
|
2016-10-06 05:55:48 +00:00
|
|
|
wstunneler.send(Packer.pack(opts, null, err && 'error' || 'end'), { binary: true });
|
2016-09-30 19:04:27 +00:00
|
|
|
} catch(e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
delete localclients[cid];
|
|
|
|
}
|
|
|
|
, _onLocalError: function (cid, opts, err) {
|
|
|
|
console.info("[error] closing '" + cid + "' because '" + err.message + "' (" + (handlers._numClients() - 1) + " clients)");
|
|
|
|
handlers._onLocalClose(cid, opts, err);
|
|
|
|
}
|
|
|
|
, _numClients: function () {
|
|
|
|
return Object.keys(localclients).length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var wsHandlers = {
|
|
|
|
onOpen: function () {
|
|
|
|
console.info("[open] connected to '" + copts.stunneld + "'");
|
2016-09-30 01:10:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 06:00:41 +00:00
|
|
|
, retry: true
|
|
|
|
, closeClients: function () {
|
2016-10-11 23:43:24 +00:00
|
|
|
console.log('[close clients]');
|
2016-10-06 06:00:41 +00:00
|
|
|
Object.keys(localclients).forEach(function (cid) {
|
|
|
|
try {
|
|
|
|
localclients[cid].end();
|
|
|
|
} catch(e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
delete localclients[cid];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-30 19:04:27 +00:00
|
|
|
, onClose: function () {
|
2016-10-11 23:43:24 +00:00
|
|
|
console.log('ON CLOSE');
|
2016-09-30 19:04:27 +00:00
|
|
|
if (!authenticated) {
|
|
|
|
console.info('[close] failed on first attempt... check authentication.');
|
|
|
|
}
|
2016-10-06 06:00:41 +00:00
|
|
|
else if (wsHandlers.retry) {
|
2016-09-30 19:04:27 +00:00
|
|
|
console.info('[retry] disconnected and waiting...');
|
|
|
|
setTimeout(run, 5000, copts);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.info('[close] closing tunnel to exit...');
|
|
|
|
}
|
2016-09-30 01:10:40 +00:00
|
|
|
|
2016-09-30 19:04:27 +00:00
|
|
|
process.removeListener('exit', wsHandlers.onExit);
|
|
|
|
process.removeListener('SIGINT', wsHandlers.onExit);
|
2016-10-06 06:00:41 +00:00
|
|
|
wsHandlers.closeClients();
|
2016-09-30 19:04:27 +00:00
|
|
|
}
|
2016-09-30 05:46:00 +00:00
|
|
|
|
2016-09-30 19:04:27 +00:00
|
|
|
, onError: function (err) {
|
|
|
|
console.error("[tunnel error] " + err.message);
|
2016-09-30 05:23:03 +00:00
|
|
|
console.error(err);
|
2016-09-30 19:04:27 +00:00
|
|
|
}
|
2016-09-30 05:46:00 +00:00
|
|
|
|
2016-09-30 19:04:27 +00:00
|
|
|
, onExit: function () {
|
2016-10-06 06:00:41 +00:00
|
|
|
console.log('[wait] closing wstunneler...');
|
|
|
|
wsHandlers.retry = false;
|
|
|
|
wsHandlers.closeClients();
|
2016-09-30 05:46:00 +00:00
|
|
|
try {
|
|
|
|
wstunneler.close();
|
|
|
|
} catch(e) {
|
2016-09-30 19:04:27 +00:00
|
|
|
console.error("[error] wstunneler.close()");
|
2016-09-30 05:46:00 +00:00
|
|
|
console.error(e);
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
2016-09-30 19:04:27 +00:00
|
|
|
};
|
2016-09-30 19:14:12 +00:00
|
|
|
var machine = require('tunnel-packer').create(handlers);
|
2016-09-30 19:04:27 +00:00
|
|
|
|
|
|
|
console.info("[connect] '" + copts.stunneld + "'");
|
|
|
|
|
|
|
|
wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: !copts.insecure });
|
|
|
|
wstunneler.on('open', wsHandlers.onOpen);
|
2016-09-30 19:14:12 +00:00
|
|
|
wstunneler.on('message', function (data, flags) {
|
|
|
|
if (data.error || '{' === data[0]) {
|
|
|
|
console.log(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
machine.fns.addChunk(data, flags);
|
|
|
|
});
|
2016-09-30 19:04:27 +00:00
|
|
|
wstunneler.on('close', wsHandlers.onClose);
|
|
|
|
wstunneler.on('error', wsHandlers.onError);
|
2016-10-11 23:43:24 +00:00
|
|
|
process.on('beforeExit', function (x) {
|
|
|
|
console.log('[beforeExit] event loop closing?', x);
|
|
|
|
});
|
|
|
|
process.on('exit', function (x) {
|
|
|
|
console.log('[exit] loop closed', x);
|
|
|
|
//wsHandlers.onExit(x);
|
|
|
|
});
|
|
|
|
process.on('SIGINT', function (x) {
|
|
|
|
console.log('SIGINT');
|
|
|
|
wsHandlers.onExit(x);
|
|
|
|
});
|
2016-09-30 19:04:27 +00:00
|
|
|
}
|
2016-09-30 05:46:00 +00:00
|
|
|
|
2016-09-30 19:04:27 +00:00
|
|
|
module.exports.connect = run;
|
2016-09-30 01:10:40 +00:00
|
|
|
|
|
|
|
}());
|