Merge pull request #34 from VoxyB89/main

Able To Use HTTP (Normal Backend) and HTTPS (Using SSL Certificates) (For VPS And Domain Use !!) (need to modify smth)
This commit is contained in:
Burlone
2024-10-28 00:38:17 +01:00
committed by GitHub
11 changed files with 713 additions and 221 deletions
+9 -1
View File
@@ -61,6 +61,14 @@
"bEnableAutoBackendRestart": false,
"bRestartTime": "",
"//": "Use HTTPS (For VPS Side Only, Or If You Were To Setup A Type Record To Your Own Public Adress From Your Domain) (You Also Need To Change The Examples In SSl Folder!)",
"bEnableHTTPS": false, "//": "Only Enable if You Have SSL Certificate In The ssl Folder",
"ssl": {
"cert": "./ssl/example_certificate.crt",
"//ca//": "./ssl/example_ca_bundle.crt", "//": "Optional (Only If You Have A CA Bundle from your ssl certificate)",
"key": "./ssl/example_private.key"
},
"//": "These are all the events you can add to the game, like the rift in the sky!",
"bEnableGeodeEvent": false,
"geodeEventStartDate": "2020-01-01T00:00:00.000Z",
@@ -78,4 +86,4 @@
"bEnableBlockbusterRiskyEvent": false,
"bEnableCubeLake": false,
"cubeLakeDate": "2020-01-01T00:00:00.000Z"
}
}
+2 -2
View File
@@ -28,7 +28,7 @@ module.exports = function(websiteApp) {
const oauthCallback = require('./Data/js/oauthCallback')(DISCORD_API_URL, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
websiteApp.get('/oauth2/callback', oauthCallback);
websiteApp.post('/register-user', require('./Data/js/registerUser'));
websiteApp.post('/register-user', require('./Data/js/registerUser.js'));
websiteApp.get('/register', (req, res) => {
res.sendFile(path.join(__dirname, './Data/html/register.html'));
@@ -37,4 +37,4 @@ module.exports = function(websiteApp) {
websiteApp.get('/account-exists', (req, res) => {
res.sendFile(path.join(__dirname, './Data/html/accountExists.html'));
});
};
};
+102 -31
View File
@@ -6,6 +6,8 @@ const jwt = require("jsonwebtoken");
const path = require("path");
const kv = require("./structs/kv.js");
const config = JSON.parse(fs.readFileSync("./Config/config.json").toString());
const WebSocket = require('ws');
const https = require("https"); // Import the https module
const log = require("./structs/log.js");
const error = require("./structs/error.js");
@@ -21,6 +23,23 @@ global.JWT_SECRET = functions.MakeID();
const PORT = config.port;
const WEBSITEPORT = config.Website.websiteport;
// Declare httpsServer once
let httpsServer;
// Check if HTTPS is enabled
if (config.bEnableHTTPS) {
const https = require('https'); // Import the https module
// Load SSL certificate options from config
const httpsOptions = {
cert: fs.readFileSync(config.ssl.cert),
ca: fs.existsSync(config.ssl.ca) ? fs.readFileSync(config.ssl.ca) : undefined, // Optional
key: fs.readFileSync(config.ssl.key)
};
httpsServer = https.createServer(httpsOptions, app);
}
if (!fs.existsSync("./ClientSettings")) fs.mkdirSync("./ClientSettings");
global.JWT_SECRET = functions.MakeID();
@@ -107,45 +126,97 @@ app.get("/unknown", (req, res) => {
res.json({ msg: "Reload Backend - Made by Burlone" });
});
app.listen(PORT, () => {
log.backend(`Backend started listening on port ${PORT}`);
require("./xmpp/xmpp.js");
if (config.discord.bUseDiscordBot === true) {
require("./DiscordBot");
}
if (config.bUseAutoRotate === true) {
require("./structs/autorotate.js")
}
}).on("error", async (err) => {
if (err.code === "EADDRINUSE") {
log.error(`Port ${PORT} is already in use!\nClosing in 3 seconds...`);
await functions.sleep(3000);
process.exit(0);
} else throw err;
});
// Start the server
let server;
if (config.bEnableHTTPS) {
server = httpsServer.listen(PORT, () => {
log.backend(`Backend started listening on port ${PORT} (HTTPS)`);
// Load additional modules
require("./xmpp/xmpp.js");
if (config.discord.bUseDiscordBot === true) {
require("./DiscordBot");
}
if (config.bUseAutoRotate === true) {
require("./structs/autorotate.js");
}
}).on("error", async (err) => {
if (err.code === "EADDRINUSE") {
log.error(`Port ${PORT} is already in use!\nClosing in 3 seconds...`);
await functions.sleep(3000);
process.exit(0);
} else {
throw err;
}
});
} else {
server = app.listen(PORT, () => {
log.backend(`Backend started listening on port ${PORT} (HTTP)`);
// Load additional modules
require("./xmpp/xmpp.js");
if (config.discord.bUseDiscordBot === true) {
require("./DiscordBot");
}
if (config.bUseAutoRotate === true) {
require("./structs/autorotate.js");
}
}).on("error", async (err) => {
if (err.code === "EADDRINUSE") {
log.error(`Port ${PORT} is already in use!\nClosing in 3 seconds...`);
await functions.sleep(3000);
process.exit(0);
} else {
throw err;
}
});
}
if (config.bEnableAutoBackendRestart === true) {
AutoBackendRestart.scheduleRestart(config.bRestartTime);
}
if (config.Website.bUseWebsite === true) {
const websiteApp = express();
require('./Website/website')(websiteApp);
websiteApp.listen(WEBSITEPORT, () => {
log.website(`Website started listening on port ${WEBSITEPORT}`);
}).on("error", async (err) => {
if (err.code === "EADDRINUSE") {
log.error(`Website port ${WEBSITEPORT} is already in use!\nClosing in 3 seconds...`);
await functions.sleep(3000);
process.exit(1);
} else {
throw err;
}
});
// Load SSL certificate options if HTTPS is enabled
let httpsOptions;
if (config.bEnableHTTPS) {
httpsOptions = {
cert: fs.readFileSync(config.ssl.cert),
ca: fs.existsSync(config.ssl.ca) ? fs.readFileSync(config.ssl.ca) : undefined, // Optional
key: fs.readFileSync(config.ssl.key)
};
}
// Create the HTTPS server for the website
if (config.bEnableHTTPS) {
const httpsServer = https.createServer(httpsOptions, websiteApp);
httpsServer.listen(config.Website.websiteport, () => {
log.website(`Website started listening on port ${config.Website.websiteport} (HTTPS)`);
}).on("error", async (err) => {
if (err.code === "EADDRINUSE") {
log.error(`Website port ${config.Website.websiteport} is already in use!\nClosing in 3 seconds...`);
await functions.sleep(3000);
process.exit(1);
} else {
throw err;
}
});
} else {
// Fallback to HTTP server
websiteApp.listen(config.Website.websiteport, () => {
log.website(`Website started listening on port ${config.Website.websiteport} (HTTP)`);
}).on("error", async (err) => {
if (err.code === "EADDRINUSE") {
log.error(`Website port ${config.Website.websiteport} is already in use!\nClosing in 3 seconds...`);
await functions.sleep(3000);
process.exit(1);
} else {
throw err;
}
});
}
}
app.use((req, res, next) => {
@@ -169,4 +240,4 @@ function DateAddHours(pdate, number) {
return date;
}
module.exports = app;
module.exports = app;
+2
View File
@@ -2,4 +2,6 @@
title Reload Backend Package Installer
npm i
npm install express
npm install ws
pause
+567 -176
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -10,7 +10,7 @@
"destr": "^2.0.3",
"discord.js": "^13.7.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express": "^4.21.1",
"express-rate-limit": "^6.7.0",
"ioredis": "^5.4.1",
"jsonwebtoken": "^8.5.1",
View File
View File
View File
View File
+30 -10
View File
@@ -2,24 +2,44 @@ const WebSocket = require("ws").Server;
const XMLBuilder = require("xmlbuilder");
const XMLParser = require("xml-parser");
const express = require("express");
const app = express();
const fs = require("fs");
const https = require("https"); // Import the https module
const config = JSON.parse(fs.readFileSync("./Config/config.json").toString());
const app = express();
const log = require("../structs/log.js");
const functions = require("../structs/functions.js");
const User = require("../model/user.js");
const Friends = require("../model/friends.js");
const port = 80;
const wss = new WebSocket({ server: app.listen(port) });
const matchmaker = require("../matchmaker/matchmaker.js");
const port = config.bEnableHTTPS ? 443 : 80; // Use port 443 for HTTPS, 80 for HTTP
let wss;
// Load SSL certificate options if HTTPS is enabled
let httpsOptions;
if (config.bEnableHTTPS) {
httpsOptions = {
cert: fs.readFileSync(config.ssl.cert),
ca: fs.existsSync(config.ssl.ca) ? fs.readFileSync(config.ssl.ca) : undefined, // Optional
key: fs.readFileSync(config.ssl.key)
};
}
// Create the WebSocket server
if (config.bEnableHTTPS) {
const httpsServer = https.createServer(httpsOptions, app);
wss = new WebSocket({ server: httpsServer });
httpsServer.listen(port, () => {
log.xmpp(`XMPP and Matchmaker started listening on port ${port} (HTTPS)`);
});
} else {
wss = new WebSocket({ server: app.listen(port) });
log.xmpp(`XMPP and Matchmaker started listening on port ${port} (HTTP)`);
}
global.xmppDomain = "prod.ol.epicgames.com";
global.Clients = [];
// multi user chat rooms (global chat/party chat)
global.MUCs = {};
global.MUCs = {}; // Multi-user chat rooms
app.get("/", (req, res) => {
res.type("application/json");
@@ -532,4 +552,4 @@ function isJSON(str) {
return false;
}
return true;
}
}