better handling and error handling

This commit is contained in:
AJ ONeal 2016-09-30 01:23:03 -04:00
parent 1bdfad6691
commit 71b3815c52
1 changed files with 120 additions and 93 deletions

View File

@ -39,8 +39,12 @@ request.get('https://pokemap.hellabit.com:3000?access_token=' + token, { rejectU
return; return;
//*/ //*/
var wstunneler = new WebSocket('wss://pokemap.hellabit.com:3000/?access_token=' + token, { rejectUnauthorized: false }); var tunnelUrl = 'wss://pokemap.hellabit.com:3000/?access_token=' + token;
wstunneler.on('open', function () {
function run() {
var wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: false });
function onOpen() {
console.log('[open] tunneler connected'); console.log('[open] tunneler connected');
var localclients = {}; var localclients = {};
@ -60,8 +64,8 @@ wstunneler.on('open', function () {
// file management // file management
// Synergy Teamwork Paradigm = Jabberwocky // Synergy Teamwork Paradigm = Jabberwocky
var pack = require('tunnel-packer').pack; var pack = require('tunnel-packer').pack;
var handlers = {
function onMessage(opts) { onmessage: function (opts) {
var cid = addrToId(opts); var cid = addrToId(opts);
console.log('[wsclient] onMessage:', cid); console.log('[wsclient] onMessage:', cid);
var service = opts.service; var service = opts.service;
@ -72,14 +76,12 @@ wstunneler.on('open', function () {
var m; var m;
if (opts.data.byteLength < 20) { if (opts.data.byteLength < 20) {
if ('|__ERROR__|' === opts.data.toString('ascii') if ('|__ERROR__|' === opts.data.toString('ascii')) {
|| '|__END__|' === opts.data.toString('ascii')) { handlers.onerror(opts);
return;
console.log("['" + opts.data.toString('ascii') + "'] '" + cid + "'");
if (localclients[cid]) {
localclients[cid].end();
delete localclients[cid];
} }
else if ('|__END__|' === opts.data.toString('ascii')) {
handlers.onend(opts);
return; return;
} }
} }
@ -116,7 +118,9 @@ wstunneler.on('open', function () {
console.log("servername: '" + servername + "'"); console.log("servername: '" + servername + "'");
lclient = localclients[cid] = net.createConnection({ port: port, host: '127.0.0.1' }, function () { lclient = localclients[cid] = net.createConnection({ port: port, host: '127.0.0.1' }, function () {
console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength);
lclient.write(opts.data);
});
lclient.on('data', function (chunk) { lclient.on('data', function (chunk) {
console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes"); console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
console.log(JSON.stringify(chunk.toString())); console.log(JSON.stringify(chunk.toString()));
@ -133,19 +137,42 @@ wstunneler.on('open', function () {
delete localclients[cid]; delete localclients[cid];
wstunneler.send(pack(opts, Buffer.from('|__END__|'), 'end'), { binary: true }); wstunneler.send(pack(opts, Buffer.from('|__END__|'), 'end'), { binary: true });
}); });
}
, onend: function (opts) {
var cid = addrToId(opts);
console.log("[end] '" + cid + "'");
handlers._onend(cid, opts);
}
, onerror: function (opts) {
var cid = addrToId(opts);
console.log("[error] '" + cid + "'", opts.code || '', opts.message);
handlers._onend(cid);
}
, _onend: function (cid) {
if (localclients[cid]) {
localclients[cid].end();
}
delete localclients[cid];
}
};
var machine = require('tunnel-packer').create(handlers);
console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength); wstunneler.on('message', machine.fns.addChunk);
lclient.write(opts.data); }
wstunneler.on('open', onOpen);
wstunneler.on('close', function () {
console.log('retry on close');
setTimeout(run, 5000);
});
wstunneler.on('error', function (err) {
console.error("[error] will retry on 'close'");
console.error(err);
}); });
} }
var machine = require('tunnel-packer').create({ onMessage: onMessage }); run();
wstunneler.on('message', machine.fns.addChunk);
wstunneler.on('close', function () {
console.log('end');
});
});
}()); }());