added support multiple-level subdomain #27

Closed
chuangbo wants to merge 2 commits from feature/multiple-level-subdomain into master
2 changed files with 11 additions and 20 deletions
Showing only changes of commit 7e021a48c2 - Show all commits
-1
View File
@@ -27,7 +27,6 @@
"stackup": "0.0.5", "stackup": "0.0.5",
"stylish": "0.5.0", "stylish": "0.5.0",
"taters": "1.0.0", "taters": "1.0.0",
"tldjs": "1.5.1",
"ws": "0.6.5" "ws": "0.6.5"
}, },
"devDependencies": { "devDependencies": {
+11 -19
View File
@@ -7,7 +7,6 @@ var stylish = require('stylish');
var makeover = require('makeover'); var makeover = require('makeover');
var makeup = require('makeup'); var makeup = require('makeup');
var browserkthx = require('browserkthx'); var browserkthx = require('browserkthx');
var tldjs = require('tldjs');
var on_finished = require('on-finished'); var on_finished = require('on-finished');
var favicon = require('serve-favicon'); var favicon = require('serve-favicon');
var debug = require('debug')('localtunnel-server'); var debug = require('debug')('localtunnel-server');
@@ -32,21 +31,12 @@ function maybe_bounce(req, res, bounce) {
return false; return false;
} }
var subdomain = tldjs.getSubdomain(hostname); var client_id = hostname;
if (!subdomain) {
return false;
}
var client_id = subdomain;
var client = clients[client_id]; var client = clients[client_id];
// no such subdomain // no such client
// we use 502 error to the client to signify we can't service the request
if (!client) { if (!client) {
res.statusCode = 502; return false;
res.end('localtunnel error: no active client for \'' + client_id + '\'');
req.connection.destroy();
return true;
} }
// flag if we already finished before we get a socket // flag if we already finished before we get a socket
@@ -180,14 +170,15 @@ module.exports = function(opt) {
} }
var req_id = rand_id(); var req_id = rand_id();
debug('making new client with id %s', req_id); var client_domain = req_id + '.' + req.headers.host;
new_client(req_id, opt, function(err, info) { debug('making new client with domain %s', client_domain);
new_client(client_domain, opt, function(err, info) {
if (err) { if (err) {
res.statusCode = 500; res.statusCode = 500;
return res.end(err.message); return res.end(err.message);
} }
var url = schema + '://' + req_id + '.' + req.headers.host; var url = schema + '://' + client_domain;
info.url = url; info.url = url;
res.json(info); res.json(info);
}); });
@@ -207,13 +198,14 @@ module.exports = function(opt) {
return next(err); return next(err);
} }
debug('making new client with id %s', req_id); var client_domain = req_id + '.' + req.headers.host;
new_client(req_id, opt, function(err, info) { debug('making new client with domain %s', client_domain);
new_client(client_domain, opt, function(err, info) {
if (err) { if (err) {
return next(err); return next(err);
} }
var url = schema + '://' + req_id + '.' + req.headers.host; var url = schema + '://' + client_domain;
info.url = url; info.url = url;
res.json(info); res.json(info);
}); });