add --sub-host option
This commit is contained in:
@@ -40,22 +40,33 @@ const stats = {
|
||||
|
||||
// handle proxying a request to a client
|
||||
// will wait for a tunnel socket to become available
|
||||
function maybe_bounce(req, res, sock, head) {
|
||||
function maybe_bounce(req, res, sock, head, opt) {
|
||||
// without a hostname, we won't know who the request is for
|
||||
const hostname = req.headers.host;
|
||||
if (!hostname) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const subdomain = tldjs.getSubdomain(hostname);
|
||||
let subdomain = tldjs.getSubdomain(hostname);
|
||||
|
||||
if (subdomain && opt.subHost) {
|
||||
const subHost = opt.subHost.reduce((found, sub) => {
|
||||
return found
|
||||
|| (subdomain.slice(-sub.length) == sub ? sub : '')
|
||||
}, '')
|
||||
subdomain = subHost
|
||||
? subdomain.slice(0, -(subHost.length + 1))
|
||||
: subdomain
|
||||
}
|
||||
|
||||
if (!subdomain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const client = clients[subdomain];
|
||||
const client = clients[subdomain];
|
||||
|
||||
// no such subdomain
|
||||
// we use 502 error to the client to signify we can't service the request
|
||||
// we use 502 error to the client to signify we can't service the request
|
||||
if (!client) {
|
||||
if (res) {
|
||||
res.statusCode = 502;
|
||||
@@ -68,7 +79,7 @@ function maybe_bounce(req, res, sock, head) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
let finished = false;
|
||||
if (sock) {
|
||||
sock.once('end', function() {
|
||||
@@ -233,7 +244,7 @@ module.exports = function(opt) {
|
||||
if (req.query['new'] === undefined) {
|
||||
return next();
|
||||
}
|
||||
|
||||
|
||||
const req_id = rand_id();
|
||||
debug('making new client with id %s', req_id);
|
||||
new_client(req_id, opt, function(err, info) {
|
||||
@@ -271,20 +282,19 @@ module.exports = function(opt) {
|
||||
|
||||
app.get('/:req_id', function(req, res, next) {
|
||||
const req_id = req.params.req_id;
|
||||
|
||||
// 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 err = new Error('Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.');
|
||||
err.statusCode = 403;
|
||||
return next(err);
|
||||
}
|
||||
|
||||
|
||||
debug('making new client with id %s', req_id);
|
||||
new_client(req_id, opt, function(err, info) {
|
||||
new_client(req_id, opt, function(err, info) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
|
||||
const url = schema + '://' + req_id + '.' + req.headers.host;
|
||||
info.url = url;
|
||||
res.json(info);
|
||||
@@ -312,7 +322,7 @@ module.exports = function(opt) {
|
||||
});
|
||||
|
||||
debug('request %s', req.url);
|
||||
if (maybe_bounce(req, res, null, null)) {
|
||||
if (maybe_bounce(req, res, null, null, opt)) {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -328,7 +338,7 @@ module.exports = function(opt) {
|
||||
console.error('ws socket', err);
|
||||
});
|
||||
|
||||
if (maybe_bounce(req, null, socket, head)) {
|
||||
if (maybe_bounce(req, null, socket, head, opt)) {
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user