summary refs log tree commit diff
diff options
context:
space:
mode:
authorWlodekM <[email protected]>2024-09-09 20:38:31 +0300
committerWlodekM <[email protected]>2024-09-09 20:38:31 +0300
commit0d841c1c4d6853bd43eb575c55a57f3de675f88b (patch)
tree36e34574f682e09e88797b46254abf2b0c398258
parentd952979c5ab76a572da7cf9eede76095be0dbb88 (diff)
ipban measures
-rw-r--r--commands.js9
-rw-r--r--commands/ban.js17
-rw-r--r--commands/kick.js11
-rw-r--r--index.js4
4 files changed, 39 insertions, 2 deletions
diff --git a/commands.js b/commands.js
index 9cc95f3..cf677b9 100644
--- a/commands.js
+++ b/commands.js
@@ -99,7 +99,16 @@ export const commands = {
             if (args.length < 2) return user.socket.send(`Usage: /login <username> <password>`);
             if (!server.accounts.checkAccount(args[0])) return user.socket.send(`Account "${args[0]}" not found!`);
             if (!server.accounts.checkPassword(args[0], args[1])) return user.socket.send(`Password incorrect.`);
+            let ipBanList = JSON.parse(String(fs.readFileSync("db/bannedIps.json")));
             if (server.config.saveIP) server.accounts.logIP(args[0], user.ip);
+            if (ipBanList['account:'+args[0]] != undefined) {
+                ipBanList[user.ip] = ipBanList['account:'+args[0]];
+                user.socket.send("Your IP is banned for " + ipBanList[user.ip]);
+                user.socket.close(1002, "Banned");
+                fs.writeFileSync('db/bannedIps.json', JSON.stringify(ipBanList));
+                return;
+            }
+            console.info(`${user.name()} logged in as ${args[0]} from ${user.ip}`)
             sendInChannel(`${user.name()} logged in as ${args[0]}!`, user.channel);
             user.username = args[0];
             user.nickname = "";
diff --git a/commands/ban.js b/commands/ban.js
new file mode 100644
index 0000000..db681d1
--- /dev/null
+++ b/commands/ban.js
@@ -0,0 +1,17 @@
+import fs from "fs";
+
+export default {
+    name: "ban",
+    aliases: [],
+    command: function ({ user, server, args }) {
+        if(!user.admin) return user.socket.send("You are not admin");
+        if(!server.accounts.checkAccount(args[0])) return user.socket.send("Account not found");
+        let theUser = Object.values(server.users).find(a => a.username == args[0]);
+        theUser.socket.send('you were kicked because you were banned, L');
+        let ipBanList = JSON.parse(String(fs.readFileSync("db/bannedIps.json")));
+        ipBanList[theUser.ip] = args[1] ?? 'reason';
+        ipBanList['account:'+args[0]] = args[1] ?? 'reason';
+        theUser.socket.close(1003, "Kicked");
+        fs.writeFileSync('db/bannedIps.json', JSON.stringify(ipBanList));
+    },
+};
diff --git a/commands/kick.js b/commands/kick.js
new file mode 100644
index 0000000..accadb0
--- /dev/null
+++ b/commands/kick.js
@@ -0,0 +1,11 @@
+export default {
+    name: "kick",
+    aliases: [],
+    command: function ({ user, server, args }) {
+        if(!user.admin) return user.socket.send("You are not admin");
+        if(!server.accounts.checkAccount(args[0])) return user.socket.send("Account not found");
+        let theUser = Object.values(server.users).find(a => a.username == args[0]);
+        theUser.socket.send('you were kicked')
+        theUser.socket.close(1003, "Kicked");
+    },
+};
diff --git a/index.js b/index.js
index f867b09..28db598 100644
--- a/index.js
+++ b/index.js
@@ -71,8 +71,8 @@ ws.on("connection", (socket, request) => {
     console.log(request.headers["x-forwarded-for"], request.socket.remoteAddress);
     let anonID = getRandomInt(0, 99999);
     server.users[userID] = {
-        username: `Anonymous${"0".repeat(5 - anonID.toString().length) + anonID.toString()}`,
-        nickname: `Anonymous${"0".repeat(5 - anonID.toString().length) + anonID.toString()}`,
+        username: `Anonymous#${"0".repeat(5 - anonID.toString().length) + anonID.toString()}`,
+        nickname: `Anonymous#${"0".repeat(5 - anonID.toString().length) + anonID.toString()}`,
         guest: true,
         socket: socket,
         joinReq: request,