From 6066f2e4b417006f7a982369b84e6cab3e14921d Mon Sep 17 00:00:00 2001 From: legonzaur Date: Wed, 3 May 2023 17:02:44 +0200 Subject: [PATCH 1/9] fix date log --- src/server/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/server.ts b/src/server/server.ts index 46aab9b..763bd58 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -233,7 +233,7 @@ router.post( .then((e) => { res.sendStatus(201) console.log( - `[${Date.now().toLocaleString()}] Kill submitted for server ${servername}, ${attacker_name} killed ${victim_name}` + `[${new Date().toLocaleString()}] Kill submitted for server ${servername}, ${attacker_name} killed ${victim_name}` ) }) .catch((e) => { From d3c8d4cecc807634a3e93be5723d3b683c8f4003 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Wed, 3 May 2023 20:27:00 +0200 Subject: [PATCH 2/9] update username on realtime --- src/process/onKill.ts | 3 +++ src/server/server.ts | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/process/onKill.ts b/src/process/onKill.ts index f662d3c..c923fc1 100644 --- a/src/process/onKill.ts +++ b/src/process/onKill.ts @@ -102,7 +102,10 @@ export default async function listenKills() { killEntry.max_distance = Math.max(killEntry.max_distance || 0, payload.distance) } deathEntry.deaths++ + deathEntry.attacker_name = payload.victim_name deathWithWeaponEntry.deaths_with_weapon++ + deathWithWeaponEntry.attacker_name = payload.victim_name + killEntry.attacker_name = payload.attacker_name }) return pgClient } diff --git a/src/server/server.ts b/src/server/server.ts index 763bd58..8c8d1a6 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -145,6 +145,7 @@ router.post( //body('servername').customSanitizer(e => e.replace(/[^a-z0-9]/gi, '')), validateErrors, async (req, res) => { + // Do we check the same thing twice ????? if (!req.headers.authorization) return res.sendStatus(403) const headers = req.headers.authorization.split(' ') if (headers[0].toLowerCase() != "bearer") return res.status(403).send("authorization must be token bearer") @@ -238,7 +239,7 @@ router.post( }) .catch((e) => { res.sendStatus(500) - console.log({ + console.error({ killstat_version, servername, host, From 8d239729d830c51134f0c629ade18be0e6498aca Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 May 2023 14:25:36 +0200 Subject: [PATCH 3/9] add keepalive ping to websocket --- src/websocket.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/websocket.ts b/src/websocket.ts index cce19d3..a086779 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -1,7 +1,7 @@ import * as dotenv from 'dotenv' dotenv.config() import { Client } from 'pg' -import { WebSocketServer } from 'ws' +import { WebSocket, WebSocketServer } from 'ws' import { KillTable } from './db/model' const port = 3002 @@ -14,7 +14,7 @@ export const pgClient = new Client({ password: process.env.POSTGRES_PASSWORD }) -wss.on('connection', function connection(ws: WebSocket, req) { +wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, req) { const ip = req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() || req.socket.remoteAddress @@ -29,8 +29,26 @@ wss.on('connection', function connection(ws: WebSocket, req) { new Date().toLocaleString() + ',' + ip + ',close,' + wss.clients.size ) } + ws.onmessage = function (msg) { + if (msg.data === "pong") { + ws.isAlive = true + } + } }) +const interval = setInterval(function ping() { + wss.clients.forEach(function each(ws) { + if ((ws as WebSocket & { isAlive: boolean }).isAlive === false) return ws.terminate(); + + (ws as WebSocket & { isAlive: boolean }).isAlive = false; + ws.ping(); + }); +}, 30000); + +wss.on('close', function close() { + clearInterval(interval); +}); + export default new Promise(async (resolve, reject) => { await pgClient.connect() await pgClient.query('LISTEN new_kill') From f751453a3487ed8c25c3cab2cb80584833bbfc6d Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 May 2023 14:30:05 +0200 Subject: [PATCH 4/9] add ping on ws connect --- src/websocket.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/websocket.ts b/src/websocket.ts index a086779..842e162 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -34,6 +34,7 @@ wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, r ws.isAlive = true } } + ws.ping(); }) const interval = setInterval(function ping() { From b99b077185750be5e501cb6efd95ba86b28f74a5 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 May 2023 14:34:32 +0200 Subject: [PATCH 5/9] fix ping --- src/websocket.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websocket.ts b/src/websocket.ts index 842e162..886d262 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -34,7 +34,7 @@ wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, r ws.isAlive = true } } - ws.ping(); + ws.send("ping"); }) const interval = setInterval(function ping() { From 31dc5be41b11999989daaa9e33c33b0de17357f7 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 May 2023 14:46:29 +0200 Subject: [PATCH 6/9] fix ping 2 --- src/websocket.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/websocket.ts b/src/websocket.ts index 886d262..141db31 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -30,6 +30,7 @@ wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, r ) } ws.onmessage = function (msg) { + console.log(msg.data) if (msg.data === "pong") { ws.isAlive = true } @@ -39,6 +40,7 @@ wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, r const interval = setInterval(function ping() { wss.clients.forEach(function each(ws) { + console.log("isAlive :" + (ws as WebSocket & { isAlive: boolean }).isAlive) if ((ws as WebSocket & { isAlive: boolean }).isAlive === false) return ws.terminate(); (ws as WebSocket & { isAlive: boolean }).isAlive = false; From 7114ac9c644053f7e3a0dc63db9f7e639f5a4199 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 May 2023 14:47:35 +0200 Subject: [PATCH 7/9] fix ping 3 --- src/websocket.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websocket.ts b/src/websocket.ts index 141db31..faffaee 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -44,7 +44,7 @@ const interval = setInterval(function ping() { if ((ws as WebSocket & { isAlive: boolean }).isAlive === false) return ws.terminate(); (ws as WebSocket & { isAlive: boolean }).isAlive = false; - ws.ping(); + ws.send("ping"); }); }, 30000); From a8512db65b74ff87e1ac114f4acd95eadd5617c0 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 May 2023 14:48:49 +0200 Subject: [PATCH 8/9] fix ping when client is sending one to the server --- src/websocket.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/websocket.ts b/src/websocket.ts index faffaee..ea1eea0 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -30,9 +30,11 @@ wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, r ) } ws.onmessage = function (msg) { - console.log(msg.data) if (msg.data === "pong") { - ws.isAlive = true + return ws.isAlive = true + } + if (msg.data === "ping") { + return ws.send("pong") } } ws.send("ping"); From 625c7730f3f8280229619860373e73932638394e Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 May 2023 15:35:49 +0200 Subject: [PATCH 9/9] remove keepalive console log --- src/websocket.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/websocket.ts b/src/websocket.ts index ea1eea0..757ae11 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -42,7 +42,6 @@ wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, r const interval = setInterval(function ping() { wss.clients.forEach(function each(ws) { - console.log("isAlive :" + (ws as WebSocket & { isAlive: boolean }).isAlive) if ((ws as WebSocket & { isAlive: boolean }).isAlive === false) return ws.terminate(); (ws as WebSocket & { isAlive: boolean }).isAlive = false;