mirror of
https://github.com/db9979/tidefront.git
synced 2026-12-06 21:04:12 +01:00
Verwaltung: Sicherungen herunterladen und „Jetzt sichern“, Statistikseite für Admins
- Abschnitt „Sicherungen“: Liste aus /backups (api schreibgeschützt eingehängt), Download über GET /api/admin/backups/:name mit Sitzung, is_admin und einmaligem 60-s-Token; Pfad-Traversal ausgeschlossen - „Jetzt sichern“ als Auftrag in backup_request; backup.sh prüft im loop jede Minute, sichert mit -manuell, trägt backup_run ein; Aufräumen gilt für tägliche und manuelle Sicherungen gleich - Statistik (7/28 Tage): Aktivität, Registrierungen, Produktion, Beute, Börse, Kampf-Siegquoten, Nester, Gebäudestufen, Auffälligkeiten; SQL-Aggregation, 60 s Zwischenspeicher, SVG-Diagramme - Router adminstats.ts, Route backup-download.ts, Tabellen am Ende von schema.ts (Migration nicht eingecheckt) - Integrationstests adminstats.test.ts, README und BAUAUFTRAG ergänzt
This commit is contained in:
1 parent
b8d68d251a
commit
fb59fa4d1f
17 files changed
+1503
-19
No files matched your search
@@ -12,6 +12,7 @@ import { createDb } from "@tidefront/db";
|
||||
import { noticeHook, playerForToken, touchLastSeen, type ChangeKind, type Engine } from "@tidefront/engine";
|
||||
import { appRouter, type AppRouter, type Ctx } from "./router.ts";
|
||||
import { registerMailLinks } from "./routers/mail-links.ts";
|
||||
import { registerBackupDownload } from "./routers/backup-download.ts";
|
||||
|
||||
const PORT = Number(process.env.PORT ?? 8080);
|
||||
const DATABASE_URL = process.env.DATABASE_URL ?? "postgres://tidefront:tidefront@localhost:5432/tidefront";
|
||||
@@ -115,6 +116,7 @@ await app.register(fastifyTRPCPlugin, {
|
||||
});
|
||||
|
||||
registerMailLinks(app, engine, rateLimit);
|
||||
registerBackupDownload(app, engine, { cookieName: COOKIE, rateLimit });
|
||||
|
||||
// ---------------------------------------------------------------- Gesundheit und Oberfläche
|
||||
app.get("/healthz", async (_req, reply) => {
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
import { seasonRouter, socialRouter } from "./routers/social.ts";
|
||||
import { authed, t, wrap, type Ctx } from "./trpc.ts";
|
||||
import { accountRouter, adminRouter, authExtras, pushRouter } from "./routers/ops.ts";
|
||||
import { adminStatsRouter } from "./routers/adminstats.ts";
|
||||
|
||||
export type { Ctx };
|
||||
|
||||
@@ -73,6 +74,7 @@ export const appRouter = t.router({
|
||||
push: pushRouter,
|
||||
account: accountRouter,
|
||||
admin: adminRouter,
|
||||
adminStats: adminStatsRouter,
|
||||
|
||||
world: t.router({
|
||||
map: authed.query(({ ctx }) => mapView(ctx.engine, ctx.player.id)),
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// Teil-Router der Verwaltung (nur Admins, adminProc prüft is_admin serverseitig bei jeder Anfrage):
|
||||
// Statistik und Sicherungen (Liste, „Jetzt sichern“, Download-Token für die Fastify-Route backup-download.ts).
|
||||
import { z } from "zod";
|
||||
import {
|
||||
adminStatsCached,
|
||||
backupRequests,
|
||||
createDownloadToken,
|
||||
listBackups,
|
||||
requestBackup,
|
||||
} from "@tidefront/engine";
|
||||
import { adminProc, t } from "../trpc.ts";
|
||||
|
||||
export const adminStatsRouter = t.router({
|
||||
stats: adminProc
|
||||
.input(z.object({ days: z.union([z.literal(7), z.literal(28)]) }))
|
||||
.query(({ ctx, input }) => adminStatsCached(ctx.engine.db, input.days)),
|
||||
|
||||
backups: adminProc.query(async ({ ctx }) => ({
|
||||
...(await listBackups()),
|
||||
requests: await backupRequests(ctx.engine.db),
|
||||
})),
|
||||
|
||||
backupNow: adminProc.mutation(async ({ ctx }) => {
|
||||
if (!(await ctx.rateLimit(`backupnow:${ctx.player.id}`, 6, 3600))) return { ok: false as const, code: "rate" };
|
||||
const r = await requestBackup(ctx.engine.db, ctx.player.id);
|
||||
return { ok: true as const, ...r };
|
||||
}),
|
||||
|
||||
backupToken: adminProc
|
||||
.input(z.object({ name: z.string().min(1).max(120) }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
if (!(await ctx.rateLimit(`backuptoken:${ctx.player.id}`, 30, 600))) return { ok: false as const, code: "rate" };
|
||||
return createDownloadToken(ctx.engine.db, ctx.player.id, input.name);
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
// Download einer Sicherung: GET /api/admin/backups/:name?t=<Token>.
|
||||
// Drei Schlüssel: Sitzungs-Cookie, is_admin (frisch aus der Datenbank) und ein einmaliges Token (60 s, über tRPC
|
||||
// angefordert, an Admin und Datei gebunden). Ein Link aus Protokoll oder Verlauf wirkt damit nicht ein zweites Mal.
|
||||
import { createReadStream } from "node:fs";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { backupDir, backupFile, consumeDownloadToken, isBackupName, playerForToken, type Engine } from "@tidefront/engine";
|
||||
|
||||
export interface BackupDownloadOptions {
|
||||
cookieName: string;
|
||||
rateLimit: (bucket: string, limit: number, windowSec: number) => Promise<boolean>;
|
||||
/** Vorgabe: backupDir() (BACKUP_PATH oder /backups). */
|
||||
dir?: string;
|
||||
}
|
||||
|
||||
export function registerBackupDownload(app: FastifyInstance, engine: Engine, opts: BackupDownloadOptions): void {
|
||||
app.get<{ Params: { name: string }; Querystring: { t?: string } }>("/api/admin/backups/:name", async (req, reply) => {
|
||||
reply.header("cache-control", "no-store").header("referrer-policy", "no-referrer").header("x-content-type-options", "nosniff");
|
||||
if (!(await opts.rateLimit(`bdl:${req.ip}`, 30, 600))) return reply.code(429).send({ error: "rate" });
|
||||
const name = String(req.params.name ?? "");
|
||||
if (!isBackupName(name)) return reply.code(400).send({ error: "bad_name" });
|
||||
const player = await playerForToken(engine.db, req.cookies[opts.cookieName]);
|
||||
if (!player) return reply.code(401).send({ error: "unauthorized" });
|
||||
if (!player.isAdmin) return reply.code(403).send({ error: "forbidden" });
|
||||
if (!(await consumeDownloadToken(engine.db, String(req.query.t ?? ""), player.id, name))) {
|
||||
return reply.code(403).send({ error: "token" });
|
||||
}
|
||||
const file = await backupFile(opts.dir ?? backupDir(), name);
|
||||
if (!file) return reply.code(404).send({ error: "not_found" });
|
||||
engine.log("backup download", { player: player.name, file: name });
|
||||
return reply
|
||||
.type("application/octet-stream")
|
||||
.header("content-disposition", `attachment; filename="${name}"`)
|
||||
.header("content-length", String(file.bytes))
|
||||
.send(createReadStream(file.path));
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user