Added HTTPS support

This commit is contained in:
rainbow napkin 2025-04-12 06:09:34 -04:00
parent 567eb5b574
commit 069cb423fd
4 changed files with 35 additions and 9 deletions

4
.gitignore vendored
View file

@ -2,4 +2,6 @@ node_modules/
package-lock.json package-lock.json
config.json config.json
state.json state.json
chatexamples.txt chatexamples.txt
server.cert
server.key

View file

@ -6,6 +6,10 @@
"domain": "localhost", "domain": "localhost",
"sessionSecret": "CHANGE_ME", "sessionSecret": "CHANGE_ME",
"altchaSecret": "CHANGE_ME", "altchaSecret": "CHANGE_ME",
"ssl":{
"cert": "./server.cert",
"key": "./server.key"
},
"db":{ "db":{
"address": "127.0.0.1", "address": "127.0.0.1",
"port": "27017", "port": "27017",

View file

@ -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 You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//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 //Define NPM imports
const express = require('express'); const express = require('express');
const session = require('express-session'); const session = require('express-session');
@ -23,9 +30,6 @@ const path = require('path');
const mongoStore = require('connect-mongo'); const mongoStore = require('connect-mongo');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
//Define global crypto variable for altcha
globalThis.crypto = require('node:crypto').webcrypto;
//Define Local Imports //Define Local Imports
//Application //Application
const channelManager = require('./app/channel/channelManager'); const channelManager = require('./app/channel/channelManager');
@ -82,9 +86,25 @@ const sessionMiddleware = session({
store: module.exports.store store: module.exports.store
}); });
//Define http and socket.io servers //Declare web server
const httpServer = createServer(app); let webServer = null;
const io = new Server(httpServer, {});
//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 //Connect mongoose to the database
mongoose.set("sanitizeFilter", true).connect(dbUrl).then(() => { mongoose.set("sanitizeFilter", true).connect(dbUrl).then(() => {
@ -164,6 +184,6 @@ scheduler.kickoff();
module.exports.channelManager = new channelManager(io) module.exports.channelManager = new channelManager(io)
//Listen Function //Listen Function
httpServer.listen(port, () => { webServer.listen(port, () => {
console.log(`Opening port ${port}`); console.log(`Opening port ${port}`);
}); });

View file

@ -26,7 +26,7 @@ const validator = require('validator');//We need validators for express-less cod
module.exports.securityCheck = function(){ module.exports.securityCheck = function(){
//Check Protocol //Check Protocol
if(config.protocol == 'http'){ if(config.protocol.toLowerCase() != 'https'){
//If it's insecure then warn the admin //If it's insecure then warn the admin
loggerUtil.consoleWarn("Starting in HTTP mode. This server should be used for development purposes only!"); loggerUtil.consoleWarn("Starting in HTTP mode. This server should be used for development purposes only!");
} }