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

View File

@ -156,8 +156,8 @@
<button v-on:click="deletePathHost(domain)">X</button>
</li>
</ul>
<form v-on:submit="createHttp(newHttp.name, newHttp.handler)">
<input v-model="newHttp.sub" type="text" placeholder="subdomain (ex: api)">
<form v-on:submit.prevent.stop="createShare(newHttp.sub, newHttp.name, newHttp.handler)">
<input v-model="newHttp.sub" type="text" placeholder="subdomain (ex: pub)">
<select v-model="newHttp.name">
<option v-for="w in status.wildDomains" v-bind:value="w.name">{{ w.name }}</option>
</select>
@ -179,7 +179,7 @@
<button v-on:click="deletePortForward(domain)">X</button>
</li>
</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)">
<select v-model="newHttp.name">
<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;
});
};
api.http = function apiHttp(name, handler) {
api.http = function apiHttp(o) {
var opts = {
url: "/api/http"
, method: "POST"
, 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) {
var json = resp.body;
@ -231,21 +231,31 @@ var appMethods = {
// 1-65536
api.ssh(port || 22);
}
, createHttp: function (domain, handler) {
api.http(domain.name, handler);
, createShare: function (sub, domain, 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 = {};
}
, changePortForward: function (domain, port) {
api.http(domain.name, port);
api.http({ name: domain.name, handler: port });
}
, deletePortForward: function (domain) {
api.http(domain.name, 'none');
api.http({ name: domain.name, handler: 'none' });
}
, changePathHost: function (domain, path) {
api.http(domain.name, path);
api.http({ name: domain.name, handler: path });
}
, deletePathHost: function (domain) {
api.http(domain.name, 'none');
api.http({ name: domain.name, handler: 'none' });
}
, changeState: changeState
};
@ -390,7 +400,7 @@ function setState(/*ev*/) {
if (appData.exit) {
console.log('previous state exiting');
appData.exit.then(function (exit) {
if ('function' === typeof appData.exit) {
if ('function' === typeof exit) {
exit();
}
});