Public: anyone can read this; the crew can change it.

Games: the people list shows no ops after a successful `op` (#17610 item 8) #17619

opened this

Merged 13h ago, live in production

scout/forge:scout/17617-games-the-people-list-shows-no-ops-after → main 0 commits fork

  • Merged. scout/17617-games-the-people-list-shows-no-ops-after was merged into main as fdf4697.

Review

Sign in to review this change.
3 files changed +181 −2
docs/features/games.md +1 −1
@@ -43,7 +43,7 @@ A service page has a People block (#17284), with no plugin on the game servers:
4343
4444 A refused player is told why in game, once (`tellRefused`): the server, the reason when the proxy logged one, and the mods to install with where to get them. The list is the lockfile's and `MODRINTH_PROJECTS`' projects and their dependencies, less those Modrinth does not mark as required on a client; a version is said only when the lockfile pins it. The limit: Velocity closes the player's old server before the new one answers, and AutoStopper does not send them back, so the words wait until a server lists the player again (within 30 minutes). They do not stop the drop itself.
4545
46 The agent reads `list uuids` each sync, and each server's whitelist.json, ops.json and banned-players.json and the proxy's `glist all` every 60 s or right after a change. Heads come from the player's UUID through the forge (`/games/head/<uuid>`), fetched once from mc-heads.net and kept on the forge's disk a week, so a page never calls out. Only a player the forge has seen on a server the person sees (on it now, or in its whitelist, ops or bans) has a head fetched; any other UUID gets the default head. A failed fetch is not tried again for 15 minutes, and at most four fetches are out at once. The terminal's service screen has a people tab (`p` takes "op Alice", "move Alice survival"), and the `games` tool has `people` with `op` list or a change.
46 The agent reads `list uuids` each sync, and each server's whitelist.json, ops.json and banned-players.json and the proxy's `glist all` every 60 s or right after a change. A change that a list does not show yet (a server writes its file a moment late) is read again on the next syncs, up to five, so a successful `op` shows in the ops (#17617). Heads come from the player's UUID through the forge (`/games/head/<uuid>`), fetched once from mc-heads.net and kept on the forge's disk a week, so a page never calls out. Only a player the forge has seen on a server the person sees (on it now, or in its whitelist, ops or bans) has a head fetched; any other UUID gets the default head. A failed fetch is not tried again for 15 minutes, and at most four fetches are out at once. The terminal's service screen has a people tab (`p` takes "op Alice", "move Alice survival"), and the `games` tool has `people` with `op` list or a change.
4747 ## Modrinth
4848
4949 A server's mods and plugins come from Modrinth, searched, previewed and pinned in a lockfile: see [Game mods](game-mods.md).
src/games/agent.sh +36
@@ -397,6 +397,38 @@ proxy_rcon() {
397397 [ -n "$pw" ] || return 1
398398 printf '%s\n' "$pw" | capped 10 "${COMPOSE[@]}" exec -T "$via" sh -c 'read -r p; RCON_PASSWORD="$p" exec rcon-cli --host velocity --port 25575 "$@"' sh "$@"
399399 }
400 # Changes a people command made that the lists have not shown yet ("service op player tries", a
401 # line each): a list the server writes late is read again on the next sync, up to 5 times, so a
402 # successful `op` shows in the ops and the next sync is not the last to look.
403 people_settle() {
404 [ -s "$DIR/.people.expect" ] || return 0
405 local svc op pl tries key has kept="" again=0
406 while IFS=$'\t' read -r svc op pl tries <&4; do
407 case "$op" in
408 op | deop) key=ops ;;
409 whitelist-add | whitelist-remove) key=whitelist ;;
410 ban | pardon) key=bans ;;
411 *) continue ;;
412 esac
413 # A server not read this time (stopped) keeps its line; one read is judged by its list.
414 has="$(jq -r --arg s "$svc" --arg k "$key" --arg n "$pl" 'if has($s) then ([.[$s][$k][] | select((.name | ascii_downcase) == ($n | ascii_downcase))] | length > 0) else "unread" end' <<<"$1" 2>/dev/null)"
415 case "$op:$has" in
416 op:true | whitelist-add:true | ban:true | deop:false | whitelist-remove:false | pardon:false) continue ;;
417 esac
418 if [ "$has" = unread ]; then
419 kept+="$svc"$'\t'"$op"$'\t'"$pl"$'\t'"$tries"$'\n'
420 continue
421 fi
422 tries=$((tries + 1))
423 if [ "$tries" -lt 5 ]; then
424 kept+="$svc"$'\t'"$op"$'\t'"$pl"$'\t'"$tries"$'\n'
425 again=1
426 fi
427 done 4<"$DIR/.people.expect"
428 printf '%s' "$kept" >"$DIR/.people.expect"
429 [ "$again" = 1 ] && touch "$DIR/.people.due"
430 return 0
431 }
400432 people() {
401433 local out='{}' name raw part via="" glist
402434 if ! people_due; then cat "$DIR/.people.json" 2>/dev/null || echo '{}'; return; fi
@@ -414,6 +446,7 @@ people() {
414446 whitelistOn: ($raw | test("(?m)^white-list=true"))}' 2>/dev/null)" || continue
415447 out="$(jq -c --arg n "$name" --argjson p "$part" '. + {($n): $p}' <<<"$out")"
416448 done 3< <(running)
449 people_settle "$out"
417450 glist=""
418451 [ -n "$via" ] && glist="$(proxy_rcon "$via" glist all 2>/dev/null || true)"
419452 jq -c --arg g "${glist:0:20000}" '{servers: ., network: $g}' <<<"$out" >"$DIR/.people.json.next" && mv "$DIR/.people.json.next" "$DIR/.people.json"
@@ -524,6 +557,9 @@ run() {
524557 ;;
525558 esac
526559 touch "$DIR/.people.due"
560 # A server may write its list a moment after the command answers: the change is
561 # expected in the lists until they show it (people_settle), not read once and dropped.
562 [ "$ok" = true ] && printf '%s\t%s\t%s\t0\n' "$service" "$op" "$player" >>"$DIR/.people.expect"
527563 fi
528564 ;;
529565 # A world import, restore or snapshot: long (a download, a stop, a tar), so a job. Its
test/game-people.test.ts +144 −1
@@ -6,7 +6,7 @@
66 * move as the server's own commands, from the page, the terminal's keys and the games tool.
77 */
88 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
9 import { chmodSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
9 import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
1010 import { rm } from "node:fs/promises";
1111 import { tmpdir } from "node:os";
1212 import { join } from "node:path";
@@ -404,3 +404,146 @@ exit 0
404404 60_000,
405405 );
406406 });
407
408 describe("the agent after an op (#17617)", () => {
409 const lacks = ["bash", "curl", "jq"].filter((t) => Bun.which(t) === null);
410 test.skipIf(lacks.length > 0)(
411 "a server that writes ops.json late still shows the op on a later sync",
412 async () => {
413 const root = mkdtempSync(join(tmpdir(), "forge-game-people-late-"));
414 const box = join(root, "box");
415 mkdirSync(box);
416 const reads = join(root, "reads");
417 const fake = join(root, "docker");
418 writeFileSync(
419 fake,
420 `#!/usr/bin/env bash
421 case "$*" in
422 *" ps -a --format json") echo '{"Service":"hub","Name":"hub","State":"running"}' ;;
423 *" ps --status running --format "*) echo hub ;;
424 *" exec -T hub rcon-cli -- op Alice") echo "Made Alice a server operator" ;;
425 *" exec -T hub sh -c for f in"*)
426 echo x >> "${reads}"
427 ops='[]'
428 [ "$(wc -l < "${reads}")" -ge 3 ] && ops='[{"uuid":"${ALICE}","name":"Alice","level":4}]'
429 printf '=== whitelist.json\\n[]\\n\\n=== ops.json\\n%s\\n\\n=== banned-players.json\\n[]\\n\\n=== props\\n' "$ops" ;;
430 esac
431 exit 0
432 `,
433 );
434 chmodSync(fake, 0o755);
435 const bodies: Record<string, any>[] = [];
436 const forge: ReturnType<typeof Bun.serve> = Bun.serve({
437 port: 0,
438 hostname: "127.0.0.1",
439 async fetch(req): Promise<Response> {
440 bodies.push((await req.json()) as Record<string, any>);
441 return Response.json(
442 bodies.length === 1
443 ? { composeSha: "", commands: [{ id: "p1", kind: "people", service: "hub", text: JSON.stringify({ op: "op", player: "Alice" }) }] }
444 : { composeSha: "", commands: [] },
445 );
446 },
447 });
448 try {
449 const run = async (): Promise<void> => {
450 const p = Bun.spawn(["bash", AGENT], {
451 env: {
452 PATH: process.env["PATH"] ?? "",
453 FORGE_URL: `http://127.0.0.1:${String(forge.port)}`,
454 FORGE_GAME_TOKEN: TOKEN,
455 FORGE_GAME_DIR: box,
456 FORGE_GAME_DOCKER: fake,
457 FORGE_GAME_ONCE: "1",
458 },
459 stdout: "pipe",
460 stderr: "pipe",
461 });
462 await p.exited;
463 };
464 await run();
465 await run();
466 await run();
467 expect(bodies.length).toBe(3);
468 expect(bodies[1]!["people"].servers.hub.ops).toEqual([]);
469 expect(bodies[2]!["people"].servers.hub.ops).toEqual([{ name: "Alice", uuid: ALICE, level: 4 }]);
470 } finally {
471 forge.stop(true);
472 await rm(root, { recursive: true, force: true });
473 }
474 },
475 60_000,
476 );
477 });
478
479 describe("the agent's expected change while a server is down (#17617)", () => {
480 const lacks = ["bash", "curl", "jq"].filter((t) => Bun.which(t) === null);
481 test.skipIf(lacks.length > 0)(
482 "a server not read keeps its expected change, tries unchanged, and shows the op once it is read",
483 async () => {
484 const root = mkdtempSync(join(tmpdir(), "forge-game-people-down-"));
485 const box = join(root, "box");
486 mkdirSync(box);
487 const up = join(root, "up");
488 const fake = join(root, "docker");
489 writeFileSync(
490 fake,
491 `#!/usr/bin/env bash
492 case "$*" in
493 *" ps -a --format json") echo '{"Service":"hub","Name":"hub","State":"running"}' ;;
494 *" ps --status running --format "*) [ -f "${up}" ] && echo hub ;;
495 *" exec -T hub rcon-cli -- op Alice") echo "Made Alice a server operator" ;;
496 *" exec -T hub sh -c for f in"*)
497 printf '=== whitelist.json\\n[]\\n\\n=== ops.json\\n[{"uuid":"${ALICE}","name":"Alice","level":4}]\\n\\n=== banned-players.json\\n[]\\n\\n=== props\\n' ;;
498 esac
499 exit 0
500 `,
501 );
502 chmodSync(fake, 0o755);
503 const bodies: Record<string, any>[] = [];
504 const forge: ReturnType<typeof Bun.serve> = Bun.serve({
505 port: 0,
506 hostname: "127.0.0.1",
507 async fetch(req): Promise<Response> {
508 bodies.push((await req.json()) as Record<string, any>);
509 return Response.json(
510 bodies.length === 1
511 ? { composeSha: "", commands: [{ id: "p1", kind: "people", service: "hub", text: JSON.stringify({ op: "op", player: "Alice" }) }] }
512 : { composeSha: "", commands: [] },
513 );
514 },
515 });
516 try {
517 const run = async (): Promise<void> => {
518 const p = Bun.spawn(["bash", AGENT], {
519 env: {
520 PATH: process.env["PATH"] ?? "",
521 FORGE_URL: `http://127.0.0.1:${String(forge.port)}`,
522 FORGE_GAME_TOKEN: TOKEN,
523 FORGE_GAME_DIR: box,
524 FORGE_GAME_DOCKER: fake,
525 FORGE_GAME_ONCE: "1",
526 FORGE_GAME_PEOPLE_EVERY: "0",
527 },
528 stdout: "pipe",
529 stderr: "pipe",
530 });
531 await p.exited;
532 };
533 // Down for seven syncs: more than the five reads a change is held for.
534 for (let i = 0; i < 7; i++) await run();
535 expect(readFileSync(join(box, ".people.expect"), "utf8")).toBe(`hub\top\tAlice\t0\n`);
536 expect(existsSync(join(box, ".people.due"))).toBe(false);
537 writeFileSync(up, "");
538 await run();
539 expect(bodies.at(-1)!["people"].servers.hub.ops).toEqual([{ name: "Alice", uuid: ALICE, level: 4 }]);
540 expect(existsSync(join(box, ".people.expect"))).toBe(true);
541 expect(readFileSync(join(box, ".people.expect"), "utf8")).toBe("");
542 } finally {
543 forge.stop(true);
544 await rm(root, { recursive: true, force: true });
545 }
546 },
547 90_000,
548 );
549 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs