diff --git a/bin/stunnel.js b/bin/stunnel.js index 1c7b75b..027dd2a 100755 --- a/bin/stunnel.js +++ b/bin/stunnel.js @@ -8,44 +8,54 @@ var program = require('commander'); var url = require('url'); 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) { 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; @@ -71,8 +81,7 @@ program.stunneld = program.stunneld || 'wss://pokemap.hellabit.com:3000'; var jwt = require('jsonwebtoken'); var domainsMap = {}; var tokenData = { - name: null -, domains: null + domains: null }; var location = url.parse(program.stunneld); @@ -86,7 +95,6 @@ program.locals.forEach(function (proxy) { domainsMap[proxy.hostname] = true; }); tokenData.domains = Object.keys(domainsMap); -tokenData.name = tokenData.domains[0]; program.token = program.token || jwt.sign(tokenData, program.secret || 'shhhhh'); diff --git a/wsclient.js b/wsclient.js index 5cf8f76..fbb31e0 100644 --- a/wsclient.js +++ b/wsclient.js @@ -6,26 +6,15 @@ var sni = require('sni'); var Packer = require('tunnel-packer'); 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) { + // TODO pair with hostname / sni copts.services = {}; copts.locals.forEach(function (proxy) { //program.services = { 'ssh': 22, 'http': 80, 'https': 443 }; copts.services[proxy.protocol] = proxy.port; }); - var services = copts.services; // TODO pair with hostname / sni - var token = copts.token; - var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + token; + var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + copts.token; var wstunneler; var localclients = {}; // BaaS / Backendless / noBackend / horizon.io @@ -38,7 +27,7 @@ function run(copts) { var net = copts.net || require('net'); var cid = Packer.addrToId(opts); var service = opts.service; - var port = services[service]; + var port = copts.services[service]; var servername; var str; var m; @@ -72,22 +61,35 @@ function run(copts) { 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({ - servername: servername - , port: port + port: port , host: '127.0.0.1' + + , servername: servername , data: opts.data , remoteFamily: opts.family , remoteAddress: opts.address , remotePort: opts.port }, function () { //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' localclients[cid].on('readable', function (size) { 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 { chunk = localclients[cid].read(size); //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)"); handlers._onLocalClose(cid, opts); }); + //localclients[cid].pause(); + localclients[cid].write(opts.data); } , onend: function (opts) { var cid = Packer.addrToId(opts); @@ -114,6 +118,7 @@ function run(copts) { handlers._onend(cid); } , _onend: function (cid) { + console.log('[_onend]'); if (localclients[cid]) { try { localclients[cid].end(); @@ -124,6 +129,7 @@ function run(copts) { delete localclients[cid]; } , _onLocalClose: function (cid, opts, err) { + console.log('[_onLocalClose]'); try { wstunneler.send(Packer.pack(opts, null, err && 'error' || 'end'), { binary: true }); } catch(e) { @@ -146,6 +152,7 @@ function run(copts) { , retry: true , closeClients: function () { + console.log('[close clients]'); Object.keys(localclients).forEach(function (cid) { try { localclients[cid].end(); @@ -157,6 +164,7 @@ function run(copts) { } , onClose: function () { + console.log('ON CLOSE'); if (!authenticated) { console.info('[close] failed on first attempt... check authentication.'); } @@ -206,8 +214,17 @@ function run(copts) { }); wstunneler.on('close', wsHandlers.onClose); wstunneler.on('error', wsHandlers.onError); - process.on('exit', wsHandlers.onExit); - process.on('SIGINT', wsHandlers.onExit); + 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); + }); } module.exports.connect = run;