diff --git a/.gitignore b/.gitignore index 57b3e3a..5d5a00a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ node_modules/ package-lock.json config.json state.json -chatexamples.txt \ No newline at end of file +chatexamples.txt +server.cert +server.key \ No newline at end of file diff --git a/config.example.json b/config.example.json index 9ca23e9..42c171d 100644 --- a/config.example.json +++ b/config.example.json @@ -6,6 +6,10 @@ "domain": "localhost", "sessionSecret": "CHANGE_ME", "altchaSecret": "CHANGE_ME", + "ssl":{ + "cert": "./server.cert", + "key": "./server.key" + }, "db":{ "address": "127.0.0.1", "port": "27017", diff --git a/src/server.js b/src/server.js index a6fb18e..5254b2d 100644 --- a/src/server.js +++ b/src/server.js @@ -14,6 +14,13 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ +//Define global crypto variable for altcha +globalThis.crypto = require('node:crypto').webcrypto; + +//Define NODE imports +const https = require('https'); +const fs = require('fs'); + //Define NPM imports const express = require('express'); const session = require('express-session'); @@ -23,9 +30,6 @@ const path = require('path'); const mongoStore = require('connect-mongo'); const mongoose = require('mongoose'); -//Define global crypto variable for altcha -globalThis.crypto = require('node:crypto').webcrypto; - //Define Local Imports //Application const channelManager = require('./app/channel/channelManager'); @@ -82,9 +86,25 @@ const sessionMiddleware = session({ store: module.exports.store }); -//Define http and socket.io servers -const httpServer = createServer(app); -const io = new Server(httpServer, {}); +//Declare web server +let webServer = null; + +//If we're using HTTPS +if(config.protocol.toLowerCase() == "https"){ + //Read key/cert files and store contents + const httpsOptions = { + key: fs.readFileSync(config.ssl.key), + cert: fs.readFileSync(config.ssl.cert) + }; + + webServer = https.createServer(httpsOptions, app); + +//Otherwise +}else{ + //Default to HTTP + webServer = createServer(app) +} +const io = new Server(webServer, {}); //Connect mongoose to the database mongoose.set("sanitizeFilter", true).connect(dbUrl).then(() => { @@ -164,6 +184,6 @@ scheduler.kickoff(); module.exports.channelManager = new channelManager(io) //Listen Function -httpServer.listen(port, () => { +webServer.listen(port, () => { console.log(`Opening port ${port}`); }); \ No newline at end of file diff --git a/src/utils/configCheck.js b/src/utils/configCheck.js index c27aa31..76b8137 100644 --- a/src/utils/configCheck.js +++ b/src/utils/configCheck.js @@ -26,7 +26,7 @@ const validator = require('validator');//We need validators for express-less cod module.exports.securityCheck = function(){ //Check Protocol - if(config.protocol == 'http'){ + if(config.protocol.toLowerCase() != 'https'){ //If it's insecure then warn the admin loggerUtil.consoleWarn("Starting in HTTP mode. This server should be used for development purposes only!"); }