From 8cc22d501ca17c91604669f8fdcefc7262613c14 Mon Sep 17 00:00:00 2001 From: Roman Shtylman Date: Sun, 20 Apr 2014 12:56:26 -0400 Subject: [PATCH] fix bad matching against subdomains Match for valid subdomains versus anything that comes before a period. fixes #7 --- package.json | 3 ++- server.js | 11 ++++------- test/basic.js | 29 ++++++++++++++++++++++++++++- test/queue.js | 2 +- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index abd4544..2bfc8e1 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "bouncy": "3.2.1", "superstack": "0.0.4", "stylish": "0.4.1", - "makeover": "0.0.1" + "makeover": "0.0.1", + "tldjs": "1.3.1" }, "devDependencies": { "mocha": "1.18.2", diff --git a/server.js b/server.js index 4ef9dc8..53ddf99 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,7 @@ var makeover = require('makeover'); var makeup = require('makeup'); var engine = require('engine.io'); var browserkthx = require('browserkthx'); +var tldjs = require('tldjs'); var debug = require('debug')('localtunnel-server'); var Proxy = require('./proxy'); @@ -32,16 +33,12 @@ function maybe_bounce(req, res, bounce) { return false; } - // extract the subdomain, which is the client id - var match = hostname.match(/^([a-z0-9]+)[.].*/); - - // not for a specific client - // pass on to regular server - if (!match) { + var subdomain = tldjs.getSubdomain(hostname); + if (!subdomain) { return false; } - var client_id = match[1]; + var client_id = subdomain; var client = clients[client_id]; // no such subdomain diff --git a/test/basic.js b/test/basic.js index 6a15d0d..8032a99 100644 --- a/test/basic.js +++ b/test/basic.js @@ -15,6 +15,33 @@ test('setup localtunnel server', function(done) { }); }); +test('landing page', function(done) { + var opt = { + host: 'localhost', + port: lt_server_port, + headers: { + host: 'example.com' + }, + path: '/' + } + + var req = http.request(opt, function(res) { + res.setEncoding('utf8'); + var body = ''; + + res.on('data', function(chunk) { + body += chunk; + }); + + res.on('end', function() { + assert(body.indexOf('

expose yourself to the world

') > 0); + done(); + }); + }); + + req.end(); +}); + test('setup local http server', function(done) { var server = http.createServer(); server.on('request', function(req, res) { @@ -52,7 +79,7 @@ test('query localtunnel server w/ ident', function(done) { host: 'localhost', port: lt_server_port, headers: { - host: hostname + host: hostname + '.tld' }, path: '/' } diff --git a/test/queue.js b/test/queue.js index af1b2a7..9313b3b 100644 --- a/test/queue.js +++ b/test/queue.js @@ -61,7 +61,7 @@ test('query localtunnel server w/ ident', function(done) { port: lt_server_port, agent: false, headers: { - host: hostname + host: hostname + '.tld' }, path: '/' }