print socket location

This commit is contained in:
AJ ONeal 2018-06-14 02:39:34 -06:00
parent aa9fa8b135
commit f112eebf44
3 changed files with 50 additions and 46 deletions

40
bin/telebit.js Normal file → Executable file
View File

@ -3,6 +3,7 @@
'use strict'; 'use strict';
var pkg = require('../package.json'); var pkg = require('../package.json');
var os = require('os');
//var url = require('url'); //var url = require('url');
var path = require('path'); var path = require('path');
@ -19,6 +20,7 @@ var argv = process.argv.slice(2);
var argIndex = argv.indexOf('--config'); var argIndex = argv.indexOf('--config');
var confpath; var confpath;
var useTty; var useTty;
var state = {};
if (-1 === argIndex) { if (-1 === argIndex) {
argIndex = argv.indexOf('-c'); argIndex = argv.indexOf('-c');
} }
@ -71,12 +73,11 @@ function help() {
console.info(''); console.info('');
} }
var verstr = '' + pkg.name + ' v' + pkg.version; var verstr = [ pkg.name + ' v' + pkg.version ];
if (!confpath) { if (!confpath) {
confpath = path.join(require('os').homedir(), '.config/telebit/telebit.yml'); confpath = path.join(os.homedir(), '.config/telebit/telebit.yml');
verstr += ' (--config "' + confpath + '")'; verstr.push('(--config "' + confpath + '")');
} }
console.info(verstr + '\n');
if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) { if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) {
help(); help();
@ -321,21 +322,14 @@ function askForConfig(answers, mainCb) {
} }
function parseConfig(err, text) { function parseConfig(err, text) {
var config;
if (err) { console.info(verstr.join(' '));
console.error("\nCouldn't load config:\n\n\t" + err.message + "\n");
if ('ENOENT' === err.code) {
text = 'relay: \'\'';
}
//askForConfig();
}
try { try {
config = JSON.parse(text); state.config = JSON.parse(text || '{}');
} catch(e1) { } catch(e1) {
try { try {
config = YAML.safeLoad(text); state.config = YAML.safeLoad(text || '{}');
} catch(e2) { } catch(e2) {
console.error(e1.message); console.error(e1.message);
console.error(e2.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) { function putConfig(service, args) {
// console.log('got it', service, args); // console.log('got it', service, args);
var req = http.get({ var req = http.get({
socketPath: common.pipename(config) socketPath: state._ipc.path
, method: 'POST' , method: 'POST'
, path: '/rpc/' + service + '?_body=' + JSON.stringify(args) , path: '/rpc/' + service + '?_body=' + JSON.stringify(args)
}, function (resp) { }, function (resp) {

View File

@ -45,16 +45,15 @@ function help() {
console.info(''); console.info('');
} }
var verstr = '' + pkg.name + ' v' + pkg.version; var verstr = [ pkg.name + ' v' + pkg.version ];
if (-1 === confIndex) { if (-1 === confIndex) {
// We have two possible valid paths if no --config is given (i.e. run from an npm-only install) // We have two possible valid paths if no --config is given (i.e. run from an npm-only install)
// * {install}/etc/telebitd.yml // * {install}/etc/telebitd.yml
// * ~/.config/telebit/telebitd.yml // * ~/.config/telebit/telebitd.yml
// We'll asume the later since the installers include --config in the system launcher script // We'll asume the later since the installers include --config in the system launcher script
confpath = path.join(state.homedir, '.config/telebit/telebitd.yml'); 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')) { if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) {
help(); help();
@ -405,9 +404,20 @@ function serveControls() {
} }
function parseConfig(err, text) { function parseConfig(err, text) {
var config;
function run() { 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; state._confpath = confpath;
if (!state.config.servernames) { if (!state.config.servernames) {
state.config.servernames = {}; state.config.servernames = {};
@ -421,18 +431,11 @@ function parseConfig(err, text) {
serveControls(); serveControls();
} }
if (err) {
console.warn("\nCouldn't load config:\n\n\t" + err.message + "\n");
state.config = {};
run();
return;
}
try { try {
config = JSON.parse(text); state.config = JSON.parse(text || '{}');
} catch(e1) { } catch(e1) {
try { try {
config = YAML.safeLoad(text); state.config = YAML.safeLoad(text || '{}');
} catch(e2) { } catch(e2) {
console.error(e1.message); console.error(e1.message);
console.error(e2.message); console.error(e2.message);
@ -441,24 +444,17 @@ function parseConfig(err, text) {
} }
} }
state.config = camelCopy(config); state.config = camelCopy(state.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;
run(); 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() { function connectTunnel() {

View File

@ -17,7 +17,7 @@ common.pipename = function (config, newApi) {
, type: (/^win/i.test(os.platform()) ? 'pipe' : 'socket') , type: (/^win/i.test(os.platform()) ? 'pipe' : 'socket')
}; };
if ('pipe' === _ipc.type) { if ('pipe' === _ipc.type) {
_ipc.path = '\\\\?\\pipe' + pipename.replace(/\//, '\\'); _ipc.path = '\\\\?\\pipe' + _ipc.path.replace(/\//, '\\');
} }
if (newApi) { if (newApi) {
return _ipc; return _ipc;