Merge pull request #20 from WebSpeak3/feature/admin-panel

Admin panel for live gateway settings
This commit is contained in:
Möpchi authored and GitHub committed 2026-11-25 13:43:46 +01:00
commit 8c0d0a3d1d
13 files changed
+1052 -81

No files matched your search

+4
View File
@@ -232,6 +232,10 @@ All optional; add them under `environment:` in your compose override next to `PO
| `WEB_DIST` | `web/dist` next to the gateway | Overrides the directory served when `WEB_STATIC` is on. Only needed for non-standard layouts. |
| `DEFAULT_SERVER` / `DEFAULT_CHANNEL` | unset | Server (and optionally channel) every visitor auto-connects to — see [Bake in a default server](#2-bake-in-a-default-server). |
| `BROADCAST_TOKEN` | unset | Bearer token required to call `/api/broadcast` (send an in-app notice to all connected clients before a maintenance restart). Endpoint 404s while unset. |
| `ADMIN_USER` / `ADMIN_PASSWORD` | unset | Enables the admin panel at `/admin` (works with `WEB_STATIC=0` too). Most settings in this table can be changed there without a restart (limits, access lists, connect dialog, features, feedback, store, news ticker, Turnstile, TURN, tokens); the ones read at startup (`PORT`, TLS, file paths, `WEB_STATIC`) are saved and applied by the panel's restart button, which relies on your restart policy (`restart: unless-stopped`) to bring the gateway back. Secrets are write-only. The panel also shows version, uptime and connected users, and can change the password (stored as an scrypt hash in `ADMIN_AUTH_FILE`; `ADMIN_PASSWORD` is then ignored). 5 login attempts per IP per 15 minutes. Without both variables `/admin` 404s. Not settable from the panel on purpose: `CONNECTOR_BIN`, `ALLOW_PRIVATE_TARGETS` and `ADMIN_USER`. |
| `ADMIN_TOTP_SECRET` | unset | Optional base32 secret: the admin login then also asks for a 6-digit authenticator code (Google Authenticator, Aegis, 2FAS, ...). Easier: switch 2FA on in the panel, which shows a QR code, checks a code from your app and asks for your password before activating. Switching it off there needs a current code. |
| `ADMIN_CONFIG_FILE` | `./admin-config.json` | Where panel changes are stored; they override `.env`. Put it on a volume (e.g. `/data/admin-config.json`) so it survives container recreation. |
| `ADMIN_AUTH_FILE` | `admin-auth.json` next to `ADMIN_CONFIG_FILE` | Password hash and 2FA secret set in the panel; they override `.env`. **Locked out?** Delete this file (and remove a bad `PORT` from `admin-config.json` if the panel is unreachable), then restart: `ADMIN_PASSWORD`/`ADMIN_TOTP_SECRET` from `.env` apply again. |
| `TLS_CERT` / `TLS_KEY` | unset | Paths to serve the gateway over HTTPS/WSS directly instead of behind a reverse proxy/tunnel. Both must be set together. |
| `STORE_ENABLED` | off | Set to `1` to turn this gateway's own `/api/store/themes` endpoint on. Only needed if you want to run your own Design Store instead of using the maintainer's shared one (see `STORE_URL` build arg above) — the endpoint 404s while unset. The store can also run as its own process (`node dist/store-server.js`, same image) so redeploying it never drops voice connections; route `/api/store/*` to it in your reverse proxy. |
| `STORE_DATA_FILE` | `store-themes.json` in the container's working dir | Where submitted themes (including screenshots) are stored. Mount a volume over this path if they should survive a container recreate. |
+7
View File
@@ -8,6 +8,7 @@
"name": "webspeak3-gateway",
"version": "0.1.0",
"dependencies": {
"qrcode-generator": "^1.4.4",
"ws": "^8.18.0"
},
"devDependencies": {
@@ -536,6 +537,12 @@
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/qrcode-generator": {
"version": "1.4.4",
"resolved": "https://registry.npmjs.org/qrcode-generator/-/qrcode-generator-1.4.4.tgz",
"integrity": "sha512-HM7yY8O2ilqhmULxGMpcHSF1EhJJ9yBj8gvDEuZ6M+KGJ0YY2hKpnXvRD+hZPLrDVck3ExIGhmPtSdcjC+guuw==",
"license": "MIT"
},
"node_modules/tsx": {
"version": "4.23.1",
"resolved": "https://registry.npmjs.org/tsx/-/tsx-4.23.1.tgz",
+3 -1
View File
@@ -7,9 +7,11 @@
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc -p tsconfig.json",
"start": "node dist/index.js"
"start": "node dist/index.js",
"test": "tsc -p tsconfig.json && node --test \"test/*.test.mjs\""
},
"dependencies": {
"qrcode-generator": "^1.4.4",
"ws": "^8.18.0"
},
"devDependencies": {
+1 -2
View File
@@ -51,7 +51,6 @@ export async function logFeedback(entry: FeedbackEntry): Promise<void> {
// and other instances shouldn't get connection logging just because it's in
// the codebase. Set LOG_CONNECTIONS=1 in the .env of the instance you want it
// on. Logs only host/server-name/timestamp — no nickname, no IP.
export const LOG_CONNECTIONS = process.env.LOG_CONNECTIONS === "1";
export interface ConnectionEvent {
type: "connected" | "disconnected";
@@ -60,7 +59,7 @@ export interface ConnectionEvent {
}
export function logConnectionEvent(event: ConnectionEvent): void {
if (!LOG_CONNECTIONS) return;
if (process.env.LOG_CONNECTIONS !== "1") return;
if (event.type === "connected") {
console.log(`[connections] connected host=${event.host} server=${event.server} at=${new Date().toISOString()}`);
} else {
+691
View File
@@ -0,0 +1,691 @@
// Admin panel at /admin: change the live settings from config.ts in the
// browser. Off unless both ADMIN_USER and ADMIN_PASSWORD are set (404
// otherwise). Optional ADMIN_TOTP_SECRET (base32) adds a 6-digit
// authenticator code to the login.
//
// The panel can also change the password and switch 2FA on or off. Those
// land in ADMIN_AUTH_FILE (next to the settings file) and override .env;
// every such change asks for the current password again, so a stolen session
// alone can't lock the operator out. Deleting that file goes back to .env.
import type { IncomingMessage, ServerResponse } from "node:http";
import type { TLSSocket } from "node:tls";
import { createHash, createHmac, randomBytes, scrypt, timingSafeEqual } from "node:crypto";
import { readFileSync } from "node:fs";
import { rename, writeFile } from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import qrcode from "qrcode-generator";
import { clientIp, createRateLimiter } from "./net.js";
import { describeSettings, updateSettings, validate } from "./config.js";
const ADMIN_USER = process.env.ADMIN_USER ?? "";
const ENV_PASSWORD = process.env.ADMIN_PASSWORD ?? "";
const ENV_TOTP = (process.env.ADMIN_TOTP_SECRET ?? "").replace(/[\s=]/g, "").toUpperCase();
export const ADMIN_ENABLED = ADMIN_USER !== "" && ENV_PASSWORD !== "";
const AUTH_FILE = process.env.ADMIN_AUTH_FILE
?? path.join(path.dirname(process.env.ADMIN_CONFIG_FILE ?? path.resolve(process.cwd(), "admin-config.json")), "admin-auth.json");
// Set from the panel. totpSecret "" = 2FA switched off there (wins over .env).
let auth: { passwordHash?: string; totpSecret?: string } = {};
try {
const raw = JSON.parse(readFileSync(AUTH_FILE, "utf8"));
if (typeof raw?.passwordHash === "string") auth.passwordHash = raw.passwordHash;
if (typeof raw?.totpSecret === "string") auth.totpSecret = raw.totpSecret;
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== "ENOENT") console.error(`[admin] Ignoring unreadable ${AUTH_FILE}:`, err);
}
const totpSecret = () => auth.totpSecret ?? ENV_TOTP;
async function saveAuth(next: typeof auth) {
const tmp = `${AUTH_FILE}.${process.pid}.tmp`;
await writeFile(tmp, JSON.stringify(next, null, 2), { mode: 0o600 });
await rename(tmp, AUTH_FILE);
auth = next;
}
const scryptAsync = promisify(scrypt) as (pw: string, salt: Buffer, len: number) => Promise<Buffer>;
async function hashPassword(pw: string): Promise<string> {
const salt = randomBytes(16);
return `scrypt$${salt.toString("base64url")}$${(await scryptAsync(pw, salt, 32)).toString("base64url")}`;
}
async function passwordOk(pw: string): Promise<boolean> {
if (!auth.passwordHash) return sameString(pw, ENV_PASSWORD);
const [, salt = "", hash = ""] = auth.passwordHash.split("$");
const want = Buffer.from(hash, "base64url");
const got = await scryptAsync(pw, Buffer.from(salt, "base64url"), 32);
return want.length === got.length && timingSafeEqual(want, got);
}
const SESSION_TTL_MS = 12 * 60 * 60 * 1000;
const COOKIE = "webspeak3_admin";
const sessions = new Map<string, number>(); // token -> expiry
// Every attempt counts, so a right guess can't be hidden between wrong ones.
const tooManyFromIp = createRateLimiter(5, 15 * 60_000);
const tooManyOverall = createRateLimiter(30, 15 * 60_000);
const tooManyReauth = createRateLimiter(10, 15 * 60_000);
let lastTotpCounter = -1; // a code works once, even within its window
const sha256 = (s: string) => createHash("sha256").update(s).digest();
const sameString = (a: string, b: string) => timingSafeEqual(sha256(a), sha256(b));
const B32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
function base32Decode(s: string): Buffer {
let bits = 0, value = 0;
const out: number[] = [];
for (const c of s) {
const i = B32.indexOf(c);
if (i === -1) throw new Error("invalid base32");
value = (value << 5) | i;
bits += 5;
if (bits >= 8) {
out.push((value >>> (bits - 8)) & 0xff);
bits -= 8;
}
}
return Buffer.from(out);
}
function base32Encode(buf: Buffer): string {
let bits = 0, value = 0, out = "";
for (const byte of buf) {
value = (value << 8) | byte;
bits += 8;
while (bits >= 5) {
out += B32[(value >>> (bits - 5)) & 31];
bits -= 5;
}
}
return bits > 0 ? out + B32[(value << (5 - bits)) & 31] : out;
}
/** RFC 6238 code (SHA-1, 6 digits, 30 s) for a given time step. */
export function totp(secret: Buffer, counter: number): string {
const msg = Buffer.alloc(8);
msg.writeBigUInt64BE(BigInt(counter));
const h = createHmac("sha1", secret).update(msg).digest();
const o = h[h.length - 1] & 0xf;
return String((h.readUInt32BE(o) & 0x7fffffff) % 1_000_000).padStart(6, "0");
}
/** Accepts the current code and one step either side (clock drift). */
/** Time step the code matches (±1 step of clock drift, only steps after `after`), or -1. */
function matchTotp(secretB32: string, code: unknown, after = -1): number {
if (typeof code !== "string" || !/^\d{6}$/.test(code)) return -1;
const secret = base32Decode(secretB32);
const now = Math.floor(Date.now() / 30_000);
for (const c of [now - 1, now, now + 1]) {
if (c > after && sameString(totp(secret, c), code)) return c;
}
return -1;
}
function checkTotp(code: unknown): boolean {
const c = matchTotp(totpSecret(), code, lastTotpCounter);
if (c < 0) return false;
lastTotpCounter = c;
return true;
}
if (ADMIN_ENABLED && totpSecret()) {
try {
if (base32Decode(totpSecret()).length < 10) throw new Error("too short");
} catch (err) {
// Fail closed: a typo here must not silently drop the second factor.
console.error(`[admin] ADMIN_TOTP_SECRET is not valid base32 (${(err as Error).message}); login disabled`);
}
}
function json(res: ServerResponse, status: number, body: unknown, headers: Record<string, string> = {}) {
res.writeHead(status, { "Content-Type": "application/json", "Cache-Control": "no-store", ...headers });
res.end(JSON.stringify(body));
}
async function readJson(req: IncomingMessage): Promise<Record<string, unknown> | undefined> {
if (!String(req.headers["content-type"] ?? "").startsWith("application/json")) return undefined;
let size = 0;
const chunks: Buffer[] = [];
for await (const chunk of req) {
size += (chunk as Buffer).length;
if (size > 64 * 1024) return undefined;
chunks.push(chunk as Buffer);
}
try {
const v = JSON.parse(Buffer.concat(chunks).toString("utf8"));
return v && typeof v === "object" && !Array.isArray(v) ? v : undefined;
} catch {
return undefined;
}
}
function sessionOf(req: IncomingMessage): string | undefined {
const m = new RegExp(`(?:^|;\\s*)${COOKIE}=([A-Za-z0-9_-]+)`).exec(req.headers.cookie ?? "");
if (!m) return undefined;
const expires = sessions.get(m[1]);
if (!expires || expires < Date.now()) {
sessions.delete(m[1]);
return undefined;
}
return m[1];
}
function cookie(req: IncomingMessage, value: string, maxAgeS: number): string {
// Secure only where the browser actually talks HTTPS, else a plain-HTTP
// LAN setup could never log in.
const https = (req.socket as TLSSocket).encrypted || req.headers["x-forwarded-proto"] === "https";
return `${COOKIE}=${value}; Path=/admin; HttpOnly; SameSite=Strict; Max-Age=${maxAgeS}${https ? "; Secure" : ""}`;
}
async function handleLogin(req: IncomingMessage, res: ServerResponse) {
const ip = clientIp(req);
if (tooManyFromIp(ip) || tooManyOverall("all")) {
console.warn("[admin] login rate-limited");
return json(res, 429, { error: "rate_limited" });
}
const body = await readJson(req);
const userOk = sameString(String(body?.user ?? ""), ADMIN_USER);
const passOk = await passwordOk(String(body?.password ?? ""));
// The code is only used up by a fully successful login, so a password typo
// doesn't burn it.
let step = -1;
if (totpSecret()) {
try {
step = matchTotp(totpSecret(), body?.code, lastTotpCounter);
} catch {
step = -1;
}
}
if (!(userOk && passOk && (step >= 0 || !totpSecret()))) {
console.warn("[admin] failed login");
return json(res, 401, { error: "invalid_login" });
}
if (step >= 0) lastTotpCounter = step;
const token = randomBytes(32).toString("base64url");
sessions.set(token, Date.now() + SESSION_TTL_MS);
for (const [t, exp] of sessions) if (exp < Date.now()) sessions.delete(t);
console.log("[admin] login");
json(res, 200, { ok: true }, { "Set-Cookie": cookie(req, token, SESSION_TTL_MS / 1000) });
}
let countConnections = () => 0;
/** index.ts hands in its WebSocket server's client count (shown before a restart). */
export function setConnectionCounter(fn: () => number) {
countConnections = fn;
}
const state = () => ({
settings: describeSettings(),
totp: totpSecret() ? (auth.totpSecret ? "panel" : "env") : "",
passwordFromPanel: Boolean(auth.passwordHash),
connections: countConnections(),
version: process.env.WEBSPEAK3_VERSION || "",
uptime: Math.round(process.uptime()),
});
// Credential changes re-check the current password (own limiter, so a few
// panel actions don't eat the login budget).
async function reauth(req: IncomingMessage, res: ServerResponse, body: Record<string, unknown> | undefined): Promise<boolean> {
if (tooManyReauth(clientIp(req))) return json(res, 429, { error: "rate_limited" }), false;
if (!(await passwordOk(String(body?.password ?? "")))) {
console.warn("[admin] wrong password on credential change");
return json(res, 403, { error: "wrong_password" }), false;
}
return true;
}
export async function handleAdmin(req: IncomingMessage, res: ServerResponse, pathname: string) {
if (!ADMIN_ENABLED) return json(res, 404, { error: "not_found" });
if (pathname === "/admin" || pathname === "/admin/") {
res.writeHead(200, {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-store",
"Content-Security-Policy": "default-src 'none'; script-src 'self'; style-src 'unsafe-inline'; img-src data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'",
"X-Frame-Options": "DENY",
});
return res.end(PAGE);
}
if (pathname === "/admin/admin.js") {
res.writeHead(200, { "Content-Type": "text/javascript; charset=utf-8", "Cache-Control": "no-store" });
return res.end(SCRIPT);
}
if (req.method !== "GET" && req.method !== "POST") return json(res, 405, { error: "method_not_allowed" });
// Cross-site requests can't set this header without a CORS preflight,
// which this endpoint never grants: CSRF protection on top of SameSite.
if (req.method === "POST" && req.headers["x-webspeak3-admin"] !== "1") return json(res, 403, { error: "forbidden" });
if (pathname === "/admin/api/login" && req.method === "POST") return handleLogin(req, res);
const session = sessionOf(req);
if (!session) return json(res, 401, { error: "unauthorized" });
if (pathname === "/admin/api/logout" && req.method === "POST") {
sessions.delete(session);
return json(res, 200, { ok: true }, { "Set-Cookie": cookie(req, "", 0) });
}
if (pathname === "/admin/api/config" && req.method === "GET") return json(res, 200, state());
if (pathname === "/admin/api/restart" && req.method === "POST") {
console.log(`[admin] restart requested (${countConnections()} connected)`);
json(res, 200, { ok: true });
// Same graceful path as `docker stop`: users get the restart notice, TS
// sessions are closed. Coming back up is the supervisor's job (Docker's
// restart policy, systemd Restart=always).
setTimeout(() => process.kill(process.pid, "SIGTERM"), 300);
return;
}
if (pathname === "/admin/api/config" && req.method === "POST") {
const body = await readJson(req);
const changes = body?.changes;
if (!changes || typeof changes !== "object" || Array.isArray(changes)) return json(res, 400, { error: "bad_request" });
const clean: Record<string, string | null> = {};
for (const [key, value] of Object.entries(changes)) {
if (value !== null && typeof value !== "string") return json(res, 400, { error: "bad_request", key });
// null = reset to .env; then only the key itself has to be known.
const err = value === null ? (validate(key, "") === "unknown setting" ? "unknown setting" : undefined) : validate(key, value.trim());
if (err) return json(res, 400, { error: err, key });
clean[key] = value === null ? null : value.trim();
}
await updateSettings(clean);
console.log(`[admin] settings changed: ${Object.keys(clean).join(", ")}`);
return json(res, 200, state());
}
if (pathname === "/admin/api/totp-secret" && req.method === "GET") {
const secret = base32Encode(randomBytes(20));
const label = encodeURIComponent(`WebSpeak3:${ADMIN_USER}`);
const uri = `otpauth://totp/${label}?secret=${secret}&issuer=WebSpeak3`;
const qr = qrcode(0, "M");
qr.addData(uri);
qr.make();
return json(res, 200, { secret, uri, qr: qr.createDataURL(5, 4) });
}
// Setup check: does the authenticator app produce codes for the secret
// just generated? Only then the panel shows the .env line.
if (pathname === "/admin/api/totp-verify" && req.method === "POST") {
const body = await readJson(req);
const secret = String(body?.secret ?? "");
if (!/^[A-Z2-7]{16,64}$/.test(secret)) return json(res, 400, { error: "bad_request" });
return json(res, 200, { ok: matchTotp(secret, String(body?.code ?? "").trim()) >= 0 });
}
if (pathname === "/admin/api/totp-enable" && req.method === "POST") {
const body = await readJson(req);
const secret = String(body?.secret ?? "");
if (!/^[A-Z2-7]{16,64}$/.test(secret)) return json(res, 400, { error: "bad_request" });
if (!(await reauth(req, res, body))) return;
const c = matchTotp(secret, String(body?.code ?? "").trim());
if (c < 0) return json(res, 400, { error: "code_wrong" });
await saveAuth({ ...auth, totpSecret: secret });
lastTotpCounter = c; // the code just typed can't be replayed at login
console.log("[admin] 2FA switched on in the panel");
return json(res, 200, state());
}
if (pathname === "/admin/api/totp-disable" && req.method === "POST") {
const body = await readJson(req);
if (!(await reauth(req, res, body))) return;
if (!checkTotp(String(body?.code ?? "").trim())) return json(res, 400, { error: "code_wrong" });
await saveAuth({ ...auth, totpSecret: "" });
console.log("[admin] 2FA switched off in the panel");
return json(res, 200, state());
}
if (pathname === "/admin/api/password" && req.method === "POST") {
const body = await readJson(req);
const next = String(body?.newPassword ?? "");
if (next.length < 10 || next.length > 200) return json(res, 400, { error: "weak_password" });
if (!(await reauth(req, res, body))) return;
await saveAuth({ ...auth, passwordHash: await hashPassword(next) });
for (const t of sessions.keys()) if (t !== session) sessions.delete(t); // other logins end
console.log("[admin] password changed in the panel");
return json(res, 200, state());
}
json(res, 404, { error: "not_found" });
}
const PAGE = `<!doctype html><html lang="en"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex">
<title>WebSpeak3 Admin</title><style>
:root{color-scheme:light;--bg:#f4f6f8;--card:#fff;--fg:#1b1f24;--muted:#5f6b7a;--line:#dde2e8;--field:#f7f9fb;--accent:#2f6fde;--on-accent:#fff;--bad:#c0392b;--warn:#a86200;--ok:#1e7f43}
@media (prefers-color-scheme:dark){:root:not([data-theme="light"]){color-scheme:dark;--bg:#131619;--card:#1c2025;--fg:#e6e9ed;--muted:#97a1ad;--line:#2f363e;--field:#15191d;--accent:#6a9cff;--on-accent:#0b1426;--bad:#ff6b5b;--warn:#f0b35a;--ok:#5cc98a}}
:root[data-theme="dark"]{color-scheme:dark;--bg:#131619;--card:#1c2025;--fg:#e6e9ed;--muted:#97a1ad;--line:#2f363e;--field:#15191d;--accent:#6a9cff;--on-accent:#0b1426;--bad:#ff6b5b;--warn:#f0b35a;--ok:#5cc98a}
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--fg);font:15px/1.45 system-ui,sans-serif}
main{max-width:1320px;margin:0 auto;padding:20px 16px 0}
header{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:center;gap:12px;margin-bottom:18px}
h1{font-size:22px;margin:0}#stat{margin:2px 0 0}.tools{display:flex;gap:8px;align-items:center;flex-wrap:wrap}
.seg{display:inline-flex;border:1px solid var(--line);border-radius:8px;overflow:hidden;background:var(--card)}
.seg button{background:transparent;color:var(--fg);border:0;border-radius:0;padding:6px 10px;font-size:13px}
.seg button[aria-pressed="true"]{background:var(--accent);color:var(--on-accent)}
.card{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:16px}
.layout{display:grid;grid-template-columns:230px minmax(0,1fr);gap:20px;align-items:start}
.side{position:sticky;top:12px;display:flex;flex-direction:column;gap:10px}
#nav{display:flex;flex-direction:column;gap:2px}
#nav button{display:flex;justify-content:space-between;align-items:center;gap:8px;background:transparent;color:var(--fg);text-align:left;padding:8px 10px;border:1px solid transparent;border-radius:8px}
#nav button:hover{background:var(--card)}
#nav button[aria-current="true"]{background:var(--card);border-color:var(--line);font-weight:600;box-shadow:inset 3px 0 0 var(--accent)}
#nav button.muted{color:var(--muted)}#nav hr{width:100%;border:0;border-top:1px solid var(--line);margin:6px 0}
.cnt{font-size:12px;line-height:18px;min-width:18px;text-align:center;border-radius:9px;padding:0 6px;background:var(--accent);color:var(--on-accent);font-weight:600}.cnt.warnc{background:var(--warn)}
#content>#groups>.card,#extras>.card{margin-bottom:16px}
.rows{display:grid;grid-template-columns:repeat(auto-fill,minmax(min(100%,340px),1fr));column-gap:32px}
.rows .row{border-top:0;border-bottom:1px solid var(--line)}
.lab{display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-bottom:6px}.lab label{margin:0}
.badge{font-size:11px;padding:1px 8px;border-radius:9px;border:1px solid var(--line);color:var(--muted)}.badge.panel{border-color:var(--accent);color:var(--accent)}
.lab .reset{margin-left:auto;font-size:13px}.help{margin-top:4px}
@media (max-width:760px){.layout{grid-template-columns:minmax(0,1fr)}.side{position:static}#nav{flex-direction:row;overflow-x:auto;padding-bottom:4px}#nav button{flex:none;white-space:nowrap}#nav hr{display:none}}
h2{font-size:15px;margin:0 0 4px;padding-bottom:8px;border-bottom:2px solid var(--accent)}
label{display:block;font-weight:600;font-size:13px;margin-bottom:4px;font-family:ui-monospace,monospace}
input,select{width:100%;padding:8px;border:1px solid var(--line);border-radius:6px;background:var(--field);color:var(--fg);font:inherit}
input:focus,select:focus{outline:2px solid var(--accent);outline-offset:-1px}
button{padding:8px 14px;border:0;border-radius:6px;background:var(--accent);color:var(--on-accent);font:inherit;cursor:pointer}
button.plain{background:transparent;color:var(--accent);padding:2px 0}
.row{padding:12px 0;border-top:1px solid var(--line)}h2+.row{border-top:0}
.help,.meta{color:var(--muted);font-size:13px}.err{color:var(--bad);min-height:1.4em;margin:0}
.changed input,.changed select{border-color:var(--accent)}
.inactive input,.inactive select{opacity:.5}button.second{background:transparent;color:var(--accent);border:1px solid var(--accent)}button.second.due{background:var(--warn);border-color:var(--warn);color:var(--on-accent)}.warn{color:var(--warn);font-size:13px}
.foot{display:flex;justify-content:space-between;align-items:center;gap:8px}
.savebar{position:sticky;bottom:0;margin:16px -16px 0;padding:12px 16px;background:var(--bg);border-top:1px solid var(--line);display:flex;gap:12px;align-items:center;flex-wrap:wrap}
#pwCard input{max-width:420px}.step{font-weight:600;font-size:14px;margin:16px 0 8px}.okbox{display:flex;width:fit-content;align-items:center;gap:8px;color:var(--ok);font-weight:600;margin:4px 0 0;padding:8px 12px;border-radius:8px;background:color-mix(in srgb,var(--ok) 12%,transparent)}#login{max-width:380px;margin:40px auto}#login p{margin:0 0 12px}
code{word-break:break-all}[hidden]{display:none!important}
</style></head><body><main>
<header><div><h1>WebSpeak3 Admin</h1><p class="meta" id="stat" hidden></p></div><div class="tools">
<span class="seg" id="langSeg"><button type="button" data-lang="de">DE</button><button type="button" data-lang="en">EN</button></span>
<span class="seg" id="themeSeg"><button type="button" data-theme-btn="auto" data-i18n="auto"></button><button type="button" data-theme-btn="light" data-i18n="light"></button><button type="button" data-theme-btn="dark" data-i18n="dark"></button></span>
<button id="logout" class="plain" type="button" data-i18n="logout" hidden></button></div></header>
<form id="login" class="card" hidden>
<p><label for="u" data-i18n="user"></label><input id="u" autocomplete="username" required></p>
<p><label for="p" data-i18n="pass"></label><input id="p" type="password" autocomplete="current-password" required></p>
<p><label for="c"><span data-i18n="code"></span> <span class="meta" data-i18n="codeHint"></span></label><input id="c" inputmode="numeric" autocomplete="one-time-code" maxlength="6"></p>
<p class="err" id="loginErr"></p><button data-i18n="login"></button></form>
<form id="panel" hidden><div class="layout"><div class="side"><input id="search" type="search" autocomplete="off"><nav id="nav"></nav></div>
<div id="content"><div id="groups"></div><p class="meta" id="noHits" hidden></p>
<div id="extras"><div class="card" id="totpCard"><h2 data-i18n="totpTitle"></h2><p class="help" id="totpState"></p>
<div id="totpOffBox" hidden><p class="help" data-i18n="offHint"></p>
<p style="display:flex;gap:8px;flex-wrap:wrap"><input id="offCode" inputmode="numeric" autocomplete="off" maxlength="6" style="max-width:9em"><input id="offPw" type="password" autocomplete="current-password" style="max-width:16em"><button type="button" id="offBtn" class="second" data-i18n="turnOff"></button></p>
<p class="err" id="offErr"></p></div>
<button class="plain" id="genTotp" type="button" data-i18n="gen"></button><div id="totpOut" hidden>
<p class="step" data-i18n="step1"></p>
<p><img id="totpQr" alt="QR" style="image-rendering:pixelated;background:#fff;padding:4px;border-radius:6px;max-width:100%"></p>
<p class="help"><span data-i18n="cantScan"></span> <code id="totpSecret"></code></p>
<p class="step" data-i18n="step2"></p>
<p style="display:flex;gap:8px"><input id="totpCheck" inputmode="numeric" autocomplete="off" maxlength="6" style="max-width:9em"><button type="button" id="totpCheckBtn" class="second" data-i18n="check"></button></p>
<p class="err" id="totpCheckErr"></p>
<div id="totpDone" hidden><p class="okbox" data-i18n="codeOk"></p><p class="step" data-i18n="step3"></p>
<p style="display:flex;gap:8px;flex-wrap:wrap"><input id="onPw" type="password" autocomplete="current-password" style="max-width:16em"><button type="button" id="onBtn" data-i18n="turnOn"></button></p>
<p class="err" id="onErr"></p>
<p class="help"><span data-i18n="envAlt"></span> <code id="totpEnv"></code></p></div></div></div>
<div class="card" id="pwCard"><h2 data-i18n="pwTitle"></h2><p class="help" id="pwState"></p>
<p><label for="pwOld" data-i18n="pwOld"></label><input id="pwOld" type="password" autocomplete="current-password"></p>
<p><label for="pwNew" data-i18n="pwNew"></label><input id="pwNew" type="password" autocomplete="new-password" minlength="10"></p>
<p><label for="pwNew2" data-i18n="pwNew2"></label><input id="pwNew2" type="password" autocomplete="new-password"></p>
<p class="err" id="pwErr"></p><button type="button" id="pwBtn" data-i18n="pwBtn"></button></div>
<div class="card" id="aboutCard"><h2 data-i18n="aboutTitle"></h2><p class="help" data-i18n="about"></p></div></div>
</div></div>
<div class="savebar"><button id="save" data-i18n="save"></button><button id="restart" type="button" class="second"></button><span class="meta" id="status"></span><p class="err" id="saveErr"></p></div>
</form></main><script src="/admin/admin.js"></script></body></html>`;
const SCRIPT = `"use strict";
const T = {
en: {
user: "Username", pass: "Password", code: "Authenticator code", codeHint: "(if 2FA is on)", login: "Log in", logout: "Log out",
auto: "Auto", light: "Light", dark: "Dark", save: "Save", saved: "Saved, active now.", unsaved: (n) => n + " unsaved change" + (n === 1 ? "" : "s"),
fromPanel: "set in panel", fromEnv: "from .env", notSet: "not set (built-in default)", reset: "Reset", on: "On", off: "Off", def: "default: ",
secretSet: "set - type to replace", secretUnset: "not set", tooMany: "Too many attempts, try again in 15 minutes.", loginFailed: "Login failed.", saveFailed: "Save failed",
inactive: "No effect here: WEB_STATIC=0, the web UI is hosted separately (set it in that build instead).",
totpTitle: "Two-factor login", totpOn: "On (set in .env).", totpOnPanel: "On (switched on here).", totpOff: "Off. Generate a secret below to require a code at login.",
offHint: "Switch off: current code from the app and your password.", turnOff: "Switch 2FA off", turnOn: "Switch 2FA on",
envAlt: "Or put this line in .env instead:", codePh: "Code", pwPh: "Password",
pwTitle: "Password", pwEnv: "Currently the password from .env (ADMIN_PASSWORD).", pwPanel: "Changed here; ADMIN_PASSWORD in .env is no longer used.",
pwOld: "Current password", pwNew: "New password (at least 10 characters)", pwNew2: "Repeat new password", pwBtn: "Change password",
pwMismatch: "The new passwords differ.", pwDone: "Password changed. Other logins were signed out.", totpDoneOn: "2FA is on now. Next login asks for the code.",
statConn: (n) => n + " connected", statUp: "up ", statVer: "version ",
gen: "Generate a new secret", step1: "1. Scan this code with your authenticator app (Google Authenticator, Aegis, 2FAS, ...):", cantScan: "Can't scan? Enter the secret manually:",
step2: "2. Enter the 6-digit code the app now shows for WebSpeak3:", check: "Check", codeWrong: "Code doesn\\u2019t match. Wait for the next code, check the phone\\u2019s clock, or scan again.",
codeOk: "\\u2713 Code is correct", step3: "3. Confirm with your password to switch 2FA on:",
credErr: { wrong_password: "Wrong password.", code_wrong: "Wrong code.", weak_password: "At least 10 characters.", rate_limited: "Too many attempts, try again in 15 minutes." },
aboutTitle: "How this works", about: "Changes apply immediately, except in \\u201cRestart required\\u201d. They override .env and are stored in admin-config.json (ADMIN_CONFIG_FILE). \\u201cReset\\u201d returns a setting to its .env value. Password and 2FA changes go to admin-auth.json; deleting that file goes back to .env (e.g. if you are locked out).",
search: "Search settings\\u2026", noHits: "No matching settings.", std: "default",
restartBtn: "Restart gateway", restartDue: (n) => "Restart gateway (" + n + " pending)", pendingRestart: "Saved \\u2013 applies after the next restart.",
restartConfirm: (n) => (n ? n + " connection(s) are open right now; they get a restart notice and are dropped. " : "Nobody is connected. ") + "Restart now? This only works if Docker's restart policy or systemd brings the gateway back up.",
restarting: "Restarting\\u2026 the page reloads when the gateway is back.", restartFailed: "Restart failed",
errors: {}, groups: {}, help: {},
},
de: {
user: "Benutzername", pass: "Passwort", code: "Authenticator-Code", codeHint: "(falls 2FA an ist)", login: "Anmelden", logout: "Abmelden",
auto: "Auto", light: "Hell", dark: "Dunkel", save: "Speichern", saved: "Gespeichert, sofort aktiv.", unsaved: (n) => n + " ungespeicherte \\u00c4nderung" + (n === 1 ? "" : "en"),
fromPanel: "im Panel gesetzt", fromEnv: "aus .env", notSet: "nicht gesetzt (Standardwert)", reset: "Zur\\u00fccksetzen", on: "An", off: "Aus", def: "Standard: ",
secretSet: "gesetzt \\u2013 zum Ersetzen tippen", secretUnset: "nicht gesetzt", tooMany: "Zu viele Versuche, bitte in 15 Minuten erneut versuchen.", loginFailed: "Anmeldung fehlgeschlagen.", saveFailed: "Speichern fehlgeschlagen",
inactive: "Hier ohne Wirkung: WEB_STATIC=0, die Web-Oberfl\\u00e4che wird separat gehostet (dort beim Build setzen).",
totpTitle: "Zwei-Faktor-Anmeldung", totpOn: "Aktiv (in der .env gesetzt).", totpOnPanel: "Aktiv (hier eingeschaltet).", totpOff: "Aus. Unten ein Secret erzeugen, um beim Login einen Code zu verlangen.",
offHint: "Abschalten: aktueller Code aus der App und dein Passwort.", turnOff: "2FA abschalten", turnOn: "2FA einschalten",
envAlt: "Oder stattdessen diese Zeile in die .env:", codePh: "Code", pwPh: "Passwort",
pwTitle: "Passwort", pwEnv: "Aktuell gilt das Passwort aus der .env (ADMIN_PASSWORD).", pwPanel: "Hier ge\\u00e4ndert; ADMIN_PASSWORD in der .env wird nicht mehr verwendet.",
pwOld: "Aktuelles Passwort", pwNew: "Neues Passwort (mindestens 10 Zeichen)", pwNew2: "Neues Passwort wiederholen", pwBtn: "Passwort \\u00e4ndern",
pwMismatch: "Die neuen Passw\\u00f6rter stimmen nicht \\u00fcberein.", pwDone: "Passwort ge\\u00e4ndert. Andere Anmeldungen wurden abgemeldet.", totpDoneOn: "2FA ist jetzt aktiv. Der n\\u00e4chste Login fragt nach dem Code.",
statConn: (n) => n + " verbunden", statUp: "l\\u00e4uft seit ", statVer: "Version ",
gen: "Neues Secret erzeugen", step1: "1. Diesen Code mit der Authenticator-App scannen (Google Authenticator, Aegis, 2FAS, ...):", cantScan: "Scannen klappt nicht? Secret von Hand eingeben:",
step2: "2. Den 6-stelligen Code eingeben, den die App jetzt f\\u00fcr WebSpeak3 anzeigt:", check: "Pr\\u00fcfen", codeWrong: "Code passt nicht. Auf den n\\u00e4chsten Code warten, die Uhrzeit am Handy pr\\u00fcfen oder neu scannen.",
codeOk: "\\u2713 Code stimmt", step3: "3. Mit deinem Passwort best\\u00e4tigen, um 2FA einzuschalten:",
credErr: { wrong_password: "Falsches Passwort.", code_wrong: "Falscher Code.", weak_password: "Mindestens 10 Zeichen.", rate_limited: "Zu viele Versuche, in 15 Minuten erneut probieren." },
aboutTitle: "So funktioniert's", about: "\\u00c4nderungen gelten sofort, au\\u00dfer unter \\u201eNeustart n\\u00f6tig\\u201c. Sie \\u00fcberschreiben die .env und liegen in admin-config.json (ADMIN_CONFIG_FILE). \\u201eZur\\u00fccksetzen\\u201c stellt den Wert aus der .env wieder her. Passwort und 2FA aus dem Panel liegen in admin-auth.json; wer die Datei l\\u00f6scht, ist wieder bei der .env (z. B. wenn man sich ausgesperrt hat).",
search: "Einstellungen suchen\\u2026", noHits: "Keine passende Einstellung.", std: "Standardwert",
restartBtn: "Gateway neu starten", restartDue: (n) => "Gateway neu starten (" + n + " ausstehend)", pendingRestart: "Gespeichert \\u2013 gilt ab dem n\\u00e4chsten Neustart.",
restartConfirm: (n) => (n ? "Gerade sind " + n + " Verbindung(en) offen; sie bekommen einen Neustart-Hinweis und werden getrennt. " : "Niemand ist verbunden. ") + "Jetzt neu starten? Das klappt nur, wenn Dockers Restart-Policy oder systemd das Gateway wieder hochf\\u00e4hrt.",
restarting: "Startet neu\\u2026 die Seite l\\u00e4dt neu, sobald das Gateway wieder da ist.", restartFailed: "Neustart fehlgeschlagen",
errors: { "must be a port (1-65535)": "muss ein Port sein (1\\u201365535)", "must be a whole number": "muss eine ganze Zahl sein", "must be 0 or 1": "muss 0 oder 1 sein", "invalid characters or too long": "ung\\u00fcltige Zeichen oder zu lang", "unknown setting": "unbekannte Einstellung" },
groups: { "Connections & limits": "Verbindungen & Limits", "Access": "Zugriff", "Connect dialog": "Verbindungsdialog", "Features": "Funktionen", "News ticker": "News-Ticker", "Bot check (Cloudflare Turnstile)": "Bot-Check (Cloudflare Turnstile)", "Screen sharing & webcam (TURN)": "Bildschirmfreigabe & Webcam (TURN)", "Feedback & GitHub": "Feedback & GitHub", "Design Store": "Design Store", "Restart required": "Neustart n\\u00f6tig" },
help: {
MAX_CONNECTIONS: "Offene WebSocket-Verbindungen insgesamt (0 = unbegrenzt).", MAX_CONNECTIONS_PER_IP: "Offene Verbindungen pro Client-IP (0 = unbegrenzt).",
CONNECT_RATE_LIMIT: "Verbindungsversuche pro IP und Minute (0 = unbegrenzt).",
ALLOWED_SERVERS: "Kommagetrennte TeamSpeak-Server, mit denen sich dieses Gateway verbinden darf (*.example.com erlaubt). Leer = alle.",
ALLOWED_ORIGINS: "Kommagetrennte Web-Origins, die dieses Gateway nutzen d\\u00fcrfen (https://client.example.com). Leer = alle.",
SERVER_PICKER: "ALLOWED_SERVERS im Verbindungsdialog als Auswahlliste anbieten.", DEFAULT_SERVER: "Im Verbindungsdialog vorausgef\\u00fcllte Serveradresse.", DEFAULT_CHANNEL: "Im Verbindungsdialog vorausgef\\u00fcllter Kanal.",
FEEDBACK_ENABLED: "Feedback aus der App annehmen (/api/feedback).", STORE_ENABLED: "Design Store betreiben (Theme-Einreichungen und Downloads).", LOG_CONNECTIONS: "Verbindungen protokollieren (gehashte Client-Kennungen, keine Klartext-IPs).",
NEWS_TICKER: "News-Ticker anzeigen.", NEWS_TICKER_CENTRAL: "Offizielle WebSpeak3-News im Ticker mit anzeigen.", NEWS_TICKER_MESSAGES: "Eigene Ticker-Meldungen, getrennt durch |.",
TURNSTILE_SITE_KEY: "Cloudflare-Turnstile-Site-Key (Bot-Check, braucht auch das Secret).", TURNSTILE_SECRET: "Cloudflare-Turnstile-Secret-Key.",
TURN_URLS: "Kommagetrennte TURN-Server-URLs f\\u00fcr Bildschirmfreigabe/Webcam.", TURN_SECRET: "coturn use-auth-secret (Zugangsdaten pro Nutzer).",
TURN_USERNAME: "Fester TURN-Benutzername (ohne TURN_SECRET).", TURN_CREDENTIAL: "Festes TURN-Passwort (ohne TURN_SECRET).",
ENABLE_TS2: "TeamSpeak-2-Server erlauben (experimentell).", TELEMETRY: "Anonymer t\\u00e4glicher Nutzungs-Ping (nur zuf\\u00e4llige Instanz-ID + Version).",
FEEDBACK_ALLOWED_ORIGIN: "Web-Origin, die Feedback senden darf (CORS). * = alle.", GITHUB_TOKEN: "GitHub-Token (Issues: write), um aus Feedback Issues zu machen.",
GITHUB_REPO: "Repository f\\u00fcr Feedback-Issues und die Release-News.", STORE_ALLOWED_ORIGIN: "Web-Origin, die den Store nutzen darf (CORS). * = alle.",
STORE_RESERVED_AUTHORS: "Kommagetrennte Autorennamen, unter denen nur der Store-Admin ver\\u00f6ffentlichen darf.", STORE_ADMIN_TOKEN: "Token zum Freigeben von Store-Einreichungen.",
TRUST_PROXY: "Hinter einem Proxy: \\"cloudflare\\" (CF-Connecting-IP) oder \\"1\\" (letzter X-Forwarded-For). Nur wenn das Gateway ausschlie\\u00dflich dar\\u00fcber erreichbar ist. Leer = aus.",
SYNC_URL: "Eigener Konto-Sync-Server (sync-server.js). Leer = der offizielle.",
PORT: "Port, auf dem das Gateway lauscht. Vorsicht: Bei Docker muss das Port-Mapping passen, sonst ist dieses Panel nicht mehr erreichbar (Abhilfe: PORT aus admin-config.json entfernen).",
TLS_CERT: "Pfad zum TLS-Zertifikat (mit TLS_KEY: HTTPS direkt ausliefern). Ein falscher Pfad verhindert den Start.", TLS_KEY: "Pfad zum privaten TLS-Schl\\u00fcssel.",
WEB_STATIC: "Web-Oberfl\\u00e4che von diesem Gateway ausliefern. Aus, wenn sie woanders gehostet wird.", WEB_DIST: "Ordner mit der gebauten Web-Oberfl\\u00e4che (web/dist).",
STORE_DATA_FILE: "Datendatei des Design Stores.", NEWS_DATA_FILE: "Datei f\\u00fcr News aus dem Stats-Panel.",
FEEDBACK_LOG_FILE: "Privates Feedback-Log (enth\\u00e4lt Kontakt-E-Mails).", TELEMETRY_ID_FILE: "Datei mit der zuf\\u00e4lligen Instanz-ID des Nutzungs-Pings.",
BROADCAST_TOKEN: "Bearer-Token f\\u00fcr /api/broadcast (Hinweise an alle Nutzer). Leer = Endpunkt aus.",
},
},
};
const $ = (id) => document.getElementById(id);
const store = { get(k) { try { return localStorage.getItem(k); } catch { return null; } }, set(k, v) { try { localStorage.setItem(k, v); } catch {} } };
let lang = store.get("wsAdminLang") || (navigator.language.toLowerCase().startsWith("de") ? "de" : "en");
let theme = store.get("wsAdminTheme") || "auto";
let data = null, pending = {};
let current = store.get("wsAdminTab") || "";
// Non-setting pages in the sidebar: [tab key, card id, title string].
const EXTRA = [["__totp", "totpCard", "totpTitle"], ["__pw", "pwCard", "pwTitle"], ["__about", "aboutCard", "aboutTitle"]];
const gname = (g) => t("groups")[g] || g;
const t = (k) => T[lang][k];
const api = (path, body) => fetch("/admin/api/" + path, body === undefined ? {} : {
method: "POST", headers: { "Content-Type": "application/json", "X-WebSpeak3-Admin": "1" }, body: JSON.stringify(body),
}).then(async (r) => ({ status: r.status, data: await r.json().catch(() => ({})) }));
function el(tag, props, ...kids) { const e = Object.assign(document.createElement(tag), props); e.append(...kids); return e; }
function applyChrome() {
document.documentElement.lang = lang;
if (theme === "auto") document.documentElement.removeAttribute("data-theme"); else document.documentElement.dataset.theme = theme;
for (const b of document.querySelectorAll("[data-lang]")) b.setAttribute("aria-pressed", String(b.dataset.lang === lang));
for (const b of document.querySelectorAll("[data-theme-btn]")) b.setAttribute("aria-pressed", String(b.dataset.themeBtn === theme));
for (const e of document.querySelectorAll("[data-i18n]")) e.textContent = t(e.dataset.i18n);
$("search").placeholder = t("search"); $("noHits").textContent = t("noHits");
$("offCode").placeholder = $("totpCheck").placeholder = t("codePh"); $("offPw").placeholder = $("onPw").placeholder = t("pwPh");
}
function updateStatus() {
const n = Object.keys(pending).length;
$("status").textContent = n ? t("unsaved")(n) : "";
}
function render() {
applyChrome();
if (!data) return;
$("login").hidden = true; $("panel").hidden = false; $("logout").hidden = false;
$("totpState").textContent = data.totp === "panel" ? t("totpOnPanel") : data.totp ? t("totpOn") : t("totpOff");
$("totpOffBox").hidden = !data.totp;
$("pwState").textContent = data.passwordFromPanel ? t("pwPanel") : t("pwEnv");
renderStat();
const groups = $("groups"); groups.replaceChildren();
const cards = {};
for (const s of data.settings) {
let card = cards[s.group];
if (!card) {
card = cards[s.group] = el("section", { className: "card" }, el("h2", { textContent: gname(s.group) }), el("div", { className: "rows" }));
card.dataset.group = s.group; groups.append(card);
}
const id = "s_" + s.key;
const help = t("help")[s.key] || s.help;
let input;
if (s.kind === "bool") {
input = el("select", { id }, el("option", { value: "1", textContent: t("on") }), el("option", { value: "0", textContent: t("off") }));
input.value = s.value || s.fallback || "0";
} else {
input = el("input", { id, type: s.kind === "secret" ? "password" : s.kind === "number" ? "number" : "text", autocomplete: "off" });
if (s.kind === "number") input.min = "0";
if (s.kind === "secret") input.placeholder = s.isSet ? t("secretSet") : t("secretUnset");
else { input.value = s.value; if (s.fallback) input.placeholder = t("def") + s.fallback; }
}
if (s.key in pending) input.value = pending[s.key];
const reset = el("button", { type: "button", className: "plain reset", textContent: t("reset"), hidden: !s.overridden });
reset.addEventListener("click", () => save({ [s.key]: null }));
const badge = el("span", { className: "badge" + (s.overridden ? " panel" : ""), textContent: s.overridden ? t("fromPanel") : s.fromEnv ? t("fromEnv") : t("std") });
const row = el("div", { className: "row" + (s.inactive ? " inactive" : "") + (s.key in pending ? " changed" : "") },
el("div", { className: "lab" }, el("label", { htmlFor: id, textContent: s.key }), badge, reset), input, el("div", { className: "help", textContent: help }));
row.dataset.find = (s.key + " " + help + " " + gname(s.group)).toLowerCase();
const mark = () => { pending[s.key] = input.value; row.classList.add("changed"); updateStatus(); renderNav(); };
input.addEventListener("input", mark); input.addEventListener("change", mark);
if (s.inactive) row.append(el("div", { className: "warn", textContent: t("inactive") }));
if (s.pendingRestart) row.append(el("div", { className: "warn", textContent: t("pendingRestart") }));
card.lastChild.append(row);
}
if (!cards[current] && !EXTRA.some(([k]) => k === current)) current = data.settings[0].group;
const due = data.settings.filter((s) => s.pendingRestart).length;
$("restart").textContent = due ? t("restartDue")(due) : t("restartBtn");
$("restart").classList.toggle("due", due > 0);
updateStatus(); renderNav(); applyView();
}
function renderNav() {
const nav = $("nav"); nav.replaceChildren();
const item = (key, label, badge) => {
const b = el("button", { type: "button" }, el("span", { textContent: label }), ...(badge ? [badge] : []));
b.dataset.tab = key;
b.addEventListener("click", () => { current = key; store.set("wsAdminTab", key); $("search").value = ""; applyView(); });
nav.append(b); return b;
};
for (const g of [...new Set(data.settings.map((s) => s.group))]) {
const ss = data.settings.filter((s) => s.group === g);
const unsaved = ss.filter((s) => s.key in pending).length;
const badge = unsaved ? el("span", { className: "cnt", textContent: String(unsaved), title: t("unsaved")(unsaved) })
: ss.some((s) => s.pendingRestart) ? el("span", { className: "cnt warnc", textContent: "!", title: t("pendingRestart") }) : null;
const b = item(g, gname(g), badge);
if (ss.every((s) => s.inactive)) { b.classList.add("muted"); b.title = t("inactive"); }
}
nav.append(el("hr"));
for (const [k, , label] of EXTRA) item(k, t(label));
markCurrent();
}
function markCurrent() {
const q = $("search").value.trim();
for (const b of $("nav").children) if (b.dataset.tab) b.setAttribute("aria-current", String(!q && b.dataset.tab === current));
}
// One page at a time; a search shows matches from every category instead.
function applyView() {
const q = $("search").value.trim().toLowerCase();
let hits = 0;
for (const card of $("groups").children) {
let any = false;
for (const row of card.lastChild.children) { const m = !q || row.dataset.find.includes(q); row.hidden = !m; if (m) { any = true; hits++; } }
card.hidden = q ? !any : card.dataset.group !== current;
}
for (const [k, id] of EXTRA) $(id).hidden = Boolean(q) || current !== k;
$("noHits").hidden = !q || hits > 0;
markCurrent();
}
function renderStat() {
const up = data.uptime, d = Math.floor(up / 86400), h = Math.floor(up / 3600) % 24, m = Math.floor(up / 60) % 60;
const parts = [t("statConn")(data.connections), t("statUp") + (d ? d + " d " : "") + (d || h ? h + " h " : "") + m + " min"];
if (data.version) parts.unshift(t("statVer") + data.version);
$("stat").textContent = parts.join(" \\u00b7 "); $("stat").hidden = false;
}
// Keeps the status line fresh without re-rendering half-typed fields.
setInterval(async () => {
if (!data || document.hidden) return;
const r = await api("config");
if (r.status === 200) { data.connections = r.data.connections; data.uptime = r.data.uptime; renderStat(); }
}, 30000);
// Credential actions: show the server's error next to the form, else take the new state.
async function cred(path, body, errId) {
$(errId).textContent = "";
const r = await api(path, body);
if (r.status === 401) { load(); return false; }
if (r.status !== 200) { $(errId).textContent = t("credErr")[r.data.error] || r.data.error || t("saveFailed"); return false; }
data = r.data; render(); return true;
}
async function load() {
const r = await api("config");
if (r.status === 200) { data = r.data; pending = {}; return render(); }
data = null; applyChrome();
$("panel").hidden = true; $("logout").hidden = true; $("stat").hidden = true; $("login").hidden = false;
}
async function save(changes) {
$("saveErr").textContent = "";
const r = await api("config", { changes });
if (r.status === 401) return load();
if (r.status !== 200) { $("saveErr").textContent = (r.data.key ? r.data.key + ": " : "") + (t("errors")[r.data.error] || r.data.error || t("saveFailed")); return; }
data = r.data; for (const k of Object.keys(changes)) delete pending[k];
render(); $("status").textContent = t("saved");
}
document.querySelectorAll("[data-lang]").forEach((b) => b.addEventListener("click", () => { lang = b.dataset.lang; store.set("wsAdminLang", lang); render(); }));
document.querySelectorAll("[data-theme-btn]").forEach((b) => b.addEventListener("click", () => { theme = b.dataset.themeBtn; store.set("wsAdminTheme", theme); applyChrome(); }));
$("login").addEventListener("submit", async (e) => {
e.preventDefault(); $("loginErr").textContent = "";
const r = await api("login", { user: $("u").value, password: $("p").value, code: $("c").value });
if (r.status === 200) { $("p").value = ""; $("c").value = ""; return load(); }
$("loginErr").textContent = r.status === 429 ? t("tooMany") : t("loginFailed");
});
$("panel").addEventListener("submit", (e) => { e.preventDefault(); if (Object.keys(pending).length) save({ ...pending }); });
$("search").addEventListener("input", applyView);
$("restart").addEventListener("click", async () => {
const fresh = await api("config"); if (fresh.status !== 200) return load();
if (!confirm(t("restartConfirm")(fresh.data.connections))) return;
const r = await api("restart", {});
if (r.status !== 200) { $("saveErr").textContent = t("restartFailed"); return; }
$("restart").disabled = $("save").disabled = true; $("status").textContent = t("restarting");
// Wait for the old process to go away, then until /admin answers again.
await new Promise((ok) => setTimeout(ok, 2000));
for (let i = 0; i < 90; i++) {
try { if ((await fetch("/admin/admin.js", { cache: "no-store" })).ok) return location.reload(); } catch {}
await new Promise((ok) => setTimeout(ok, 2000));
}
$("saveErr").textContent = t("restartFailed");
});
$("logout").addEventListener("click", async () => { await api("logout", {}); load(); });
$("genTotp").addEventListener("click", async () => {
const r = await api("totp-secret"); if (r.status !== 200) return load();
$("totpQr").src = r.data.qr; $("totpSecret").textContent = r.data.secret; $("totpEnv").textContent = "ADMIN_TOTP_SECRET=" + r.data.secret; $("totpOut").hidden = false; $("totpDone").hidden = true; $("totpCheck").value = ""; $("totpCheckErr").textContent = "";
});
$("totpCheckBtn").addEventListener("click", async () => {
$("totpCheckErr").textContent = "";
const r = await api("totp-verify", { secret: $("totpSecret").textContent, code: $("totpCheck").value });
if (r.status === 401) return load();
if (r.status === 200 && r.data.ok) { $("totpDone").hidden = false; return; }
$("totpDone").hidden = true; $("totpCheckErr").textContent = t("codeWrong");
});
$("onBtn").addEventListener("click", async () => {
if (await cred("totp-enable", { secret: $("totpSecret").textContent, code: $("totpCheck").value, password: $("onPw").value }, "onErr")) {
$("onPw").value = ""; $("totpOut").hidden = true; $("totpState").textContent = t("totpDoneOn");
}
});
$("offBtn").addEventListener("click", async () => {
if (await cred("totp-disable", { code: $("offCode").value, password: $("offPw").value }, "offErr")) { $("offCode").value = $("offPw").value = ""; }
});
$("pwBtn").addEventListener("click", async () => {
$("pwErr").textContent = "";
if ($("pwNew").value !== $("pwNew2").value) { $("pwErr").textContent = t("pwMismatch"); return; }
if (await cred("password", { password: $("pwOld").value, newPassword: $("pwNew").value }, "pwErr")) {
$("pwOld").value = $("pwNew").value = $("pwNew2").value = ""; $("pwState").textContent = t("pwDone");
}
});
applyChrome();
load();
`;
+142
View File
@@ -0,0 +1,142 @@
// Settings the admin panel (admin.ts) may change at runtime. Values saved
// there live in ADMIN_CONFIG_FILE and are written onto process.env, both at
// startup and on every save, so code that reads process.env at use time picks
// them up without a restart. Must be imported before any module that reads
// these variables at load time (first import in index.ts).
//
// Settings marked `restart` are only written onto process.env at startup:
// they are read once at load time, so the panel saves them and asks for a
// restart instead of pretending they are live.
//
// Deliberately absent: CONNECTOR_BIN (would be code execution), ALLOW_PRIVATE_TARGETS (opens the
// operator's LAN), and the admin credentials themselves.
import { readFileSync } from "node:fs";
import { rename, writeFile } from "node:fs/promises";
import path from "node:path";
export type SettingKind = "number" | "bool" | "list" | "text" | "secret";
export interface Setting {
key: string;
kind: SettingKind;
/** Value used when neither .env nor the panel sets one (shown as hint). */
fallback?: string;
help: string;
group: string;
/** Only ends up in the UI this gateway serves itself (meta tags). */
staticOnly?: boolean;
/** Read once at startup; changes take effect after a restart. */
restart?: boolean;
}
export const SETTINGS: Setting[] = [
{ group: "Connections & limits", key: "MAX_CONNECTIONS", kind: "number", fallback: "60", help: "Open WebSocket connections in total (0 = unlimited)." },
{ group: "Connections & limits", key: "MAX_CONNECTIONS_PER_IP", kind: "number", fallback: "10", help: "Open connections per client IP (0 = unlimited)." },
{ group: "Connections & limits", key: "CONNECT_RATE_LIMIT", kind: "number", fallback: "30", help: "Connect attempts per IP per minute (0 = unlimited)." },
{ group: "Access", key: "ALLOWED_SERVERS", kind: "list", help: "Comma-separated TeamSpeak servers this gateway may connect to (*.example.com allowed). Empty = any." },
{ group: "Access", key: "ALLOWED_ORIGINS", kind: "list", help: "Comma-separated web origins allowed to use this gateway (https://client.example.com). Empty = any." },
{ group: "Access", key: "TRUST_PROXY", kind: "text", help: "Behind a proxy: \"cloudflare\" (CF-Connecting-IP) or \"1\" (last X-Forwarded-For). Only if the gateway is reachable solely through it. Empty = off." },
{ group: "Connect dialog", staticOnly: true, key: "SERVER_PICKER", kind: "bool", fallback: "1", help: "Offer ALLOWED_SERVERS as a dropdown in the connect dialog." },
{ group: "Connect dialog", staticOnly: true, key: "DEFAULT_SERVER", kind: "text", help: "Server address prefilled in the connect dialog." },
{ group: "Connect dialog", staticOnly: true, key: "DEFAULT_CHANNEL", kind: "text", help: "Channel prefilled in the connect dialog." },
{ group: "Connect dialog", staticOnly: true, key: "SYNC_URL", kind: "text", help: "Own account-sync server (sync-server.js). Empty = the official one." },
{ group: "Features", key: "FEEDBACK_ENABLED", kind: "bool", fallback: "0", help: "Accept in-app feedback (/api/feedback)." },
{ group: "Features", key: "STORE_ENABLED", kind: "bool", fallback: "0", help: "Run the Design Store (theme submissions and downloads)." },
{ group: "Features", key: "LOG_CONNECTIONS", kind: "bool", fallback: "0", help: "Log connects/disconnects (hashed client tags, no plain IPs)." },
{ group: "Features", key: "ENABLE_TS2", kind: "bool", fallback: "0", help: "Allow TeamSpeak 2 servers (experimental)." },
{ group: "Features", key: "TELEMETRY", kind: "bool", fallback: "1", help: "Anonymous daily usage ping (random instance id + version only)." },
{ group: "Feedback & GitHub", key: "FEEDBACK_ALLOWED_ORIGIN", kind: "text", fallback: "*", help: "Web origin allowed to send feedback (CORS). * = any." },
{ group: "Feedback & GitHub", key: "GITHUB_TOKEN", kind: "secret", help: "GitHub token (Issues: write) to turn feedback into issues." },
{ group: "Feedback & GitHub", key: "GITHUB_REPO", kind: "text", fallback: "WebSpeak3/webspeak3", help: "Repository for feedback issues and the release news." },
{ group: "Design Store", key: "STORE_ALLOWED_ORIGIN", kind: "text", fallback: "*", help: "Web origin allowed to use the store (CORS). * = any." },
{ group: "Design Store", key: "STORE_RESERVED_AUTHORS", kind: "list", fallback: "Möpchi,Moepchi", help: "Comma-separated author names only the store admin may publish under." },
{ group: "Design Store", key: "STORE_ADMIN_TOKEN", kind: "secret", help: "Token for approving store submissions." },
{ group: "Access", key: "BROADCAST_TOKEN", kind: "secret", help: "Bearer token for /api/broadcast (notices to all users). Empty = endpoint off." },
{ group: "News ticker", key: "NEWS_TICKER", kind: "bool", fallback: "1", help: "Show the news ticker." },
{ group: "News ticker", key: "NEWS_TICKER_CENTRAL", kind: "bool", fallback: "1", help: "Include the official WebSpeak3 news in the ticker." },
{ group: "News ticker", key: "NEWS_TICKER_MESSAGES", kind: "text", help: "Own ticker messages, separated by |." },
{ group: "Bot check (Cloudflare Turnstile)", key: "TURNSTILE_SITE_KEY", kind: "text", help: "Cloudflare Turnstile site key (bot check, needs the secret too)." },
{ group: "Bot check (Cloudflare Turnstile)", key: "TURNSTILE_SECRET", kind: "secret", help: "Cloudflare Turnstile secret key." },
{ group: "Screen sharing & webcam (TURN)", key: "TURN_URLS", kind: "list", help: "Comma-separated TURN server URLs for screen sharing/webcam." },
{ group: "Screen sharing & webcam (TURN)", key: "TURN_SECRET", kind: "secret", help: "coturn use-auth-secret (per-user credentials)." },
{ group: "Screen sharing & webcam (TURN)", key: "TURN_USERNAME", kind: "text", help: "Static TURN username (if no TURN_SECRET)." },
{ group: "Screen sharing & webcam (TURN)", key: "TURN_CREDENTIAL", kind: "secret", help: "Static TURN password (if no TURN_SECRET)." },
{ group: "Restart required", restart: true, key: "PORT", kind: "number", fallback: "8080", help: "Port the gateway listens on. Careful: with Docker the port mapping must match, or this panel becomes unreachable (fix: remove PORT from admin-config.json)." },
{ group: "Restart required", restart: true, key: "TLS_CERT", kind: "text", help: "Path to the TLS certificate (with TLS_KEY: serve HTTPS directly). A wrong path stops the gateway from starting." },
{ group: "Restart required", restart: true, key: "TLS_KEY", kind: "text", help: "Path to the TLS private key." },
{ group: "Restart required", restart: true, key: "WEB_STATIC", kind: "bool", fallback: "1", help: "Serve the web UI from this gateway. Off when the UI is hosted elsewhere." },
{ group: "Restart required", restart: true, key: "WEB_DIST", kind: "text", help: "Folder with the built web UI (web/dist)." },
{ group: "Restart required", restart: true, key: "STORE_DATA_FILE", kind: "text", fallback: "./store-themes.json", help: "Design Store data file." },
{ group: "Restart required", restart: true, key: "NEWS_DATA_FILE", kind: "text", help: "File for news managed in the stats panel." },
{ group: "Restart required", restart: true, key: "FEEDBACK_LOG_FILE", kind: "text", fallback: "./feedback.log", help: "Private feedback log (includes contact e-mails)." },
{ group: "Restart required", restart: true, key: "TELEMETRY_ID_FILE", kind: "text", fallback: "./instance-id", help: "File holding the random instance id of the usage ping." },
];
const BY_KEY = new Map(SETTINGS.map((s) => [s.key, s]));
const CONFIG_FILE = process.env.ADMIN_CONFIG_FILE ?? path.resolve(process.cwd(), "admin-config.json");
// What .env said before any override, so "reset" can go back to it.
const ENV_DEFAULTS = new Map(SETTINGS.map((s) => [s.key, process.env[s.key]]));
let overrides: Record<string, string> = {};
const configured = (key: string) => (key in overrides ? overrides[key] : ENV_DEFAULTS.get(key));
function apply(startup = false): void {
for (const { key, restart } of SETTINGS) {
if (restart && !startup) continue;
const value = configured(key);
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
}
try {
const raw = JSON.parse(readFileSync(CONFIG_FILE, "utf8")) as Record<string, unknown>;
for (const [key, value] of Object.entries(raw)) {
if (BY_KEY.has(key) && typeof value === "string") overrides[key] = value;
}
apply(true);
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== "ENOENT") console.error(`[admin] Ignoring unreadable ${CONFIG_FILE}:`, err);
}
/** Returns an error message, or undefined when the value is acceptable. */
export function validate(key: string, value: string): string | undefined {
const setting = BY_KEY.get(key);
if (!setting) return "unknown setting";
if (value.length > 4000 || /[\p{Cc}]/u.test(value)) return "invalid characters or too long";
if (setting.kind === "number" && !/^\d{1,6}$/.test(value)) return "must be a whole number";
if (setting.kind === "bool" && value !== "0" && value !== "1") return "must be 0 or 1";
if (key === "PORT" && value !== "" && (Number(value) < 1 || Number(value) > 65535)) return "must be a port (1-65535)";
return undefined;
}
/** Applies and persists changes. `null` removes the override (back to .env). */
export async function updateSettings(changes: Record<string, string | null>): Promise<void> {
const next = { ...overrides };
for (const [key, value] of Object.entries(changes)) {
if (value === null) delete next[key];
else next[key] = value;
}
const tmp = `${CONFIG_FILE}.${process.pid}.tmp`;
await writeFile(tmp, JSON.stringify(next, null, 2), { mode: 0o600 });
await rename(tmp, CONFIG_FILE);
overrides = next;
apply();
}
/** Panel view: secrets only say whether they are set. */
export function describeSettings() {
return SETTINGS.map((s) => {
// Restart settings show what is saved, not what the process still runs with.
const value = s.restart ? configured(s.key) : process.env[s.key];
return {
...s,
overridden: s.key in overrides,
fromEnv: ENV_DEFAULTS.get(s.key) !== undefined,
value: s.kind === "secret" ? undefined : (value ?? ""),
isSet: Boolean(value),
// With WEB_STATIC=0 the UI comes from elsewhere and never sees these.
inactive: Boolean(s.staticOnly && process.env.WEB_STATIC === "0"),
pendingRestart: Boolean(s.restart && (value ?? "") !== (process.env[s.key] ?? "")),
};
});
}
+63 -43
View File
@@ -1,3 +1,4 @@
import "./config.js"; // first: applies admin-panel overrides to process.env
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
import { createServer as createSecureServer } from "node:https";
import { readFileSync } from "node:fs";
@@ -9,17 +10,18 @@ import { WebSocketServer, type WebSocket } from "ws";
import { Ts3Connection, type ServerType, type Ts3ConnectOptions } from "./ts3/connection.js";
import { handleStore } from "./store.js";
import { handleNewsConfig } from "./news.js";
import { handleAdmin, setConnectionCounter } from "./admin.js";
import { logFeedback, logConnectionEvent } from "./activity-log.js";
import { clientIp, createRateLimiter } from "./net.js";
import { TURNSTILE_ENABLED, TURNSTILE_SITE_KEY, isValidPass, issuePass, verifyToken } from "./turnstile.js";
import { turnstileEnabled, turnstileSiteKey, isValidPass, issuePass, verifyToken } from "./turnstile.js";
import "./telemetry.js";
// TeamSpeak 2 support is experimental and opt-in (ENABLE_TS2=1).
const TS2_ENABLED = process.env.ENABLE_TS2 === "1";
const SERVER_TYPES = new Set<ServerType>(["teamspeak", "teaspeak", "auto", ...(TS2_ENABLED ? (["ts2"] as const) : [])]);
const ts2Enabled = () => process.env.ENABLE_TS2 === "1";
const serverTypes = () => new Set<ServerType>(["teamspeak", "teaspeak", "auto", ...(ts2Enabled() ? (["ts2"] as const) : [])]);
function parseServerType(value: unknown): ServerType | undefined {
return typeof value === "string" && SERVER_TYPES.has(value as ServerType)
return typeof value === "string" && serverTypes().has(value as ServerType)
? (value as ServerType)
: undefined;
}
@@ -34,10 +36,13 @@ function parsePrivilegeKey(msg: { privilegeKey?: unknown; token?: unknown }): st
// Self-hosting admins may want to lock their instance to specific server(s)
// instead of leaving it as a general-purpose "connect anywhere" client.
// Unset (the default) means unrestricted, unchanged from before this existed.
const ALLOWED_SERVERS = (process.env.ALLOWED_SERVERS ?? "")
.split(",")
.map((s) => s.trim().toLowerCase())
.filter(Boolean);
// Live settings below are functions, not constants: the admin panel
// (config.ts/admin.ts) can change process.env while the gateway runs.
const allowedServers = () =>
(process.env.ALLOWED_SERVERS ?? "")
.split(",")
.map((s) => s.trim().toLowerCase())
.filter(Boolean);
// Strips a trailing ":port" so "ts.example.com:9987" and "ts.example.com"
// both match a plain "ts.example.com" allowlist entry. IPv6 literals come
@@ -53,6 +58,7 @@ function stripPort(host: string): string {
}
function isServerAllowed(host: unknown): boolean {
const ALLOWED_SERVERS = allowedServers();
if (ALLOWED_SERVERS.length === 0) return true;
if (typeof host !== "string") return false;
const target = stripPort(host).trim().toLowerCase();
@@ -88,24 +94,25 @@ function isValidNickname(nickname: unknown): nickname is string {
// Unset keeps the old "any origin" behavior for self-hosters; entries are
// full origins ("https://client.example.com"). Non-browser clients send no
// Origin header at all - they could fake one anyway, so they aren't blocked.
const ALLOWED_ORIGINS = (process.env.ALLOWED_ORIGINS ?? "")
.split(",")
.map((s) => s.trim().replace(/\/+$/, "").toLowerCase())
.filter(Boolean);
const allowedOrigins = () =>
(process.env.ALLOWED_ORIGINS ?? "")
.split(",")
.map((s) => s.trim().replace(/\/+$/, "").toLowerCase())
.filter(Boolean);
// Each /ws socket can spawn a connector process holding a live TeamSpeak
// session, so both need a ceiling. 0 disables the respective limit.
const MAX_CONNECTIONS_PER_IP = Number(process.env.MAX_CONNECTIONS_PER_IP ?? 10);
const maxConnectionsPerIp = () => Number(process.env.MAX_CONNECTIONS_PER_IP ?? 10);
// Measured 2026-10-25: ~8-9 % of a core per client listening to constant audio
// (connector + node + cloudflared), ~10-15 MB RAM. 60 suits a 2-core VPS.
const MAX_CONNECTIONS = Number(process.env.MAX_CONNECTIONS ?? 60);
const maxConnections = () => Number(process.env.MAX_CONNECTIONS ?? 60);
const connectionsPerIp = new Map<string, number>();
// Every "connect" spawns a fresh connector, and a socket can send any number
// of them - without this, 10 sockets were enough to join-flood a TS server
// through this gateway at several joins per second. Real clients (reconnect
// backoff, restoring a handful of tabs) stay far below it.
const CONNECT_RATE_LIMIT = Number(process.env.CONNECT_RATE_LIMIT ?? 30);
const isConnectRateLimited = createRateLimiter(CONNECT_RATE_LIMIT, 60_000);
const connectRateLimit = () => Number(process.env.CONNECT_RATE_LIMIT ?? 30);
const isConnectRateLimited = createRateLimiter(connectRateLimit, 60_000);
// Largest single WS message. Uploads now arrive in small "uploadFileChunk"
// pieces; this size only stays for browsers still running a client from
@@ -127,15 +134,16 @@ const WEB_DIST = process.env.WEB_DIST ?? path.resolve(__dirname, "../../web/dist
const SERVE_STATIC = process.env.WEB_STATIC !== "0";
// Sent to every browser in the "hello" message on socket open; see there.
const GATEWAY_FEATURES = ["clientVolume", "keepalive", "chunkedUpload", "whisperPolicy", "transferLimit", ...(TS2_ENABLED ? ["ts2"] : [])];
const gatewayFeatures = () => ["clientVolume", "keepalive", "chunkedUpload", "whisperPolicy", "transferLimit", ...(ts2Enabled() ? ["ts2"] : [])];
// Optional TURN relay for screen streams, also handed out in "hello". With
// TURN_SECRET (coturn's use-auth-secret) every socket gets its own credential,
// valid for a day; TURN_USERNAME/TURN_CREDENTIAL are static and so readable by
// every visitor. ponytail: a socket open longer than a day hands an expired
// credential to a stream started after that; reconnecting fixes it.
const TURN_URLS = (process.env.TURN_URLS ?? "").split(",").map((u) => u.trim()).filter(Boolean);
function turnServers() {
const TURN_URLS = (process.env.TURN_URLS ?? "").split(",").map((u) => u.trim()).filter(Boolean);
if (!TURN_URLS.length) return undefined;
const secret = process.env.TURN_SECRET;
if (!secret) {
@@ -152,11 +160,14 @@ function turnServers() {
// The allowlist goes along so the client can offer it as a dropdown (or a
// locked field for a single entry) instead of a free-text address; not with
// wildcards, which can't be listed, nor with SERVER_PICKER=0.
const SERVER_PICKER =
process.env.SERVER_PICKER !== "0" && !ALLOWED_SERVERS.some((e) => e.startsWith("*."))
? ALLOWED_SERVERS.join(",")
: undefined;
const RUNTIME_CONFIG_META = Object.entries({
// Built per request, since the admin panel can change these at runtime.
const runtimeConfigMeta = () => {
const ALLOWED_SERVERS = allowedServers();
const SERVER_PICKER =
process.env.SERVER_PICKER !== "0" && !ALLOWED_SERVERS.some((e) => e.startsWith("*."))
? ALLOWED_SERVERS.join(",")
: undefined;
return Object.entries({
"webspeak3-default-server": process.env.DEFAULT_SERVER,
"webspeak3-default-channel": process.env.DEFAULT_CHANNEL,
"webspeak3-allowed-servers": SERVER_PICKER,
@@ -167,6 +178,7 @@ const RUNTIME_CONFIG_META = Object.entries({
value ? [`<meta name="${name}" content="${value.replace(/[&"<>]/g, (c) => `&#${c.charCodeAt(0)};`)}">`] : [],
)
.join("");
};
const MIME_TYPES: Record<string, string> = {
".html": "text/html",
@@ -190,16 +202,15 @@ const MIME_TYPES: Record<string, string> = {
// (see IS_OWN_HOSTED_INSTANCE in web/src/App.tsx), but this endpoint
// enforces its own opt-in independently since it's reachable directly over
// HTTP by anyone who finds the gateway's address, not just from that UI.
const FEEDBACK_ENABLED = process.env.FEEDBACK_ENABLED === "1";
// The web app is commonly hosted on a different origin than the gateway
// (see mem:deployment / GATEWAY_URL in web/), so this needs its own CORS
// allowance; default "*" since the endpoint is opt-in, rate-limited, and
// only ever accepts a feedback submission, nothing that reads data back.
const FEEDBACK_ALLOWED_ORIGIN = process.env.FEEDBACK_ALLOWED_ORIGIN ?? "*";
const feedbackAllowedOrigin = () => process.env.FEEDBACK_ALLOWED_ORIGIN || "*";
// A token with "Issues: write" access on the target repo. Without it,
// feedback is still accepted and logged, just never turned into an issue.
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const GITHUB_REPO = process.env.GITHUB_REPO ?? "WebSpeak3/webspeak3";
const githubRepo = () => process.env.GITHUB_REPO || "WebSpeak3/webspeak3";
const FEEDBACK_CATEGORIES = new Set(["bug", "idea", "report", "other"]);
const FEEDBACK_MESSAGE_MAX_LENGTH = 4000;
@@ -210,18 +221,18 @@ const FEEDBACK_MESSAGE_MAX_LENGTH = 4000;
// tab (reuses the same {type:"notice"} message the SIGTERM handler already
// sends, which the frontend renders even without a messageKey). Unset
// BROADCAST_TOKEN and the endpoint 404s, same as feedback when disabled.
const BROADCAST_TOKEN = process.env.BROADCAST_TOKEN;
const BROADCAST_MESSAGE_MAX_LENGTH = 500;
function isValidBroadcastToken(provided: string): boolean {
if (!BROADCAST_TOKEN) return false;
const token = process.env.BROADCAST_TOKEN;
if (!token) return false;
const a = Buffer.from(provided);
const b = Buffer.from(BROADCAST_TOKEN);
const b = Buffer.from(token);
return a.length === b.length && timingSafeEqual(a, b);
}
async function handleBroadcast(req: IncomingMessage, res: ServerResponse) {
if (!BROADCAST_TOKEN) {
if (!process.env.BROADCAST_TOKEN) {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "not_found" }));
return;
@@ -300,7 +311,8 @@ function neutralizeMentions(text: string): string {
// The contact e-mail is deliberately *not* part of this: it's only kept in
// the private feedback log (logFeedback), never in the public issue.
async function createGithubIssue(payload: { category: string; message: string }): Promise<string | undefined> {
if (!GITHUB_TOKEN) return undefined;
const githubToken = process.env.GITHUB_TOKEN;
if (!githubToken) return undefined;
const title = neutralizeMentions(
`[Feedback/${payload.category}] ${payload.message.replace(/\s+/g, " ").trim().slice(0, 72)}`,
);
@@ -316,10 +328,10 @@ async function createGithubIssue(payload: { category: string; message: string })
"_Submitted via the in-client feedback form._",
].join("\n");
const res = await fetch(`https://api.github.com/repos/${GITHUB_REPO}/issues`, {
const res = await fetch(`https://api.github.com/repos/${githubRepo()}/issues`, {
method: "POST",
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
Authorization: `Bearer ${githubToken}`,
Accept: "application/vnd.github+json",
"Content-Type": "application/json",
"User-Agent": "webspeak3-gateway",
@@ -337,7 +349,7 @@ async function createGithubIssue(payload: { category: string; message: string })
}
async function handleFeedback(req: IncomingMessage, res: ServerResponse) {
res.setHeader("Access-Control-Allow-Origin", FEEDBACK_ALLOWED_ORIGIN);
res.setHeader("Access-Control-Allow-Origin", feedbackAllowedOrigin());
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
@@ -346,7 +358,7 @@ async function handleFeedback(req: IncomingMessage, res: ServerResponse) {
res.end();
return;
}
if (!FEEDBACK_ENABLED) {
if (process.env.FEEDBACK_ENABLED !== "1") {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "not_found" }));
return;
@@ -491,13 +503,17 @@ const requestHandler = (req: IncomingMessage, res: ServerResponse) => {
// Same list as the "hello" message, readable before any connect
// (the connect dialog offers TeamSpeak 2 only when it is enabled).
res.writeHead(200, { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Cache-Control": "public, max-age=300" });
res.end(JSON.stringify({ features: GATEWAY_FEATURES, ...(TURNSTILE_ENABLED ? { turnstileSiteKey: TURNSTILE_SITE_KEY } : {}) }));
res.end(JSON.stringify({ features: gatewayFeatures(), ...(turnstileEnabled() ? { turnstileSiteKey: turnstileSiteKey() } : {}) }));
return;
}
if (requestUrl.pathname === "/api/news") {
handleNewsConfig(res);
return;
}
if (requestUrl.pathname === "/admin" || requestUrl.pathname.startsWith("/admin/")) {
await handleAdmin(req, res, requestUrl.pathname);
return;
}
if (requestUrl.pathname === "/api/broadcast") {
await handleBroadcast(req, res);
return;
@@ -551,9 +567,10 @@ const requestHandler = (req: IncomingMessage, res: ServerResponse) => {
filePath = path.join(WEB_DIST, "index.html");
body = await readFile(filePath);
}
if (RUNTIME_CONFIG_META && filePath === path.join(WEB_DIST, "index.html")) {
const meta = filePath === path.join(WEB_DIST, "index.html") ? runtimeConfigMeta() : "";
if (meta) {
// Function replacer: a string one would expand "$&" etc. in the values.
body = Buffer.from(body.toString("utf8").replace("</head>", () => `${RUNTIME_CONFIG_META}</head>`));
body = Buffer.from(body.toString("utf8").replace("</head>", () => `${meta}</head>`));
}
const ext = path.extname(filePath);
res.writeHead(200, {
@@ -579,10 +596,12 @@ const wss = new WebSocketServer({
path: "/ws",
maxPayload: WS_MAX_PAYLOAD,
verifyClient: ({ origin, req }, done) => {
const ALLOWED_ORIGINS = allowedOrigins();
if (ALLOWED_ORIGINS.length > 0 && origin && !ALLOWED_ORIGINS.includes(origin.toLowerCase())) {
return done(false, 403, "Origin not allowed");
}
const ip = clientIp(req);
const MAX_CONNECTIONS_PER_IP = maxConnectionsPerIp();
if (MAX_CONNECTIONS_PER_IP > 0 && (connectionsPerIp.get(ip) ?? 0) >= MAX_CONNECTIONS_PER_IP) {
console.warn(`[ratelimit] too many open connections from one IP (limit ${MAX_CONNECTIONS_PER_IP})`);
return done(false, 429, "Too many connections");
@@ -590,6 +609,7 @@ const wss = new WebSocketServer({
done(true);
},
});
setConnectionCounter(() => wss.clients.size);
// Heartbeat: a browser tab that loses its network mid-session (mobile
// handover, Wi-Fi drop, backgrounded/frozen tab) often never sends a proper
@@ -690,7 +710,7 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
let connection: Ts3Connection | undefined;
// Full: accept and say so, since a browser can't read why an upgrade was refused.
if (MAX_CONNECTIONS > 0 && wss.clients.size > MAX_CONNECTIONS) {
if (maxConnections() > 0 && wss.clients.size > maxConnections()) {
socket.send(JSON.stringify({ type: "error", code: "gateway_full", message: "This gateway is full right now. Please try again in a few minutes." }));
socket.close(1013, "Gateway is full");
return;
@@ -703,7 +723,7 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
// pulled, so the browser must not assume a feature exists just because it
// knows about it. A gateway without "hello" (before v0.15) supports the
// v0.14 feature set, streaming included; anything newer gets a name here.
socket.send(JSON.stringify({ type: "hello", features: GATEWAY_FEATURES, iceServers: turnServers() }));
socket.send(JSON.stringify({ type: "hello", features: gatewayFeatures(), iceServers: turnServers() }));
heartbeats.set(socket, { isAlive: true });
socket.on("pong", () => {
@@ -749,14 +769,14 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
);
break;
}
if (CONNECT_RATE_LIMIT > 0 && isConnectRateLimited(ip)) {
if (connectRateLimit() > 0 && isConnectRateLimited(ip)) {
console.warn(`[ratelimit] too many connects from one IP, target host=${msg.host}`);
socket.send(
JSON.stringify({ type: "error", message: "Too many connection attempts. Please wait a minute." }),
);
break;
}
if (TURNSTILE_ENABLED && !isValidPass(msg.turnstilePass, ip)) {
if (turnstileEnabled() && !isValidPass(msg.turnstilePass, ip)) {
// No (valid) pass yet: ask the browser to solve the check, it
// resends this connect with the token.
if (msg.turnstileToken === undefined) {
+4 -4
View File
@@ -6,9 +6,9 @@ import type { IncomingMessage } from "node:http";
// - "cloudflare": CF-Connecting-IP, which Cloudflare always sets itself.
// - "1": the last X-Forwarded-For entry, the one our own proxy appended;
// anything before it came from the client and can be made up.
const TRUST_PROXY = process.env.TRUST_PROXY;
export function clientIp(req: IncomingMessage): string {
// Read per request: the admin panel can change it live.
const TRUST_PROXY = process.env.TRUST_PROXY;
if (TRUST_PROXY === "cloudflare") {
const cf = req.headers["cf-connecting-ip"];
if (typeof cf === "string" && cf.trim()) return cf.trim();
@@ -22,7 +22,7 @@ export function clientIp(req: IncomingMessage): string {
/** Sliding-window "N per window per key" limiter. Expired keys are swept
* periodically so the map can't grow without bound under a stream of
* one-shot IPs. */
export function createRateLimiter(limit: number, windowMs: number): (key: string) => boolean {
export function createRateLimiter(limit: number | (() => number), windowMs: number): (key: string) => boolean {
const hits = new Map<string, number[]>();
setInterval(() => {
const now = Date.now();
@@ -35,6 +35,6 @@ export function createRateLimiter(limit: number, windowMs: number): (key: string
const recent = (hits.get(key) ?? []).filter((t) => now - t < windowMs);
recent.push(now);
hits.set(key, recent);
return recent.length > limit;
return recent.length > (typeof limit === "function" ? limit() : limit);
};
}
+15 -10
View File
@@ -24,7 +24,6 @@ type NewsItem =
type TextItem = Extract<NewsItem, { kind: "text" }>;
const NEWS_DATA_FILE = process.env.NEWS_DATA_FILE;
const GITHUB_REPO = process.env.GITHUB_REPO ?? "WebSpeak3/webspeak3";
const MAX_ITEMS = 20;
const MAX_TEXT = 300;
const AUTO_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
@@ -57,7 +56,7 @@ async function latestRelease(): Promise<NewsItem | null> {
if (Date.now() - release.fetchedAt < 60 * 60 * 1000) return release.item;
release.fetchedAt = Date.now();
try {
const res = await fetch(`https://api.github.com/repos/${GITHUB_REPO}/releases?per_page=1`, {
const res = await fetch(`https://api.github.com/repos/${process.env.GITHUB_REPO || "WebSpeak3/webspeak3"}/releases?per_page=1`, {
headers: { Accept: "application/vnd.github+json", "User-Agent": "webspeak3-store" },
});
if (!res.ok) return release.item;
@@ -135,16 +134,22 @@ export async function handleNewsFeed(req: IncomingMessage, res: ServerResponse,
json(res, 400, { error: "bad_request" });
}
const NEWS_TICKER = process.env.NEWS_TICKER !== "0";
const NEWS_TICKER_CENTRAL = process.env.NEWS_TICKER_CENTRAL !== "0";
const LOCAL_ITEMS: NewsItem[] = (process.env.NEWS_TICKER_MESSAGES ?? "")
.split("|")
.map((text) => text.trim().slice(0, MAX_TEXT))
.filter(Boolean)
.map((text, i) => ({ id: `local-${i}-${text}`, kind: "text", text, at: "" }));
// Read per request: the admin panel (config.ts) can change these at runtime.
const localItems = (): NewsItem[] =>
(process.env.NEWS_TICKER_MESSAGES ?? "")
.split("|")
.map((text) => text.trim().slice(0, MAX_TEXT))
.filter(Boolean)
.map((text, i) => ({ id: `local-${i}-${text}`, kind: "text", text, at: "" }));
export function handleNewsConfig(res: ServerResponse) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Cache-Control", "public, max-age=300");
json(res, 200, NEWS_TICKER ? { enabled: true, central: NEWS_TICKER_CENTRAL, items: LOCAL_ITEMS } : { enabled: false });
json(
res,
200,
process.env.NEWS_TICKER !== "0"
? { enabled: true, central: process.env.NEWS_TICKER_CENTRAL !== "0", items: localItems() }
: { enabled: false },
);
}
+8 -9
View File
@@ -16,8 +16,7 @@ import { timingSafeEqual, randomUUID } from "node:crypto";
import path from "node:path";
import { clientIp, createRateLimiter } from "./net.js";
const STORE_ENABLED = process.env.STORE_ENABLED === "1";
const STORE_ALLOWED_ORIGIN = process.env.STORE_ALLOWED_ORIGIN ?? "*";
const storeAllowedOrigin = () => process.env.STORE_ALLOWED_ORIGIN || "*";
// Plain JSON file (whole-array read-modify-write, not JSON-lines like
// feedback.log in index.ts): the store needs to list and look up by id, and
// the expected volume (community theme submissions) is small enough that
@@ -42,18 +41,18 @@ const STORE_BASE_THEMES = new Set(["standard", "nova", "pulse", "ide"]);
// STORE_ADMIN_TOKEN may publish under - stops anyone else from impersonating
// the maintainer in the author field. Everyone else's name is first-come,
// unenforced, same as any other free-text field here.
const STORE_RESERVED_AUTHORS = new Set(
const storeReservedAuthors = () => new Set(
(process.env.STORE_RESERVED_AUTHORS ?? "Möpchi,Moepchi")
.split(",")
.map((s) => s.trim().toLowerCase())
.filter(Boolean)
);
const STORE_ADMIN_TOKEN = process.env.STORE_ADMIN_TOKEN;
function isValidStoreAdminToken(provided: string): boolean {
if (!STORE_ADMIN_TOKEN) return false;
const token = process.env.STORE_ADMIN_TOKEN;
if (!token) return false;
const a = Buffer.from(provided);
const b = Buffer.from(STORE_ADMIN_TOKEN);
const b = Buffer.from(token);
return a.length === b.length && timingSafeEqual(a, b);
}
@@ -165,7 +164,7 @@ const isStoreRateLimited = createRateLimiter(STORE_RATE_LIMIT, STORE_RATE_WINDOW
const storeRatingsSeen = new Set<string>();
function setStoreCors(res: ServerResponse) {
res.setHeader("Access-Control-Allow-Origin", STORE_ALLOWED_ORIGIN);
res.setHeader("Access-Control-Allow-Origin", storeAllowedOrigin());
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
}
@@ -346,7 +345,7 @@ async function handleStoreSubmit(req: IncomingMessage, res: ServerResponse) {
}
const isAdmin = isAdminRequest(req);
if (STORE_RESERVED_AUTHORS.has(author.toLowerCase())) {
if (storeReservedAuthors().has(author.toLowerCase())) {
if (!isAdmin) {
res.writeHead(403, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "author_reserved" }));
@@ -506,7 +505,7 @@ export async function handleStore(req: IncomingMessage, res: ServerResponse, pat
res.end();
return;
}
if (!STORE_ENABLED) {
if (process.env.STORE_ENABLED !== "1") {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "not_found" }));
return;
+9 -6
View File
@@ -33,12 +33,17 @@ async function instanceId(): Promise<string> {
return id;
}
async function ping(id: string) {
let id: Promise<string> | undefined;
async function ping() {
// Checked on every ping: the admin panel can switch it on/off live.
if (process.env.TELEMETRY === "0") return;
id ??= instanceId();
try {
await fetch(TELEMETRY_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id, version: VERSION }),
body: JSON.stringify({ id: await id, version: VERSION }),
signal: AbortSignal.timeout(10_000),
});
} catch {
@@ -50,8 +55,6 @@ if (process.env.TELEMETRY !== "0") {
console.log(
`Sending an anonymous usage ping (random instance id + version ${VERSION}) to ${new URL(TELEMETRY_URL).host} at startup and once a day. Disable with TELEMETRY=0.`
);
void instanceId().then((id) => {
void ping(id);
setInterval(() => void ping(id), DAY_MS).unref();
});
}
void ping();
setInterval(() => void ping(), DAY_MS).unref();
+7 -6
View File
@@ -5,16 +5,17 @@
// most once in that window.
import { createHmac, timingSafeEqual } from "node:crypto";
export const TURNSTILE_SITE_KEY = process.env.TURNSTILE_SITE_KEY ?? "";
const SECRET = process.env.TURNSTILE_SECRET ?? "";
export const TURNSTILE_ENABLED = TURNSTILE_SITE_KEY !== "" && SECRET !== "";
// Read at use time: the admin panel (config.ts) can change both at runtime.
export const turnstileSiteKey = () => process.env.TURNSTILE_SITE_KEY ?? "";
const secret = () => process.env.TURNSTILE_SECRET ?? "";
export const turnstileEnabled = () => turnstileSiteKey() !== "" && secret() !== "";
const PASS_TTL_MS = 12 * 60 * 60 * 1000;
// Derived from the secret, so passes survive a gateway restart.
const PASS_KEY = createHmac("sha256", SECRET).update("webspeak3-turnstile-pass").digest();
const passKey = () => createHmac("sha256", secret()).update("webspeak3-turnstile-pass").digest();
const sign = (ip: string, expires: number) =>
createHmac("sha256", PASS_KEY).update(`${ip}|${expires}`).digest("base64url");
createHmac("sha256", passKey()).update(`${ip}|${expires}`).digest("base64url");
export function issuePass(ip: string): string {
const expires = Date.now() + PASS_TTL_MS;
@@ -36,7 +37,7 @@ export async function verifyToken(token: unknown, ip: string): Promise<boolean>
try {
const res = await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
method: "POST",
body: new URLSearchParams({ secret: SECRET, response: token, remoteip: ip }),
body: new URLSearchParams({ secret: secret(), response: token, remoteip: ip }),
signal: AbortSignal.timeout(10_000),
});
const data = (await res.json()) as { success?: boolean; "error-codes"?: string[] };
+98
View File
@@ -0,0 +1,98 @@
// Admin panel login and credential changes, against the built gateway
// (npm test builds first). Runs a real process on a free port in a temp dir.
import { test } from "node:test";
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import { createHmac } from "node:crypto";
import { mkdtempSync, rmSync } from "node:fs";
import { createServer } from "node:net";
import { tmpdir } from "node:os";
import path from "node:path";
const INDEX = path.resolve(import.meta.dirname, "../dist/index.js");
function totp(secretB32, step) {
let bits = "";
for (const c of secretB32) bits += "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".indexOf(c).toString(2).padStart(5, "0");
const key = Buffer.from(bits.match(/.{8}/g).map((b) => parseInt(b, 2)));
const msg = Buffer.alloc(8);
msg.writeBigUInt64BE(BigInt(step));
const h = createHmac("sha1", key).update(msg).digest();
return String((h.readUInt32BE(h[19] & 15) & 0x7fffffff) % 1e6).padStart(6, "0");
}
const freePort = () => new Promise((ok) => {
const s = createServer().listen(0, () => { const p = s.address().port; s.close(() => ok(p)); });
});
async function startGateway(dir, port) {
const child = spawn(process.execPath, [INDEX], {
cwd: dir,
env: { PATH: process.env.PATH, PORT: String(port), WEB_STATIC: "0", TELEMETRY: "0", ADMIN_USER: "admin", ADMIN_PASSWORD: "env-password" },
stdio: "ignore",
});
for (let i = 0; i < 50; i++) {
try { await fetch(`http://127.0.0.1:${port}/admin/admin.js`); return child; } catch { await new Promise((r) => setTimeout(r, 100)); }
}
child.kill();
throw new Error("gateway did not start");
}
const stop = (child) => new Promise((ok) => { child.once("exit", ok); child.kill("SIGTERM"); });
function client(port) {
let cookie = "";
return async (p, body) => {
const r = await fetch(`http://127.0.0.1:${port}/admin/api/${p}`, {
method: body === undefined ? "GET" : "POST",
headers: { "Content-Type": "application/json", "X-WebSpeak3-Admin": "1", Cookie: cookie },
body: body === undefined ? undefined : JSON.stringify(body),
});
const set = r.headers.get("set-cookie");
if (set) cookie = set.split(";")[0];
return { status: r.status, data: await r.json().catch(() => ({})) };
};
}
test("password and 2FA set in the panel", async () => {
const dir = mkdtempSync(path.join(tmpdir(), "ws3-admin-"));
const port = await freePort();
let gw = await startGateway(dir, port);
try {
const now = Math.floor(Date.now() / 30000);
const a = client(port);
// Mind the limit of 5 login attempts per IP per process start.
assert.equal((await a("login", { user: "admin", password: "env-password" })).status, 200);
const { secret } = (await a("totp-secret")).data;
assert.equal((await a("totp-enable", { secret, code: totp(secret, now), password: "wrong" })).status, 403);
assert.equal((await a("totp-enable", { secret, code: "000000", password: "env-password" })).data.error, "code_wrong");
assert.equal((await a("totp-enable", { secret, code: totp(secret, now), password: "env-password" })).data.totp, "panel");
const b = client(port);
assert.equal((await b("login", { user: "admin", password: "env-password" })).status, 401, "code required now");
assert.equal((await b("login", { user: "admin", password: "env-password", code: totp(secret, now) })).status, 401, "enable code can't be replayed");
const next = totp(secret, now + 1);
assert.equal((await b("login", { user: "admin", password: "typo", code: next })).status, 401);
assert.equal((await b("login", { user: "admin", password: "env-password", code: next })).status, 200, "password typo must not burn the code");
assert.equal((await b("password", { password: "env-password", newPassword: "short" })).data.error, "weak_password");
assert.equal((await b("password", { password: "wrong", newPassword: "new-password-42" })).status, 403);
assert.equal((await b("password", { password: "env-password", newPassword: "new-password-42" })).data.passwordFromPanel, true);
assert.equal((await a("config")).status, 401, "other sessions end on password change");
assert.equal((await b("config")).status, 200);
// Both survive a restart and win over .env.
await stop(gw);
gw = await startGateway(dir, port);
const c = client(port);
assert.equal((await c("login", { user: "admin", password: "env-password", code: totp(secret, now) })).status, 401);
assert.equal((await c("login", { user: "admin", password: "new-password-42", code: totp(secret, now) })).status, 200);
assert.equal((await client(port)("login", { user: "admin", password: "new-password-42", code: totp(secret, now) })).status, 401, "login code can't be replayed");
assert.equal((await c("totp-disable", { password: "new-password-42", code: "000000" })).data.error, "code_wrong");
assert.equal((await c("totp-disable", { password: "new-password-42", code: totp(secret, now + 1) })).data.totp, "");
assert.equal((await client(port)("login", { user: "admin", password: "new-password-42" })).status, 200);
} finally {
await stop(gw);
rmSync(dir, { recursive: true, force: true });
}
});