From 0574586c68563a369cf39fdd37e8eadff0cdbac1 Mon Sep 17 00:00:00 2001 From: bhasher Date: Wed, 25 Nov 2020 16:05:07 +0100 Subject: [PATCH 1/3] Redirect http to https --- src/app.js | 19 ++++++++++++------- src/config/config dist.json | 1 + src/server.js | 15 ++++++++++----- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/app.js b/src/app.js index 2cf4f86..9e401e2 100644 --- a/src/app.js +++ b/src/app.js @@ -16,10 +16,22 @@ const index = require('./routes/index'); const editor = require('./routes/editor'); const legal = require('./routes/legal'); const config = require('./config/config'); +const ssl = config.SSL; const app = express(); app.disable("x-powered-by"); +if (ssl) { + app.use('*', function (req, res, next) { + if (req.secure) { + next(); + } else { + const target = (req.headers.host.includes(':') ? req.headers.host.split(':')[0] : req.headers.host) + ':' + config.PORT; + res.redirect('https://' + target + req.url); + } + }); +} + // Configure views folder nunjucks.configure(path.join(__dirname, 'views'), { autoescape: true, @@ -51,13 +63,6 @@ app.use('/', index); app.use('/editor', editor); app.use('/legal', legal); -// redirect to https when ssl is active -if (config.SSL) { - app.get('*', function(req, res) { - res.redirect('https://' + req.headers.host + req.url); - }); -} - // 404 error app.all('*', (req, res) => { res.status(404).render('404.html', {production: config.PRODUCTION, client_versobe: config.CLIENT_VERBOSE}); diff --git a/src/config/config dist.json b/src/config/config dist.json index 6b7e87e..f404a6e 100644 --- a/src/config/config dist.json +++ b/src/config/config dist.json @@ -8,6 +8,7 @@ "DAYS_TO_DELETE_DOCUMENT": 7, "DISCORD_WEBHOOK": null, "SSL": false, + "REDIRECT_PORT": null, "KEY_FILE_SSL": null, "CERT_FILE_SSL" : null, "METRICS": false, diff --git a/src/server.js b/src/server.js index b59d5d6..21fae2c 100644 --- a/src/server.js +++ b/src/server.js @@ -14,6 +14,7 @@ const host = configs.HOST; const port = configs.PORT; const DEBUG = configs.DEBUG; const ssl = configs.SSL; +const metrics = configs.METRICS; const sslKeyPath = configs.KEY_FILE_SSL; const sslCertPath = configs.CERT_FILE_SSL; @@ -27,9 +28,11 @@ const config = require('./config/config'); const http = ssl ? require('https') : require('http'); const server = http.createServer(options, app); -if (configs.METRICS) { - const {metricsApp} = require('./metricsApp'); - var metricsServer = require('http').createServer(metricsApp); + +if (ssl) { + require('http').createServer(app).listen(configs.REDIRECT_PORT, host, () => { + console.log('http requests are redirected to https.') + }) } // config websockets @@ -39,9 +42,11 @@ require('./socket/socket')(wss); server.listen(port, host, () => { - console.log('Server Started!'); + console.log(`Server Started on ${host}:${port}`); }); -if (config.METRICS) { +if (metrics) { + const {metricsApp} = require('./metricsApp'); + const metricsServer = require('http').createServer(metricsApp); metricsServer.listen(config.METRICS_PORT); } From 1e0baee29d52c3d178011e72bc2edeb8e4266ae9 Mon Sep 17 00:00:00 2001 From: bhasher Date: Wed, 25 Nov 2020 16:10:50 +0100 Subject: [PATCH 2/3] Update text on start --- src/config/config dist.json | 2 +- src/server.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/config dist.json b/src/config/config dist.json index f404a6e..4a547f3 100644 --- a/src/config/config dist.json +++ b/src/config/config dist.json @@ -8,9 +8,9 @@ "DAYS_TO_DELETE_DOCUMENT": 7, "DISCORD_WEBHOOK": null, "SSL": false, - "REDIRECT_PORT": null, "KEY_FILE_SSL": null, "CERT_FILE_SSL" : null, + "REDIRECT_PORT": null, "METRICS": false, "METRICS_PORT": 8000 } diff --git a/src/server.js b/src/server.js index 21fae2c..cca4f48 100644 --- a/src/server.js +++ b/src/server.js @@ -31,7 +31,7 @@ const server = http.createServer(options, app); if (ssl) { require('http').createServer(app).listen(configs.REDIRECT_PORT, host, () => { - console.log('http requests are redirected to https.') + console.log(`http requests from ${configs.REDIRECT_PORT} are redirected to https on ${configs.PORT}`) }) } From 8b007594e93612b756a9fb15ef9b4aca32707f36 Mon Sep 17 00:00:00 2001 From: bhasher Date: Wed, 25 Nov 2020 16:14:44 +0100 Subject: [PATCH 3/3] Verify than redirect port is not null --- src/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.js b/src/server.js index cca4f48..9042037 100644 --- a/src/server.js +++ b/src/server.js @@ -29,7 +29,7 @@ const config = require('./config/config'); const http = ssl ? require('https') : require('http'); const server = http.createServer(options, app); -if (ssl) { +if (ssl && configs.REDIRECT_PORT !== null) { require('http').createServer(app).listen(configs.REDIRECT_PORT, host, () => { console.log(`http requests from ${configs.REDIRECT_PORT} are redirected to https on ${configs.PORT}`) })