diff --git a/bin/telebitd.js b/bin/telebitd.js index 46fd747..decc22e 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -135,9 +135,9 @@ controllers.http = function (req, res, opts) { return; } var active = true; - var portOrPath = opts.body[0]; + var portOrPath = opts.body.handler || opts.body[0]; var appname = getAppname(portOrPath); - var subdomain = opts.body[1]; + var subdomain = opts.body.name || opts.body[1]; var remoteHost; // Assign an FQDN to brief subdomains @@ -310,8 +310,9 @@ controllers.ssh = function (req, res, opts) { }); } - var sshAuto = opts.body[0]; - if (-1 !== [ 'false', 'none', 'off', 'disable' ].indexOf(sshAuto)) { + var rawSshAuto = opts.body.port || opts.body[0]; + var sshAuto = rawSshAuto; + if (-1 !== [ -1, 'false', 'none', 'off', 'disable' ].indexOf(sshAuto)) { state.config.sshAuto = false; sshSuccess(); return; @@ -325,7 +326,7 @@ controllers.ssh = function (req, res, opts) { if (!sshAuto || sshAuto <= 0 || sshAuto > 65535) { res.statusCode = 400; res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({ error: { message: "bad ssh_auto option '" + opts.body[0] + "'" } })); + res.end(JSON.stringify({ error: { message: "bad ssh_auto option '" + rawSshAuto + "'" } })); return; } state.config.sshAuto = sshAuto; @@ -640,6 +641,7 @@ function handleApi(req, res) { , runtime: isConnected && connectTimes.length && (now - connectTimes[0]) || 0 , reconnects: connectTimes.length , servernames: state.servernames + , ssh: state.config.sshAuto } )); } diff --git a/lib/admin/index.html b/lib/admin/index.html index 9af0f31..a0e9028 100644 --- a/lib/admin/index.html +++ b/lib/admin/index.html @@ -115,10 +115,64 @@
- - + +
+
+ http://localhost:{{ status.port }} +
+
+ + SSH: + {{ status.ssh }} + + + +
+
+ + Path Hosting: + +
+ + + + +
+
+ + Port Forwarding: + +
+ + + + +
+
Proctime: {{ statusProctime }}
diff --git a/lib/admin/js/app.js b/lib/admin/js/app.js index a40a5bc..2dbedf2 100644 --- a/lib/admin/js/app.js +++ b/lib/admin/js/app.js @@ -37,6 +37,36 @@ api.status = function apiStatus() { return json; }); }; +api.http = function apiHttp(name, handler) { + var opts = { + url: "/api/http" + , method: "POST" + , headers: { 'Content-Type': 'application/json' } + , json: { name: name, handler: handler } + }; + return Telebit.reqLocalAsync(opts).then(function (resp) { + var json = resp.body; + appData.initResult = json; + return json; + }).catch(function (err) { + window.alert("Error: [init] " + (err.message || JSON.stringify(err, null, 2))); + }); +}; +api.ssh = function apiSsh(port) { + var opts = { + url: "/api/ssh" + , method: "POST" + , headers: { 'Content-Type': 'application/json' } + , json: { port: port } + }; + return Telebit.reqLocalAsync(opts).then(function (resp) { + var json = resp.body; + appData.initResult = json; + return json; + }).catch(function (err) { + window.alert("Error: [init] " + (err.message || JSON.stringify(err, null, 2))); + }); +}; api.enable = function apiEnable() { var opts = { url: "/api/enable" @@ -45,11 +75,10 @@ api.enable = function apiEnable() { }; return Telebit.reqLocalAsync(opts).then(function (resp) { var json = resp.body; - appData.initResult = json; - window.alert("Error: [success] " + JSON.stringify(json, null, 2)); + console.log('enable', json); return json; }).catch(function (err) { - window.alert("Error: [init] " + (err.message || JSON.stringify(err, null, 2))); + window.alert("Error: [enable] " + (err.message || JSON.stringify(err, null, 2))); }); }; api.disable = function apiDisable() { @@ -60,11 +89,10 @@ api.disable = function apiDisable() { }; return Telebit.reqLocalAsync(opts).then(function (resp) { var json = resp.body; - appData.initResult = json; - window.alert("Error: [success] " + JSON.stringify(json, null, 2)); + console.log('disable', json); return json; }).catch(function (err) { - window.alert("Error: [init] " + (err.message || JSON.stringify(err, null, 2))); + window.alert("Error: [disable] " + (err.message || JSON.stringify(err, null, 2))); }); }; @@ -129,9 +157,7 @@ var appData = { , telemetry: true , acmeServer: PRODUCTION_ACME } -, http: null -, tcp: null -, ssh: null +, state: {} , views: { flash: { error: "" @@ -144,6 +170,7 @@ var appData = { , status: false } } +, newHttp: {} }; var telebitState = {}; var appMethods = { @@ -199,6 +226,28 @@ var appMethods = { , disable: function () { api.disable(); } +, ssh: function (port) { + // -1 to disable + // 0 is auto (22) + // 1-65536 + api.ssh(port || 22); + } +, createHttp: function (domain, handler) { + api.http(domain.name, handler); + appData.newHttp = {}; + } +, changePortForward: function (domain, port) { + api.http(domain.name, port); + } +, deletePortForward: function (domain) { + api.http(domain.name, 'none'); + } +, changePathHost: function (domain, path) { + api.http(domain.name, path); + } +, deletePathHost: function (domain) { + api.http(domain.name, 'none'); + } }; var appStates = { setup: function () { @@ -214,7 +263,36 @@ var appStates = { appData.views.section = { status: true }; var tok = setInterval(function () { api.status().then(function (status) { + var wilddomains = []; + var rootdomains = []; + var subdomains = []; + var directories = []; + var portforwards = []; + var free = []; appData.status = status; + Object.keys(appData.status.servernames).forEach(function (k) { + var s = appData.status.servernames[k]; + s.name = k; + if (s.wildcard) { wilddomains.push(s); } + if (!s.sub && !s.wildcard) { rootdomains.push(s); } + if (s.sub) { subdomains.push(s); } + if (s.handler) { + if (s.handler.toString() === parseInt(s.handler, 10).toString()) { + s._port = s.handler; + portforwards.push(s); + } else { + s.path = s.handler; + directories.push(s); + } + } else { + free.push(s); + } + }); + appData.status.portForwards = portforwards; + appData.status.pathHosting = directories; + appData.status.wildDomains = wilddomains; + appData.newHttp.name = (appData.status.wildDomains[0] || {}).name; + appData.state.ssh = (appData.status.ssh > 0) && appData.status.ssh || undefined; }); }, 2000);