working on server registration
This commit is contained in:
@@ -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