Merge pull request #35 from Legonzaur/main
add websocket keepalive packets
This commit is contained in:
@@ -102,7 +102,10 @@ export default async function listenKills() {
|
|||||||
killEntry.max_distance = Math.max(killEntry.max_distance || 0, payload.distance)
|
killEntry.max_distance = Math.max(killEntry.max_distance || 0, payload.distance)
|
||||||
}
|
}
|
||||||
deathEntry.deaths++
|
deathEntry.deaths++
|
||||||
|
deathEntry.attacker_name = payload.victim_name
|
||||||
deathWithWeaponEntry.deaths_with_weapon++
|
deathWithWeaponEntry.deaths_with_weapon++
|
||||||
|
deathWithWeaponEntry.attacker_name = payload.victim_name
|
||||||
|
killEntry.attacker_name = payload.attacker_name
|
||||||
})
|
})
|
||||||
return pgClient
|
return pgClient
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -145,6 +145,7 @@ router.post(
|
|||||||
//body('servername').customSanitizer(e => e.replace(/[^a-z0-9]/gi, '')),
|
//body('servername').customSanitizer(e => e.replace(/[^a-z0-9]/gi, '')),
|
||||||
validateErrors,
|
validateErrors,
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
|
// Do we check the same thing twice ?????
|
||||||
if (!req.headers.authorization) return res.sendStatus(403)
|
if (!req.headers.authorization) return res.sendStatus(403)
|
||||||
const headers = req.headers.authorization.split(' ')
|
const headers = req.headers.authorization.split(' ')
|
||||||
if (headers[0].toLowerCase() != "bearer") return res.status(403).send("authorization must be token bearer")
|
if (headers[0].toLowerCase() != "bearer") return res.status(403).send("authorization must be token bearer")
|
||||||
@@ -233,12 +234,12 @@ router.post(
|
|||||||
.then((e) => {
|
.then((e) => {
|
||||||
res.sendStatus(201)
|
res.sendStatus(201)
|
||||||
console.log(
|
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) => {
|
.catch((e) => {
|
||||||
res.sendStatus(500)
|
res.sendStatus(500)
|
||||||
console.log({
|
console.error({
|
||||||
killstat_version,
|
killstat_version,
|
||||||
servername,
|
servername,
|
||||||
host,
|
host,
|
||||||
|
|||||||
+24
-2
@@ -1,7 +1,7 @@
|
|||||||
import * as dotenv from 'dotenv'
|
import * as dotenv from 'dotenv'
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
import { Client } from 'pg'
|
import { Client } from 'pg'
|
||||||
import { WebSocketServer } from 'ws'
|
import { WebSocket, WebSocketServer } from 'ws'
|
||||||
import { KillTable } from './db/model'
|
import { KillTable } from './db/model'
|
||||||
|
|
||||||
const port = 3002
|
const port = 3002
|
||||||
@@ -14,7 +14,7 @@ export const pgClient = new Client({
|
|||||||
password: process.env.POSTGRES_PASSWORD
|
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 =
|
const ip =
|
||||||
req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ||
|
req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ||
|
||||||
req.socket.remoteAddress
|
req.socket.remoteAddress
|
||||||
@@ -29,8 +29,30 @@ wss.on('connection', function connection(ws: WebSocket, req) {
|
|||||||
new Date().toLocaleString() + ',' + ip + ',close,' + wss.clients.size
|
new Date().toLocaleString() + ',' + ip + ',close,' + wss.clients.size
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
ws.onmessage = function (msg) {
|
||||||
|
if (msg.data === "pong") {
|
||||||
|
return ws.isAlive = true
|
||||||
|
}
|
||||||
|
if (msg.data === "ping") {
|
||||||
|
return ws.send("pong")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ws.send("ping");
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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.send("ping");
|
||||||
|
});
|
||||||
|
}, 30000);
|
||||||
|
|
||||||
|
wss.on('close', function close() {
|
||||||
|
clearInterval(interval);
|
||||||
|
});
|
||||||
|
|
||||||
export default new Promise(async (resolve, reject) => {
|
export default new Promise(async (resolve, reject) => {
|
||||||
await pgClient.connect()
|
await pgClient.connect()
|
||||||
await pgClient.query('LISTEN new_kill')
|
await pgClient.query('LISTEN new_kill')
|
||||||
|
|||||||
Reference in New Issue
Block a user