From 307ce9a53f5fe525d877fff7410579d1f7ea813d Mon Sep 17 00:00:00 2001 From: Nicolas Surleraux Date: Sat, 26 May 2018 13:27:58 +0200 Subject: [PATCH 1/2] add secure parameter to force https --- lib/Client.js | 9 +++++++++ lib/ClientManager.js | 3 ++- server.js | 7 +++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/Client.js b/lib/Client.js index 369eb07..2bfa910 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -13,6 +13,7 @@ class Client extends EventEmitter { const agent = this.agent = options.agent; const id = this.id = options.id; + const secure = this.secure = options.secure; this.debug = Debug(`lt:Client[${this.id}]`); @@ -56,6 +57,14 @@ class Client extends EventEmitter { } handleRequest(req, res) { + if(!req.secure && this.secure) { + // Must redirect to https + this.debug('Redirect to https > %s', req.url); + let urlObject = url.parse('http://' + this.request.header.host); + let httpsHost = urlObject.hostname; + res.redirect('https://' + httpsHost + this.request.url); + } + this.debug('> %s', req.url); const opt = { path: req.url, diff --git a/lib/ClientManager.js b/lib/ClientManager.js index e1a7838..54ebbeb 100644 --- a/lib/ClientManager.js +++ b/lib/ClientManager.js @@ -28,7 +28,7 @@ class ClientManager { // create a new tunnel with `id` // if the id is already used, a random id is assigned // if the tunnel could not be created, throws an error - async newClient(id) { + async newClient(id, secure) { const clients = this.clients; const stats = this.stats; @@ -46,6 +46,7 @@ class ClientManager { const client = new Client({ id, agent, + secure, }); // add to clients map immediately diff --git a/server.js b/server.js index 5a53c47..47beefa 100644 --- a/server.js +++ b/server.js @@ -64,10 +64,12 @@ export default function(opt) { } const isNewClientRequest = ctx.query['new'] !== undefined; + const isSecure = ctx.query['secure'] !== undefined; + if (isNewClientRequest) { const reqId = hri.random(); debug('making new client with id %s', reqId); - const info = await manager.newClient(reqId); + const info = await manager.newClient(reqId, isSecure); const url = schema + '://' + info.id + '.' + ctx.request.host; info.url = url; @@ -105,7 +107,8 @@ export default function(opt) { } debug('making new client with id %s', reqId); - const info = await manager.newClient(reqId); + const isSecure = ctx.query['secure'] !== undefined; + const info = await manager.newClient(reqId, isSecure); const url = schema + '://' + info.id + '.' + ctx.request.host; info.url = url; -- 2.54.0 From 78c06f8fc43ce0eacea2aeb88f2d1d63afaf4e19 Mon Sep 17 00:00:00 2001 From: Jo Hyun Woo Date: Wed, 27 Mar 2019 00:56:17 +0900 Subject: [PATCH 2/2] fix typo in server.js (#109) `sock` is not defined in the scope. --- server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 5a53c47..21a4af6 100644 --- a/server.js +++ b/server.js @@ -145,13 +145,13 @@ export default function(opt) { server.on('upgrade', (req, socket, head) => { const hostname = req.headers.host; if (!hostname) { - sock.destroy(); + socket.destroy(); return; } const clientId = GetClientIdFromHostname(hostname); if (!clientId) { - sock.destroy(); + socket.destroy(); return; } -- 2.54.0