From edfd0e2bdceb59e405b274a7f7a3285487f656b1 Mon Sep 17 00:00:00 2001 From: Adrix <129524793+VoxyB89@users.noreply.github.com> Date: Sun, 27 Oct 2024 17:12:44 +0200 Subject: [PATCH] Updated The App.listen Method to Use Either Http Or Https. It Still Has All Functions Of Burlone Nothing Else That IT Doesnt --- index.js | 76 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 07dc5ac..a551158 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,21 @@ global.JWT_SECRET = functions.MakeID(); const PORT = config.port; const WEBSITEPORT = config.Website.websiteport; +// Check if HTTPS is enabled +let httpsServer; +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,25 +122,50 @@ app.get("/unknown", (req, res) => { res.json({ msg: "Reload Backend - Made by Burlone" }); }); -app.listen(PORT, () => { - log.backend(`Backend started listening on port ${PORT}`); +// Start the server +const startServer = (server) => { + server.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") - } + // 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; -}); + }).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; + } + }); +}; + +// Check if HTTPS is enabled +if (config.bEnableHTTPS) { + 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) + }; + + const httpsServer = https.createServer(httpsOptions, app); + wss = new WebSocket({ server: httpsServer }); + + // Start the HTTPS server + startServer(httpsServer); +} else { + // Start the HTTP server + wss = new WebSocket({ server: app.listen(PORT) }); + startServer(app); +} if (config.bEnableAutoBackendRestart === true) { AutoBackendRestart.scheduleRestart(config.bRestartTime); @@ -169,4 +209,4 @@ function DateAddHours(pdate, number) { return date; } -module.exports = app; \ No newline at end of file +module.exports = app;