From f112eebf44739d33ff1322933c4ce85359fa2879 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 14 Jun 2018 02:39:34 -0600 Subject: [PATCH] print socket location --- bin/telebit.js | 40 +++++++++++++++++++++-------------- bin/telebitd.js | 54 ++++++++++++++++++++++------------------------- lib/cli-common.js | 2 +- 3 files changed, 50 insertions(+), 46 deletions(-) mode change 100644 => 100755 bin/telebit.js diff --git a/bin/telebit.js b/bin/telebit.js old mode 100644 new mode 100755 index 8f7203d..0462a92 --- a/bin/telebit.js +++ b/bin/telebit.js @@ -3,6 +3,7 @@ 'use strict'; var pkg = require('../package.json'); +var os = require('os'); //var url = require('url'); var path = require('path'); @@ -19,6 +20,7 @@ var argv = process.argv.slice(2); var argIndex = argv.indexOf('--config'); var confpath; var useTty; +var state = {}; if (-1 === argIndex) { argIndex = argv.indexOf('-c'); } @@ -71,12 +73,11 @@ function help() { console.info(''); } -var verstr = '' + pkg.name + ' v' + pkg.version; +var verstr = [ pkg.name + ' v' + pkg.version ]; if (!confpath) { - confpath = path.join(require('os').homedir(), '.config/telebit/telebit.yml'); - verstr += ' (--config "' + confpath + '")'; + confpath = path.join(os.homedir(), '.config/telebit/telebit.yml'); + verstr.push('(--config "' + confpath + '")'); } -console.info(verstr + '\n'); if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) { help(); @@ -321,21 +322,14 @@ function askForConfig(answers, mainCb) { } 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(); - } + console.info(verstr.join(' ')); try { - config = JSON.parse(text); + state.config = JSON.parse(text || '{}'); } catch(e1) { try { - config = YAML.safeLoad(text); + state.config = YAML.safeLoad(text || '{}'); } catch(e2) { console.error(e1.message); console.error(e2.message); @@ -344,12 +338,26 @@ function parseConfig(err, text) { } } - config = camelCopy(config); + state.config = camelCopy(state.config || {}) || {}; + state._ipc = common.pipename(state.config, true); + + if (!Object.keys(state.config).length) { + console.info('(' + state._ipc.comment + ": " + state._ipc.path + ')'); + } + console.info(""); + + if ((err && 'ENOENT' === err.code) || !Object.keys(state.config).length) { + if (!err || 'ENOENT' === err.code) { + //console.warn("Empty config file. Run 'telebit init' to configure.\n"); + } else { + console.warn("Couldn't load config:\n\n\t" + err.message + "\n"); + } + } function putConfig(service, args) { // console.log('got it', service, args); var req = http.get({ - socketPath: common.pipename(config) + socketPath: state._ipc.path , method: 'POST' , path: '/rpc/' + service + '?_body=' + JSON.stringify(args) }, function (resp) { diff --git a/bin/telebitd.js b/bin/telebitd.js index 60a64b0..d60d043 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -45,16 +45,15 @@ function help() { console.info(''); } -var verstr = '' + pkg.name + ' v' + pkg.version; +var verstr = [ pkg.name + ' v' + pkg.version ]; if (-1 === confIndex) { // We have two possible valid paths if no --config is given (i.e. run from an npm-only install) // * {install}/etc/telebitd.yml // * ~/.config/telebit/telebitd.yml // We'll asume the later since the installers include --config in the system launcher script confpath = path.join(state.homedir, '.config/telebit/telebitd.yml'); - verstr += ' (--config "' + confpath + '")'; + verstr.push('(--config "' + confpath + '")'); } -console.info(verstr + '\n'); if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) { help(); @@ -405,9 +404,20 @@ function serveControls() { } function parseConfig(err, text) { - var config; function run() { + if (!state.config) { + state.config = {}; + } + state._ipc = common.pipename(state.config, true); + console.info(''); + console.info(verstr.join(' ')); + if (!state.config.sock) { + console.info('(' + state._ipc.comment + ': "' + state._ipc.path + '")'); + } + console.info(''); + state.token = state.token || state.config.token || token; + state._confpath = confpath; if (!state.config.servernames) { state.config.servernames = {}; @@ -421,18 +431,11 @@ function parseConfig(err, text) { serveControls(); } - if (err) { - console.warn("\nCouldn't load config:\n\n\t" + err.message + "\n"); - state.config = {}; - run(); - return; - } - try { - config = JSON.parse(text); + state.config = JSON.parse(text || '{}'); } catch(e1) { try { - config = YAML.safeLoad(text); + state.config = YAML.safeLoad(text || '{}'); } catch(e2) { console.error(e1.message); console.error(e2.message); @@ -441,24 +444,17 @@ function parseConfig(err, text) { } } - state.config = camelCopy(config); - state._ipc = common.pipename(state.config, true); - if (!state.config.sock) { - console.info('(' + state._ipc.comment + ': ' + state._ipc.path + ')'); - } - if (state.config.token && token) { - console.warn(); - console.warn("Found two tokens:"); - console.warn(); - console.warn("\t1. " + tokenpath); - console.warn("\n2. " + confpath); - console.warn(); - console.warn("Choosing the first."); - console.warn(); - } - state.token = token; + state.config = camelCopy(state.config || {}) || {}; run(); + + if ((err && 'ENOENT' === err.code) || !Object.keys(state.config).length) { + if (!err || 'ENOENT' === err.code) { + console.warn("Empty config file. Run 'telebit init' to configure.\n"); + } else { + console.warn("Couldn't load config:\n\n\t" + err.message + "\n"); + } + } } function connectTunnel() { diff --git a/lib/cli-common.js b/lib/cli-common.js index db90174..8238e10 100644 --- a/lib/cli-common.js +++ b/lib/cli-common.js @@ -17,7 +17,7 @@ common.pipename = function (config, newApi) { , type: (/^win/i.test(os.platform()) ? 'pipe' : 'socket') }; if ('pipe' === _ipc.type) { - _ipc.path = '\\\\?\\pipe' + pipename.replace(/\//, '\\'); + _ipc.path = '\\\\?\\pipe' + _ipc.path.replace(/\//, '\\'); } if (newApi) { return _ipc;