added subdomain support

This commit is contained in:
mgudesblatart
2018-02-28 21:10:33 -05:00
parent 2a9bc27117
commit c624943877
6 changed files with 2243 additions and 14 deletions
+22 -7
View File
@@ -10,11 +10,27 @@ import rand_id from './lib/rand_id';
const debug = Debug('localtunnel:server');
function GetClientIdFromHostname(hostname) {
return tldjs.getSubdomain(hostname);
function GetClientIdFromHostname(hostname,opt) {
let subdomain = tldjs.getSubdomain(hostname);
console.log(opt,'option');
console.log(hostname,'hostname');
console.log(subdomain,'subdomain');
if (subdomain && opt.subHost) {
const subHosts = Array.isArray(opt.subHost)
? opt.subHost : [opt.subHost]
const subHost = subHosts.reduce((found, sub) => {
return found
|| (subdomain.slice(-sub.length) == sub ? sub : '')
}, '')
subdomain = subHost
? subdomain.slice(0, -(subHost.length + 1))
: subdomain
}
return subdomain;
}
module.exports = function(opt) {
console.log(opt);
opt = opt || {};
const manager = new ClientManager(opt);
@@ -42,7 +58,6 @@ module.exports = function(opt) {
// root endpoint
app.use(async (ctx, next) => {
const path = ctx.request.path;
// skip anything not on the root path
if (path !== '/') {
await next();
@@ -54,7 +69,6 @@ module.exports = function(opt) {
const req_id = rand_id();
debug('making new client with id %s', req_id);
const info = await manager.newClient(req_id);
const url = schema + '://' + info.id + '.' + ctx.request.host;
info.url = url;
ctx.body = info;
@@ -81,7 +95,7 @@ module.exports = function(opt) {
const req_id = parts[1];
// limit requested hostnames to 63 characters
if (! /^[a-z0-9]{4,63}$/.test(req_id)) {
if (! /^[a-z0-9-]{4,63}$/.test(req_id)) {
const msg = 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.';
ctx.status = 403;
ctx.body = {
@@ -111,7 +125,8 @@ module.exports = function(opt) {
return;
}
const clientId = GetClientIdFromHostname(hostname);
const clientId = GetClientIdFromHostname(hostname,opt);
console.log(clientId,'clientId response');
if (!clientId) {
appCallback(req, res);
return;
@@ -133,7 +148,7 @@ module.exports = function(opt) {
return;
}
const clientId = GetClientIdFromHostname(hostname);
const clientId = GetClientIdFromHostname(hostname,opt);
if (!clientId) {
sock.destroy();
return;