Compare commits

..

11 Commits

10 changed files with 11472 additions and 353 deletions

View File

@ -546,6 +546,10 @@ function parseConfig(err, text) {
} else { } else {
console.info('> Rejecting SSH-over-HTTPS for now'); console.info('> Rejecting SSH-over-HTTPS for now');
} }
} else if ('status' === body.module) {
// TODO funny one this one
console.info('http://localhost:' + (body.port || state.config.ipc.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 (false && opts.query._body) { if (false && 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)'
@ -675,7 +683,9 @@ function serveControlsHelper() {
} }
route(); route();
}); });
}); }
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);
@ -688,15 +698,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;
} }
@ -709,6 +734,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

View File

@ -59,7 +59,7 @@ Use \"telebit help [command]\" for more information about a command, including f
Copyright 2015-2018 AJ ONeal https://telebit.cloud MPL-2.0 Licensed (RAWR!)" Copyright 2015-2018 AJ ONeal https://telebit.cloud MPL-2.0 Licensed (RAWR!)"
status = "usage: telebit status <path/port/none> [subdomain] status = "usage: telebit status
'telebit status' shows details about the current connections (or lack thereof). 'telebit status' shows details about the current connections (or lack thereof).

View File

@ -79,11 +79,25 @@ module.exports.create = function (state) {
url += ('?_body=' + encodeURIComponent(json)); url += ('?_body=' + encodeURIComponent(json));
} }
var method = opts.method || (args && 'POST') || 'GET'; var method = opts.method || (args && 'POST') || 'GET';
var req = http.request({ var reqOpts = {
socketPath: state._ipc.path method: method
, method: method
, path: url , path: url
}, function (resp) { };
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);
if (!state.config.ipc) {
state.config.ipc = {};
}
state.config.ipc.type = 'port';
state.config.ipc.path = path.dirname(state._ipc.path);
state.config.ipc.port = reqOpts.port;
} else {
reqOpts.socketPath = state._ipc.path;
}
var req = http.request(reqOpts, function (resp) {
makeResponder(service, resp, fn); makeResponder(service, resp, fn);
}); });

View File

@ -84,8 +84,8 @@ Launcher.install = function (things, fn) {
}; };
vars.telebitBinTpl = path.join(telebitRoot, 'usr/share/dist/bin/telebit.tpl'); vars.telebitBinTpl = path.join(telebitRoot, 'usr/share/dist/bin/telebit.tpl');
vars.telebitNpm = path.resolve(vars.telebitNode, '../npm'); vars.telebitNpm = path.resolve(vars.telebitNode, '../npm');
vars.nodePath = path.resolve(vars.telebitNode, '../lib/node_modules'); vars.nodePath = path.resolve(vars.telebitNode, '../../lib/node_modules');
vars.npmConfigPrefix = path.resolve(vars.telebitNode, '..'); vars.npmConfigPrefix = path.resolve(vars.telebitNode, '..', '..');
vars.userspace = (!things.telebitUser || (things.telebitUser === os.userInfo().username)) ? true : false; vars.userspace = (!things.telebitUser || (things.telebitUser === os.userInfo().username)) ? true : false;
if (-1 === vars.telebitRwDirs.indexOf(vars.npmConfigPrefix)) { if (-1 === vars.telebitRwDirs.indexOf(vars.npmConfigPrefix)) {
vars.telebitRwDirs.push(vars.npmConfigPrefix); vars.telebitRwDirs.push(vars.npmConfigPrefix);

View File

@ -224,16 +224,20 @@ pushd $TELEBIT_TMP >/dev/null
else else
echo -n "." echo -n "."
fi fi
set +e
$tmp_npm install >/dev/null 2>/dev/null & $tmp_npm install >/dev/null 2>/dev/null &
# ursa is now an entirely optional dependency for key generation
# but very much needed on ARM devices
$tmp_npm install ursa >/dev/null 2>/dev/null &
tmp_npm_pid=$! tmp_npm_pid=$!
while [ -n "$tmp_npm_pid" ]; do while [ -n "$tmp_npm_pid" ]; do
sleep 2 sleep 2
echo -n "." echo -n "."
kill -s 0 $tmp_npm_pid >/dev/null 2>/dev/null || tmp_npm_pid="" kill -s 0 $tmp_npm_pid >/dev/null 2>/dev/null || tmp_npm_pid=""
done done
set -e
echo -n "."
$tmp_npm install >/dev/null 2>/dev/null
# ursa is now an entirely optional dependency for key generation
# but very much needed on ARM devices
$tmp_npm install ursa >/dev/null 2>/dev/null || true
popd >/dev/null popd >/dev/null
if [ -n "${TELEBIT_DEBUG}" ]; then if [ -n "${TELEBIT_DEBUG}" ]; then
@ -425,8 +429,8 @@ if [ -d "/Library/LaunchDaemons" ]; then
if [ -n "${TELEBIT_DEBUG}" ]; then if [ -n "${TELEBIT_DEBUG}" ]; then
echo " > launchctl unload -w $my_app_launchd_service >/dev/null 2>/dev/null" echo " > launchctl unload -w $my_app_launchd_service >/dev/null 2>/dev/null"
launchctl unload -w "$my_app_launchd_service" >/dev/null 2>/dev/null
fi fi
launchctl unload -w "$my_app_launchd_service" >/dev/null 2>/dev/null
else else
my_app_launchd_service_skel="usr/share/dist/Library/LaunchDaemons/${my_app_pkg_name}.plist" my_app_launchd_service_skel="usr/share/dist/Library/LaunchDaemons/${my_app_pkg_name}.plist"
my_app_launchd_service="$my_root/Library/LaunchDaemons/${my_app_pkg_name}.plist" my_app_launchd_service="$my_root/Library/LaunchDaemons/${my_app_pkg_name}.plist"

View File

@ -79,8 +79,8 @@ function run() {
, TELEBIT_LOG_DIR: process.env.TELEBIT_LOG_DIR || path.join(os.homedir(), '.local/share/telebit/var/log') , TELEBIT_LOG_DIR: process.env.TELEBIT_LOG_DIR || path.join(os.homedir(), '.local/share/telebit/var/log')
}; };
vars.telebitNpm = process.env.TELEBIT_NPM || path.resolve(vars.telebitNode, '../npm'); vars.telebitNpm = process.env.TELEBIT_NPM || path.resolve(vars.telebitNode, '../npm');
vars.nodePath = process.env.NODE_PATH || path.resolve(vars.telebitNode, '../lib/node_modules'); vars.nodePath = process.env.NODE_PATH || path.resolve(vars.telebitNode, '../../lib/node_modules');
vars.npmConfigPrefix = process.env.NPM_CONFIG_PREFIX || path.resolve(vars.telebitNode, '..'); vars.npmConfigPrefix = process.env.NPM_CONFIG_PREFIX || path.resolve(vars.telebitNode, '..', '..');
if (-1 === vars.telebitRwDirs.indexOf(vars.npmConfigPrefix)) { if (-1 === vars.telebitRwDirs.indexOf(vars.npmConfigPrefix)) {
vars.telebitRwDirs.push(vars.npmConfigPrefix); vars.telebitRwDirs.push(vars.npmConfigPrefix);
} }