WIP web remote client

This commit is contained in:
AJ ONeal 2018-10-15 20:37:07 -06:00
parent 8ffc30797a
commit be7a895dc7
5 changed files with 11428 additions and 312 deletions

View File

@ -73,7 +73,6 @@ if (!confpath || /^--/.test(confpath)) {
} }
function askForConfig(state, mainCb) { function askForConfig(state, mainCb) {
var fs = require('fs');
var ttyname = '/dev/tty'; var ttyname = '/dev/tty';
var stdin = useTty ? fs.createReadStream(ttyname, { var stdin = useTty ? fs.createReadStream(ttyname, {
fd: fs.openSync(ttyname, fs.constants.O_RDONLY | fs.constants.O_NOCTTY) fd: fs.openSync(ttyname, fs.constants.O_RDONLY | fs.constants.O_NOCTTY)
@ -318,11 +317,20 @@ var utils = {
request: function request(opts, fn) { request: function request(opts, fn) {
if (!opts) { opts = {}; } if (!opts) { opts = {}; }
var service = opts.service || 'config'; var service = opts.service || 'config';
var req = http.request({
socketPath: state._ipc.path var reqOpts = {
, method: opts.method || 'GET' method: opts.method || 'GET'
, path: '/rpc/' + service , path: '/rpc/' + service
}, function (resp) { };
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);
} else {
reqOpts.socketPath = state._ipc.path;
}
var req = http.request(reqOpts, function (resp) {
var body = ''; var body = '';
function finish() { function finish() {
@ -383,12 +391,20 @@ var utils = {
req.end(); req.end();
} }
, putConfig: function putConfig(service, args, fn) { , putConfig: function putConfig(service, args, fn) {
var req = http.request({
socketPath: state._ipc.path
, method: 'POST'
, path: '/rpc/' + service + '?_body=' + encodeURIComponent(JSON.stringify(args))
}, function (resp) {
var reqOpts = {
method: 'POST'
, path: '/rpc/' + service + '?_body=' + encodeURIComponent(JSON.stringify(args))
};
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);
} else {
reqOpts.socketPath = state._ipc.path;
}
var req = http.request(reqOpts, function (resp) {
function finish() { function finish() {
if ('function' === typeof fn) { if ('function' === typeof fn) {
fn(null, resp); fn(null, resp);
@ -434,6 +450,9 @@ var utils = {
} else if ('ssh' === body.module) { } else if ('ssh' === body.module) {
//console.info('> Forwarding ' + state.config.relay + ' -p ' + JSON.stringify(body) + ' => localhost:' + body.local); //console.info('> Forwarding ' + state.config.relay + ' -p ' + JSON.stringify(body) + ' => localhost:' + body.local);
console.info('> Forwarding ssh+https (openssl proxy) => localhost:' + body.local); console.info('> Forwarding ssh+https (openssl proxy) => localhost:' + body.local);
} else if ('status' === body.module) {
console.info('http://localhost:' + reqOpts.port);
console.info(JSON.stringify(body, null, 2));
} else { } else {
console.info(JSON.stringify(body, null, 2)); console.info(JSON.stringify(body, null, 2));
} }

View File

@ -326,8 +326,15 @@ controllers.ssh = function (req, res, opts) {
state.config.sshAuto = sshAuto; state.config.sshAuto = sshAuto;
sshSuccess(); sshSuccess();
}; };
function serveControlsHelper() {
controlServer = http.createServer(function (req, res) { var serveStatic = require('serve-static')(path.join(__dirname, '../lib/admin/'));
function handleRemoteClient(req, res) {
if (/^\/(rpc|api)\//.test(req.url)) {
return handleApi(req, res);
}
serveStatic(req, res, require('finalhandler')(req, res));
}
function handleApi(req, res) {
var opts = url.parse(req.url, true); var opts = url.parse(req.url, true);
if (opts.query._body) { if (opts.query._body) {
try { try {
@ -581,7 +588,8 @@ function serveControlsHelper() {
function getStatus() { function getStatus() {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify( res.end(JSON.stringify(
{ status: (state.config.disable ? 'disabled' : 'enabled') { module: 'status'
, status: (state.config.disable ? 'disabled' : 'enabled')
, ready: ((state.config.relay && (state.config.token || state.config.agreeTos)) ? true : false) , ready: ((state.config.relay && (state.config.token || state.config.agreeTos)) ? true : false)
, active: !!myRemote , active: !!myRemote
, connected: 'maybe (todo)' , connected: 'maybe (todo)'
@ -648,7 +656,9 @@ function serveControlsHelper() {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}})); res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}}));
}); }
function serveControlsHelper() {
controlServer = http.createServer(handleRemoteClient);
if (fs.existsSync(state._ipc.path)) { if (fs.existsSync(state._ipc.path)) {
fs.unlinkSync(state._ipc.path); fs.unlinkSync(state._ipc.path);
@ -661,15 +671,30 @@ function serveControlsHelper() {
, readableAll: true , readableAll: true
, exclusive: false , exclusive: false
}; };
if (!state.config.ipc) {
state.config.ipc = {};
}
if (!state.config.ipc.path) {
state.config.ipc.path = path.dirname(state._ipc.path);
}
require('mkdirp').sync(state.config.ipc.path);
if (!state.config.ipc.type) {
state.config.ipc.type = 'port';
}
var portFile = path.join(state.config.ipc.path, 'telebit.port');
if (fs.existsSync(portFile)) {
state._ipc.port = parseInt(fs.readFileSync(portFile, 'utf8').trim(), 10);
}
if ('socket' === state._ipc.type) { if ('socket' === state._ipc.type) {
require('mkdirp').sync(path.dirname(state._ipc.path)); require('mkdirp').sync(path.dirname(state._ipc.path));
} }
// https://nodejs.org/api/net.html#net_server_listen_options_callback // https://nodejs.org/api/net.html#net_server_listen_options_callback
// path is ignore if port is defined // path is ignore if port is defined
// https://git.coolaj86.com/coolaj86/telebit.js/issues/23#issuecomment-326 // https://git.coolaj86.com/coolaj86/telebit.js/issues/23#issuecomment-326
if (state._ipc.port) { if ('port' === state.config.ipc.type) {
serverOpts.host = 'localhost'; serverOpts.host = 'localhost';
serverOpts.port = state._ipc.port; serverOpts.port = state._ipc.port || 0;
} else { } else {
serverOpts.path = state._ipc.path; serverOpts.path = state._ipc.path;
} }
@ -682,6 +707,21 @@ function serveControlsHelper() {
//console.log(this.address()); //console.log(this.address());
console.info("[info] Listening for commands on", address); console.info("[info] Listening for commands on", address);
}); });
controlServer.on('error', function (err) {
if ('EADDRINUSE' === err.code) {
try {
fs.unlinkSync(portFile);
} catch(e) {
// nada
}
setTimeout(function () {
console.log("trying again");
serveControlsHelper();
}, 1000);
return;
}
console.error('failed to start c&c server:', err);
});
} }
function serveControls() { function serveControls() {

60
lib/admin/index.html Normal file
View File

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html>
<head>
<title>Telebit Admin</title>
</head>
<body>
<div class="v-app">
<h1>Telebit Admin</h1>
<section>
<h2>GET /api/config</h2>
<pre><code>{{ config }}</code></pre>
</section>
<section>
<h2>GET /api/status</h2>
<pre><code>{{ status }}</code></pre>
</section>
<section>
<h2>POST /api/init</h2>
<form v-on:submit.stop.prevent="initialize">
<label for="-email">Email:</label>
<input id="-email" v-model="init.email" type="text" placeholder="john@example.com">
<br>
<label for="-teletos"><input id="-teletos" v-model="init.teletos" type="checkbox">
Accept Telebit Terms of Service</label>
<br>
<label for="-letos"><input id="-letos" v-model="init.letos" type="checkbox">
Accept Let's Encrypt Terms of Service</label>
<br>
</form>
<pre><code>{{ init }}</code></pre>
</section>
<section>
<h2>POST /api/http</h2>
<pre><code>{{ http }}</code></pre>
</section>
<section>
<h2>POST /api/tcp</h2>
<pre><code>{{ tcp }}</code></pre>
</section>
<section>
<h2>POST /api/ssh</h2>
<pre><code>{{ ssh }}</code></pre>
</section>
</div>
<script src="js/vue.js"></script>
<script src="js/app.js"></script>
</body>
</html>

50
lib/admin/js/app.js Normal file
View File

@ -0,0 +1,50 @@
;(function () {
'use strict';
console.log("hello");
var Vue = window.Vue;
var api = {};
api.config = function apiConfig() {
return window.fetch("/api/config", { method: "GET" }).then(function (resp) {
return resp.json().then(function (json) {
appData.config = json;
return json;
});
});
};
api.status = function apiStatus() {
return window.fetch("/api/status", { method: "GET" }).then(function (resp) {
return resp.json().then(function (json) {
appData.status = json;
return json;
});
});
};
var appData = {
config: null
, status: null
, init: {}
, http: null
, tcp: null
, ssh: null
};
var appMethods = {
initialize: function () {
console.log("call initialize");
}
};
new Vue({
el: ".v-app"
, data: appData
, methods: appMethods
});
api.config();
api.status();
window.api = api;
}());

10947
lib/admin/js/vue.js Normal file

File diff suppressed because it is too large Load Diff