handle registration

This commit is contained in:
2023-03-03 20:14:18 +01:00
parent a882160d5e
commit fa52b4d498
2 changed files with 22 additions and 9 deletions
+15 -5
View File
@@ -10,6 +10,7 @@ const router = Router()
const hostsCount: { [id: string]: number } = {}
const hostsTimeout: { [id: string]: NodeJS.Timeout } = {}
//Very simple rate limiting. max 2 registers per IP every 5 mins. Maybe 2 is a bit few ?
router.post('/servers/register', (req, res, next) => {
let ip =
@@ -31,12 +32,15 @@ router.post('/servers/register', (req, res, next) => {
router.post(
'/servers/register',
body(['name', 'description']).isString(),
body('auth_endpoint').isURL(),
body(['name', 'description']).isString().withMessage('must be strings'),
body('auth_port')
.toInt()
.isInt({ min: 1, max: 65535 })
.withMessage('must be between 1 and 65535'),
async (req, res, next) => {
const errors = validationResult(req)
console.log(req.body)
if (!errors.isEmpty()) {
console.log(JSON.stringify(errors))
return res.status(400).json({ errors: errors.array() })
}
try {
@@ -59,9 +63,15 @@ router.post(
//Send request to verify server. Not very useful for now, but maybe a future method for auth ?
//Maybe should set a blacklist here for local domain ?
if ((await GetRequest(req.body.auth_endpoint)) != verificationString) {
const endpoint =
'http://' +
(req.header('x-forwarded-for') || req.socket.remoteAddress) +
':' +
req.body.auth_port +
'/verify'
if ((await GetRequest(endpoint)) != verificationString) {
return res.status(400).json({
error: "Couldn't reach gameserver at " + req.body.auth_endpoint
error: "Couldn't reach gameserver at " + endpoint
})
}
+7 -4
View File
@@ -1,4 +1,4 @@
import { Router } from 'express'
import { NextFunction, Router } from 'express'
import expressBasicAuth from 'express-basic-auth'
import { body, header, validationResult } from 'express-validator'
import register from './register'
@@ -10,7 +10,7 @@ router.use('/', register)
//auth middleware
router.post(
'/servers/:serverId/kill',
'/servers/:serverId/*',
header('authorization')
.exists({ checkFalsy: true })
.withMessage('Missing Authorization Header')
@@ -36,6 +36,10 @@ router.post(
}
)
router.post('/servers/:serverId', (req, res) => {
res.send(200)
})
const serversCount: { [id: string]: number } = {}
const serversTimeout: { [id: string]: NodeJS.Timeout } = {}
@@ -116,7 +120,6 @@ router.post(
console.log(JSON.stringify(errors))
return res.status(400).json({ errors: errors.array() })
}
const server = 1 // set server ID here
const {
killstat_version,
match_id,
@@ -153,7 +156,7 @@ router.post(
} = req.body
CreateKillRecord({
killstat_version,
server,
server: req.body.serverId,
match_id,
game_mode,
map,