From 568bcdcab245bc041e43ca8a00ed30bd03007f33 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 27 Jul 2018 22:30:58 -0600 Subject: [PATCH] updates --- bin/telebit.js | 7 +++++-- bin/telebitd.js | 42 ++++++++++++++++++++++++++++++++++-------- lib/sorting-hat.js | 11 +++++------ package.json | 2 +- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/bin/telebit.js b/bin/telebit.js index 92848bd..d27b545 100755 --- a/bin/telebit.js +++ b/bin/telebit.js @@ -57,8 +57,8 @@ function help() { console.info('\ttelebit http /module/path # load a node module to handle all https traffic'); console.info(''); console.info('\ttelebit http none example.com # remove https handler from example.com'); - console.info('\ttelebit http 3001 example.com # forward https traffic for example.com to port 3001'); - console.info('\ttelebit http /module/path example.com # forward https traffic for example.com to port 3001'); + console.info('\ttelebit http 3001 sub.example.com # forward https traffic for sub.example.com to port 3001'); + console.info('\ttelebit http /module/path sub # forward https traffic for sub.example.com to port 3001'); console.info(''); console.info('\ttelebit tcp none # remove all tcp handlers'); console.info('\ttelebit tcp 5050 # forward all tcp to port 5050'); @@ -644,6 +644,7 @@ function parseCli(/*state*/) { } else if (/\/|\\/.test(argv[1])) { // looks like a path argv[1] = path.resolve(argv[1]); + // TODO make a default assignment here } else if (-1 === special.indexOf(argv[1])) { console.error("Not sure what you meant by '" + argv[1] + "'."); console.error("Remember: paths should begin with ." + path.sep + ", like '." + path.sep + argv[1] + "'"); @@ -669,6 +670,8 @@ function handleConfig(err, config) { //console.log('CONFIG'); //console.log(config); state.config = config; + var verstr = [ pkg.name + ' daemon v' + state.config.version ]; + console.info(verstr.join(' ')); if (err) { console.error(err); process.exit(101); return; } diff --git a/bin/telebitd.js b/bin/telebitd.js index 73b0f8e..d777c85 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -132,7 +132,22 @@ controllers.http = function (req, res, opts) { var appname = getAppname(portOrPath); var subdomain = opts.body[1]; var remoteHost; - if (subdomain) { + + // Assign an FQDN to brief subdomains + // ex: foo => foo.rando.telebit.cloud + if (subdomain && !/\./.test(subdomain)) { + Object.keys(state.servernames).some(function (key) { + if (state.servernames[key].wildcard) { + subdomain += '.' + key; + } + }); + } + + if ('none' === portOrPath) { + delete state.servernames[subdomain]; + remoteHost = 'none'; + } else if (subdomain) { + // use a subdomain with this handler var handlerName = getServername(state.servernames, subdomain); if (!handlerName) { active = false; @@ -143,6 +158,7 @@ controllers.http = function (req, res, opts) { state.servernames[subdomain].handler = portOrPath; remoteHost = subdomain; } else { + // just replace the default domain if (!Object.keys(state.servernames).sort(function (a, b) { return b.length - a.length; }).some(function (key) { @@ -152,17 +168,21 @@ controllers.http = function (req, res, opts) { return true; } if (state.servernames[key].wildcard) { - if (!state.servernames[appname + '.' + key]) { - state.servernames[appname + '.' + key] = {}; + //var prefix = appname + '.' + key; + var prefix = key; + if (!state.servernames[prefix]) { + state.servernames[prefix] = {}; } - state.servernames[appname + '.' + key].handler = portOrPath; - remoteHost = appname + '.' + key; + state.servernames[prefix].handler = portOrPath; + remoteHost = prefix; return true; } })) { Object.keys(state.servernames).some(function (key) { + //var prefix = appname + '.' + key; + var prefix = key; state.servernames[key].handler = portOrPath; - remoteHost = appname + '.' + key; + remoteHost = prefix; return true; }); } @@ -306,8 +326,10 @@ function serveControlsHelper() { } if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) { - res.setHeader('Content-Type', 'appliCation/json'); - res.end(JSON.stringify(state.config)); + var resp = JSON.parse(JSON.stringify(state.config)); + resp.version = pkg.version; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(resp)); return; } @@ -552,6 +574,10 @@ function serveControlsHelper() { res.end(JSON.stringify( { status: (state.config.disable ? 'disabled' : 'enabled') , ready: ((state.config.relay && (state.config.token || state.config.agreeTos)) ? true : false) + , active: !!tun + , connected: 'maybe (todo)' + , version: pkg.version + , servernames: state.servernames } )); return; diff --git a/lib/sorting-hat.js b/lib/sorting-hat.js index e492015..78d294f 100644 --- a/lib/sorting-hat.js +++ b/lib/sorting-hat.js @@ -157,15 +157,15 @@ module.exports.assign = function (state, tun, cb) { //if (tun.data) { conn.write(tun.data); } return conn; } - function fileDirTcp(conf, cb) { + function fileDirTcp(opts, cb) { var socketPair = require('socket-pair'); var conn = socketPair.create(function (err, other) { if (err) { cb(err); return; } - if (conf._stat.isFile()) { - fs.createReadStream(conf.handler).pipe(other); + if (opts.stat.isFile()) { + fs.createReadStream(opts.config.handler).pipe(other); } else { - fs.readdir(conf.handler, function (err, nodes) { + fs.readdir(opts.config.handler, function (err, nodes) { other.write('\n' + nodes.join('\n') + '\n\n'); other.end(); }); @@ -282,8 +282,7 @@ module.exports.assign = function (state, tun, cb) { errorTcp(conf, cb); return; } - conf._stat = stat; - fileDirTcp(conf, cb); + fileDirTcp({ config: conf, stat: stat }, cb); }); }); } diff --git a/package.json b/package.json index 3f048b1..dd1f04f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telebit", - "version": "0.19.26", + "version": "0.19.27", "description": "Break out of localhost. Connect to any device from anywhere over any tcp port or securely in a browser. A secure tunnel. A poor man's reverse VPN.", "main": "lib/remote.js", "files": [