working on server registration
This commit is contained in:
Generated
+31
@@ -5,6 +5,7 @@
|
||||
"packages": {
|
||||
"": {
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
"express-validator": "^6.15.0",
|
||||
@@ -16,6 +17,7 @@
|
||||
"@babel/core": "^7.21.0",
|
||||
"@babel/preset-env": "^7.20.2",
|
||||
"@jest/globals": "^29.4.3",
|
||||
"@types/cors": "^2.8.13",
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/pg": "^8.6.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.54.0",
|
||||
@@ -2654,6 +2656,15 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/cors": {
|
||||
"version": "2.8.13",
|
||||
"resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.13.tgz",
|
||||
"integrity": "sha512-RG8AStHlUiV5ysZQKq97copd2UmVYw3/pRMLefISZ3S1hK104Cwm7iLQ3fTKx+lsUH2CE8FlLaYeEA2LSeqYUA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/express": {
|
||||
"version": "4.17.17",
|
||||
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.17.tgz",
|
||||
@@ -3768,6 +3779,18 @@
|
||||
"url": "https://opencollective.com/core-js"
|
||||
}
|
||||
},
|
||||
"node_modules/cors": {
|
||||
"version": "2.8.5",
|
||||
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
|
||||
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
|
||||
"dependencies": {
|
||||
"object-assign": "^4",
|
||||
"vary": "^1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.10"
|
||||
}
|
||||
},
|
||||
"node_modules/create-require": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
|
||||
@@ -6531,6 +6554,14 @@
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/object-assign": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/object-inspect": {
|
||||
"version": "1.12.3",
|
||||
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
"express-validator": "^6.15.0",
|
||||
@@ -11,6 +12,7 @@
|
||||
"@babel/core": "^7.21.0",
|
||||
"@babel/preset-env": "^7.20.2",
|
||||
"@jest/globals": "^29.4.3",
|
||||
"@types/cors": "^2.8.13",
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/pg": "^8.6.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.54.0",
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import http from 'http'
|
||||
import https from 'https'
|
||||
|
||||
export async function GetRequest(url: string) {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
let handler = (resp: http.IncomingMessage) => {
|
||||
let data = ''
|
||||
|
||||
// A chunk of data has been received.
|
||||
resp.on('data', (chunk) => {
|
||||
data += chunk
|
||||
})
|
||||
|
||||
// The whole response has been received. Print out the result.
|
||||
resp.on('end', () => {
|
||||
resolve(data)
|
||||
})
|
||||
resp.on('error', (err) => {
|
||||
reject(err)
|
||||
})
|
||||
}
|
||||
|
||||
if (url.startsWith('https')) {
|
||||
https.get(url, handler)
|
||||
} else {
|
||||
http.get(url, handler)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -29,6 +29,14 @@ export async function CreateKillRecord(data: KillRecord) {
|
||||
.values({ ...data })
|
||||
.execute()
|
||||
}
|
||||
|
||||
export async function FindServer({ name }: { name: string }) {
|
||||
return await db
|
||||
.selectFrom('server')
|
||||
.select(['server.name', 'server.description'])
|
||||
.where('server.name', '=', name)
|
||||
.executeTakeFirst()
|
||||
}
|
||||
/*
|
||||
async function demo() {
|
||||
const { id } = await db
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
import * as dotenv from 'dotenv'
|
||||
dotenv.config()
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import client from './client'
|
||||
import { dbReady } from './db/db'
|
||||
import server from './server'
|
||||
@@ -9,7 +10,7 @@ const app = express()
|
||||
const port = 3001
|
||||
|
||||
app.use(express.json())
|
||||
|
||||
app.use(cors())
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hello World!')
|
||||
})
|
||||
|
||||
+69
-16
@@ -1,12 +1,59 @@
|
||||
import { Router } from 'express'
|
||||
import db, { CreateKillRecord } from './db/db'
|
||||
import { body } from 'express-validator'
|
||||
import db, { CreateKillRecord, FindServer } from './db/db'
|
||||
import { body, validationResult } from 'express-validator'
|
||||
import { GetRequest } from './common'
|
||||
const router = Router()
|
||||
|
||||
const verificationString = 'I am a northstar server!'
|
||||
const masterServerURL = 'https://northstar.tf'
|
||||
//auth middleware, maybe some timeout ?
|
||||
router.post('/*', (req, res, next) => {
|
||||
next()
|
||||
})
|
||||
|
||||
router.post(
|
||||
'/servers/register',
|
||||
body(['name', 'description']).isString(),
|
||||
body('auth_endpoint').isURL(),
|
||||
async (req, res, next) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) {
|
||||
console.log(JSON.stringify(errors))
|
||||
return res.status(400).json({ errors: errors.array() })
|
||||
}
|
||||
try {
|
||||
//Check if server name isn't already in database
|
||||
if (await FindServer(req.body.name)) {
|
||||
return res
|
||||
.status(403)
|
||||
.json({ error: 'Server already exists in the database' })
|
||||
}
|
||||
|
||||
//Check if server is in masterserver's list
|
||||
const masterServerList = JSON.parse(
|
||||
await GetRequest(masterServerURL + '/client/servers')
|
||||
) as Array<any>
|
||||
if (!masterServerList.find((e) => e.name == req.body.name)) {
|
||||
return res
|
||||
.status(403)
|
||||
.json({ error: 'Server not listed in masterserver' })
|
||||
}
|
||||
|
||||
//Send request to verify server. Not very useful for now, but maybe a future method for auth ?
|
||||
if ((await GetRequest(req.body.auth_endpoint)) != verificationString) {
|
||||
//Maybe should set a blacklist here for local domain ?
|
||||
return res.status(400).json({ error: "Couldn't verify gameserver" })
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
return res.status(400).json({
|
||||
error: "Server encountered an error, Couldn't verify gameserver"
|
||||
})
|
||||
}
|
||||
//register server here
|
||||
}
|
||||
)
|
||||
|
||||
router.post(
|
||||
'/servers/:serverId/kill',
|
||||
body([
|
||||
@@ -26,8 +73,12 @@ router.post(
|
||||
'victim_offhand_weapon_2'
|
||||
])
|
||||
.toInt()
|
||||
.isInt(),
|
||||
body('game_time').toFloat().isFloat(),
|
||||
.isInt()
|
||||
.withMessage('must be a valid int'),
|
||||
body(['distance', 'game_time'])
|
||||
.toFloat()
|
||||
.isFloat()
|
||||
.withMessage('must be a valid float'),
|
||||
body(
|
||||
[
|
||||
'attacker_id',
|
||||
@@ -36,7 +87,6 @@ router.post(
|
||||
'match_id',
|
||||
'game_mode',
|
||||
'map',
|
||||
'player_count',
|
||||
'attacker_name',
|
||||
'attacker_current_weapon',
|
||||
'attacker_weapon_1',
|
||||
@@ -49,22 +99,21 @@ router.post(
|
||||
'victim_weapon_3',
|
||||
'cause_of_death'
|
||||
],
|
||||
'string values must be composed of a maximum of 50 valid ascii characters'
|
||||
'must be composed of a maximum of 50 valid ascii characters'
|
||||
)
|
||||
.isString()
|
||||
.isLength({ max: 50 })
|
||||
.isAscii(),
|
||||
body(
|
||||
['distance', 'game_time'],
|
||||
'distance and game_time must be postitive floats'
|
||||
).isFloat({
|
||||
body(['distance', 'game_time'], 'must be postitive floats').isFloat({
|
||||
min: 0
|
||||
}),
|
||||
body(
|
||||
['cause_of_death', 'victim_id'],
|
||||
'cause_of_death and victim_id are mandatory'
|
||||
).notEmpty(),
|
||||
body(['cause_of_death', 'victim_id'], 'mandatory').exists().notEmpty(),
|
||||
(req, res, next) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) {
|
||||
console.log(JSON.stringify(errors))
|
||||
return res.status(400).json({ errors: errors.array() })
|
||||
}
|
||||
const server = 1 // set server ID here
|
||||
const {
|
||||
killstat_version,
|
||||
@@ -135,8 +184,12 @@ router.post(
|
||||
cause_of_death,
|
||||
distance
|
||||
})
|
||||
res.send(200)
|
||||
return
|
||||
.then((e) => {
|
||||
res.send(200)
|
||||
})
|
||||
.catch((e) => {
|
||||
res.send(500)
|
||||
})
|
||||
}
|
||||
)
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user