Compare commits

..

2 commits

Author SHA1 Message Date
Christopher Cookman 4249815c55 Add REALIP env var 2025-05-08 01:19:31 -06:00
Christopher Cookman bca8efef23 Fix crash when non-json data 2025-05-08 01:17:31 -06:00

View file

@ -59,6 +59,7 @@ global.wsConnections = [];
var roomList = []; var roomList = [];
// IEM WebSocket // IEM WebSocket
app.ws('/iem', (ws, req) => { app.ws('/iem', (ws, req) => {
req.ip = process.env.REALIP ? req.headers[process.env.REALIP.toLowerCase()] : req.ip;
console.log(`connection from ${req.ip}`); console.log(`connection from ${req.ip}`);
const hostname = process.env.HOST_OVERRIDE || os.hostname(); const hostname = process.env.HOST_OVERRIDE || os.hostname();
@ -90,7 +91,20 @@ app.ws('/iem', (ws, req) => {
}); });
try { try {
ws.on('message', (msg) => { ws.on('message', (msg) => {
const data = JSON.parse(msg); let data;
try {
data = JSON.parse(msg);
} catch (error) {
ws.send(JSON.stringify({
"type": "internal-response",
"code": 400,
"data": {
"error": "Invalid JSON format."
}
}));
console.log(`${colors.red("[ERROR]")} Invalid JSON format from ${req.ip}: ${msg}`);
return;
}
if (!data.type) return ws.send(JSON.stringify({ if (!data.type) return ws.send(JSON.stringify({
"type": "internal-response", "type": "internal-response",
"code": 400, "code": 400,
@ -191,6 +205,15 @@ app.ws('/iem', (ws, req) => {
})); }));
} }
break; break;
case "ping":
ws.send(JSON.stringify({
"type": "internal-response",
"code": 200,
"data": {
"message": "pong"
}
}));
break;
default: default:
ws.send(JSON.stringify({ ws.send(JSON.stringify({
"type": "internal-response", "type": "internal-response",
@ -551,25 +574,6 @@ xmpp.reconnect.on("reconnected", () => {
}); });
}) })
const createDiscordEmbed = (data) => {
const embed = {
description: `<t:${Math.floor(data.timestamp / 1000)}:T> <t:${Math.floor(data.timestamp / 1000)}:R> ${data.body}`,
color: parseInt(config.priorityColors[data.event.priority].replace("#", ""), 16) || 0x000000,
timestamp: new Date(data.timestamp).toISOString(),
footer: {
text: `Station: ${data.station} PID: ${data.raw} Channel: ${data.channel}`
}
};
if (data.image) {
embed.image = {
url: data.image
};
}
return embed;
};
xmpp.on("online", async (address) => { xmpp.on("online", async (address) => {