WIP web remote client
This commit is contained in:
parent
8ffc30797a
commit
be7a895dc7
|
@ -73,7 +73,6 @@ if (!confpath || /^--/.test(confpath)) {
|
|||
}
|
||||
|
||||
function askForConfig(state, mainCb) {
|
||||
var fs = require('fs');
|
||||
var ttyname = '/dev/tty';
|
||||
var stdin = useTty ? fs.createReadStream(ttyname, {
|
||||
fd: fs.openSync(ttyname, fs.constants.O_RDONLY | fs.constants.O_NOCTTY)
|
||||
|
@ -318,11 +317,20 @@ var utils = {
|
|||
request: function request(opts, fn) {
|
||||
if (!opts) { opts = {}; }
|
||||
var service = opts.service || 'config';
|
||||
var req = http.request({
|
||||
socketPath: state._ipc.path
|
||||
, method: opts.method || 'GET'
|
||||
|
||||
var reqOpts = {
|
||||
method: opts.method || 'GET'
|
||||
, 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 = '';
|
||||
|
||||
function finish() {
|
||||
|
@ -383,12 +391,20 @@ var utils = {
|
|||
req.end();
|
||||
}
|
||||
, 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() {
|
||||
if ('function' === typeof fn) {
|
||||
fn(null, resp);
|
||||
|
@ -434,6 +450,9 @@ var utils = {
|
|||
} else if ('ssh' === body.module) {
|
||||
//console.info('> Forwarding ' + state.config.relay + ' -p ' + JSON.stringify(body) + ' => 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 {
|
||||
console.info(JSON.stringify(body, null, 2));
|
||||
}
|
||||
|
|
|
@ -326,8 +326,15 @@ controllers.ssh = function (req, res, opts) {
|
|||
state.config.sshAuto = sshAuto;
|
||||
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);
|
||||
if (opts.query._body) {
|
||||
try {
|
||||
|
@ -581,7 +588,8 @@ function serveControlsHelper() {
|
|||
function getStatus() {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
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)
|
||||
, active: !!myRemote
|
||||
, connected: 'maybe (todo)'
|
||||
|
@ -648,7 +656,9 @@ function serveControlsHelper() {
|
|||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}}));
|
||||
});
|
||||
}
|
||||
function serveControlsHelper() {
|
||||
controlServer = http.createServer(handleRemoteClient);
|
||||
|
||||
if (fs.existsSync(state._ipc.path)) {
|
||||
fs.unlinkSync(state._ipc.path);
|
||||
|
@ -661,15 +671,30 @@ function serveControlsHelper() {
|
|||
, readableAll: true
|
||||
, 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) {
|
||||
require('mkdirp').sync(path.dirname(state._ipc.path));
|
||||
}
|
||||
// https://nodejs.org/api/net.html#net_server_listen_options_callback
|
||||
// path is ignore if port is defined
|
||||
// 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.port = state._ipc.port;
|
||||
serverOpts.port = state._ipc.port || 0;
|
||||
} else {
|
||||
serverOpts.path = state._ipc.path;
|
||||
}
|
||||
|
@ -682,6 +707,21 @@ function serveControlsHelper() {
|
|||
//console.log(this.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() {
|
||||
|
|
|
@ -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>
|
|
@ -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;
|
||||
}());
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue