This commit is contained in:
AJ ONeal 2016-10-11 17:43:24 -06:00
parent 6584b1ef89
commit 606858bf10
2 changed files with 83 additions and 58 deletions

View File

@ -8,44 +8,54 @@ var program = require('commander');
var url = require('url'); var url = require('url');
var stunnel = require('../wsclient.js'); var stunnel = require('../wsclient.js');
function parseProxy(location) {
// http:john.example.com:3000
// http://john.example.com:3000
var parts = location.split(':');
//var dual = false;
if (/\./.test(parts[0])) {
//dual = true;
parts[2] = parts[1];
parts[1] = parts[0];
parts[0] = 'https';
}
parts[0] = parts[0].toLowerCase();
parts[1] = parts[1].toLowerCase().replace(/(\/\/)?/, '') || '*';
parts[2] = parseInt(parts[2], 10) || 0;
if (!parts[2]) {
// TODO grab OS list of standard ports?
if ('http' === parts[0]) {
parts[2] = 80;
}
else if ('https' === parts[0]) {
parts[2] = 443;
}
else {
throw new Error("port must be specified - ex: tls:*:1337");
}
}
return {
protocol: parts[0]
, hostname: parts[1]
, port: parts[2]
};
}
function collectProxies(val, memo) { function collectProxies(val, memo) {
var vals = val.split(/,/g); var vals = val.split(/,/g);
vals.map(parseProxy).forEach(function (val) {
memo.push(val); function parseProxy(location) {
// http:john.example.com:3000
// http://john.example.com:3000
var parts = location.split(':');
var dual = false;
if (/\./.test(parts[0])) {
//dual = true;
parts[2] = parts[1];
parts[1] = parts[0];
parts[0] = 'https';
dual = true;
}
parts[0] = parts[0].toLowerCase();
parts[1] = parts[1].toLowerCase().replace(/(\/\/)?/, '') || '*';
parts[2] = parseInt(parts[2], 10) || 0;
if (!parts[2]) {
// TODO grab OS list of standard ports?
if ('http' === parts[0]) {
parts[2] = 80;
}
else if ('https' === parts[0]) {
parts[2] = 443;
}
else {
throw new Error("port must be specified - ex: tls:*:1337");
}
}
memo.push({
protocol: parts[0]
, hostname: parts[1]
, port: parts[2]
});
if (dual) {
memo.push({
protocol: 'http'
, hostname: parts[1]
, port: parts[2]
});
}
}
vals.map(function (val) {
return parseProxy(val);
}); });
return memo; return memo;
@ -71,8 +81,7 @@ program.stunneld = program.stunneld || 'wss://pokemap.hellabit.com:3000';
var jwt = require('jsonwebtoken'); var jwt = require('jsonwebtoken');
var domainsMap = {}; var domainsMap = {};
var tokenData = { var tokenData = {
name: null domains: null
, domains: null
}; };
var location = url.parse(program.stunneld); var location = url.parse(program.stunneld);
@ -86,7 +95,6 @@ program.locals.forEach(function (proxy) {
domainsMap[proxy.hostname] = true; domainsMap[proxy.hostname] = true;
}); });
tokenData.domains = Object.keys(domainsMap); tokenData.domains = Object.keys(domainsMap);
tokenData.name = tokenData.domains[0];
program.token = program.token || jwt.sign(tokenData, program.secret || 'shhhhh'); program.token = program.token || jwt.sign(tokenData, program.secret || 'shhhhh');

View File

