handle shares a little better

This commit is contained in:
AJ ONeal 2018-11-01 03:11:47 -06:00
parent 535c9732c6
commit fff8f318c0
3 changed files with 53 additions and 17 deletions

View File

@ -128,18 +128,38 @@ controllers.http = function (req, res, opts) {
name = name.replace(/\./, '-').replace(/-+/, '-'); name = name.replace(/\./, '-').replace(/-+/, '-');
return name; return name;
} }
function assign(target, handler, indexes) {
target.handler = handler;
if (indexes) {
target.indexes = true;
} else {
delete target.indexes;
}
}
if (!opts.body) { if (!opts.body) {
res.statusCode = 422; res.statusCode = 422;
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({"error":{"message":"module \'http\' needs more arguments"}})); res.end(JSON.stringify({"error":{"message":"module \'http\' needs more arguments"}}));
return; return;
} }
var active = true; var active = true;
var portOrPath = opts.body.handler || opts.body[0]; var portOrPath = opts.body.handler || opts.body[0];
var appname = getAppname(portOrPath);
var subdomain = opts.body.name || opts.body[1]; var subdomain = opts.body.name || opts.body[1];
var indexes = opts.body.indexes;
var remoteHost; var remoteHost;
if (!subdomain) {
res.statusCode = 422;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: { message: "module 'http' needs more arguments" } }));
return;
}
var appname = getAppname(portOrPath);
// Assign an FQDN to brief subdomains // Assign an FQDN to brief subdomains
// ex: foo => foo.rando.telebit.cloud // ex: foo => foo.rando.telebit.cloud
if (subdomain && !/\./.test(subdomain)) { if (subdomain && !/\./.test(subdomain)) {
@ -174,7 +194,13 @@ controllers.http = function (req, res, opts) {
return; return;
} }
}); });
delete state.servernames[subdomain]; if (state.servernames[subdomain]) {
// TODO remove all non-essential keys
delete state.servernames[subdomain].handler;
if (state.servernames[subdomain].sub) {
delete state.servernames[subdomain];
}
}
remoteHost = 'none'; remoteHost = 'none';
} else if (subdomain && 'none' !== subdomain) { } else if (subdomain && 'none' !== subdomain) {
// use a subdomain with this handler // use a subdomain with this handler
@ -188,7 +214,7 @@ controllers.http = function (req, res, opts) {
if ('none' === portOrPath) { if ('none' === portOrPath) {
delete state.servernames[subdomain].handler; delete state.servernames[subdomain].handler;
} else { } else {
state.servernames[subdomain].handler = portOrPath; assign(state.servernames[subdomain], portOrPath, indexes);
} }
remoteHost = subdomain; remoteHost = subdomain;
} else { } else {
@ -207,7 +233,7 @@ controllers.http = function (req, res, opts) {
if (!state.servernames[prefix]) { if (!state.servernames[prefix]) {
state.servernames[prefix] = { sub: undefined }; state.servernames[prefix] = { sub: undefined };
} }
state.servernames[prefix].handler = portOrPath; assign(state.servernames[prefix], portOrPath, indexes);
remoteHost = prefix; remoteHost = prefix;
return true; return true;
} }
@ -215,7 +241,7 @@ controllers.http = function (req, res, opts) {
Object.keys(state.servernames).some(function (key) { Object.keys(state.servernames).some(function (key) {
//var prefix = appname + '.' + key; //var prefix = appname + '.' + key;
var prefix = key; var prefix = key;
state.servernames[key].handler = portOrPath; assign(state.servernames[key], portOrPath, indexes);
remoteHost = prefix; remoteHost = prefix;
return true; return true;
}); });

View File

@ -156,8 +156,8 @@
<button v-on:click="deletePathHost(domain)">X</button> <button v-on:click="deletePathHost(domain)">X</button>
</li> </li>
</ul> </ul>
<form v-on:submit="createHttp(newHttp.name, newHttp.handler)"> <form v-on:submit.prevent.stop="createShare(newHttp.sub, newHttp.name, newHttp.handler)">
<input v-model="newHttp.sub" type="text" placeholder="subdomain (ex: api)"> <input v-model="newHttp.sub" type="text" placeholder="subdomain (ex: pub)">
<select v-model="newHttp.name"> <select v-model="newHttp.name">
<option v-for="w in status.wildDomains" v-bind:value="w.name">{{ w.name }}</option> <option v-for="w in status.wildDomains" v-bind:value="w.name">{{ w.name }}</option>
</select> </select>
@ -179,7 +179,7 @@
<button v-on:click="deletePortForward(domain)">X</button> <button v-on:click="deletePortForward(domain)">X</button>
</li> </li>
</ul> </ul>
<form v-on:submit="createHttp(newHttp.name, newHttp.handler)"> <form v-on:submit="createHost(newHttp.sub, newHttp.name, newHttp.handler)">
<input v-model="newHttp.sub" type="text" placeholder="subdomain (ex: api)"> <input v-model="newHttp.sub" type="text" placeholder="subdomain (ex: api)">
<select v-model="newHttp.name"> <select v-model="newHttp.name">
<option v-for="w in status.wildDomains" v-bind:value="w.name">{{ w.name }}</option> <option v-for="w in status.wildDomains" v-bind:value="w.name">{{ w.name }}</option>

View File

@ -37,12 +37,12 @@ api.status = function apiStatus() {
return json; return json;
}); });
}; };
api.http = function apiHttp(name, handler) { api.http = function apiHttp(o) {
var opts = { var opts = {
url: "/api/http" url: "/api/http"
, method: "POST" , method: "POST"
, headers: { 'Content-Type': 'application/json' } , headers: { 'Content-Type': 'application/json' }
, json: { name: name, handler: handler } , json: { name: o.name, handler: o.handler, indexes: o.indexes }
}; };
return Telebit.reqLocalAsync(opts).then(function (resp) { return Telebit.reqLocalAsync(opts).then(function (resp) {
var json = resp.body; var json = resp.body;
@ -231,21 +231,31 @@ var appMethods = {
// 1-65536 // 1-65536
api.ssh(port || 22); api.ssh(port || 22);
} }
, createHttp: function (domain, handler) { , createShare: function (sub, domain, handler) {
api.http(domain.name, handler); if (sub) {
domain = sub + '.' + domain;
}
api.http({ name: domain, handler: handler, indexes: true });
appData.newHttp = {};
}
, createHost: function (sub, domain, handler) {
if (sub) {
domain = sub + '.' + domain;
}
api.http({ name: domain, handler: handler, 'x-forwarded-for': name });
appData.newHttp = {}; appData.newHttp = {};
} }
, changePortForward: function (domain, port) { , changePortForward: function (domain, port) {
api.http(domain.name, port); api.http({ name: domain.name, handler: port });
} }
, deletePortForward: function (domain) { , deletePortForward: function (domain) {
api.http(domain.name, 'none'); api.http({ name: domain.name, handler: 'none' });
} }
, changePathHost: function (domain, path) { , changePathHost: function (domain, path) {
api.http(domain.name, path); api.http({ name: domain.name, handler: path });
} }
, deletePathHost: function (domain) { , deletePathHost: function (domain) {
api.http(domain.name, 'none'); api.http({ name: domain.name, handler: 'none' });
} }
, changeState: changeState , changeState: changeState
}; };
@ -390,7 +400,7 @@ function setState(/*ev*/) {
if (appData.exit) { if (appData.exit) {
console.log('previous state exiting'); console.log('previous state exiting');
appData.exit.then(function (exit) { appData.exit.then(function (exit) {
if ('function' === typeof appData.exit) { if ('function' === typeof exit) {
exit(); exit();
} }
}); });