From 82f5545d72ed084f7662ceb8c078eb46b7e1abe2 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 25 Sep 2018 01:14:54 -0600 Subject: [PATCH] wip minor refactoring --- bin/telebit-remote.js | 3 ++- bin/telebitd.js | 2 +- lib/{remote.js => daemon/index.js} | 10 ++++++---- lib/{remote-control-client.js => rc/index.js} | 2 +- package.json | 4 ++-- 5 files changed, 12 insertions(+), 9 deletions(-) rename lib/{remote.js => daemon/index.js} (98%) rename lib/{remote-control-client.js => rc/index.js} (98%) diff --git a/bin/telebit-remote.js b/bin/telebit-remote.js index bb4f51f..0d53829 100755 --- a/bin/telebit-remote.js +++ b/bin/telebit-remote.js @@ -401,6 +401,7 @@ function parseConfig(err, text) { //console.log("no questioning:"); parseCli(state); } + function parseCli(/*state*/) { var special = [ 'false', 'none', 'off', 'disable' @@ -472,7 +473,7 @@ function parseConfig(err, text) { } state._clientConfig = camelCopy(state._clientConfig || {}) || {}; - RC = require('../lib/remote-control-client.js').create(state); + RC = require('../lib/rc/index.js').create(state); if (!Object.keys(state._clientConfig).length) { console.info('(' + state._ipc.comment + ": " + state._ipc.path + ')'); diff --git a/bin/telebitd.js b/bin/telebitd.js index 4014b8c..245901f 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -24,7 +24,7 @@ var camelCopy = recase.camelCopy.bind(recase); var snakeCopy = recase.snakeCopy.bind(recase); var TPLS = TOML.parse(fs.readFileSync(path.join(__dirname, "../lib/en-us.toml"), 'utf8')); -var TelebitRemote = require('../').TelebitRemote; +var TelebitRemote = require('../lib/daemon/index.js').TelebitRemote; var state = { homedir: os.homedir(), servernames: {}, ports: {}, keepAlive: { state: false } }; diff --git a/lib/remote.js b/lib/daemon/index.js similarity index 98% rename from lib/remote.js rename to lib/daemon/index.js index 0f59c7b..22fcd7b 100644 --- a/lib/remote.js +++ b/lib/daemon/index.js @@ -28,6 +28,7 @@ function TelebitRemote(state) { EventEmitter.call(this); var me = this; var priv = {}; + var path = require('path'); //var defaultHttpTimeout = (2 * 60); //var activityTimeout = state.activityTimeout || (defaultHttpTimeout - 5) * 1000; @@ -39,8 +40,9 @@ function TelebitRemote(state) { priv.tokens = []; var auth; if(!state.sortingHat) { - state.sortingHat = "./sorting-hat.js"; + state.sortingHat = path.join(__dirname, '../sorting-hat.js'); } + state._connectionHandler = require(state.sortingHat); if (state.token) { if ('undefined' === state.token) { throw new Error("passed string 'undefined' as token"); @@ -349,7 +351,7 @@ function TelebitRemote(state) { // TODO use readable streams instead wstunneler._socket.pause(); - require(state.sortingHat).assign(state, tun, function (err, conn) { + state._connectionHandler.assign(state, tun, function (err, conn) { if (err) { err.message = err.message.replace(/:tun_id/, tun._id); packerHandlers._onConnectError(cid, tun, err); @@ -472,12 +474,12 @@ function TelebitRemote(state) { priv.timeoutId = null; var machine = Packer.create(packerHandlers); - console.info("[telebit:lib/remote.js] [connect] '" + (state.wss || state.relay) + "'"); + console.info("[telebit:lib/daemon.js] [connect] '" + (state.wss || state.relay) + "'"); var tunnelUrl = (state.wss || state.relay).replace(/\/$/, '') + '/'; // + auth; wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: !state.insecure }); // XXXXXX wstunneler.on('open', function () { - console.info("[telebit:lib/remote.js] [open] connected to '" + (state.wss || state.relay) + "'"); + console.info("[telebit:lib/daemon.js] [open] connected to '" + (state.wss || state.relay) + "'"); me.emit('connect'); priv.refreshTimeout(); priv.timeoutId = setTimeout(priv.checkTimeout, activityTimeout); diff --git a/lib/remote-control-client.js b/lib/rc/index.js similarity index 98% rename from lib/remote-control-client.js rename to lib/rc/index.js index 0f284fd..6d9bb12 100644 --- a/lib/remote-control-client.js +++ b/lib/rc/index.js @@ -4,7 +4,7 @@ var os = require('os'); var path = require('path'); var http = require('http'); -var common = require('./cli-common.js'); +var common = require('../cli-common.js'); function packConfig(config) { return Object.keys(config).map(function (key) { diff --git a/package.json b/package.json index 289ad92..1edbf3e 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "telebit", - "version": "0.20.4", + "version": "0.20.5-wip", "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", + "main": "lib/daemon/index.js", "files": [ "bin", "lib",