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,113 +39,140 @@ 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 () {
console.log('[open] tunneler connected');
var localclients = {};
/* function run() {
setInterval(function () { var wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: false });
console.log('');
console.log('localclients.length:', Object.keys(localclients).length);
console.log('');
}, 5000);
*/
//wstunneler.send(token); function onOpen() {
console.log('[open] tunneler connected');
var localclients = {};
// BaaS / Backendless / noBackend / horizon.io /*
// user authentication setInterval(function () {
// a place to store data console.log('');
// file management console.log('localclients.length:', Object.keys(localclients).length);
// Synergy Teamwork Paradigm = Jabberwocky console.log('');
var pack = require('tunnel-packer').pack; }, 5000);
*/
function onMessage(opts) { //wstunneler.send(token);
var cid = addrToId(opts);
console.log('[wsclient] onMessage:', cid);
var service = opts.service;
var port = services[service];
var lclient;
var servername;
var str;
var m;
if (opts.data.byteLength < 20) { // BaaS / Backendless / noBackend / horizon.io
if ('|__ERROR__|' === opts.data.toString('ascii') // user authentication
|| '|__END__|' === opts.data.toString('ascii')) { // a place to store data
// file management
// Synergy Teamwork Paradigm = Jabberwocky
var pack = require('tunnel-packer').pack;
var handlers = {
onmessage: function (opts) {
var cid = addrToId(opts);
console.log('[wsclient] onMessage:', cid);
var service = opts.service;
var port = services[service];
var lclient;
var servername;
var str;
var m;
console.log("['" + opts.data.toString('ascii') + "'] '" + cid + "'"); if (opts.data.byteLength < 20) {
if (localclients[cid]) { if ('|__ERROR__|' === opts.data.toString('ascii')) {
localclients[cid].end(); handlers.onerror(opts);
return;
}
else if ('|__END__|' === opts.data.toString('ascii')) {
handlers.onend(opts);
return;
}
}
function endWithError() {
wstunneler.send(pack(opts, Buffer.from('|__ERROR__|'), 'error'), { binary: true });
}
if (localclients[cid]) {
console.log("[=>] received data from '" + cid + "' =>", opts.data.byteLength);
localclients[cid].write(opts.data);
return;
}
else if ('http' === service) {
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 {
endWithError();
return;
}
if (!servername) {
console.warn("|__ERROR__| no servername found for '" + cid + "'");
console.warn(opts.data.toString());
wstunneler.send(pack(opts, Buffer.from('|__ERROR__|'), 'error'), { binary: true });
return;
}
console.log("servername: '" + servername + "'");
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) {
console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
console.log(JSON.stringify(chunk.toString()));
wstunneler.send(pack(opts, chunk), { binary: true });
});
lclient.on('error', function (err) {
console.error("[error] local '" + opts.service + "' '" + cid + "'");
console.error(err);
delete localclients[cid];
wstunneler.send(pack(opts, Buffer.from('|__ERROR__|'), 'error'), { binary: true });
});
lclient.on('end', function () {
console.log("[end] local '" + opts.service + "' '" + cid + "'");
delete localclients[cid];
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]; delete localclients[cid];
} }
return; };
} var machine = require('tunnel-packer').create(handlers);
wstunneler.on('message', machine.fns.addChunk);
} }
function endWithError() { wstunneler.on('open', onOpen);
wstunneler.send(pack(opts, Buffer.from('|__ERROR__|'), 'error'), { binary: true });
}
if (localclients[cid]) { wstunneler.on('close', function () {
console.log("[=>] received data from '" + cid + "' =>", opts.data.byteLength); console.log('retry on close');
localclients[cid].write(opts.data); setTimeout(run, 5000);
return; });
}
else if ('http' === service) {
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 {
endWithError();
return;
}
if (!servername) { wstunneler.on('error', function (err) {
console.warn("|__ERROR__| no servername found for '" + cid + "'"); console.error("[error] will retry on 'close'");
console.warn(opts.data.toString()); console.error(err);
wstunneler.send(pack(opts, Buffer.from('|__ERROR__|'), 'error'), { binary: true });
return;
}
console.log("servername: '" + servername + "'");
lclient = localclients[cid] = net.createConnection({ port: port, host: '127.0.0.1' }, function () {
lclient.on('data', function (chunk) {
console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
console.log(JSON.stringify(chunk.toString()));
wstunneler.send(pack(opts, chunk), { binary: true });
});
lclient.on('error', function (err) {
console.error("[error] local '" + opts.service + "' '" + cid + "'");
console.error(err);
delete localclients[cid];
wstunneler.send(pack(opts, Buffer.from('|__ERROR__|'), 'error'), { binary: true });
});
lclient.on('end', function () {
console.log("[end] local '" + opts.service + "' '" + cid + "'");
delete localclients[cid];
wstunneler.send(pack(opts, Buffer.from('|__END__|'), 'end'), { binary: true });
});
console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength);
lclient.write(opts.data);
}); });
} }
var machine = require('tunnel-packer').create({ onMessage: onMessage }); run();
wstunneler.on('message', machine.fns.addChunk);
wstunneler.on('close', function () {
console.log('end');
});
});
}()); }());