@ -6,26 +6,15 @@ var sni = require('sni');
var Packer = require('tunnel-packer'); var Packer = require('tunnel-packer');
var authenticated = false; var authenticated = false;
/*
var request = require('request');
request.get('https://pokemap.hellabit.com/?access_token=' + token, { rejectUnauthorized: false }, function (err, resp) {
console.log('resp.body');
console.log(resp.body);
});
return;
//*/
function run(copts) { function run(copts) {
// TODO pair with hostname / sni
copts.services = {}; copts.services = {};
copts.locals.forEach(function (proxy) { copts.locals.forEach(function (proxy) {
//program.services = { 'ssh': 22, 'http': 80, 'https': 443 }; //program.services = { 'ssh': 22, 'http': 80, 'https': 443 };
copts.services[proxy.protocol] = proxy.port; copts.services[proxy.protocol] = proxy.port;
}); });
var services = copts.services; // TODO pair with hostname / sni var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + copts.token;
var token = copts.token;
var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + token;
var wstunneler; var wstunneler;
var localclients = {}; var localclients = {};
// BaaS / Backendless / noBackend / horizon.io // BaaS / Backendless / noBackend / horizon.io
@ -38,7 +27,7 @@ function run(copts) {
var net = copts.net || require('net'); var net = copts.net || require('net');
var cid = Packer.addrToId(opts); var cid = Packer.addrToId(opts);
var service = opts.service; var service = opts.service;
var port = services[service]; var port = copts.services[service];
var servername; var servername;
var str; var str;
var m; var m;
@ -72,22 +61,35 @@ function run(copts) {
console.info("[connect] new client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() + 1) + " clients)"); console.info("[connect] new client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() + 1) + " clients)");
console.log('port', port, opts.port, service, copts.services);
localclients[cid] = net.createConnection({ localclients[cid] = net.createConnection({
servername: servername port: port
, port: port
, host: '127.0.0.1' , host: '127.0.0.1'
, servername: servername
, data: opts.data , data: opts.data
, remoteFamily: opts.family , remoteFamily: opts.family
, remoteAddress: opts.address , remoteAddress: opts.address
, remotePort: opts.port , remotePort: opts.port
}, function () { }, function () {
//console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength); //console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength);
localclients[cid].write(opts.data); //localclients[cid].write(opts.data);
//localclients[cid].resume();
}); });
// 'data' // 'data'
localclients[cid].on('readable', function (size) { localclients[cid].on('readable', function (size) {
var chunk; var chunk;
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;
}
do { do {
chunk = localclients[cid].read(size); chunk = localclients[cid].read(size);
//console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes"); //console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
@ -102,6 +104,8 @@ function run(copts) {
console.info("[end] closing client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() - 1) + " clients)"); console.info("[end] closing client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() - 1) + " clients)");
handlers._onLocalClose(cid, opts); handlers._onLocalClose(cid, opts);
}); });
//localclients[cid].pause();
localclients[cid].write(opts.data);
} }
, onend: function (opts) { , onend: function (opts) {
var cid = Packer.addrToId(opts); var cid = Packer.addrToId(opts);
@ -114,6 +118,7 @@ function run(copts) {
handlers._onend(cid); handlers._onend(cid);
} }
, _onend: function (cid) { , _onend: function (cid) {
console.log('[_onend]');
if (localclients[cid]) { if (localclients[cid]) {
try { try {
localclients[cid].end(); localclients[cid].end();
@ -124,6 +129,7 @@ function run(copts) {
delete localclients[cid]; delete localclients[cid];
} }
, _onLocalClose: function (cid, opts, err) { , _onLocalClose: function (cid, opts, err) {
console.log('[_onLocalClose]');
try { try {
wstunneler.send(Packer.pack(opts, null, err && 'error' || 'end'), { binary: true }); wstunneler.send(Packer.pack(opts, null, err && 'error' || 'end'), { binary: true });
} catch(e) { } catch(e) {
@ -146,6 +152,7 @@ function run(copts) {
, retry: true , retry: true
, closeClients: function () { , closeClients: function () {
console.log('[close clients]');
Object.keys(localclients).forEach(function (cid) { Object.keys(localclients).forEach(function (cid) {
try { try {
localclients[cid].end(); localclients[cid].end();
@ -157,6 +164,7 @@ function run(copts) {
} }
, onClose: function () { , onClose: function () {
console.log('ON CLOSE');
if (!authenticated) { if (!authenticated) {
console.info('[close] failed on first attempt... check authentication.'); console.info('[close] failed on first attempt... check authentication.');
} }
@ -206,8 +214,17 @@ function run(copts) {
}); });
wstunneler.on('close', wsHandlers.onClose); wstunneler.on('close', wsHandlers.onClose);
wstunneler.on('error', wsHandlers.onError); wstunneler.on('error', wsHandlers.onError);
process.on('exit', wsHandlers.onExit); process.on('beforeExit', function (x) {
process.on('SIGINT', wsHandlers.onExit); 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);
});
} }
module.exports.connect = run; module.exports.connect = run;