added a http redirection to https

This commit is contained in:
catvayor 2023-09-09 16:33:03 +02:00
parent f231daf4e9
commit 26b28fad3e
2 changed files with 18 additions and 7 deletions

View file

@ -12,11 +12,12 @@ function validator(id){
module.exports = { module.exports = {
"port": 9000, "port": 9000,
"http_port": 9001,
"key": "certif/server.key", "key": "certif/server.key",
"cert": "certif/server.crt", "cert": "certif/server.crt",
"validator": validator, "validator": validator,
// Offset for the randomization of the blurred status // Offset for the randomization of the blurred status
"lat_ofs": 0.0005, "lat_ofs": 0.0005,
"long_ofs": 0.0005, "long_ofs": 0.0005,
} }

View file

@ -27,7 +27,8 @@ Les messages à transmettre par le serveur :
*/ */
// require = include // require = include
var http = require('https');//require('http'); var https = require('https');
var http = require('http');
var url = require('url'); var url = require('url');
var fs = require('fs'); var fs = require('fs');
var config = require('./config.js'); var config = require('./config.js');
@ -44,7 +45,7 @@ const bonus_delay = 10000;// 3*60*1000;
var equipes = {}; var equipes = {};
console.log("Setup http server"); console.log("Setup https server");
const option = { const option = {
key: fs.readFileSync(config.key), key: fs.readFileSync(config.key),
@ -52,7 +53,7 @@ const option = {
}; };
// The server // The server
var server = http.createServer(option, function(req, res){ var server = https.createServer(option, function(req, res){
var q = url.parse(req.url, true); var q = url.parse(req.url, true);
var filename = "static" + q.pathname; var filename = "static" + q.pathname;
if(q.pathname.includes("..")) if(q.pathname.includes(".."))
@ -110,6 +111,14 @@ var server = http.createServer(option, function(req, res){
}); });
}); });
console.log("Setup http redirection");
//HTTP -> HTTPS redirect
var redirect_server = http.createServer(function(req, res){
var url = `https://${req.headers.host.split(":")[0]}:${config.port}${req.url}`;
res.writeHead(301,{Location: url});
res.end();
});
console.log("Setup io server"); console.log("Setup io server");
const { Server } = require("socket.io"); const { Server } = require("socket.io");
var io = new Server(server); var io = new Server(server);
@ -370,4 +379,5 @@ io.on('connection', function(socket){
console.log("Launch server"); console.log("Launch server");
server.listen(config.port, "::"); server.listen(config.port, "::");
redirect_server.listen(config.http_port, "::");
console.log("Running !"); console.log("Running !");