From f1e70f77636c1e371d66c7ccd42a6959737ad2b5 Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Tue, 10 Nov 2020 21:47:35 +0100 Subject: [PATCH] Fixed CORS issue and added chat spam safeguard --- src/index.js | 7 +++++++ src/services/chat.js | 30 ++++++++++++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 2f11e6c..a321f52 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,13 @@ var ns = { tanks: io.of("/tanks") }; +// Allow CORS from ARNweb.nl +app.use(function (req, res, next) { + res.header("Access-Control-Allow-Origin", "https://arnweb.nl"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + next(); +}); + // Request documentation app.get("/", (req, res) => res.status(200).sendFile(path.resolve(__dirname + "/../html/index.html")) diff --git a/src/services/chat.js b/src/services/chat.js index 8e864ce..9514f4e 100644 --- a/src/services/chat.js +++ b/src/services/chat.js @@ -38,20 +38,22 @@ function handle(io, socket) { // Handle messages socket.on("message_send", function (message) { - // Build message data for clients - message = striptags(message, "
"); - if (message !== "") { - message = md.render(message); - message = message.replace(/\r?\n/g, "
"); - var msg = { - "username": socket.username, - "mailHash": socket.mailHash, - "message": message - }; - // Send message to clients - io.emit("message_receive", msg); - } else { - socket.emit("message_receive", serverMsg("Empty messages will not be sent.")); + if (socket.username) { + // Build message data for clients + message = striptags(message, "
"); + if (message !== "") { + message = md.render(message); + message = message.replace(/\r?\n/g, "
"); + var msg = { + "username": socket.username, + "mailHash": socket.mailHash, + "message": message + }; + // Send message to clients + io.emit("message_receive", msg); + } else { + socket.emit("message_receive", serverMsg("Empty messages will not be sent.")); + } } }); }