added support multiple-level subdomain #27

Closed
chuangbo wants to merge 2 commits from feature/multiple-level-subdomain into master
5 changed files with 16 additions and 25 deletions
-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);
}); });
+3 -3
View File
@@ -5,7 +5,7 @@ var localtunnel = require('localtunnel');
var localtunnel_server = require('../server')(); var localtunnel_server = require('../server')();
var lt_server_port var lt_server_port;
test('setup localtunnel server', function(done) { test('setup localtunnel server', function(done) {
var server = localtunnel_server.listen(function() { var server = localtunnel_server.listen(function() {
@@ -79,7 +79,7 @@ test('query localtunnel server w/ ident', function(done) {
host: 'localhost', host: 'localhost',
port: lt_server_port, port: lt_server_port,
headers: { headers: {
host: hostname + '.tld' host: hostname + ':' + lt_server_port
}, },
path: '/' path: '/'
} }
@@ -112,7 +112,7 @@ test('request specific domain', function(done) {
localtunnel(test._fake_port, opt, function(err, tunnel) { localtunnel(test._fake_port, opt, function(err, tunnel) {
assert.ifError(err); assert.ifError(err);
var url = tunnel.url; var url = tunnel.url;
assert.ok(new RegExp('^http:\/\/.*localhost:' + lt_server_port + '$').test(url)); assert.ok(new RegExp('^http:\/\/.*\.localhost:' + lt_server_port + '$').test(url));
test._fake_url = url; test._fake_url = url;
done(err); done(err);
}); });
+1 -1
View File
@@ -61,7 +61,7 @@ test('query localtunnel server w/ ident', function(done) {
port: lt_server_port, port: lt_server_port,
agent: false, agent: false,
headers: { headers: {
host: hostname + '.tld' host: hostname + ':' + lt_server_port
}, },
path: '/' path: '/'
} }
+1 -1
View File
@@ -49,7 +49,7 @@ test('test websocket server request', function(done) {
var hostname = url.parse(test._fake_url).hostname; var hostname = url.parse(test._fake_url).hostname;
var ws = new WebSocket('http://localhost:' + lt_server_port, { var ws = new WebSocket('http://localhost:' + lt_server_port, {
headers: { headers: {
host: hostname + '.tld' host: hostname + ':' + lt_server_port
} }
}); });
ws.on('message', function(msg) { ws.on('message', function(msg) {