From ef4be1c2967ab54bf0da4ed9283fc5889eef842c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 8 Jun 2018 02:50:00 -0600 Subject: [PATCH] new stuff appears to work --- bin/telebit.js | 53 +++++++++++------ lib/html/index.html | 31 ++++++++-- lib/html/js/app.js | 15 ++++- lib/sorting-hat.js | 57 +++++++++++++++++-- .../LaunchDaemons/cloud.telebit.remote.plist | 1 + .../dist/etc/systemd/system/telebit.service | 2 +- usr/share/install_helper.sh | 4 +- 7 files changed, 130 insertions(+), 33 deletions(-) diff --git a/bin/telebit.js b/bin/telebit.js index 2846dfe..2b88000 100755 --- a/bin/telebit.js +++ b/bin/telebit.js @@ -27,28 +27,38 @@ function help() { console.info(''); console.info('Telebit Remote v' + pkg.version); console.info(''); - console.info('Usage:'); + console.info('Daemon Usage:'); + console.info(''); + console.info('\tsudo telebit daemon --config '); + console.info('\tex: sudo telebit daemon --config /opt/telebit/etc/telebit.yml'); + console.info(''); + console.info('Remote Usage:'); console.info(''); console.info('\ttelebit [--config ] '); console.info(''); console.info('Examples:'); console.info(''); - console.info('\ttelebit --config ~/.config/telebit/telebit.yml status'); + console.info('\ttelebit status # whether enabled or disabled'); + console.info('\ttelebit enable # disallow incoming connections'); + console.info('\ttelebit disable # allow incoming connections'); console.info(''); - console.info('\ttelebit status'); - console.info('\ttelebit enable'); - console.info('\ttelebit disable'); + console.info('\ttelebit list # list rules for servernames and ports'); console.info(''); - console.info('\ttelebit list'); + console.info('\ttelebit http none # remove all https handlers'); + console.info('\ttelebit http 3000 # forward all https traffic to port 3000'); + console.info('\ttelebit http /module/path # load a node module to handle all https traffic'); console.info(''); - console.info('\ttelebit http 3000'); - console.info('\ttelebit tcp 5050'); + 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(''); - console.info('\ttelebit http default'); - console.info('\ttelebit tcp default'); + console.info('\ttelebit tcp none # remove all tcp handlers'); + console.info('\ttelebit tcp 5050 # forward all tcp to port 5050'); + console.info('\ttelebit tcp /module/path # handle all tcp with a node module'); console.info(''); - console.info('\ttelebit http /path/to/module'); - console.info('\ttelebit tcp /path/to/module'); + console.info('\ttelebit tcp none 6565 # remove tcp handler from external port 6565'); + console.info('\ttelebit tcp 5050 6565 # forward external port 6565 to local 5050'); + console.info('\ttelebit tcp /module/path 6565 # handle external port 6565 with a node module'); console.info(''); console.info('Config:'); console.info(''); @@ -187,7 +197,7 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { } if (/enable/.test(opts.path)) { - state.config.disable = undefined; + delete state.config.disable;// = undefined; if (!tun) { tun = rawTunnel(); } fs.writeFile(confpath, require('js-yaml').safeDump(snakeCopy(state.config)), function () { if (err) { @@ -269,16 +279,18 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { return; } + // portnum if (opts.body[1]) { - if (!state.servernames[opts.body[1]]) { + if (!state.ports[opts.body[1]]) { res.statusCode = 400; res.end('{"error":{"message":"bad servername \'' + opts.body[1] + '\'"'); return; } - state.servernames[opts.body[1]].handler = opts.body[0]; + // forward-to port-or-module + state.ports[opts.body[1]].handler = opts.body[0]; } else { - Object.keys(state.servernames).forEach(function (key) { - state.servernames[key].handler = opts.body[0]; + Object.keys(state.ports).forEach(function (key) { + state.ports[key].handler = opts.body[0]; }); } res.end('{"success":true}'); @@ -332,7 +344,12 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { return true; } - serveControls(); + if (-1 !== argv.indexOf('daemon')) { + serveControls(); + return; + } + + help(); }); function connectTunnel() { diff --git a/lib/html/index.html b/lib/html/index.html index 39cddf4..341cccd 100644 --- a/lib/html/index.html +++ b/lib/html/index.html @@ -8,14 +8,33 @@

Welcome Home

+

Go ahead and bookmark this page. It's yours now.

-

You've claimed {{servername}}

-

Here's same ways you can use it:

-
telebit http 3000
+
+

You've claimed {{servername}}

+

Here's some ways you can use it:

+

+telebit http 3000                # forward all https traffic to localhost:3000
+telebit http /path/to/module     # handle incoming https traffic with a node module
+telebit http none                # remove all https handlers
+
+

You can always use this port for SSH over HTTPS, even while you're using it for something else:

+

+ssh -o ProxyCommand='openssl s_client -connect %h:443 -quiet' {{servername}}
+ + + +

You can always use this port for SSH, even while you're using it for something else:

+

+ssh {{servername}} -p {{serviceport}}
-

You've claimed {{serviceport}}

-

Here's same ways you can use it:

-
#telebit tcp 3000
diff --git a/lib/html/js/app.js b/lib/html/js/app.js index 3bda909..52631d6 100644 --- a/lib/html/js/app.js +++ b/lib/html/js/app.js @@ -3,7 +3,8 @@ document.body.hidden = false; -var hash = window.location.hash.slice(1); +var hash = window.location.hash.substr(1); +var query = window.location.search; function parseQuery(search) { var args = search.substring(1).split('&'); @@ -31,6 +32,18 @@ function parseQuery(search) { return argsParsed; } +document.querySelectorAll('.js-servername').forEach(function ($el) { + $el.innerText = window.location.host; +}); + console.log(parseQuery(hash)); +console.log(parseQuery(query)); +var port = parseQuery(hash).serviceport || parseQuery(query).serviceport; +if (port) { + document.querySelector('.js-port').hidden = false; + document.querySelectorAll('.js-serviceport').forEach(function ($el) { + $el.innerText = port; + }); +} }()); diff --git a/lib/sorting-hat.js b/lib/sorting-hat.js index 21ee94d..e8fdbd6 100644 --- a/lib/sorting-hat.js +++ b/lib/sorting-hat.js @@ -141,7 +141,14 @@ module.exports.assign = function (state, tun, cb) { cb(null, conn); - other.write("[Telebit Echo Server] v1.0\nPlease edit your config file to port forward somewhere more useful.\n\n"); + other.write("\n" + + [ "[Telebit Echo Server] v1.0" + , "To configure tcp run the following:" + , "\ttelebit tcp " + , "\tex: telebit tcp 5050" + , "\tex: telebit tcp /path/to/module" + , "\tex: telebit tcp none" + ].join("\n") + "\n\n"); }); //if (tun.data) { conn.write(tun.data); } return conn; @@ -178,10 +185,50 @@ module.exports.assign = function (state, tun, cb) { }); } + function invokeTcpHandler(conf, socket, tun, id, cb) { + var conn; + if (parseInt(conf.handler, 10)) { + conn = getNetConn(conf.handler); + cb(null, conn); + return conn; + } + + var handle = tun.port; + var handler; + var handlerpath = conf.handler; + var homedir = os.homedir(); + var localshare = path.join(homedir, '.local/share/telebit/apps'); + + if (/^~/.test(handlerpath)) { + handlerpath = path.join(homedir, handlerpath.replace(/^~(\/?)/, '')); + } + + try { + handler = require(handlerpath); + console.info("Handling '" + handle + ":" + id + "' with '" + handlerpath + "'"); + } catch(e1) { + try { + handler = require(path.join(localshare, handlerpath)); + console.info("Handling '" + handle + ":" + id + "' with '" + handlerpath + "'"); + } catch(e2) { + console.error("Failed to load '" + handlerpath + "':", e1.message); + console.error("Failed to load '" + path.join(localshare, handlerpath) + "':", e2.message); + console.warn("Using default handler for '" + handle + ":" + id + "'"); + echoTcp(cb); + } + } + var socketPair = require('socket-pair'); + conn = socketPair.create(function (err, other) { + handler(other, tun, id); + cb(null, conn); + }); + return conn; + } function invokeHandler(conf, tlsSocket, tun, id) { + var conn; if (parseInt(conf.handler, 10)) { // TODO http-proxy with proper headers and ws support - var conn = getNetConn(conf.handler); + conn = getNetConn(conf.handler); console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'"); conn.pipe(tlsSocket); tlsSocket.pipe(conn); @@ -190,7 +237,6 @@ module.exports.assign = function (state, tun, cb) { var handle = tun.name || tun.port; var handler; var handlerpath = conf.handler; - var path = require('path'); var homedir = os.homedir(); var localshare = path.join(homedir, '.local/share/telebit/apps'); @@ -340,16 +386,17 @@ module.exports.assign = function (state, tun, cb) { trySsh(tun, function (err, conn) { if (conn) { cb(null, conn); return; } // TODO add TCP handlers - console.log('Using echo server for tcp'); var conf = state.ports[tun.serviceport]; if (!conf || !conf.handler || 'none' === conf.handler) { + console.log('Using echo server for tcp'); echoTcp(cb); + return; } var Packer = require('proxy-packer'); //var addr = Packer.socketToAddr(conn); var id = Packer.addrToId(tun); - invokeHandler(conf, conn, tun, id); + invokeTcpHandler(conf, conn, tun, id, cb); }); return; } diff --git a/usr/share/dist/Library/LaunchDaemons/cloud.telebit.remote.plist b/usr/share/dist/Library/LaunchDaemons/cloud.telebit.remote.plist index e44928a..8add056 100644 --- a/usr/share/dist/Library/LaunchDaemons/cloud.telebit.remote.plist +++ b/usr/share/dist/Library/LaunchDaemons/cloud.telebit.remote.plist @@ -8,6 +8,7 @@ /opt/telebit/bin/node /opt/telebit/bin/telebit.js + daemon --config /opt/telebit/etc/telebit.yml diff --git a/usr/share/dist/etc/systemd/system/telebit.service b/usr/share/dist/etc/systemd/system/telebit.service index 624ed6a..6694e43 100644 --- a/usr/share/dist/etc/systemd/system/telebit.service +++ b/usr/share/dist/etc/systemd/system/telebit.service @@ -24,7 +24,7 @@ Group=telebit WorkingDirectory=/opt/telebit # custom directory cannot be set and will be the place where this exists, not the working directory -ExecStart=/opt/telebit/bin/node /opt/telebit/bin/telebit.js --config /opt/telebit/etc/telebit.yml +ExecStart=/opt/telebit/bin/node /opt/telebit/bin/telebit.js daemon --config /opt/telebit/etc/telebit.yml ExecReload=/bin/kill -USR1 $MAINPID # Limit the number of file descriptors and processes; see `man systemd.exec` for more limit settings. diff --git a/usr/share/install_helper.sh b/usr/share/install_helper.sh index bfbcd6d..bde5d92 100644 --- a/usr/share/install_helper.sh +++ b/usr/share/install_helper.sh @@ -320,7 +320,7 @@ $sudo_cmd chown -R $my_user "$TELEBIT_PATH" # "/etc/$my_app" # ~/.config/systemd/user/ # %h/.config/telebit/telebit.yml -echo "### Adding $my_app is a system service" +echo "### Adding $my_app as a system service" # TODO detect with type -p my_system_launcher="" if [ -d "/Library/LaunchDaemons" ]; then @@ -406,7 +406,7 @@ elif [ "launchd" == "$my_system_launcher" ]; then echo "Or disabled the service and start manually:" echo "" echo " ${sudo_cmde}launchctl unload -w $my_root/$my_app_launchd_service" - echo " $my_app --config $TELEBIT_PATH/etc/$my_app.yml" + echo " $my_app daemon --config $TELEBIT_PATH/etc/$my_app.yml" else