From a3066ec5a3066ec065988f45c77ebaa0c72a7a10 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Sun, 25 May 2025 17:47:34 -0600 Subject: [PATCH] I think stuff changed --- index.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ff37a77..b9e35b3 100644 --- a/index.js +++ b/index.js @@ -57,13 +57,26 @@ app.get('/health', (req, res) => { global.wsConnections = []; var roomList = []; + // IEM WebSocket app.ws('/iem', (ws, req) => { console.log(req.headers) - const clientIp = process.env.REALIP ? req.headers[process.env.REALIP.toLowerCase()] : req.ip; + const clientIp = process.env.REALIP ? req.headers[process.env.REALIP.toLowerCase()] || req.ip : req.ip; console.log(`connection from ${clientIp}`); const hostname = process.env.HOST_OVERRIDE || os.hostname(); + if (!req.headers['user-agent']) { + ws.send(JSON.stringify({ + "type": "internal-response", + "code": 400, + "data": { + "error": "User-Agent header is required." + } + })); + console.log(`${colors.red("[ERROR]")} User-Agent header is required from ${clientIp}`); + ws.close(); + return; + } ws.send(JSON.stringify({ "type": "connection-info", "status": socketStatus, @@ -399,8 +412,14 @@ xmpp.on("offline", () => { xmpp.disconnect().then(() => { xmpp.stop().then(() => { start(); - }) - }) + }).catch((err) => { + console.log(`${colors.red("[ERROR]")} XMPP failed to stop: ${err}. Gonna go ahead and try to reconnect...`); + start(); + }); + }).catch((err) => { + console.log(`${colors.red("[ERROR]")} XMPP failed to disconnect: ${err}. Gonna go ahead and try to reconnect...`); + start(); + }); }); var restartTimer = null; @@ -633,6 +652,14 @@ xmpp.on("close", () => { const start = () => { startup = true; + if (xmpp.status !== "offline") { + console.log(`${colors.yellow("[WARN]")} XMPP is already running, stopping it...`); + xmpp.stop().then(() => { + console.log(`${colors.cyan("[INFO]")} XMPP stopped, starting it again...`); + }).catch((err) => { + console.log(`${colors.red("[ERROR]")} Failed to stop XMPP: ${err}. Trying to start anyway...`); + }); + } xmpp.start().catch((err) => { console.log(`${colors.red("[ERROR]")} XMPP failed to start: ${err}.`); setTimeout(start, 5000);