print socket location

This commit is contained in:
AJ ONeal 2018-06-14 02:39:34 -06:00
parent a98bd306f3
commit 2045b054f8
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';
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) {

View File

@ -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() {

View File

@ -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;