Public: anyone can read this; the crew can change it.
Game door: each player sees and reaches only the servers their forge grant allows #18301
Merged 5h 49m ago, live in production
scout/forge:scout/18297-game-door-each-player-sees-and-reaches-o → main 0 commits fork 3 threads · 3 unresolved
9 files
changed +312
−2
docs/features/games.md +2
| @@ -181,6 +181,8 @@ Each sync, the agent asks every running server with `rcon-cli` for `list`, `tps` | ||
| 181 | 181 | |
| 182 | 182 | Grants per box, optionally for one service: **owner** (everything and grants), **operator** (console, power, env, mods), **viewer** (read). Forge admins own every box; owners grant on the box page. |
| 183 | 183 | |
| 184 | Each server's whitelist follows the grants (#18297, `src/games/reach.ts`): a player counts only when the network checks accounts with Mojang (the proxy's own velocity.toml, inline in the compose file, says `online-mode` true or leaves Velocity's default; a velocity.toml the forge cannot read counts as not checking; with no proxy, each server's `ONLINE_MODE`), their name is linked to a forge account with the UUID the server listed when they linked, the account is active, and it holds viewer or above on that server. All of it is asked again at every full sync, so a grant taken or an account suspended drops the name on the next one. Each full sync queues `whitelist add` for a linked name that may reach a server and `whitelist remove` for a linked name that may not; a name nobody linked was put there by hand and stays, and so does a name linked before UUIDs were kept (it counts for nothing until it links again). A change waiting for the box or done in the last five minutes is not queued again. A Bedrock name (".name") is linked on its own. On an offline-mode network no name links or acts through `!forge` or `/forge`. The whitelist keeps people out only while it is on: while it is off, the People block on the service page and the service's people tab in the terminal (`src/tui/screens/games.ts`) say anyone may join, grant or not. The proxy's own server list, hub selector and `/server` are filtered by the pinned forge-velocity plugin in #18303, not yet. | |
| 185 | ||
| 184 | 186 | ## What players do, and why it failed (#17610) |
| 185 | 187 | |
| 186 | 188 | The forge hears the logs and keeps each thing that happens to or by a player as an event (`game_player_event`, 30 days): joined, left, **kicked** (Paper's "Disconnecting Alice (/ip): reason", "Alice lost connection: reason", a client's own leaving is a leave), a **join that failed** (the proxy's "has disconnected: reason"), a **command** (Paper's "Alice issued server command: /plugins"; the words after `/login`, `/register` and the like are never kept), and a **start** with the time the server said it took ("ready in 52.3 s", which is also how long a wake took). The service page's People block, the terminal's people tab and the `games` tool's `people` `list` (`latest`, `findings`) say them in the same words (`src/games/findings.ts`). |
src/games/chat.ts +3
| @@ -40,6 +40,7 @@ import { asksIn, keepAsk, say, takeConfirmed, type Confirmed, type Where } from | ||
| 40 | 40 | import { readLines, readOf } from "./forge_read.ts"; |
| 41 | 41 | import { commitServer, planServer, wishOn } from "./presets.ts"; |
| 42 | 42 | import { peopleOf } from "./people.ts"; |
| 43 | import { checksAccounts, OFFLINE_WORDS } from "./reach.ts"; | |
| 43 | 44 | import { proxyOf } from "./sleep.ts"; |
| 44 | 45 | import { LINES_OUT, listBoxes, mayDo, roleOn, SERVICE_NAME, type GameBox, type Role } from "./store.ts"; |
| 45 | 46 | |
| @@ -377,6 +378,8 @@ function one( | ||
| 377 | 378 | const help = lower === "" || lower === "help"; |
| 378 | 379 | const exposed = exposedBackend(compose); |
| 379 | 380 | if (exposed !== undefined) return refuse("", `off: ${exposed}`, "!forge is off on this network; its owner can see why on the forge.", "red", true); |
| 381 | // An offline-mode network lets any name in: no name there links or acts (#18297). | |
| 382 | if (!checksAccounts(compose)) return refuse("", "offline mode", OFFLINE_WORDS); | |
| 380 | 383 | // Linking: the code from the forge, once, within ten minutes; five wrong codes an hour a name. |
| 381 | 384 | const typed = /^link\s+([A-Za-z0-9]{6})$/i.exec(words)?.[1]; |
| 382 | 385 | const codeHash = confirmed?.action === "link" ? confirmed.words : typed === undefined ? undefined : sha256(typed.toUpperCase()); |
src/games/reach.ts added +138
| @@ -0,0 +1,138 @@ | ||
| 1 | /** | |
| 2 | * Who may reach which server of a game network (#18297): a player counts only when the network | |
| 3 | * checks accounts with Mojang (online-mode), their name is linked to a forge account with the UUID | |
| 4 | * the server listed when they typed the code (a link from before UUIDs has none and counts for | |
| 5 | * nothing), that account is active, and it holds at least a viewer grant on the server. A Bedrock | |
| 6 | * name (Floodgate's ".name") is its own link: a Java link never covers it. | |
| 7 | * | |
| 8 | * Each server's whitelist follows that: a linked name the forge sees whitelisted without a grant is | |
| 9 | * removed, a name with one is added. A name nobody linked was put there by hand, and stays; so does | |
| 10 | * a name linked before UUIDs (it counts for nothing, but is not taken off: it links again). | |
| 11 | */ | |
| 12 | ||
| 13 | import type { Database } from "bun:sqlite"; | |
| 14 | import type { User } from "../core/users.ts"; | |
| 15 | import { getUserById, isActive } from "../core/users.ts"; | |
| 16 | import { composeServices, serviceEnv } from "./compose.ts"; | |
| 17 | import { peopleOf } from "./people.ts"; | |
| 18 | import { proxyOf } from "./sleep.ts"; | |
| 19 | import { mayDo, queueCommand, roleOn, type GameBox } from "./store.ts"; | |
| 20 | ||
| 21 | /** The words a player hears when the network lets any name in. */ | |
| 22 | export const OFFLINE_WORDS = "This network does not check accounts with Mojang (online-mode is off), so no name here can act for a forge account."; | |
| 23 | ||
| 24 | /** | |
| 25 | * The proxy's own velocity.toml as the compose file carries it inline (a `configs` entry whose | |
| 26 | * target is velocity.toml, its top-level `content`), or undefined when it is elsewhere. | |
| 27 | */ | |
| 28 | function proxyToml(compose: string, proxy: string): string | undefined { | |
| 29 | try { | |
| 30 | const doc = Bun.YAML.parse(compose) as { services?: Record<string, { configs?: unknown }>; configs?: Record<string, { content?: unknown }> } | null; | |
| 31 | const mounts = doc?.services?.[proxy]?.configs; | |
| 32 | const source = (Array.isArray(mounts) ? mounts : []) | |
| 33 | .map((m: unknown) => (m !== null && typeof m === "object" ? (m as { source?: unknown; target?: unknown }) : {})) | |
| 34 | .find((m) => typeof m.target === "string" && m.target.endsWith("/velocity.toml"))?.source; | |
| 35 | const content = typeof source === "string" ? doc?.configs?.[source]?.content : undefined; | |
| 36 | return typeof content === "string" ? content : undefined; | |
| 37 | } catch { | |
| 38 | return undefined; | |
| 39 | } | |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * Whether the network checks accounts. With a proxy, its own velocity.toml says: `online-mode` | |
| 44 | * among its top-level keys (before the first table), Velocity's default true when it is not said; a | |
| 45 | * velocity.toml the forge cannot read counts as not checking. With no proxy, every server's | |
| 46 | * ONLINE_MODE. | |
| 47 | */ | |
| 48 | export function checksAccounts(compose: string): boolean { | |
| 49 | const proxy = proxyOf(compose); | |
| 50 | if (proxy !== undefined) { | |
| 51 | const toml = proxyToml(compose, proxy); | |
| 52 | if (toml === undefined) return false; | |
| 53 | for (const line of toml.split("\n")) { | |
| 54 | if (/^\s*\[/.test(line)) break; | |
| 55 | const m = /^\s*online-mode\s*=\s*([^#\s]*)\s*(?:#.*)?$/.exec(line); | |
| 56 | if (m !== null) return m[1] === "true"; | |
| 57 | } | |
| 58 | return true; | |
| 59 | } | |
| 60 | const servers = composeServices(compose); | |
| 61 | return servers.length > 0 && servers.every((s) => (serviceEnv(compose, s)["ONLINE_MODE"] ?? "true").toLowerCase() !== "false"); | |
| 62 | } | |
| 63 | ||
| 64 | /** What a server with its whitelist off says beside its lists, on the page and in the terminal. */ | |
| 65 | export const openDoorWords = (whitelistOn: boolean): string | undefined => | |
| 66 | whitelistOn | |
| 67 | ? undefined | |
| 68 | : "The whitelist is off, so anyone may join, grant or not. The forge keeps the list from grants; it decides who gets in only once Only listed players may join is on."; | |
| 69 | ||
| 70 | /** The game servers of `compose`: the proxy is the door, not one of them. */ | |
| 71 | const serversOf = (compose: string): string[] => { | |
| 72 | const proxy = proxyOf(compose); | |
| 73 | return composeServices(compose).filter((s) => s !== proxy); | |
| 74 | }; | |
| 75 | ||
| 76 | /** | |
| 77 | * Each server's players who may reach it, by name; none at all when the network does not check | |
| 78 | * accounts. | |
| 79 | */ | |
| 80 | export function whoMayReach(db: Database, box: Pick<GameBox, "id">, compose: string): Record<string, string[]> { | |
| 81 | const servers = serversOf(compose); | |
| 82 | const out: Record<string, string[]> = Object.fromEntries(servers.map((s) => [s, []])); | |
| 83 | if (!checksAccounts(compose)) return out; | |
| 84 | const links = db | |
| 85 | .query<{ player: string; user_id: string }, [string]>("SELECT player, user_id FROM game_link WHERE box_id = ?1 AND uuid != '' ORDER BY player") | |
| 86 | .all(box.id); | |
| 87 | const users = new Map<string, User | undefined>(); | |
| 88 | for (const l of links) { | |
| 89 | if (!users.has(l.user_id)) users.set(l.user_id, getUserById(db, l.user_id) ?? undefined); | |
| 90 | const user = users.get(l.user_id); | |
| 91 | if (user === undefined || !isActive(user)) continue; | |
| 92 | for (const s of servers) if (mayDo(roleOn(db, user, box.id, s), "viewer")) out[s]!.push(l.player); | |
| 93 | } | |
| 94 | return out; | |
| 95 | } | |
| 96 | ||
| 97 | /** The whitelist changes that make each server's list follow the grants, as people commands. */ | |
| 98 | export function whitelistPlan(db: Database, box: Pick<GameBox, "id">, compose: string): { service: string; op: "whitelist-add" | "whitelist-remove"; player: string }[] { | |
| 99 | const reach = whoMayReach(db, box, compose); | |
| 100 | const linked = new Set( | |
| 101 | db.query<{ player: string }, [string]>("SELECT player FROM game_link WHERE box_id = ?1 AND uuid != ''").all(box.id).map((r) => r.player.toLowerCase()), | |
| 102 | ); | |
| 103 | const plan: { service: string; op: "whitelist-add" | "whitelist-remove"; player: string }[] = []; | |
| 104 | for (const [service, may] of Object.entries(reach)) { | |
| 105 | const listed = peopleOf(db, box.id, service).lists.whitelist.map((p) => p.name); | |
| 106 | const has = new Set(listed.map((n) => n.toLowerCase())); | |
| 107 | const allowed = new Set(may.map((n) => n.toLowerCase())); | |
| 108 | for (const name of may) if (!has.has(name.toLowerCase())) plan.push({ service, op: "whitelist-add", player: name }); | |
| 109 | for (const name of listed) | |
| 110 | if (linked.has(name.toLowerCase()) && !allowed.has(name.toLowerCase())) plan.push({ service, op: "whitelist-remove", player: name }); | |
| 111 | } | |
| 112 | return plan; | |
| 113 | } | |
| 114 | ||
| 115 | /** How long a done change is believed while the box's lists may not show it yet. */ | |
| 116 | const DONE_MS = 5 * 60_000; | |
| 117 | ||
| 118 | /** | |
| 119 | * Queue the plan, each change once: one waiting for the box, or done in the last five minutes (the | |
| 120 | * lists it sends may not show it yet), is not queued again. | |
| 121 | */ | |
| 122 | export function followGrants(db: Database, box: Pick<GameBox, "id">, compose: string | undefined, now = Date.now()): number { | |
| 123 | if (compose === undefined) return 0; | |
| 124 | const waiting = new Set( | |
| 125 | db | |
| 126 | .query<{ service: string; text: string }, [string, string]>("SELECT service, text FROM game_command WHERE box_id = ?1 AND kind = 'people' AND (done_at = '' OR done_at > ?2)") | |
| 127 | .all(box.id, new Date(now - DONE_MS).toISOString()) | |
| 128 | .map((r) => `${r.service}\n${r.text}`), | |
| 129 | ); | |
| 130 | let queued = 0; | |
| 131 | for (const c of whitelistPlan(db, box, compose)) { | |
| 132 | const text = JSON.stringify({ op: c.op, player: c.player }); | |
| 133 | if (waiting.has(`${c.service}\n${text}`)) continue; | |
| 134 | queueCommand(db, box.id, c.service, "people", text, ""); | |
| 135 | queued++; | |
| 136 | } | |
| 137 | return queued; | |
| 138 | } |
src/http/games_routes.ts +3
| @@ -13,6 +13,7 @@ import { synced } from "../games/notify.ts"; | ||
| 13 | 13 | import { auditShellResults } from "../games/shell.ts"; |
| 14 | 14 | import { recordMetrics } from "../games/metrics.ts"; |
| 15 | 15 | import { recordPeople } from "../games/people.ts"; |
| 16 | import { followGrants } from "../games/reach.ts"; | |
| 16 | 17 | import { gameAsk, gameChat, runChat } from "../games/chat.ts"; |
| 17 | 18 | import { snapshotFor } from "../games/desk_snapshot.ts"; |
| 18 | 19 | import { toldGot, toldTo } from "../games/door.ts"; |
| @@ -103,6 +104,8 @@ export async function gameAgentSync(ctx: Ctx): Promise<Response> { | ||
| 103 | 104 | // A pack server's files against its lockfile: the answers judged, a check asked where one is due. |
| 104 | 105 | if (body.boot === undefined) await packChecks(ctx.app, box, body); |
| 105 | 106 | const compose = await readCompose(ctx.app, box); |
| 107 | // Each server's whitelist follows the forge's grants (`games/reach.ts`). | |
| 108 | followGrants(ctx.app.db, box, compose?.text); | |
| 106 | 109 | // A server its sleep stopped is asleep, not down: the compose file says which sleep. |
| 107 | 110 | markAsleep(ctx.app.db, box.id, compose === undefined ? [] : sleeping(compose.text)); |
| 108 | 111 | // Refused joins and wakes, from the proxy's own log. |
src/tui/screens/games.ts +2
| @@ -28,6 +28,7 @@ import { containersIn, figuresOf } from "../../cloud/containers.ts"; | ||
| 28 | 28 | import { joinAddress, NO_ADDRESS, nowWords, powerNow, PREVIEW_WORDS, privateWords, SAVE_WORDS } from "../../games/gist.ts"; |
| 29 | 29 | import { findingLine, findingsOf as kickFindingsOf } from "../../games/findings.ts"; |
| 30 | 30 | import { eventsOf, eventWords, isBedrock, peopleOf, peopleWords, queuePeople } from "../../games/people.ts"; |
| 31 | import { openDoorWords } from "../../games/reach.ts"; | |
| 31 | 32 | import { findingsOf } from "../../games/player_events.ts"; |
| 32 | 33 | import { queueWorld, type WorldOp } from "../../games/worlds.ts"; |
| 33 | 34 | import { lendWords, lentRunnerOf, setLend } from "../../games/lend.ts"; |
| @@ -368,6 +369,7 @@ export function gameServiceScreen(f: Session, ctx: Ctx, box: GameBox, service: s | ||
| 368 | 369 | ...findingsOf(f.app.db, box.id, service).flatMap((x) => [x.says, ...x.fix.map((l) => `Fix: ${l}`)]), |
| 369 | 370 | online.length === 0 ? `Nobody is on ${service} right now.` : `Online: ${online.map((p) => tag(p.name)).join(", ")}`, |
| 370 | 371 | `${lists.whitelistOn ? "Only listed players may join" : "Anyone may join"} · ${lists.whitelist.length} listed · ${lists.ops.length} operators · ${lists.bans.length} banned`, |
| 372 | ...[openDoorWords(lists.whitelistOn) ?? []].flat(), | |
| 371 | 373 | ...lists.bans.map((b) => `banned: ${b.name}${b.reason === "" ? "" : ` (${b.reason})`}`), |
| 372 | 374 | ...kickFindingsOf(f.app.db, box.id, service).map(findingLine), |
| 373 | 375 | ...eventsOf(f.app.db, box.id, service, 20).map((e) => `${e.at.slice(11, 16)} ${eventWords(e, service)}`), |
src/web/pages/games.ts +2
| @@ -85,6 +85,7 @@ import { CLOUD_EVENT, cloudFacts, GAME_CLOUD_EVENT, pinnedOfBox } from "../../ac | ||
| 85 | 85 | import { topologyBlock, TOPOLOGY_CSS } from "./games_topology.ts"; |
| 86 | 86 | import { defaultHeadResponse, headResponse } from "../../games/heads.ts"; |
| 87 | 87 | import { eventsOf, eventWords, isBedrock, knowsPlayer, peopleOf, peopleWords, queuePeople, UNDO, type PeopleOp } from "../../games/people.ts"; |
| 88 | import { openDoorWords } from "../../games/reach.ts"; | |
| 88 | 89 | import { queueWorld, undoOf } from "../../games/worlds.ts"; |
| 89 | 90 | import { START_BOX } from "../../core/empty_words.ts"; |
| 90 | 91 | import { emptyLine, emptyNote } from "../empty_words.ts"; |
| @@ -1334,6 +1335,7 @@ function peopleBlock(ctx: Ctx, box: GameBox, service: string, act: boolean): Htm | ||
| 1334 | 1335 | ${peopleButton(base, lists.whitelistOn ? "whitelist-off" : "whitelist-on", "", lists.whitelistOn ? "Let anyone join" : "Only listed players may join")}` |
| 1335 | 1336 | : null} |
| 1336 | 1337 | <p class="muted small">${lists.whitelistOn ? "Only listed players may join" : "Anyone may join"} · ${lists.whitelist.length} listed · ${lists.ops.length} operators · ${lists.bans.length} banned</p> |
| 1338 | ${openDoorWords(lists.whitelistOn) === undefined ? null : html`<p class="muted small">${openDoorWords(lists.whitelistOn)}</p>`} | |
| 1337 | 1339 | ${lists.bans.length === 0 ? null : html`<h3>Banned</h3>${rowList(lists.bans.map((b) => row({ title: who(b.name, b.uuid), meta: html`${b.reason}`, ...(act ? { after: peopleButton(base, "pardon", b.name, PEOPLE_BUTTON["pardon"]!) } : {}) })))}`} |
| 1338 | 1340 | ${lists.ops.length === 0 ? null : html`<h3>Operators</h3>${rowList(lists.ops.map((o) => row({ title: who(o.name, o.uuid), meta: html`level ${o.level}`, ...(act ? { after: peopleButton(base, "deop", o.name, PEOPLE_BUTTON["deop"]!) } : {}) })))}`} |
| 1339 | 1341 | ${lists.whitelistOn && lists.whitelist.length > 0 ? html`<h3>Allowed to join</h3>${rowList(lists.whitelist.map((w) => row({ title: who(w.name, w.uuid), ...(act ? { after: peopleButton(base, "whitelist-remove", w.name, PEOPLE_BUTTON["whitelist-remove"]!) } : {}) })))}` : null} |
test/game-ask.test.ts +3
test/game-chat.test.ts +5 −2
| @@ -283,13 +283,16 @@ describe("on a forge", () => { | ||
| 283 | 283 | // The plans, commits and messages the sync left to its worker. |
| 284 | 284 | if (settle) await runChat(app, box().id); |
| 285 | 285 | }; |
| 286 | /** The tellraws queued since the last look: to whom, on which server, the text and its colour. */ | |
| 286 | /** | |
| 287 | * The tellraws queued since the last look: to whom, on which server, the text and its colour. | |
| 288 | * The whitelist changes a link queues (`games/reach.ts`) are not chat's. | |
| 289 | */ | |
| 287 | 290 | const told = (): { service: string; to: string; text: string; color: string }[] => { |
| 288 | 291 | const rows = app.db |
| 289 | 292 | .query< |
| 290 | 293 | { n: number; service: string; kind: string; text: string }, |
| 291 | 294 | [number] |
| 292 | >("SELECT rowid AS n, service, kind, text FROM game_command WHERE rowid > ?1 ORDER BY rowid") | |
| 295 | >("SELECT rowid AS n, service, kind, text FROM game_command WHERE rowid > ?1 AND kind != 'people' ORDER BY rowid") | |
| 293 | 296 | .all(seen); |
| 294 | 297 | if (rows.length > 0) seen = rows.at(-1)!.n; |
| 295 | 298 | return rows.map((r) => { |
test/game-reach.test.ts added +154
| @@ -0,0 +1,154 @@ | ||
| 1 | /** | |
| 2 | * Who may reach which server (#18297): a linked, active account with a grant on an online-mode | |
| 3 | * network, and nobody else; each server's whitelist follows it. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtempSync } from "node:fs"; | |
| 7 | import { rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { createUser, type User } from "../src/core/users.ts"; | |
| 12 | import { gameChat } from "../src/games/chat.ts"; | |
| 13 | import { checksAccounts, followGrants, OFFLINE_WORDS, openDoorWords, whitelistPlan, whoMayReach } from "../src/games/reach.ts"; | |
| 14 | import { boxNamed, createGameBox, grant } from "../src/games/store.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | const compose = (online: boolean) => `name: game | |
| 20 | ||
| 21 | configs: | |
| 22 | velocity_toml: | |
| 23 | content: | | |
| 24 | online-mode = ${String(online)} | |
| 25 | [servers] | |
| 26 | hub = "hub:25565" | |
| 27 | mods = "mods:25565" | |
| 28 | try = ["hub"] | |
| 29 | ||
| 30 | services: | |
| 31 | velocity: | |
| 32 | image: itzg/mc-proxy:latest | |
| 33 | ports: ["25565:25565"] | |
| 34 | configs: | |
| 35 | - source: velocity_toml | |
| 36 | target: /config/velocity.toml | |
| 37 | environment: | |
| 38 | TYPE: VELOCITY | |
| 39 | hub: | |
| 40 | image: itzg/minecraft-server:latest | |
| 41 | environment: | |
| 42 | TYPE: PAPER | |
| 43 | mods: | |
| 44 | image: itzg/minecraft-server:latest | |
| 45 | environment: | |
| 46 | TYPE: PAPER | |
| 47 | `; | |
| 48 | const ONLINE = compose(true); | |
| 49 | const OFFLINE = compose(false); | |
| 50 | ||
| 51 | describe("reach", () => { | |
| 52 | let dataDir = ""; | |
| 53 | let app: App; | |
| 54 | let ann: User; | |
| 55 | let cy: User; | |
| 56 | let boxId = ""; | |
| 57 | ||
| 58 | const user = async (username: string): Promise<User> => { | |
| 59 | const u = await createUser(app.db, { username, email: `${username}@f.test`, password: "a-good-passphrase", origin: "seed", trusted: true }); | |
| 60 | if (!u.ok) throw new Error("user"); | |
| 61 | return u.value; | |
| 62 | }; | |
| 63 | const link = (player: string, u: User, uuid: string) => | |
| 64 | app.db | |
| 65 | .query("INSERT OR REPLACE INTO game_link (box_id, player, user_id, uuid, created_at) VALUES (?1, ?2, ?3, ?4, ?5)") | |
| 66 | .run(boxId, player, u.id, uuid, new Date().toISOString()); | |
| 67 | const whitelist = (service: string, names: string[]) => | |
| 68 | app.db | |
| 69 | .query("UPDATE game_service SET people_json = ?3 WHERE box_id = ?1 AND name = ?2") | |
| 70 | .run(boxId, service, JSON.stringify({ whitelist: names.map((name) => ({ name, uuid: "" })), whitelistOn: true })); | |
| 71 | ||
| 72 | beforeAll(async () => { | |
| 73 | dataDir = mkdtempSync(join(tmpdir(), "forge-game-reach-")); | |
| 74 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 75 | ann = await user("ann"); | |
| 76 | cy = await user("cy"); | |
| 77 | boxId = createGameBox(app.db, { name: "mc" }).id; | |
| 78 | for (const s of ["velocity", "hub", "mods"]) | |
| 79 | app.db.query("INSERT OR IGNORE INTO game_service (box_id, name, state) VALUES (?1, ?2, 'running')").run(boxId, s); | |
| 80 | link("ann", ann, "00000000-0000-4000-8000-000000000001"); | |
| 81 | link("cy", cy, "00000000-0000-4000-8000-000000000002"); | |
| 82 | grant(app.db, boxId, ann.id, "viewer", "hub"); | |
| 83 | }); | |
| 84 | afterAll(async () => { | |
| 85 | app.close(); | |
| 86 | await rm(dataDir, { recursive: true, force: true }); | |
| 87 | }); | |
| 88 | ||
| 89 | test("a player without a grant reaches no server; one with a grant reaches only that one", () => { | |
| 90 | expect(whoMayReach(app.db, { id: boxId }, ONLINE)).toEqual({ hub: ["ann"], mods: [] }); | |
| 91 | }); | |
| 92 | ||
| 93 | test("an offline-mode network lets no name count", () => { | |
| 94 | expect(checksAccounts(ONLINE)).toBe(true); | |
| 95 | expect(checksAccounts(OFFLINE)).toBe(false); | |
| 96 | // Velocity's own default, true, when its config does not say. | |
| 97 | expect(checksAccounts(ONLINE.replace(" online-mode = true\n", ""))).toBe(true); | |
| 98 | // Read from the proxy's velocity.toml only: a comment, a table's key or another config says | |
| 99 | // nothing. | |
| 100 | expect(checksAccounts(ONLINE.replace("online-mode = true", "online-mode = true # online-mode = false"))).toBe(true); | |
| 101 | expect(checksAccounts(ONLINE.replace(" try = [\"hub\"]", " try = [\"hub\"]\n [advanced]\n online-mode = false"))).toBe(true); | |
| 102 | expect(checksAccounts(`${ONLINE}\n# online-mode = false\n`)).toBe(true); | |
| 103 | // A velocity.toml the forge cannot read is the safe side: not checking. | |
| 104 | expect(checksAccounts(ONLINE.replace("target: /config/velocity.toml", "target: /config/other.toml"))).toBe(false); | |
| 105 | expect(whoMayReach(app.db, { id: boxId }, OFFLINE)).toEqual({ hub: [], mods: [] }); | |
| 106 | }); | |
| 107 | ||
| 108 | test("a link without a UUID counts for nothing, and a Java link does not cover the Bedrock name", () => { | |
| 109 | link("dee", ann, ""); | |
| 110 | expect(whoMayReach(app.db, { id: boxId }, ONLINE)["hub"]).toEqual(["ann"]); | |
| 111 | link(".ann", cy, "00000000-0000-4000-8000-000000000003"); | |
| 112 | expect(whoMayReach(app.db, { id: boxId }, ONLINE)["hub"]).toEqual(["ann"]); | |
| 113 | app.db.query("DELETE FROM game_link WHERE box_id = ?1 AND player IN ('dee', '.ann')").run(boxId); | |
| 114 | }); | |
| 115 | ||
| 116 | test("the whitelist follows grants: add the granted, drop the linked without one, keep hand-added names", () => { | |
| 117 | whitelist("hub", ["cy", "zed"]); | |
| 118 | whitelist("mods", ["ann"]); | |
| 119 | expect(whitelistPlan(app.db, { id: boxId }, ONLINE)).toEqual([ | |
| 120 | { service: "hub", op: "whitelist-add", player: "ann" }, | |
| 121 | { service: "hub", op: "whitelist-remove", player: "cy" }, | |
| 122 | { service: "mods", op: "whitelist-remove", player: "ann" }, | |
| 123 | ]); | |
| 124 | expect(followGrants(app.db, { id: boxId }, ONLINE)).toBe(3); | |
| 125 | // Waiting for the box: not queued again. | |
| 126 | expect(followGrants(app.db, { id: boxId }, ONLINE)).toBe(0); | |
| 127 | // Done, and the lists the box sent do not show it yet: still not queued again, for a while. | |
| 128 | const done = new Date().toISOString(); | |
| 129 | app.db.query("UPDATE game_command SET handed_at = ?2, done_at = ?2, ok = 1 WHERE box_id = ?1 AND kind = 'people'").run(boxId, done); | |
| 130 | expect(followGrants(app.db, { id: boxId }, ONLINE)).toBe(0); | |
| 131 | expect(followGrants(app.db, { id: boxId }, ONLINE, Date.now() + 6 * 60_000)).toBe(3); | |
| 132 | }); | |
| 133 | ||
| 134 | test("a name linked before UUIDs counts for nothing, but is not taken off a whitelist", () => { | |
| 135 | link("old", cy, ""); | |
| 136 | whitelist("mods", ["old"]); | |
| 137 | expect(whitelistPlan(app.db, { id: boxId }, ONLINE).filter((c) => c.service === "mods")).toEqual([]); | |
| 138 | app.db.query("DELETE FROM game_link WHERE box_id = ?1 AND player = 'old'").run(boxId); | |
| 139 | }); | |
| 140 | ||
| 141 | test("a server with its whitelist off says anyone may join, grant or not", () => { | |
| 142 | expect(openDoorWords(false)).toContain("anyone may join, grant or not"); | |
| 143 | expect(openDoorWords(true)).toBeUndefined(); | |
| 144 | }); | |
| 145 | ||
| 146 | test("an offline-mode name cannot link or act through !forge", () => { | |
| 147 | app.db.query("UPDATE game_service SET online_json = ?2 WHERE box_id = ?1 AND name = 'hub'").run(boxId, JSON.stringify([{ name: "ann", uuid: "00000000-0000-4000-8000-000000000001" }])); | |
| 148 | gameChat(app, boxNamed(app.db, "mc")!, { hub: ["[12:00:01] [Async Chat Thread - #3/INFO]: <ann> !forge help"] }, OFFLINE, "http://forge.test", Date.now()); | |
| 149 | const row = app.db.query<{ outcome: string }, [string]>("SELECT outcome FROM game_chat WHERE box_id = ?1 ORDER BY id DESC LIMIT 1").get(boxId); | |
| 150 | expect(row?.outcome).toBe("offline mode"); | |
| 151 | const told = app.db.query<{ text: string }, [string]>("SELECT text FROM game_command WHERE box_id = ?1 AND kind = 'tell' ORDER BY rowid DESC LIMIT 1").get(boxId); | |
| 152 | expect(told?.text).toContain(OFFLINE_WORDS.slice(0, 40)); | |
| 153 | }); | |
| 154 | }); |
new above
offline
This matches
online-mode = falseanywhere in the compose text, including a comment or an unrelated service's config. It also ignores a velocity.toml that is not inline, where the default is true but the file could say false. Parse the proxy's own config. When the config cannot be read, treat the network as not checking accounts, which is the safe side.