Handle subdomains #36

Closed
worg wants to merge 2 commits from feat-subdomain into master
2 changed files with 17 additions and 1 deletions
+6 -1
View File
@@ -20,6 +20,10 @@ var argv = optimist
default: 10, default: 10,
describe: 'maximum number of tcp sockets each client is allowed to establish at one time (the tunnels)' describe: 'maximum number of tcp sockets each client is allowed to establish at one time (the tunnels)'
}) })
.options('sub', {
default: false,
describe: 'use this flag to indicate that server runs in a subdomain'
})
.argv; .argv;
if (argv.help) { if (argv.help) {
@@ -29,7 +33,8 @@ if (argv.help) {
var server = require('../server')({ var server = require('../server')({
max_tcp_sockets: argv['max-sockets'], max_tcp_sockets: argv['max-sockets'],
secure: argv.secure secure: argv.secure,
sub: argv.sub,
}); });
server.listen(argv.port, function() { server.listen(argv.port, function() {
+11
View File
@@ -36,6 +36,9 @@ var stats = {
tunnels: 0 tunnels: 0
}; };
// are we serving from a subdomain?
var sub = false;
function maybe_bounce(req, res, sock, head) { function maybe_bounce(req, res, sock, head) {
// without a hostname, we won't know who the request is for // without a hostname, we won't know who the request is for
var hostname = req.headers.host; var hostname = req.headers.host;
@@ -44,6 +47,12 @@ function maybe_bounce(req, res, sock, head) {
} }
var subdomain = tldjs.getSubdomain(hostname); var subdomain = tldjs.getSubdomain(hostname);
// if we're serving from a subdomain do a proper check
if (sub) {
var subsub = (subdomain || '').split('.');
subdomain = subsub.length > 1 ? subsub[0] : '';
}
if (!subdomain) { if (!subdomain) {
return false; return false;
} }
@@ -181,6 +190,8 @@ module.exports = function(opt) {
var schema = opt.secure ? 'https' : 'http'; var schema = opt.secure ? 'https' : 'http';
sub = opt.sub || false;
var app = express(); var app = express();
app.get('/', function(req, res, next) { app.get('/', function(req, res, next) {