2018-09-25 06:45:32 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var os = require('os');
|
|
|
|
var path = require('path');
|
|
|
|
var http = require('http');
|
2019-03-07 08:38:21 +00:00
|
|
|
var keypairs = require('keypairs');
|
2018-09-25 06:45:32 +00:00
|
|
|
|
2018-09-25 07:14:54 +00:00
|
|
|
var common = require('../cli-common.js');
|
2018-09-25 06:45:32 +00:00
|
|
|
|
2019-03-07 08:38:21 +00:00
|
|
|
/*
|
2018-09-25 06:45:32 +00:00
|
|
|
function packConfig(config) {
|
|
|
|
return Object.keys(config).map(function (key) {
|
|
|
|
var val = config[key];
|
|
|
|
if ('undefined' === val) {
|
|
|
|
throw new Error("'undefined' used as a string value");
|
|
|
|
}
|
|
|
|
if ('undefined' === typeof val) {
|
|
|
|
//console.warn('[DEBUG]', key, 'is present but undefined');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (val && 'object' === typeof val && !Array.isArray(val)) {
|
|
|
|
val = JSON.stringify(val);
|
|
|
|
}
|
|
|
|
return key + ':' + val; // converts arrays to strings with ,
|
|
|
|
});
|
|
|
|
}
|
2019-03-07 08:38:21 +00:00
|
|
|
*/
|
2018-09-25 06:45:32 +00:00
|
|
|
|
|
|
|
module.exports.create = function (state) {
|
|
|
|
common._init(
|
|
|
|
// make a default working dir and log dir
|
|
|
|
state._clientConfig.root || path.join(os.homedir(), '.local/share/telebit')
|
|
|
|
, (state._clientConfig.root && path.join(state._clientConfig.root, 'etc'))
|
|
|
|
|| path.resolve(common.DEFAULT_CONFIG_PATH, '..')
|
|
|
|
);
|
|
|
|
state._ipc = common.pipename(state._clientConfig, true);
|
|
|
|
|
|
|
|
function makeResponder(service, resp, fn) {
|
|
|
|
var body = '';
|
|
|
|
|
|
|
|
function finish() {
|
|
|
|
var err;
|
|
|
|
|
|
|
|
if (200 !== resp.statusCode) {
|
2018-11-04 21:28:07 +00:00
|
|
|
err = new Error(body || ('get ' + service + ' failed'));
|
2018-09-25 06:45:32 +00:00
|
|
|
err.statusCode = resp.statusCode;
|
|
|
|
err.code = "E_REQUEST";
|
|
|
|
}
|
|
|
|
|
2018-11-04 21:28:07 +00:00
|
|
|
if (body) {
|
|
|
|
try {
|
|
|
|
body = JSON.parse(body);
|
|
|
|
} catch(e) {
|
|
|
|
console.error('Error:', err);
|
|
|
|
// ignore
|
|
|
|
}
|
2018-09-25 06:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn(err, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resp.headers['content-length'] && !resp.headers['content-type']) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO use readable
|
|
|
|
resp.on('data', function (chunk) {
|
|
|
|
body += chunk.toString();
|
|
|
|
});
|
|
|
|
resp.on('end', finish);
|
|
|
|
}
|
|
|
|
|
|
|
|
var RC = {};
|
2019-03-28 09:23:53 +00:00
|
|
|
RC.resolve = function (pathstr) {
|
|
|
|
// TODO use real hostname and return reqOpts rather than string?
|
|
|
|
return 'http://localhost:' + (RC.port({}).port||'1').toString() + '/' + pathstr.replace(/^\//, '');
|
|
|
|
};
|
|
|
|
RC.port = function (reqOpts) {
|
2018-10-16 03:30:29 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var portFile = path.join(path.dirname(state._ipc.path), 'telebit.port');
|
|
|
|
if (fs.existsSync(portFile)) {
|
|
|
|
reqOpts.host = 'localhost';
|
|
|
|
reqOpts.port = parseInt(fs.readFileSync(portFile, 'utf8').trim(), 10);
|
2018-10-16 04:17:10 +00:00
|
|
|
if (!state.ipc) {
|
|
|
|
state.ipc = {};
|
2018-10-16 03:48:28 +00:00
|
|
|
}
|
2018-10-16 04:17:10 +00:00
|
|
|
state.ipc.type = 'port';
|
|
|
|
state.ipc.path = path.dirname(state._ipc.path);
|
|
|
|
state.ipc.port = reqOpts.port;
|
2018-10-16 03:30:29 +00:00
|
|
|
} else {
|
|
|
|
reqOpts.socketPath = state._ipc.path;
|
|
|
|
}
|
2019-03-28 09:23:53 +00:00
|
|
|
return reqOpts;
|
|
|
|
};
|
|
|
|
RC.createErrorHandler = function (replay, opts, cb) {
|
|
|
|
return function (err) {
|
2018-09-25 06:45:32 +00:00
|
|
|
// ENOENT - never started, cleanly exited last start, or creating socket at a different path
|
|
|
|
// ECONNREFUSED - leftover socket just needs to be restarted
|
|
|
|
if ('ENOENT' === err.code || 'ECONNREFUSED' === err.code) {
|
|
|
|
if (opts._taketwo) {
|
2019-03-28 09:23:53 +00:00
|
|
|
cb(err);
|
2018-09-25 06:45:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-09-25 07:26:47 +00:00
|
|
|
require('../../usr/share/install-launcher.js').install({ env: process.env }, function (err) {
|
2019-03-28 09:23:53 +00:00
|
|
|
if (err) { cb(err); return; }
|
2018-09-25 06:45:32 +00:00
|
|
|
opts._taketwo = true;
|
|
|
|
setTimeout(function () {
|
2019-03-28 09:23:53 +00:00
|
|
|
replay(opts, cb);
|
2018-09-25 06:45:32 +00:00
|
|
|
}, 2500);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:23:53 +00:00
|
|
|
cb(err);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
RC.request = function request(opts, fn) {
|
|
|
|
if (!opts) { opts = {}; }
|
|
|
|
var service = opts.service || 'config';
|
|
|
|
/*
|
|
|
|
var args = opts.data;
|
|
|
|
if (args && 'control' === service) {
|
|
|
|
args = packConfig(args);
|
|
|
|
}
|
|
|
|
var json = JSON.stringify(opts.data);
|
|
|
|
*/
|
|
|
|
var url = '/rpc/' + service;
|
|
|
|
/*
|
|
|
|
if (json) {
|
|
|
|
url += ('?_body=' + encodeURIComponent(json));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
var method = opts.method || (opts.data && 'POST') || 'GET';
|
|
|
|
var reqOpts = {
|
|
|
|
method: method
|
|
|
|
, path: url
|
|
|
|
};
|
|
|
|
reqOpts = RC.port(reqOpts);
|
|
|
|
var req = http.request(reqOpts, function (resp) {
|
|
|
|
makeResponder(service, resp, fn);
|
2018-09-25 06:45:32 +00:00
|
|
|
});
|
2019-03-07 08:38:21 +00:00
|
|
|
|
2019-03-28 09:23:53 +00:00
|
|
|
req.on('error', RC.createErrorHandler(RC.request, opts, fn));
|
|
|
|
|
2019-03-07 08:38:21 +00:00
|
|
|
// Simple GET
|
|
|
|
if ('POST' !== method || !opts.data) {
|
|
|
|
return keypairs.signJwt({
|
|
|
|
jwk: state.key
|
|
|
|
, claims: { iss: false, exp: Math.round(Date.now()/1000) + (15 * 60) }
|
|
|
|
//TODO , exp: '15m'
|
|
|
|
}).then(function (jwt) {
|
2019-05-12 05:40:22 +00:00
|
|
|
req.setHeader("Authorization", 'Bearer ' + jwt);
|
2019-03-07 08:38:21 +00:00
|
|
|
req.end();
|
|
|
|
});
|
2018-09-25 06:45:32 +00:00
|
|
|
}
|
2019-03-07 08:38:21 +00:00
|
|
|
|
|
|
|
return keypairs.signJws({
|
|
|
|
jwk: state.key
|
|
|
|
, protected: {
|
|
|
|
// alg will be filled out automatically
|
|
|
|
jwk: state.pub
|
2019-05-11 20:34:55 +00:00
|
|
|
, kid: false
|
2019-03-07 08:38:21 +00:00
|
|
|
, nonce: require('crypto').randomBytes(16).toString('hex') // TODO get from server
|
2019-03-28 09:23:53 +00:00
|
|
|
// TODO make localhost exceptional
|
|
|
|
, url: RC.resolve(reqOpts.path)
|
2019-03-07 08:38:21 +00:00
|
|
|
}
|
|
|
|
, payload: JSON.stringify(opts.data)
|
|
|
|
}).then(function (jws) {
|
2019-03-21 05:27:25 +00:00
|
|
|
req.setHeader("Content-Type", 'application/jose+json');
|
2019-03-07 08:38:21 +00:00
|
|
|
req.write(JSON.stringify(jws));
|
|
|
|
req.end();
|
|
|
|
});
|
2018-09-25 06:45:32 +00:00
|
|
|
};
|
|
|
|
return RC;
|
|
|
|
};
|