chore: harden release operations and freeze docs

This commit is contained in:
illusia committed 2026-09-01 18:24:09 +08:00
1 parent 87fcd736be
commit 6337789afa
9 files changed
+176 -23

No files matched your search

+2 -1
View File
@@ -50,6 +50,7 @@ export class AdminService {
private readonly logger: Logger,
private readonly legacyConfigPath: string,
private readonly probe: ProbeFunction = probeTeamSpeak,
private readonly version = "0.1.0",
) {}
async initialize(): Promise<void> {
@@ -166,7 +167,7 @@ export class AdminService {
return {
gateway: {
status: "running",
version: "0.1.0",
version: this.version,
uptimeSeconds: Math.max(0, Math.floor((Date.now() - startedAt) / 1000)),
},
teamSpeak: {
+55 -16
View File
@@ -1,5 +1,9 @@
import pino from "pino";
import { mkdirSync } from "node:fs";
import { appendFile, existsSync, mkdirSync, renameSync, statSync } from "node:fs";
import { Writable } from "node:stream";
const MAX_LOG_BYTES = 10 * 1024 * 1024;
const MAX_ROTATED_LOGS = 3;
export interface Logger {
debug(obj: Record<string, unknown>, msg: string): void;
@@ -14,23 +18,17 @@ export interface Logger {
}
export function createLogger(logDir?: string): Logger {
const targets: pino.TransportTargetOptions[] = [
{ target: "pino/file", options: { destination: 1 }, level: "info" },
];
if (logDir) {
mkdirSync(logDir, { recursive: true });
targets.push({
target: "pino/file",
options: { destination: `${logDir}/webspeak.log` },
level: "debug",
});
}
const baseLogger = pino({
const loggerOptions = {
level: "debug",
timestamp: pino.stdTimeFunctions.isoTime,
...(targets.length > 1 ? { transport: { targets } } : {}),
});
redact: {
paths: ["password", "*.password", "serverPassword", "*.serverPassword", "token", "*.token", "identity", "*.identity"],
censor: "[Redacted]",
},
};
const baseLogger = logDir
? createFileLogger(loggerOptions, `${logDir}/webspeak.log`)
: pino(loggerOptions, pino.destination(1));
function wrap(l: pino.Logger): Logger {
function doLog(
@@ -60,3 +58,44 @@ export function createLogger(logDir?: string): Logger {
return wrap(baseLogger);
}
function createFileLogger(options: { level: string; timestamp: typeof pino.stdTimeFunctions.isoTime }, logPath: string): pino.Logger {
mkdirSync(logPath.replace(/[\\/][^\\/]+$/, ""), { recursive: true });
return pino(options, pino.multistream([
{ level: "info", stream: process.stdout },
{ level: "debug", stream: new RotatingFileStream(logPath) },
]));
}
class RotatingFileStream extends Writable {
private bytes: number;
constructor(private readonly filePath: string) {
super();
this.bytes = existsSync(filePath) ? statSync(filePath).size : 0;
if (this.bytes >= MAX_LOG_BYTES) this.rotate();
}
override _write(chunk: unknown, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk), encoding);
try {
if (this.bytes + buffer.length > MAX_LOG_BYTES) this.rotate();
appendFile(this.filePath, buffer, (error) => {
if (!error) this.bytes += buffer.length;
callback(error);
});
} catch (error: unknown) {
callback(error instanceof Error ? error : new Error(String(error)));
}
}
private rotate(): void {
for (let index = MAX_ROTATED_LOGS - 1; index >= 1; index--) {
const source = `${this.filePath}.${index}`;
const destination = `${this.filePath}.${index + 1}`;
if (existsSync(source)) renameSync(source, destination);
}
if (existsSync(this.filePath)) renameSync(this.filePath, `${this.filePath}.1`);
this.bytes = 0;
}
}
+37
View File
@@ -0,0 +1,37 @@
const WINDOW_MS = 60 * 1000;
const MAX_REQUESTS = 30;
interface PeerWindow {
startedAt: number;
requests: number;
}
/** Bounds opaque join-ticket creation so one peer cannot fill the in-memory store. */
export class JoinRateLimiter {
private readonly peers = new Map<string, PeerWindow>();
constructor(private readonly maxTrackedPeers = 2048) {}
allow(peer: string, now = Date.now()): boolean {
const current = this.peers.get(peer);
if (!current || now - current.startedAt >= WINDOW_MS) {
this.prune(now);
while (this.peers.size >= this.maxTrackedPeers) {
const oldest = this.peers.keys().next().value;
if (!oldest) break;
this.peers.delete(oldest);
}
this.peers.set(peer, { startedAt: now, requests: 1 });
return true;
}
if (current.requests >= MAX_REQUESTS) return false;
current.requests += 1;
return true;
}
private prune(now: number): void {
for (const [peer, window] of this.peers) {
if (now - window.startedAt >= WINDOW_MS) this.peers.delete(peer);
}
}
}
+6
View File
@@ -11,6 +11,7 @@ import type { AdminService } from "../admin/admin-service.js";
import { AdminSessionStore } from "../admin/admin-session.js";
import { resolveSafeOpenTarget } from "../security/open-target-policy.js";
import { identityFromString } from "@honeybbq/teamspeak-client";
import { JoinRateLimiter } from "./join-rate-limit.js";
export interface WebServerOptions {
port: number;
@@ -47,6 +48,7 @@ export function createWebServer(options: WebServerOptions): WebServer {
const voiceBridge = new VoiceBridge(options.voiceBridgeOptions, logger);
const adminSessions = new AdminSessionStore();
const joinRateLimiter = new JoinRateLimiter();
const startedAt = Date.now();
const healthHandler: express.RequestHandler = (_request, response) => {
@@ -70,6 +72,10 @@ export function createWebServer(options: WebServerOptions): WebServer {
response.status(503).json({ ok: false, code: "NOT_INITIALIZED" });
return;
}
if (!joinRateLimiter.allow(request.socket.remoteAddress ?? "unknown")) {
response.status(429).json({ ok: false, code: "RATE_LIMITED" });
return;
}
const body = isRecord(request.body) ? request.body : {};
const nickname = typeof body.nickname === "string" ? body.nickname.trim().slice(0, 30) : "";
const requestedChannel = typeof body.channel === "string" ? body.channel.trim().slice(0, 100) : "";