From 587665806bb12e3e08772708029bc57a4b5d4f2e Mon Sep 17 00:00:00 2001 From: CannonBallz Date: Wed, 9 Dec 2015 13:53:47 -0800 Subject: [PATCH] Added HTTP to HTTPS redirect for servers using SSL This change allows the server to redirect all http traffic on a specified port to the port used for SSL connections. To enable this option you will need to add two extra options to the server block for SSL servers in config.js, the following example details the options (using standard https and http ports for clarity but not required): conf.servers.push({ port: "443", address: "0.0.0.0", ssl: true, ssl_key: "example.key", ssl_ca: "example.ca", ssl_cert: "example.crt", // Custom HTTP redirect options http_redirect: true, http_redirect_port: "80" }); --- server/weblistener.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/weblistener.js b/server/weblistener.js index 75a0756f8..ed60e7bdd 100644 --- a/server/weblistener.js +++ b/server/weblistener.js @@ -8,7 +8,7 @@ var engine = require('engine.io'), dns = require('dns'), url = require('url'), _ = require('lodash'), - spdy = require('spdy'), + spdy = require('https'), ipaddr = require('ipaddr.js'), winston = require('winston'), Client = require('./client.js').Client, @@ -58,6 +58,15 @@ var WebListener = module.exports = function (web_config) { hs.listen(web_config.port, web_config.address, function () { that.emit('listening'); + + //Redirect from http port to https + if (web_config.http_redirect) { + var http = require('http'); + http.createServer(function (req, res) { + res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url }); + res.end(); + }).listen(web_config.http_redirect_port); + } }); } else {