From de7221ef37a14e4489ab2965a0c16673c8009e93 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 11 Jun 2018 14:52:01 -0600 Subject: [PATCH] separate remote from daemon --- README.md | 25 +- bin/telebit.js | 190 ++++- bin/telebitd.js | 753 ++++++------------ lib/cli-common.js | 27 + package.json | 3 +- usr/share/install_helper.sh | 9 +- .../{telebit.tpl.yml => telebitd.tpl.yml} | 0 7 files changed, 486 insertions(+), 521 deletions(-) create mode 100644 lib/cli-common.js rename usr/share/{telebit.tpl.yml => telebitd.tpl.yml} (100%) diff --git a/README.md b/README.md index d59a1b4..453ef76 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,12 @@ Examples As a user service ```bash -telebitd --config ~/.config/telebit/telebit.yml & +telebitd --config ~/.config/telebit/telebitd.yml & ``` As a system service ```bash -sudo telebitd --config ~/.config/telebit/telebit.yml +sudo telebitd --config ~/.config/telebit/telebitd.yml ``` Example output: @@ -101,9 +101,7 @@ curl -fsSL https://get.telebit.cloud/ > get.sh; bash get.sh --> -Of course, feel free to inspect the install script before you run it: `curl -fsSL https://get.telebit.cloud` - -This will +What does the installer do? * install Telebit Remote to `/opt/telebit` * symlink the executables to `/usr/local/bin` for convenience @@ -112,6 +110,11 @@ This will * create the appropriate system launcher file * `/etc/systemd/system/telebit.service` * `/Library/LaunchDaemons/cloud.telebit.remote.plist` + * create local user config + * `~/.config/telebit/telebit.yml` + * `~/.local/share/telebit` + +Of course, feel free to inspect it before you run it: `curl -fsSL https://get.telebit.cloud` **You can customize the installation**: @@ -137,9 +140,11 @@ Windows & Node.js 1. Install [node.js](https://nodejs.org) 2. Open _Node.js_ 2. Run the command `npm install -g telebit` -2. Copy the example conifg to your user folder `.config/telebit/telebit.yml` (such as `/Users/John/.config/telebit/telebit.yml`) -2. Change the edaim address -2. Run `telebit daemon --config /Users/John/.config/telebit/telebit.yml` +2. Copy the example daemon conifg to your user folder `.config/telebit/telebitd.yml` (such as `/Users/John/.config/telebit/telebitd.yml`) +2. Copy the example remote conifg to your user folder `.config/telebit/telebit.yml` (such as `/Users/John/.config/telebit/telebit.yml`) +2. Change the email address +2. Run `telebitd` +2. Run `telebit list` **Note**: Use node.js v8.x or v10.x @@ -217,12 +222,12 @@ Daemon Usage ============ ```bash -telebit daemon --config /opt/telebit/etc/telebit.yml +telebitd --config /opt/telebit/etc/telebitd.yml ``` Options -`/opt/telebit/etc/telebit.yml:` +`/opt/telebit/etc/telebitd.yml:` ``` email: 'jon@example.com' # must be valid (for certificate recovery and security alerts) agree_tos: true # agree to the Telebit, Greenlock, and Let's Encrypt TOSes diff --git a/bin/telebit.js b/bin/telebit.js index 2805be6..90f2e35 100644 --- a/bin/telebit.js +++ b/bin/telebit.js @@ -1,3 +1,191 @@ +#!/usr/bin/env node +(function () { 'use strict'; -require('./telebitd.js'); +var pkg = require('../package.json'); + +//var url = require('url'); +var path = require('path'); +var http = require('http'); +var YAML = require('js-yaml'); +var recase = require('recase').create({}); +var camelCopy = recase.camelCopy.bind(recase); +//var snakeCopy = recase.snakeCopy.bind(recase); + +var common = require('../lib/cli-common.js'); + +var argv = process.argv.slice(2); + +var confIndex = argv.indexOf('--config'); +var confpath; +var confargs; +if (-1 === confIndex) { + confIndex = argv.indexOf('-c'); +} +if (-1 !== confIndex) { + confargs = argv.splice(confIndex, 2); + confpath = confargs[1]; +} + +function help() { + console.info(''); + console.info('Telebit Remote v' + pkg.version); + console.info(''); + console.info('Usage:'); + console.info(''); + console.info('\ttelebit [--config ] '); + console.info(''); + console.info('Examples:'); + console.info(''); + 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 list # list rules for servernames and ports'); + console.info(''); + 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 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 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 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(''); + console.info('\tSee https://git.coolaj86.com/coolaj86/telebit.js'); + console.info(''); + console.info(''); +} + +var verstr = '' + pkg.name + ' v' + pkg.version; +if (-1 === confIndex) { + confpath = path.join(require('os').homedir(), '.config/telebit/telebit.yml'); + verstr += ' (--config "' + confpath + '")'; +} +console.info(verstr + '\n'); + +if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) { + help(); + process.exit(0); +} +if (!confpath || /^--/.test(confpath)) { + help(); + process.exit(1); +} + +function askForConfig() { + console.log("Please create a config file at '" + confpath + "' or specify --config /path/to/config"); +} + +function parseConfig(err, text) { + var config; + + if (err) { + console.error("\nCouldn't load config:\n\n\t" + err.message + "\n"); + if ('ENOENT' === err.code) { + text = 'relay: \'\''; + } + askForConfig(); + } + + try { + config = JSON.parse(text); + } catch(e1) { + try { + config = YAML.safeLoad(text); + } catch(e2) { + console.error(e1.message); + console.error(e2.message); + process.exit(1); + return; + } + } + + config = camelCopy(config); + + function putConfig(service, args) { + // console.log('got it', service, args); + var req = http.get({ + socketPath: common.pipename(config) + , method: 'POST' + , path: '/rpc/' + service + '?_body=' + JSON.stringify(args) + }, function (resp) { + + function finish() { + if (200 !== resp.statusCode) { + console.warn("'" + service + "' may have failed." + + " Consider peaking at the logs either with 'journalctl -xeu telebit' or /opt/telebit/var/log/error.log"); + console.warn(resp.statusCode, body); + } else { + if (body) { + console.info('Response'); + console.info(body); + } else { + console.info("👌"); + } + } + } + + var body = ''; + if (resp.headers['content-length']) { + resp.on('data', function (chunk) { + body += chunk.toString(); + }); + resp.on('end', function () { + finish(); + }); + } else { + finish(); + } + }); + req.on('error', function (err) { + console.error('Error'); + console.error(err); + return; + }); + } + + // Two styles: + // http 3000 + // http modulename + function makeRpc(key) { + if (key !== argv[0]) { + return false; + } + putConfig(argv[0], argv.slice(1)); + return true; + } + + if ([ 'ssh', 'http', 'tcp' ].some(function (key) { + if (key !== argv[0]) { + return false; + } + if (argv[1]) { + putConfig(argv[0], argv.slice(1)); + return true; + } + help(); + return true; + })) { + return true; + } + + if ([ 'status', 'enable', 'disable', 'restart', 'list', 'save' ].some(makeRpc)) { + return; + } + + help(); +} + +require('fs').readFile(confpath, 'utf8', parseConfig); + +}()); diff --git a/bin/telebitd.js b/bin/telebitd.js index dc2dd06..50f00dd 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -6,9 +6,14 @@ var pkg = require('../package.json'); var url = require('url'); var path = require('path'); +var os = require('os'); +var common = require('../lib/cli-common.js'); var http = require('http'); var YAML = require('js-yaml'); -var state = { servernames: {}, ports: {} }; +var recase = require('recase').create({}); +var camelCopy = recase.camelCopy.bind(recase); +var snakeCopy = recase.snakeCopy.bind(recase); +var state = { homedir: os.homedir(), servernames: {}, ports: {} }; var argv = process.argv.slice(2); @@ -25,65 +30,37 @@ if (-1 !== confIndex) { function help() { console.info(''); - console.info('Telebit Remote v' + pkg.version); + console.info('Telebit Daemon v' + pkg.version); console.info(''); - console.info('Daemon Usage:'); + console.info('Usage:'); console.info(''); - console.info('\tsudo telebitd --config '); - console.info('\tex: sudo telebitd --config /opt/telebit/etc/telebit.yml'); + console.info('\ttelebitd --config '); + console.info('\tex: telebitd --config ~/.config/telebit/telebitd.yml'); console.info(''); - console.info('Remote Usage:'); - console.info(''); - console.info('\ttelebit [--config ] '); - console.info(''); - console.info('Examples:'); - console.info(''); - 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 list # list rules for servernames and ports'); - console.info(''); - 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 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 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 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(''); console.info('\tSee https://git.coolaj86.com/coolaj86/telebit.js'); console.info(''); console.info(''); - process.exit(0); } var verstr = '' + pkg.name + ' v' + pkg.version; if (-1 === confIndex) { - confpath = path.join(require('os').homedir(), '.config/telebit/telebit.yml'); + confpath = path.join(state.homedir, '.config/telebit/telebitd.yml'); verstr += ' (--config "' + confpath + '")'; } console.info(verstr + '\n'); if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) { help(); + process.exit(0); } if (!confpath || /^--/.test(confpath)) { help(); + process.exit(1); } -var defaultSockname = '/opt/telebit/var/telebit.sock'; -var tokenfile = 'access_token.txt'; -var tokenpath = path.join(path.dirname(confpath), tokenfile); +var tokenpath = path.join(path.dirname(confpath), 'access_token.txt'); var token; try { token = require('fs').readFileSync(tokenpath, 'ascii').trim(); @@ -91,16 +68,238 @@ try { // ignore } var controlServer; -require('fs').readFile(confpath, 'utf8', function (err, text) { + +var tun; +function serveControls() { + if (!state.config.disable && state.config.relay && (state.config.token || state.config.agreeTos)) { + tun = rawTunnel(); + } + controlServer = http.createServer(function (req, res) { + var opts = url.parse(req.url, true); + if (opts.query._body) { + try { + opts.body = JSON.parse(opts.query._body, true); + } catch(e) { + res.statusCode = 500; + res.end('{"error":{"message":"?_body={{bad_format}}"}}'); + return; + } + } + + function listSuccess() { + res.end(YAML.safeDump({ + servernames: state.servernames + , ports: state.ports + , ssh: state.config.sshAuto || 'disabled' + })); + } + + function sshSuccess() { + fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { + if (err) { + res.statusCode = 500; + res.end('{"error":{"message":"Could not save config file. Perhaps you\'re not running as root?"}}'); + return; + } + res.end('{"success":true}'); + }); + } + + if (!state.config.relay || !state.config.email || !state.config.agreeTos) { + res.statusCode = 400; + res.end('{"error":{"code":"E_CONFIG","message":"Invalid config file. Please run \'telebit init\'"}}'); + return; + } + + // + // With proper config + // + if (/http/.test(opts.path)) { + if (!opts.body) { + res.statusCode = 422; + res.end('{"error":{"message":"needs more arguments"}}'); + return; + } + if (opts.body[1]) { + if (!state.servernames[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]; + } else { + Object.keys(state.servernames).forEach(function (key) { + state.servernames[key].handler = opts.body[0]; + }); + } + res.end('{"success":true}'); + return; + } + + if (/tcp/.test(opts.path)) { + if (!opts.body) { + res.statusCode = 422; + res.end('{"error":{"message":"needs more arguments"}}'); + return; + } + + // portnum + if (opts.body[1]) { + if (!state.ports[opts.body[1]]) { + res.statusCode = 400; + res.end('{"error":{"message":"bad port \'' + opts.body[1] + '\'"'); + return; + } + // forward-to port-or-module + state.ports[opts.body[1]].handler = opts.body[0]; + } else { + Object.keys(state.ports).forEach(function (key) { + state.ports[key].handler = opts.body[0]; + }); + } + res.end('{"success":true}'); + return; + } + + if (/save|commit/.test(opts.path)) { + state.config.servernames = state.servernames; + state.config.ports = state.ports; + fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { + if (err) { + res.statusCode = 500; + res.end('{"error":{"message":"Could not save config file. Perhaps you\'re not running as root?"}}'); + return; + } + listSuccess(); + }); + return; + } + + if (/ssh/.test(opts.path)) { + var sshAuto; + if (!opts.body) { + res.statusCode = 422; + res.end('{"error":{"message":"needs more arguments"}}'); + return; + } + + sshAuto = opts.body[0]; + if (-1 !== [ 'false', 'none', 'off', 'disable' ].indexOf(sshAuto)) { + state.config.sshAuto = false; + sshSuccess(); + return; + } + if (-1 !== [ 'true', 'auto', 'on', 'enable' ].indexOf(sshAuto)) { + state.config.sshAuto = 22; + sshSuccess(); + return; + } + sshAuto = parseInt(sshAuto, 10); + if (!sshAuto || sshAuto <= 0 || sshAuto > 65535) { + res.statusCode = 400; + res.end('{"error":{"message":"bad ssh_auto option \'' + opts.body[0] + '\'"'); + return; + } + state.config.sshAuto = sshAuto; + sshSuccess(); + return; + } + + if (/enable/.test(opts.path)) { + delete state.config.disable;// = undefined; + if (!tun) { tun = rawTunnel(); } + fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { + if (err) { + res.statusCode = 500; + res.end('{"error":{"message":"Could not save config file. Perhaps you\'re not running as root?"}}'); + return; + } + listSuccess(); + }); + return; + } + + if (/disable/.test(opts.path)) { + state.config.disable = true; + if (tun) { tun.end(); tun = null; } + fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { + if (err) { + res.statusCode = 500; + res.end('{"error":{"message":"Could not save config file. Perhaps you\'re not running as root?"}}'); + return; + } + res.end('{"success":true}'); + }); + return; + } + + if (/status/.test(opts.path)) { + res.end( + '{"status":' + (state.config.disable ? 'disabled' : 'enabled') + + ',"ready":' + (state.config.relay && (state.config.token || state.config.agreeTos)) ? 'true' : 'false' + + '}'); + return; + } + + if (/restart/.test(opts.path)) { + tun.end(); + res.end('{"success":true}'); + controlServer.close(function () { + // TODO closeAll other things + process.nextTick(function () { + // system daemon will restart the process + process.exit(22); // use non-success exit code + }); + }); + return; + } + + if (/list/.test(opts.path)) { + listSuccess(); + return; + } + + res.end('{"error":{"message":"unrecognized rpc"}}'); + }); + var pipename = common.pipename(state.config); + var fs = require('fs'); + if (fs.existsSync(pipename)) { + fs.unlinkSync(pipename); + } + // mask is so that processes owned by other users + // can speak to this process, which is probably root-owned + var oldUmask = process.umask(0x0000); + controlServer.listen({ + path: pipename + , writableAll: true + , readableAll: true + , exclusive: false + }, function () { + process.umask(oldUmask); + }); +} + +function parseConfig(err, text) { var config; - var recase = require('recase').create({}); - var camelCopy = recase.camelCopy.bind(recase); - var snakeCopy = recase.snakeCopy.bind(recase); + function run() { + state._confpath = confpath; + if (!state.config.servernames) { + state.config.servernames = {}; + } + if (!state.config.ports) { + state.config.ports = {}; + } + state.servernames = JSON.parse(JSON.stringify(state.config.servernames)); + state.ports = JSON.parse(JSON.stringify(state.config.ports)); + + serveControls(); + } if (err) { - console.error("\nCouldn't load config:\n\n\t" + err.message + "\n"); - process.exit(1); + console.warn("\nCouldn't load config:\n\n\t" + err.message + "\n"); + state.config = {}; + run(); return; } @@ -117,7 +316,6 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { } } - state._confpath = confpath; state.config = camelCopy(config); if (state.config.token && token) { console.warn(); @@ -131,295 +329,8 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { } state.token = token; - if (!state.config.servernames) { - state.config.servernames = {}; - } - if (!state.config.ports) { - state.config.ports = {}; - } - state.servernames = JSON.parse(JSON.stringify(state.config.servernames)); - state.ports = JSON.parse(JSON.stringify(state.config.ports)); - - function putConfig(service, args) { - // console.log('got it', service, args); - var http = require('http'); - var req = http.get({ - socketPath: state.config.sock || defaultSockname - , method: 'POST' - , path: '/rpc/' + service + '?_body=' + JSON.stringify(args) - }, function (resp) { - - function finish() { - if (200 !== resp.statusCode) { - console.warn("'" + service + "' may have failed." - + " Consider peaking at the logs either with 'journalctl -xeu telebit' or /opt/telebit/var/log/error.log"); - console.warn(resp.statusCode, body); - } else { - if (body) { - console.info('Response'); - console.info(body); - } else { - console.info("👌"); - } - } - } - - var body = ''; - if (resp.headers['content-length']) { - resp.on('data', function (chunk) { - body += chunk.toString(); - }); - resp.on('end', function () { - finish(); - }); - } else { - finish(); - } - }); - req.on('error', function (err) { - console.error('Error'); - console.error(err); - return; - }); - } - - var tun; - function serveControls() { - if (!state.config.disable) { - tun = rawTunnel(); - } - controlServer = http.createServer(function (req, res) { - var opts = url.parse(req.url, true); - if (opts.query._body) { - try { - opts.body = JSON.parse(opts.query._body, true); - } catch(e) { - res.statusCode = 500; - res.end('{"error":{"message":"?_body={{bad_format}}"}}'); - return; - } - } - - function listSuccess() { - res.end(YAML.safeDump({ - servernames: state.servernames - , ports: state.ports - , ssh: state.config.sshAuto || 'disabled' - })); - } - - function sshSuccess() { - fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { - if (err) { - res.statusCode = 500; - res.end('{"error":{"message":"Could not save config file. Perhaps you\'re not running as root?"}}'); - return; - } - res.end('{"success":true}'); - }); - } - - if (/http/.test(opts.path)) { - if (!opts.body) { - res.statusCode = 422; - res.end('{"error":{"message":"needs more arguments"}}'); - return; - } - if (opts.body[1]) { - if (!state.servernames[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]; - } else { - Object.keys(state.servernames).forEach(function (key) { - state.servernames[key].handler = opts.body[0]; - }); - } - res.end('{"success":true}'); - return; - } - - if (/tcp/.test(opts.path)) { - if (!opts.body) { - res.statusCode = 422; - res.end('{"error":{"message":"needs more arguments"}}'); - return; - } - - // portnum - if (opts.body[1]) { - if (!state.ports[opts.body[1]]) { - res.statusCode = 400; - res.end('{"error":{"message":"bad port \'' + opts.body[1] + '\'"'); - return; - } - // forward-to port-or-module - state.ports[opts.body[1]].handler = opts.body[0]; - } else { - Object.keys(state.ports).forEach(function (key) { - state.ports[key].handler = opts.body[0]; - }); - } - res.end('{"success":true}'); - return; - } - - if (/save|commit/.test(opts.path)) { - state.config.servernames = state.servernames; - state.config.ports = state.ports; - fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { - if (err) { - res.statusCode = 500; - res.end('{"error":{"message":"Could not save config file. Perhaps you\'re not running as root?"}}'); - return; - } - listSuccess(); - }); - return; - } - - if (/ssh/.test(opts.path)) { - var sshAuto; - if (!opts.body) { - res.statusCode = 422; - res.end('{"error":{"message":"needs more arguments"}}'); - return; - } - - sshAuto = opts.body[0]; - if (-1 !== [ 'false', 'none', 'off', 'disable' ].indexOf(sshAuto)) { - state.config.sshAuto = false; - sshSuccess(); - return; - } - if (-1 !== [ 'true', 'auto', 'on', 'enable' ].indexOf(sshAuto)) { - state.config.sshAuto = 22; - sshSuccess(); - return; - } - sshAuto = parseInt(sshAuto, 10); - if (!sshAuto || sshAuto <= 0 || sshAuto > 65535) { - res.statusCode = 400; - res.end('{"error":{"message":"bad ssh_auto option \'' + opts.body[0] + '\'"'); - return; - } - state.config.sshAuto = sshAuto; - sshSuccess(); - return; - } - - if (/enable/.test(opts.path)) { - delete state.config.disable;// = undefined; - if (!tun) { tun = rawTunnel(); } - fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { - if (err) { - res.statusCode = 500; - res.end('{"error":{"message":"Could not save config file. Perhaps you\'re not running as root?"}}'); - return; - } - listSuccess(); - }); - return; - } - - if (/disable/.test(opts.path)) { - state.config.disable = true; - if (tun) { tun.end(); tun = null; } - fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { - if (err) { - res.statusCode = 500; - res.end('{"error":{"message":"Could not save config file. Perhaps you\'re not running as root?"}}'); - return; - } - res.end('{"success":true}'); - }); - return; - } - - if (/status/.test(opts.path)) { - res.end('{"status":' + (state.config.disable ? 'disabled' : 'enabled') + '}'); - return; - } - - if (/restart/.test(opts.path)) { - tun.end(); - res.end('{"success":true}'); - controlServer.close(function () { - // TODO closeAll other things - process.nextTick(function () { - // system daemon will restart the process - process.exit(22); // use non-success exit code - }); - }); - return; - } - - if (/list/.test(opts.path)) { - listSuccess(); - return; - } - - res.end('{"error":{"message":"unrecognized rpc"}}'); - }); - var pipename = (state.config.sock || defaultSockname); - var fs = require('fs'); - if (fs.existsSync(pipename)) { - fs.unlinkSync(pipename); - } - if (/^win/i.test(require('os').platform())) { - pipename = '\\\\?\\pipe' + pipename.replace(/\//, '\\'); - } - // mask is so that processes owned by other users - // can speak to this process, which is probably root-owned - var oldUmask = process.umask(0x0000); - controlServer.listen({ - path: pipename - , writableAll: true - , readableAll: true - , exclusive: false - }, function () { - process.umask(oldUmask); - }); - } - - // Two styles: - // http 3000 - // http modulename - function makeRpc(key) { - if (key !== argv[0]) { - return false; - } - putConfig(argv[0], argv.slice(1)); - return true; - } - - if ([ 'ssh', 'http', 'tcp' ].some(function (key) { - if (key !== argv[0]) { - return false; - } - if (argv[1]) { - putConfig(argv[0], argv.slice(1)); - return true; - } - help(); - return true; - })) { - return true; - } - - if ([ 'status', 'enable', 'disable', 'restart', 'list', 'save' ].some(makeRpc)) { - return; - } - - if (-1 !== argv.indexOf('daemon')) { - serveControls(); - return; - } - - help(); -}); + run(); +} function connectTunnel() { function sigHandler() { @@ -558,6 +469,10 @@ function connectTunnel() { } function rawTunnel() { + if (state.config.disable || !state.config.relay || !(state.config.token || state.config.agreeTos)) { + return; + } + state.relay = state.config.relay; if (!state.relay) { throw new Error("'" + state._confpath + "' is missing 'relay'"); @@ -597,176 +512,6 @@ function rawTunnel() { return connectTunnel(); } -/* -var domainsMap = {}; -var services = {}; - -function collectDomains(val, memo) { - var vals = val.split(/,/g); - - function parseProxy(location) { - // john.example.com - // http:john.example.com:3000 - // http://john.example.com:3000 - var parts = location.split(':'); - if (1 === parts.length) { - // john.example.com -> :john.example.com:0 - parts[1] = parts[0]; - - parts[0] = ''; - parts[2] = 0; - } - else if (2 === parts.length) { - throw new Error("invalid arguments for --domains, should use the format or ::"); - } - if (!parts[1]) { - throw new Error("invalid arguments for --domains, should use the format or ::"); - } - - parts[0] = parts[0].toLowerCase(); - parts[1] = parts[1].toLowerCase().replace(/(\/\/)?/, ''); - parts[2] = parseInt(parts[2], 10) || 0; - - memo.push({ - protocol: parts[0] - , hostname: parts[1] - , port: parts[2] - }); - } - - vals.map(function (val) { - return parseProxy(val); - }); - - return memo; -} -function collectProxies(val, memo) { - var vals = val.split(/,/g); - - function parseProxy(location) { - // john.example.com - // https:3443 - // http:john.example.com:3000 - // http://john.example.com:3000 - var parts = location.split(':'); - var dual = false; - if (1 === parts.length) { - // john.example.com -> :john.example.com:0 - parts[1] = parts[0]; - - parts[0] = ''; - parts[2] = 0; - - dual = true; - } - else if (2 === parts.length) { - // https:3443 -> https:*:3443 - parts[2] = parts[1]; - - parts[1] = '*'; - } - - 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 (!parts[0] || '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] || 'https' - , hostname: parts[1] - , port: parts[2] || 443 - }); - - if (dual) { - memo.push({ - protocol: 'http' - , hostname: parts[1] - , port: 80 - }); - } - } - - vals.map(function (val) { - return parseProxy(val); - }); - - return memo; -} - -var program = require('commander'); -program - .version(pkg.version) - //.command('jsurl ') - .arguments('') - .action(function (url) { - program.url = url; - }) - .option('-k --insecure', 'Allow TLS connections to a Telebit Relay without valid certs (rejectUnauthorized: false)') - .option('--locals ', 'comma separated list of : to which matching incoming http and https should forward (reverse proxy). Ex: https:8443,smtps:8465', collectProxies, [ ]) // --reverse-proxies - .option('--domains ', 'comma separated list of domain names to set to the tunnel (to capture a specific protocol to a specific local port use the format https:example.com:1337 instead). Ex: example.com,example.net', collectDomains, [ ]) - .option('--device [HOSTNAME]', 'Tunnel all domains associated with this device instead of specific domainnames. Use with --locals :. Ex: macbook-pro.local (the output of `hostname`)') - .option('--relay ', 'the domain (or ip address) at which you are running Telebit Relay (the proxy)') // --proxy - .option('--secret ', 'the same secret used by the Telebit Relay (used for JWT authentication)') - .option('--token ', 'a pre-generated token for use with the Telebit Relay (instead of generating one with --secret)') - .option('--agree-tos', 'agree to the Telebit Terms of Service (requires user validation)') - .option('--email ', 'email address (or cloud address) for user validation') - .option('--oauth3-url ', 'Cloud Authentication to use (default: https://oauth3.org)') - .parse(process.argv) - ; - - -program.locals = (program.locals || []).concat(program.domains || []); -program.locals.forEach(function (proxy) { - // Create a map from which we can derive a list of all domains we want forwarded to us. - if (proxy.hostname && proxy.hostname !== '*') { - domainsMap[proxy.hostname] = true; - } - - // Create a map of which port different protocols should be forwarded to, allowing for specific - // domains to go to different ports if need be (though that only works for HTTP and HTTPS). - if (proxy.protocol && proxy.port) { - services[proxy.protocol] = services[proxy.protocol] || {}; - - if (/http/.test(proxy.protocol) && proxy.hostname && proxy.hostname !== '*') { - services[proxy.protocol][proxy.hostname] = proxy.port; - } - else { - if (services[proxy.protocol]['*'] && services[proxy.protocol]['*'] !== proxy.port) { - console.error('cannot forward generic', proxy.protocol, 'traffic to multiple ports'); - process.exit(1); - } - else { - services[proxy.protocol]['*'] = proxy.port; - } - } - } -}); - -if (Object.keys(domainsMap).length === 0) { - console.error('no domains specified'); - process.exit(1); - return; -} - -// Make sure we have generic ports for HTTP and HTTPS -services.https = services.https || {}; -services.https['*'] = services.https['*'] || 8443; - -services.http = services.http || {}; -services.http['*'] = services.http['*'] || services.https['*']; - -program.services = services; -*/ +require('fs').readFile(confpath, 'utf8', parseConfig); }()); diff --git a/lib/cli-common.js b/lib/cli-common.js new file mode 100644 index 0000000..05a6706 --- /dev/null +++ b/lib/cli-common.js @@ -0,0 +1,27 @@ +'use strict'; + +var common = module.exports; + +var path = require('path'); +var mkdirp = require('mkdirp'); +var os = require('os'); +var homedir = os.homedir(); + +var localshare = '.local/share/telebit/var'; +var localconf = '.config/telebit'; + +common.pipename = function (config) { + var pipename = (config.sock || common.DEFAULT_SOCK_NAME); + if (/^win/i.test(os.platform())) { + pipename = '\\\\?\\pipe' + pipename.replace(/\//, '\\'); + } + return pipename; +}; +common.DEFAULT_SOCK_NAME = path.join(homedir, localshare, 'telebit.sock'); + +try { + mkdirp.sync(path.join(homedir, localshare)); + mkdirp.sync(path.join(homedir, localconf)); +} catch(e) { + console.error(e); +} diff --git a/package.json b/package.json index 0eaedce..937e4d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telebit", - "version": "0.13.3", + "version": "0.14.0", "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", "bin": { @@ -53,6 +53,7 @@ "greenlock": "^2.2.19", "js-yaml": "^3.11.0", "jsonwebtoken": "^7.1.9", + "mkdirp": "^0.5.1", "proxy-packer": "^1.4.3", "recase": "^1.0.4", "redirect-https": "^1.1.5", diff --git a/usr/share/install_helper.sh b/usr/share/install_helper.sh index 8856ab4..9fa1a43 100644 --- a/usr/share/install_helper.sh +++ b/usr/share/install_helper.sh @@ -339,7 +339,7 @@ if [ -n "$my_relay" ] && [ "$my_relay" != "telebit.cloud" ]; then fi # TODO don't create this in TMP_PATH if it exists in TELEBIT_PATH -my_config="$TELEBIT_PATH/etc/$my_app.yml" +my_config="$TELEBIT_PATH/etc/$my_daemon.yml" mkdir -p "$(dirname $my_config)" if [ ! -e "$my_config" ]; then @@ -352,6 +352,7 @@ if [ ! -e "$my_config" ]; then echo "#email: jon@example.com # used for Automated HTTPS and Telebit.Cloud registrations" >> "$my_config" echo "#agree_tos: true # must be enabled to use Automated HTTPS and Telebit.Cloud" >> "$my_config" fi + echo "sock: $TELEBIT_PATH/var/telebit.sock" >> "$my_config" if [ -n "$my_relay" ]; then echo "relay: $my_relay" >> "$my_config" @@ -369,7 +370,7 @@ if [ ! -e "$my_config" ]; then echo "relay: telebit.cloud # the relay server to use" >> "$my_config" fi #echo "dynamic_ports:\n []" >> "$my_config" - cat $TELEBIT_PATH/usr/share/$my_app.tpl.yml >> "$my_config" + cat $TELEBIT_PATH/usr/share/$my_daemon.tpl.yml >> "$my_config" fi @@ -385,6 +386,7 @@ mkdir -p "$(dirname $my_config)" if [ ! -e "$my_config" ]; then echo "cli: true" >> "$my_config" + echo "sock: $TELEBIT_PATH/var/telebit.sock" >> "$my_config" if [ -n "$my_email" ]; then echo "email: $my_email" >> "$my_config" @@ -403,9 +405,6 @@ if [ ! -e "$my_config" ]; then else echo "relay: telebit.cloud # the relay server to use" >> "$my_config" fi - - cat $TELEBIT_PATH/usr/share/$my_app.tpl.yml >> "$my_config" - fi #echo "${sudo_cmde}chown -R $my_user '$TELEBIT_PATH' diff --git a/usr/share/telebit.tpl.yml b/usr/share/telebitd.tpl.yml similarity index 100% rename from usr/share/telebit.tpl.yml rename to usr/share/telebitd.tpl.yml