From fdeec74d2644c2fca4be1a18b19835f31ff7efaa Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Mon, 30 Sep 2024 06:18:25 -0700 Subject: [PATCH] feat(lobbies): add admin api (#589) --- modules/lobbies/module.json | 19 ++++++++++++------- .../scripts/fetch_lobby_manager_state.ts | 6 ++++-- modules/lobbies/scripts/force_gc.ts | 5 ++++- .../scripts/reset_lobby_manager_state.ts | 6 ++++-- modules/lobbies/utils/admin.ts | 7 +++++++ 5 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 modules/lobbies/utils/admin.ts diff --git a/modules/lobbies/module.json b/modules/lobbies/module.json index 54580c08..837c1ab1 100644 --- a/modules/lobbies/module.json +++ b/modules/lobbies/module.json @@ -57,25 +57,25 @@ "description": "Finds an existing lobby with a given query. This will not create a new lobby, see `find_or_create` instead.", "public": true }, - "force_gc": { - "name": "Force Garbage Collection", - "description": "Rarely used. Forces the matchmaker to purge lobbies & players.", - "public": false - }, "list_regions": { "name": "List Regions", "description": "List available regions.", "public": true }, + "force_gc": { + "name": "Force Garbage Collection", + "description": "Rarely used. Forces the matchmaker to purge lobbies & players.", + "public": true + }, "fetch_lobby_manager_state": { "name": "Fetch Lobby Manager State", "description": "See full state of the lobby manager for debugging.", - "public": false + "public": true }, "reset_lobby_manager_state": { "name": "Reset Lobby Manager State", "description": "Reset lobby manager state. For debugging only.", - "public": false + "public": true } }, "actors": { @@ -151,6 +151,11 @@ "name": "Build Not Found", "description": "Build not found. Check that there is a build with the provided version & that the bulid is enabled.", "internal": false + }, + "forbidden": { + "name": "Forbidden", + "description": "Cannot access this command.", + "internal": false } }, "dependencies": { diff --git a/modules/lobbies/scripts/fetch_lobby_manager_state.ts b/modules/lobbies/scripts/fetch_lobby_manager_state.ts index 1e100f3f..2579f106 100644 --- a/modules/lobbies/scripts/fetch_lobby_manager_state.ts +++ b/modules/lobbies/scripts/fetch_lobby_manager_state.ts @@ -1,7 +1,8 @@ import { ScriptContext } from "../module.gen.ts"; +import { adminGuard } from "../utils/admin.ts"; export interface Request { - + adminToken: string; } export interface Response { @@ -10,8 +11,9 @@ export interface Response { export async function run( ctx: ScriptContext, - _req: Request, + req: Request, ): Promise { + adminGuard(ctx, req.adminToken); const state = await ctx.actors .lobbyManager.getOrCreateAndCall( "default", diff --git a/modules/lobbies/scripts/force_gc.ts b/modules/lobbies/scripts/force_gc.ts index f4367c43..bf3b73bf 100644 --- a/modules/lobbies/scripts/force_gc.ts +++ b/modules/lobbies/scripts/force_gc.ts @@ -1,6 +1,8 @@ import { ScriptContext } from "../module.gen.ts"; +import { adminGuard } from "../utils/admin.ts"; export interface Request { + adminToken: string; } export interface Response { @@ -8,8 +10,9 @@ export interface Response { export async function run( ctx: ScriptContext, - _req: Request, + req: Request, ): Promise { + adminGuard(ctx, req.adminToken); await ctx.actors.lobbyManager.getOrCreateAndCall( "default", undefined, diff --git a/modules/lobbies/scripts/reset_lobby_manager_state.ts b/modules/lobbies/scripts/reset_lobby_manager_state.ts index 2f5ea4d8..a1e992a4 100644 --- a/modules/lobbies/scripts/reset_lobby_manager_state.ts +++ b/modules/lobbies/scripts/reset_lobby_manager_state.ts @@ -1,7 +1,8 @@ import { ScriptContext } from "../module.gen.ts"; +import { adminGuard } from "../utils/admin.ts"; export interface Request { - + adminToken: string; } export interface Response { @@ -10,8 +11,9 @@ export interface Response { export async function run( ctx: ScriptContext, - _req: Request, + req: Request, ): Promise { + adminGuard(ctx, req.adminToken); await ctx.actors.lobbyManager.destroy("default"); return {}; } diff --git a/modules/lobbies/utils/admin.ts b/modules/lobbies/utils/admin.ts new file mode 100644 index 00000000..312a3424 --- /dev/null +++ b/modules/lobbies/utils/admin.ts @@ -0,0 +1,7 @@ +import { RuntimeError } from "../module.gen.ts"; + +export function adminGuard(ctx: any, inputToken: string) { + const adminToken = ctx.environment.get("ADMIN_TOKEN"); + if (!adminToken) throw new RuntimeError("forbidden"); + if (inputToken != adminToken) throw new RuntimeError("forbidden"); +}