fix eslint errors

This commit is contained in:
2023-08-09 15:49:53 +02:00
parent 0aafd390a7
commit 7470d8174f
8 changed files with 561 additions and 659 deletions
+11 -9
View File
@@ -1,17 +1,19 @@
module.exports = {
env: {
browser: true,
es2021: true
es2021: true,
},
extends: 'standard-with-typescript',
extends: "standard-with-typescript",
overrides: [],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json']
ecmaVersion: "latest",
sourceType: "module",
project: ["./tsconfig.json"],
},
rules: {
'@typescript-eslint/strict-boolean-expressions': 0,
'@typescript-eslint/explicit-function-return-type': 'off'
}
}
"@typescript-eslint/strict-boolean-expressions": 0,
"@typescript-eslint/explicit-function-return-type": "off",
},
plugins: ["jest"],
"jest/globals": true,
};
+2 -1
View File
@@ -6,5 +6,6 @@
"eslint.format.enable": true,
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
},
"typescript.tsdk": "node_modules\\typescript\\lib"
}
+306 -410
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -3,6 +3,7 @@
"@types/ws": "^8.5.4",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"eslint-plugin-jest": "^27.2.3",
"express": "^4.18.2",
"express-basic-auth": "^1.2.1",
"express-validator": "^6.15.0",
+1 -1
View File
@@ -43,7 +43,7 @@ router.get('/hosts', (_req, res) => {
void (async () => {
const result = await getHostList()
const data: Record<number, string> = {}
result.forEach((e) => (data[Number(e.id)] = e.name))
result.forEach((e) => (data[Number(e.host_id)] = e.host_name))
const dataString = JSON.stringify(data)
const buffer = Buffer.from(dataString)
+170 -161
View File
@@ -1,47 +1,58 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Router } from 'express'
import { body, header } from 'express-validator'
import { CreateKillRecord, CheckServerToken } from '../db/db'
import { /* CreateKillRecord, */ CheckServerToken } from '../db/db'
import { validateErrors } from '../common'
const router = Router()
//auth middleware
// auth middleware
router.post(
'/*',
header('authorization')
.exists({ checkFalsy: true })
.withMessage('Missing Authorization Header')
.bail()
.custom(e => e.split(' ')[0].toLowerCase() == 'bearer')
.custom((e) => e.split(' ')[0].toLowerCase() === 'bearer')
.withMessage('Authorization Token is not Bearer'),
validateErrors,
async (req, res, next) => {
if (!req) return res.sendStatus(500)
if (!req.headers.authorization) {
console.error("no authorization header")
return res.sendStatus(403)
}
const query = await CheckServerToken(req.headers.authorization.split(' ')[1])
if (!query || !query.id) {
console.error("incorrect token : " + req.headers.authorization.split(' ')[1] + " for " + req.body.servername + " with IP " + (req.headers['x-forwarded-for']?.toString() ||
req.socket.remoteAddress?.toString() ||
''))
return res.sendStatus(403)
}
next()
(req, res, next) => {
void (async () => {
if (!req) return res.sendStatus(500)
if (!req.headers.authorization) {
console.error('no authorization header')
return res.sendStatus(403)
}
const query = await CheckServerToken(
req.headers.authorization.split(' ')[1]
)
if (!query?.host_id) {
console.error(
`incorrect token : ${
req.headers.authorization.split(' ')[1]
} with IP ${
req.headers['x-forwarded-for']?.toString() ??
req.socket.remoteAddress?.toString() ??
''
}`
)
return res.sendStatus(403)
}
next()
})()
}
)
//Route to check auth
// Route to check auth
router.post('/', (req, res) => {
res.sendStatus(200)
})
const serversCount: { [id: string]: number } = {}
const serversTimeout: { [id: string]: NodeJS.Timeout } = {}
// const serversCount: Record<string, number> = {}
// const serversTimeout: Record<string, NodeJS.Timeout> = {}
//same rate limiting code as register. max 10 kills per server every 1 sec. should be enough.
/*router.post('/kill', (req, res, next) => {
// same rate limiting code as register. max 10 kills per server every 1 sec. should be enough.
/* router.post('/kill', (req, res, next) => {
let host = Number(req.query.serverId)
if (serversCount[serverId] > 2) {
return res.status(429).json({
@@ -55,7 +66,7 @@ const serversTimeout: { [id: string]: NodeJS.Timeout } = {}
serversCount[serverId] = 0
}, 1000)
next()
})*/
}) */
router.post(
'/kill',
@@ -134,149 +145,147 @@ router.post(
.isString()
.isLength({ max: 50 })
.isAscii(),
body('servername',).isString()
.isLength({ max: 100 })
.isAscii(),
body('servername').isString().isLength({ max: 100 }).isAscii(),
body(['distance', 'game_time'], 'must be postitive floats').isFloat({
min: 0
}),
body(['cause_of_death', 'victim_id'], 'mandatory').exists().notEmpty(),
//do we need this ?
//body('servername').customSanitizer(e => e.replace(/[^a-z0-9]/gi, '')),
// do we need this ?
// 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")
const query = (await CheckServerToken(headers[1]))
if (!query) return res.sendStatus(403)
const host = query.id
const {
servername,
killstat_version,
match_id,
game_mode,
map,
game_time,
player_count,
attacker_name,
attacker_id,
attacker_current_weapon,
attacker_current_weapon_mods,
attacker_weapon_1,
attacker_weapon_1_mods,
attacker_weapon_2,
attacker_weapon_2_mods,
attacker_weapon_3,
attacker_weapon_3_mods,
attacker_offhand_weapon_1,
attacker_offhand_weapon_1_mods,
attacker_offhand_weapon_2,
attacker_offhand_weapon_2_mods,
victim_name,
victim_id,
victim_current_weapon,
victim_current_weapon_mods,
victim_weapon_1,
victim_weapon_1_mods,
victim_weapon_2,
victim_weapon_2_mods,
victim_weapon_3,
victim_weapon_3_mods,
victim_offhand_weapon_1,
victim_offhand_weapon_1_mods,
victim_offhand_weapon_2,
victim_offhand_weapon_2_mods,
cause_of_death,
distance
} = req.body
CreateKillRecord({
killstat_version,
servername,
host,
match_id,
game_mode,
map,
game_time,
player_count,
attacker_name,
attacker_id,
attacker_current_weapon,
attacker_current_weapon_mods,
attacker_weapon_1,
attacker_weapon_1_mods,
attacker_weapon_2,
attacker_weapon_2_mods,
attacker_weapon_3,
attacker_weapon_3_mods,
attacker_offhand_weapon_1,
attacker_offhand_weapon_1_mods,
attacker_offhand_weapon_2,
attacker_offhand_weapon_2_mods,
victim_name,
victim_id,
victim_current_weapon,
victim_current_weapon_mods,
victim_weapon_1,
victim_weapon_1_mods,
victim_weapon_2,
victim_weapon_2_mods,
victim_weapon_3,
victim_weapon_3_mods,
victim_offhand_weapon_1,
victim_offhand_weapon_1_mods,
victim_offhand_weapon_2,
victim_offhand_weapon_2_mods,
cause_of_death,
distance
})
.then((e) => {
res.sendStatus(201)
console.log(
`[${new Date().toLocaleString()}] Kill submitted for server ${servername}, ${attacker_name} killed ${victim_name}`
)
})
.catch((e) => {
res.sendStatus(500)
console.error({
killstat_version,
servername,
host,
match_id,
game_mode,
map,
game_time,
player_count,
attacker_name,
attacker_id,
attacker_current_weapon,
attacker_current_weapon_mods,
attacker_weapon_1,
attacker_weapon_1_mods,
attacker_weapon_2,
attacker_weapon_2_mods,
attacker_weapon_3,
attacker_weapon_3_mods,
attacker_offhand_weapon_1,
attacker_offhand_weapon_2,
victim_name,
victim_id,
victim_current_weapon,
victim_current_weapon_mods,
victim_weapon_1,
victim_weapon_1_mods,
victim_weapon_2,
victim_weapon_2_mods,
victim_weapon_3,
victim_weapon_3_mods,
victim_offhand_weapon_1,
victim_offhand_weapon_2,
cause_of_death,
distance
})
console.error(e)
})
(req, res) => {
void (async () => {
// 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') }
const query = await CheckServerToken(headers[1])
if (!query) return res.sendStatus(403)
// const host = query.id
// const {
// servername,
// match_id,
// game_mode,
// map,
// game_time,
// player_count,
// attacker_name,
// attacker_id,
// attacker_current_weapon,
// attacker_current_weapon_mods,
// attacker_weapon_1,
// attacker_weapon_1_mods,
// attacker_weapon_2,
// attacker_weapon_2_mods,
// attacker_weapon_3,
// attacker_weapon_3_mods,
// attacker_offhand_weapon_1,
// attacker_offhand_weapon_1_mods,
// attacker_offhand_weapon_2,
// attacker_offhand_weapon_2_mods,
// victim_name,
// victim_id,
// victim_current_weapon,
// victim_current_weapon_mods,
// victim_weapon_1,
// victim_weapon_1_mods,
// victim_weapon_2,
// victim_weapon_2_mods,
// victim_weapon_3,
// victim_weapon_3_mods,
// victim_offhand_weapon_1,
// victim_offhand_weapon_1_mods,
// victim_offhand_weapon_2,
// victim_offhand_weapon_2_mods,
// cause_of_death,
// distance
// } = req.body
// CreateKillRecord({
// servername,
// host,
// match_id,
// game_mode,
// map,
// game_time,
// player_count,
// attacker_name,
// attacker_id,
// attacker_current_weapon,
// attacker_current_weapon_mods,
// attacker_weapon_1,
// attacker_weapon_1_mods,
// attacker_weapon_2,
// attacker_weapon_2_mods,
// attacker_weapon_3,
// attacker_weapon_3_mods,
// attacker_offhand_weapon_1,
// attacker_offhand_weapon_1_mods,
// attacker_offhand_weapon_2,
// attacker_offhand_weapon_2_mods,
// victim_name,
// victim_id,
// victim_current_weapon,
// victim_current_weapon_mods,
// victim_weapon_1,
// victim_weapon_1_mods,
// victim_weapon_2,
// victim_weapon_2_mods,
// victim_weapon_3,
// victim_weapon_3_mods,
// victim_offhand_weapon_1,
// victim_offhand_weapon_1_mods,
// victim_offhand_weapon_2,
// victim_offhand_weapon_2_mods,
// cause_of_death,
// distance
// })
// .then((e) => {
// res.sendStatus(201)
// console.log(
// `[${new Date().toLocaleString()}] Kill submitted for server ${servername}, ${attacker_name} killed ${victim_name}`
// )
// })
// .catch((e) => {
// res.sendStatus(500)
// console.error({
// killstat_version,
// servername,
// host,
// match_id,
// game_mode,
// map,
// game_time,
// player_count,
// attacker_name,
// attacker_id,
// attacker_current_weapon,
// attacker_current_weapon_mods,
// attacker_weapon_1,
// attacker_weapon_1_mods,
// attacker_weapon_2,
// attacker_weapon_2_mods,
// attacker_weapon_3,
// attacker_weapon_3_mods,
// attacker_offhand_weapon_1,
// attacker_offhand_weapon_2,
// victim_name,
// victim_id,
// victim_current_weapon,
// victim_current_weapon_mods,
// victim_weapon_1,
// victim_weapon_1_mods,
// victim_weapon_2,
// victim_weapon_2_mods,
// victim_weapon_3,
// victim_weapon_3_mods,
// victim_offhand_weapon_1,
// victim_offhand_weapon_2,
// cause_of_death,
// distance
// })
// console.error(e)
// })
})()
}
)
export default router
+3 -3
View File
@@ -1,9 +1,9 @@
import * as dotenv from 'dotenv'
dotenv.config()
import express from 'express'
import cors from 'cors'
import db, { dbReady } from './db/db'
import { dbReady } from './db/db'
import server from './server/server'
dotenv.config()
const app = express()
const port = 3001
@@ -18,7 +18,7 @@ app.get('/', (req, res) => {
app.use('/', server)
export default new Promise((resolve, reject) => {
dbReady().then((e) => {
void dbReady().then((e) => {
const listenServer = app.listen(port, '0.0.0.0', () => {
console.log(`Tone server api listening on port ${port}`)
resolve(listenServer)
+67 -74
View File
@@ -1,85 +1,78 @@
import * as dotenv from 'dotenv'
dotenv.config()
import { Client } from 'pg'
import { WebSocket, WebSocketServer } from 'ws'
import { KillTable } from './db/model'
import { type WebSocket, WebSocketServer } from 'ws'
import { type KillTable } from './db/model'
dotenv.config()
const port = 3002
const wss = new WebSocketServer({ port })
export const pgClient = new Client({
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DATABASE,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DATABASE,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD
})
wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, req) {
const ip =
req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ||
req.socket.remoteAddress
console.log(
new Date().toLocaleString() + ',' + ip + ',connect,' + wss.clients.size
)
ws.onclose = function () {
const ip =
req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ||
req.socket.remoteAddress
console.log(
new Date().toLocaleString() + ',' + ip + ',close,' + wss.clients.size
wss.on('connection', function connection (ws: WebSocket & { isAlive: boolean }, req) {
const ip = req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ??
req.socket.remoteAddress ?? 'ip_unknown'
console.log(`${new Date().toLocaleString()},${ip},connect,${wss.clients.size}`)
ws.onclose = function () {
console.log(`${new Date().toLocaleString()},${ip},connect,${wss.clients.size}`)
}
ws.onmessage = function (msg) {
if (msg.data === 'pong') {
ws.isAlive = true
return
}
if (msg.data === 'ping') {
ws.send('pong')
}
}
ws.send('ping')
})
const interval = setInterval(function ping () {
wss.clients.forEach(function each (ws) {
if (!(ws as WebSocket & { isAlive: boolean }).isAlive) { ws.terminate(); return }
(ws as WebSocket & { isAlive: boolean }).isAlive = false
ws.send('ping')
})
}, 30000)
wss.on('close', function close () {
clearInterval(interval)
})
export default async () => {
await pgClient.connect()
await pgClient.query('LISTEN new_kill')
pgClient.on('notification', (data) => {
if (!data.payload) return
const payload = JSON.parse(data.payload) as KillTable
wss.clients.forEach(function (client) {
if (client.readyState === client.OPEN) {
client.send(
JSON.stringify({
match_id: payload.match_id,
attacker_id: payload.attacker_id
// attacker_name: payload.attacker_name,
// cause_of_death: payload.cause_of_death,
// victim_id: payload.victim_id,
// victim_name: payload.victim_name,
// attacker_current_weapon: payload.attacker_current_weapon,
// victim_current_weapon: payload.victim_current_weapon,
// distance: payload.distance,
// game_mode: payload.game_mode,
// servername: payload.servername,
// map: payload.map,
// host: payload.host
})
)
}
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) => {
await pgClient.connect()
await pgClient.query('LISTEN new_kill')
pgClient.on('notification', (data) => {
if (!data.payload) return
const payload = JSON.parse(data.payload) as KillTable
wss.clients.forEach(function (client) {
if (client.readyState === client.OPEN) {
client.send(
JSON.stringify({
match_id: payload.match_id,
attacker_id: payload.attacker_id,
attacker_name: payload.attacker_name,
cause_of_death: payload.cause_of_death,
victim_id: payload.victim_id,
victim_name: payload.victim_name,
attacker_current_weapon: payload.attacker_current_weapon,
victim_current_weapon: payload.victim_current_weapon,
distance: payload.distance,
game_mode: payload.game_mode,
servername: payload.servername,
map: payload.map,
host: payload.host
})
)
}
})
}
})
})
})
}