Added https support

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-16 11:01:01 +01:00
parent e8945185e2
commit 05b034974c

View file

@ -23,6 +23,8 @@ var bodyParser = require('body-parser');
var busboy = require('connect-busboy');
var fs = require('fs');
var app = express();
var http = require('http');
var https = require('https');
var logger = require('./logger.js');
var utils = require('./utils.js');
@ -41,7 +43,14 @@ const simpOutFile = "public/simplified";
const inputFile = "stats/inputs";
const msgFile = "stats/msgs";
const logFile = "stats/logs";
// https://certbot.eff.org/
const key = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/privkey.pem", "utf8");
const cert = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/fullchain.pem", "utf8");
const ca = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/chain.pem", "utf8");
var certs = {key:key, cert:cert, ca:ca};
const port = 8080;
const httpsPort = 8443;
var highlights = ["public", "static", "manual", "isgetting", "postfeedback",
"postquestions"
@ -66,6 +75,13 @@ try {
var newMessages = "";
app.set('view engine', 'ejs');
//app.all('*', function(req, res, next) {
// if(req.secure) {
// next();
// } else {
// res.redirect('https://' + req.hostname + req.url);
// }
//});
app.use(function(req, res, next) {
res.on('finish', function() {
Log(req, true);
@ -203,14 +219,18 @@ app.post('*', function(req, res) {
});
stat.Load();
app.listen(port);
const httpServer = http.createServer(app);
const httpsServer = https.createServer(certs, app);
httpServer.listen(port);
httpsServer.listen(httpsPort);
logger.Log("[STRT]: Server listening on port " + port + "...", logger.GetColor("yellow"));
function Log(req, toFile) {
try {
var ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress;
var logEntry = "[RSND]: " + ip + ", " + req.headers['user-agent'] +
" " + req.method + " " + req.url;
" " + req.method + " ";
logEntry += req.url;
var color = logger.GetColor("green");
for (var i = 0; i < highlights.length; i++)
if (req.url.toLowerCase().includes(highlights[i])) {