From 95050849d84739a3c36c1f87345f0d3b83a0f17e Mon Sep 17 00:00:00 2001 From: rot0xd Date: Thu, 23 Mar 2017 03:24:14 -0600 Subject: [PATCH] Adding option for http to redirect to https --- config.js | 1 + server.js | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/config.js b/config.js index 3e70a6a..88826a1 100644 --- a/config.js +++ b/config.js @@ -2,6 +2,7 @@ "http": true, "http_host": "0.0.0.0", "http_port": 7777, + "http_redirect_to_https": false, "https": false, "https_host": "0.0.0.0", diff --git a/server.js b/server.js index c674085..f791d79 100644 --- a/server.js +++ b/server.js @@ -186,15 +186,20 @@ app.use(connect_st({ index: 'index.html' })); - if (config.http) { - http.createServer(app).listen(config.http_port, config.http_host); - winston.info('listening on http:\/\/' + config.http_host + ':' + config.http_port); + if (config.http && config.https && config.http_redirect_to_https){ + http.createServer(function(req, res) { + res.writeHead(301, { "Location": "https:\/\/" + req.headers['host'] + req.url }); + res.end(); + }).listen(config.http_port, config.http_host) + winston.info('redirecting http traffic to https'); + } else { + http.createServer(app).listen(config.http_port, config.http_host); + winston.info('listening on http:\/\/' + config.http_host + ':' + config.http_port); + } } if (config.https) { https.createServer(https_options, app).listen(config.https_port, config.https_host); winston.info('listening on https:\/\/' + config.https_host + ':' + config.https_port); } - -