From 50a4d9360abd9c3722d95b2b7813830152faa9ac Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 5 Apr 2017 03:01:43 -0400 Subject: [PATCH] refactor: separate handlers, allow multiple devices to respond --- handlers.js | 6 ++++++ wstunneld.js | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/handlers.js b/handlers.js index f6915d9..6f6e19c 100644 --- a/handlers.js +++ b/handlers.js @@ -3,12 +3,18 @@ var http = require('http'); var tls = require('tls'); var packerStream = require('tunnel-packer').Stream; +var redirectHttps = require('redirect-https')(); module.exports.create = function (program) { program.httpServer = http.createServer(function (req, res) { console.log('req.socket.encrypted', req.socket.encrypted); res.end("Look! I can do a thing!"); }); + + program.httpInsecureServer = http.createServer(function (req, res) { + res.setHeader('Connection', 'close'); + redirectHttps(req, res); + }); program.httpTunnelServer = http.createServer(function (req, res) { console.log('req.socket.encrypted', req.socket.encrypted); res.end('Hello, World!'); diff --git a/wstunneld.js b/wstunneld.js index 52012f6..4c490c7 100644 --- a/wstunneld.js +++ b/wstunneld.js @@ -42,13 +42,15 @@ Devices.exist = function (store, servername) { }; Devices.next = function (store, servername) { var devices = Devices.list(store, servername); + var device; if (devices._index >= devices.length) { devices._index = 0; } + device = devices[devices._index || 0]; devices._index = (devices._index || 0) + 1; - return devices[devices._index]; + return device; }; module.exports.store = { Devices: Devices }; @@ -337,6 +339,8 @@ module.exports.create = function (copts) { console.log('servername', servername); if (/HTTP\//i.test(str)) { service = 'http'; + // TODO disallow http entirely + // /^\/\.well-known\/acme-challenge\//.test(str) if (/well-known/.test(str)) { // HTTP if (Devices.exist(deviceLists, servername)) {