Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Relay's GET /admin page is served from src/admin-routes.ts - no behaviour change #133
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/121-relay-s-get-admin-page-is-served-from-sr → main 4 commits fork 1 thread · 1 unresolved
2 files
changed +47
−16
Software/so101/src/admin-routes.ts added +41
| @@ -0,0 +1,41 @@ | ||
| 1 | /** GET /admin: the owner's page and the refusals. The POST actions stay with the relay, which is handed what they need. */ | |
| 2 | import type { IncomingMessage, ServerResponse } from "node:http"; | |
| 3 | import type { Person } from "./identity.ts"; | |
| 4 | import type { Lobby } from "./lobby.ts"; | |
| 5 | import { ADMIN_CSP, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts"; | |
| 6 | ||
| 7 | /** What a POST needs once the caller is known to be allowed on the page. */ | |
| 8 | export interface AdminSession { | |
| 9 | who: Person; | |
| 10 | canEdit: boolean; | |
| 11 | /** Render the page with this status, and a notice or an error. */ | |
| 12 | show: (status: number, done: Done | null, error: string | null) => void; | |
| 13 | /** Answer with the refusal page. */ | |
| 14 | refuse: (status: number) => void; | |
| 15 | } | |
| 16 | ||
| 17 | /** | |
| 18 | * Answer 401/403 to anyone not allowed, and the page itself to GET and HEAD. Returns the session for any other | |
| 19 | * method by an allowed caller, and null when the response has been sent. | |
| 20 | */ | |
| 21 | export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL): AdminSession | null { | |
| 22 | const who = lobby.who(req); | |
| 23 | const html = (status: number, text: string) => { | |
| 24 | res.writeHead(status, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "content-security-policy": ADMIN_CSP }); | |
| 25 | res.end(text); | |
| 26 | }; | |
| 27 | const canEdit = Boolean(who?.owner); | |
| 28 | // a moderator is named by forge username and must hold that forge identity: a guest who types the same name is not them | |
| 29 | if (!who || !(canEdit || (lobby.verified(req)?.id === who.id && lobby.admin.isModerator(who.name)))) { | |
| 30 | html(who ? 403 : 401, renderRefusal(who)); | |
| 31 | return null; | |
| 32 | } | |
| 33 | const show = (status: number, done: Done | null, error: string | null) => | |
| 34 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, done, error })); | |
| 35 | if (req.method === "GET" || req.method === "HEAD") { | |
| 36 | const done = url.searchParams.get("done"); | |
| 37 | show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); | |
| 38 | return null; | |
| 39 | } | |
| 40 | return { who, canEdit, show, refuse: (status) => html(status, renderRefusal(who)) }; | |
| 41 | } |
Software/so101/src/relay.ts +6 −16
| @@ -62,7 +62,8 @@ import { parseFrame } from "./safety.ts"; | ||
| 62 | 62 | import { acceptsGzip, GzipCache, worthCompressing } from "./compress.ts"; |
| 63 | 63 | import { AccountError, sessionCookie, visitor } from "./accounts.ts"; |
| 64 | 64 | import { AdminError, changeFromForm } from "./admin.ts"; |
| 65 | import { ADMIN_CSP, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts"; | |
| 65 | import type { Done } from "./admin-page.ts"; | |
| 66 | import { serveAdminPage } from "./admin-routes.ts"; | |
| 66 | 67 | import { parseFps, type FeedOptions } from "./camera-feed.ts"; |
| 67 | 68 | import { CameraError, CameraHub } from "./cameras.ts"; |
| 68 | 69 | import { LOCAL as LOCAL_PERSON } from "./identity.ts"; |
| @@ -463,22 +464,11 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 463 | 464 | |
| 464 | 465 | /** The owner's page. Anyone else is told so; a change is a form post, answered with a redirect back. */ |
| 465 | 466 | const admin = async (lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL) => { |
| 466 | const who = lobby.who(req); | |
| 467 | const html = (status: number, text: string) => { | |
| 468 | res.writeHead(status, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "content-security-policy": ADMIN_CSP }); | |
| 469 | res.end(text); | |
| 470 | }; | |
| 471 | const canEdit = Boolean(who?.owner); | |
| 472 | // a moderator is named by forge username and must hold that forge identity: a guest who types the same name is not them | |
| 473 | if (!who || !(canEdit || (lobby.verified(req)?.id === who.id && lobby.admin.isModerator(who.name)))) return html(who ? 403 : 401, renderRefusal(who)); | |
| 474 | const show = (status: number, done: Done | null, error: string | null) => | |
| 475 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, done, error })); | |
| 476 | if (req.method === "GET" || req.method === "HEAD") { | |
| 477 | const done = url.searchParams.get("done"); | |
| 478 | return show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); | |
| 479 | } | |
| 467 | const session = serveAdminPage(lobby, req, res, url); | |
| 468 | if (!session) return; | |
| 469 | const { who, canEdit, show } = session; | |
| 480 | 470 | if (req.method !== "POST") return json(res, 405, { error: "GET or POST" }); |
| 481 | if (!canEdit) return html(403, renderRefusal(who)); | |
| 471 | if (!canEdit) return session.refuse(403); | |
| 482 | 472 | let body: Buffer; |
| 483 | 473 | try { |
| 484 | 474 | body = await readBody(req, MAX_FORM); |
new above
offline
Name says serve, but it also returns a session for POST. Consider admitAdmin or similar so callers read the null/session contract.