diff --git a/README.md b/README.md index 8918376..5b46d5d 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ bin/server --port 1234 The localtunnel server is now running and waiting for client requests on port 1234. You will most likely want to set up a reverse proxy to listen on port 80 (or start localtunnel on port 80 directly). +**NOTE** By default, localtunnel will use subdomains for clients, if you plan to host your localtunnel server itself on a subdomain you will need to use the _--domain_ option and specify the domain name behind which you are hosting localtunnel. (i.e. my-localtunnel-server.example.com) + #### use your server You can now use your domain with the `--host` flag for the `lt` client. diff --git a/bin/server b/bin/server index d5e3fa3..a6bba9d 100755 --- a/bin/server +++ b/bin/server @@ -24,6 +24,9 @@ const argv = optimist default: '0.0.0.0', describe: 'IP address to bind to' }) + .options('domain', { + describe: 'Specify the base domain name. This is optional if hosting localtunnel from a regular example.com domain. This is required if hosting a localtunnel server from a subdomain (i.e. lt.example.dom where clients will be client-app.lt.example.come)', + }) .options('max-sockets', { default: 10, describe: 'maximum number of tcp sockets each client is allowed to establish at one time (the tunnels)' @@ -37,7 +40,8 @@ if (argv.help) { const server = CreateServer({ max_tcp_sockets: argv['max-sockets'], - secure: argv.secure + secure: argv.secure, + domain: argv.domain, }); server.listen(argv.port, argv.address, () => { diff --git a/package.json b/package.json index e64988d..aad8c63 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "on-finished": "2.3.0", "optimist": "0.6.1", "pump": "2.0.0", - "tldjs": "1.6.2" + "tldjs": "2.3.1" }, "devDependencies": { "localtunnel": "1.8.0", diff --git a/server.js b/server.js index ebe22c2..86b2d57 100644 --- a/server.js +++ b/server.js @@ -9,13 +9,16 @@ import ClientManager from './lib/ClientManager'; const debug = Debug('localtunnel:server'); -function GetClientIdFromHostname(hostname) { - return tldjs.getSubdomain(hostname); -} - export default function(opt) { opt = opt || {}; + const validHosts = (opt.domain) ? [opt.domain] : undefined; + const myTldjs = tldjs.fromUserSettings({ validHosts }); + + function GetClientIdFromHostname(hostname) { + return myTldjs.getSubdomain(hostname); + } + const manager = new ClientManager(opt); const schema = opt.secure ? 'https' : 'http'; diff --git a/test/domain.js b/test/domain.js new file mode 100644 index 0000000..6fa2b02 --- /dev/null +++ b/test/domain.js @@ -0,0 +1,52 @@ +import http from 'http'; +import url from 'url'; +import assert from 'assert'; +import localtunnel from 'localtunnel'; + +import CreateServer from '../server'; + +const localtunnel_server = CreateServer({ + domain: 'domain.example.com', +}); + +process.on('uncaughtException', (err) => { + console.error(err); +}); + +process.on('unhandledRejection', (reason, promise) => { + console.error(reason); +}); + +suite('domain'); + +var lt_server_port; + +before('set up localtunnel server', function(done) { + var server = localtunnel_server.listen(function() { + lt_server_port = server.address().port; + done(); + }); +}); + +test('landing page', function(done) { + var opt = { + host: 'localhost', + port: lt_server_port, + headers: { + host: 'domain.example.com' + }, + path: '/' + } + + var req = http.request(opt, function(res) { + res.setEncoding('utf8'); + assert.equal(res.headers.location, 'https://localtunnel.github.io/www/') + done(); + }); + + req.end(); +}); + +after('shutdown', function() { + localtunnel_server.close(); +}); diff --git a/yarn.lock b/yarn.lock index 7f84082..d6628aa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -977,6 +977,10 @@ pump@2.0.0: end-of-stream "^1.1.0" once "^1.3.1" +punycode@^1.4.1: + version "1.4.1" + resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e" + qs@~5.2.0: version "5.2.1" resolved "https://registry.yarnpkg.com/qs/-/qs-5.2.1.tgz#801fee030e0b9450d6385adc48a4cc55b44aedfc" @@ -1146,9 +1150,11 @@ supports-color@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" -tldjs@1.6.2: - version "1.6.2" - resolved "https://registry.yarnpkg.com/tldjs/-/tldjs-1.6.2.tgz#0d8b7145651f1052ac0b3c688843296636183b13" +tldjs@2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/tldjs/-/tldjs-2.3.1.tgz#cf09c3eb5d7403a9e214b7d65f3cf9651c0ab039" + dependencies: + punycode "^1.4.1" to-iso-string@0.0.2: version "0.0.2"