import { createHash, randomBytes } from "node:crypto"; import { existsSync } from "node:fs"; import type { Logger } from "../logger.js"; import { loadConfig } from "../config.js"; import { formatTeamSpeakTarget, parseTeamSpeakTarget, type TeamSpeakTarget } from "../domain/teamspeak-target.js"; import { type AccessMode, type ManagedInviteRecord, type SettingsUpdate, WebSpeakDatabase } from "../persistence/database.js"; import { hashAdminPassword, validateAdminPassword, verifyAdminPassword } from "../security/admin-password.js"; import { decryptSecret, encryptSecret } from "../security/secret-crypto.js"; import { probeTeamSpeak, TeamSpeakProbeError, type TeamSpeakProbeResult } from "../server/teamspeak-probe.js"; export interface AdminSettingsInput { target: string; serverPassword?: string; passwordAction?: "keep" | "replace" | "remove"; accessMode: AccessMode; siteName: string; welcomeText: string; } export interface ConnectionPolicy { defaultTarget: TeamSpeakTarget; serverPassword: string; accessMode: AccessMode; } export interface ManagedInviteInput { channel: string; expiresInHours: number; maxUses: number; } export interface ManagedInviteView { id: string; target: string; channel: string; expiresAt: string; maxUses: number; useCount: number; createdAt: string; revokedAt: string | null; status: "active" | "expired" | "exhausted" | "revoked"; } type ProbeFunction = typeof probeTeamSpeak; export class AdminService { constructor( readonly database: WebSpeakDatabase, private readonly masterSecret: Buffer, private readonly logger: Logger, private readonly legacyConfigPath: string, private readonly probe: ProbeFunction = probeTeamSpeak, private readonly version = "0.1.0", ) {} async initialize(): Promise { this.importLegacyConfigOnce(); if (!this.database.hasAdmin()) { const credential = await hashAdminPassword("admin", { username: "admin", mustChangePassword: true, allowWeakPassword: true }); this.database.initializeAdmin(credential, this.toSettingsUpdate(this.database.getSettings())); this.logger.warn("Default admin account created. Change the password on first login."); } } isInitialized(): boolean { return this.database.hasAdmin(); } async verifyPassword(username: string, password: string): Promise { const credential = this.database.getAdminCredential(); return credential?.username === username && await verifyAdminPassword(password, credential); } isPasswordChangeRequired(): boolean { return this.database.getAdminCredential()?.mustChangePassword === true; } async changePassword(password: string): Promise { const credential = this.database.getAdminCredential(); if (!credential) throw new AdminInputError("NOT_INITIALIZED", "The administrator account is not initialized"); const passwordError = validateAdminPassword(password); if (passwordError) throw new AdminInputError("INVALID_ADMIN_PASSWORD", passwordError); const replacement = await hashAdminPassword(password, { username: credential.username, mustChangePassword: false }); this.database.updateAdminCredential(replacement); this.database.addAudit("ADMIN_PASSWORD_CHANGED"); } getPublicConfig(): Record { const settings = this.database.getSettings(); return { initialized: this.isInitialized(), siteName: settings.siteName, welcomeText: settings.welcomeText, accessMode: settings.accessMode, target: formatTeamSpeakTarget({ host: settings.tsHost, port: settings.tsPort }), }; } getAdminSettings(): Record { const settings = this.database.getSettings(); return { target: formatTeamSpeakTarget({ host: settings.tsHost, port: settings.tsPort }), hasPassword: Boolean(settings.tsPasswordEncrypted), accessMode: settings.accessMode, siteName: settings.siteName, welcomeText: settings.welcomeText, detectedProtocol: settings.detectedProtocol, lastTestAt: settings.lastTestAt, lastTestLatencyMs: settings.lastTestLatencyMs, lastTestError: settings.lastTestError, internalPort: 3040, updatedAt: settings.updatedAt, }; } updateSettings(input: AdminSettingsInput): void { const current = this.database.getSettings(); const settings = this.normalizeSettings(input, current); const targetChanged = current.tsHost !== settings.tsHost || current.tsPort !== settings.tsPort; this.database.updateSettings(settings); if (targetChanged) this.database.clearConnectionTest(); } getConnectionPolicy(): ConnectionPolicy { const settings = this.database.getSettings(); let serverPassword = ""; try { serverPassword = decryptSecret(settings.tsPasswordEncrypted, this.masterSecret); } catch (error: unknown) { this.logger.error({ err: error instanceof Error ? error.message : String(error) }, "Stored TeamSpeak password could not be decrypted"); } return { defaultTarget: { host: settings.tsHost, port: settings.tsPort }, serverPassword, accessMode: settings.accessMode, }; } async testConnection(targetText: string, password: string, persistResult: boolean): Promise { let target: TeamSpeakTarget; try { target = parseTeamSpeakTarget(targetText); } catch { throw new AdminInputError("INVALID_TARGET", "TeamSpeak target is invalid"); } try { const result = await this.probe(target, password, this.logger); if (persistResult) { this.database.recordConnectionTest({ protocol: result.protocol, latencyMs: result.latencyMs, error: null }); this.database.addAudit("CONNECTION_TEST_SUCCEEDED", { protocol: result.protocol, latencyMs: result.latencyMs }); } return result; } catch (error: unknown) { const probeError = error instanceof TeamSpeakProbeError ? error : new TeamSpeakProbeError("INTERNAL_ERROR", "Connection test failed", error); if (persistResult) { this.database.recordConnectionTest({ protocol: null, latencyMs: null, error: probeError.code }); this.database.addAudit("CONNECTION_TEST_FAILED", { code: probeError.code }); } throw probeError; } } getOverview(activeSessions: number, peakSessions: number, startedAt: number): Record { const settings = this.database.getSettings(); return { gateway: { status: "running", version: this.version, uptimeSeconds: Math.max(0, Math.floor((Date.now() - startedAt) / 1000)), }, teamSpeak: { target: formatTeamSpeakTarget({ host: settings.tsHost, port: settings.tsPort }), status: settings.lastTestError ? "unreachable" : settings.lastTestAt ? "reachable" : "unknown", protocol: settings.detectedProtocol, lastTestAt: settings.lastTestAt, latencyMs: settings.lastTestLatencyMs, lastError: settings.lastTestError, }, sessions: { active: activeSessions, peak: peakSessions, limit: 100 }, recentEvents: this.database.recentAudit(), legacyConfigImported: this.database.getMeta("legacy_import_notice_pending") === "1", }; } createManagedInvite(input: ManagedInviteInput): { invite: ManagedInviteView; token: string } { const channel = input.channel.trim(); if (channel.length > 100) throw new AdminInputError("INVALID_INVITE_CHANNEL", "Invite channel cannot exceed 100 characters"); if (!Number.isFinite(input.expiresInHours) || input.expiresInHours < 1 || input.expiresInHours > 720) { throw new AdminInputError("INVALID_INVITE_EXPIRY", "Invite expiry must be between 1 and 720 hours"); } if (!Number.isInteger(input.maxUses) || input.maxUses < 0 || input.maxUses > 10000) { throw new AdminInputError("INVALID_INVITE_USES", "Invite max uses must be between 0 and 10000"); } const settings = this.database.getSettings(); const token = randomBytes(32).toString("base64url"); const record = this.database.createManagedInvite({ id: `invite-${Date.now().toString(36)}-${randomBytes(4).toString("hex")}`, tokenHash: hashInviteToken(token), targetHost: settings.tsHost, targetPort: settings.tsPort, serverPasswordEncrypted: settings.tsPasswordEncrypted, channel, expiresAt: new Date(Date.now() + input.expiresInHours * 60 * 60 * 1000).toISOString(), maxUses: input.maxUses, }); this.database.addAudit("INVITE_CREATED", { id: record.id, channel, maxUses: input.maxUses }); return { invite: this.toInviteView(record), token }; } listManagedInvites(): ManagedInviteView[] { return this.database.listManagedInvites().map((record) => this.toInviteView(record)); } revokeManagedInvite(id: string): boolean { if (!/^invite-[a-z0-9-]+$/i.test(id) || id.length > 100) throw new AdminInputError("INVALID_INVITE_ID", "Invite id is invalid"); const revoked = this.database.revokeManagedInvite(id); if (revoked) this.database.addAudit("INVITE_REVOKED", { id }); return revoked; } consumeManagedInvite(token: string): { target: TeamSpeakTarget; serverPassword: string; channel: string } | null { if (!/^[A-Za-z0-9_-]{32,128}$/.test(token)) return null; const record = this.database.consumeManagedInvite(hashInviteToken(token)); if (!record) return null; let serverPassword = ""; try { serverPassword = decryptSecret(record.serverPasswordEncrypted, this.masterSecret); } catch (error: unknown) { this.logger.error({ err: error instanceof Error ? error.message : String(error), inviteId: record.id }, "Managed invite password could not be decrypted"); } this.database.addAudit("INVITE_CONSUMED", { id: record.id }); return { target: { host: record.targetHost, port: record.targetPort }, serverPassword, channel: record.channel }; } dismissLegacyImportNotice(): void { this.database.setMeta("legacy_import_notice_pending", "0"); } private normalizeSettings(input: AdminSettingsInput, current: ReturnType): SettingsUpdate { let target: TeamSpeakTarget; try { target = parseTeamSpeakTarget(input.target); } catch { throw new AdminInputError("INVALID_TARGET", "TeamSpeak target is invalid"); } const siteName = input.siteName.trim(); const welcomeText = input.welcomeText.trim(); if (!siteName || siteName.length > 80) throw new AdminInputError("INVALID_SITE_NAME", "Site name must contain 1 to 80 characters"); if (welcomeText.length > 500) throw new AdminInputError("INVALID_WELCOME_TEXT", "Welcome text cannot exceed 500 characters"); if (input.accessMode !== "fixed" && input.accessMode !== "open") { throw new AdminInputError("INVALID_ACCESS_MODE", "Access mode is invalid"); } let encryptedPassword = current.tsPasswordEncrypted; const action = input.passwordAction ?? (input.serverPassword === undefined ? "keep" : "replace"); if (action === "remove") encryptedPassword = null; if (action === "replace") encryptedPassword = input.serverPassword ? encryptSecret(input.serverPassword, this.masterSecret) : null; return { siteName, welcomeText, accessMode: input.accessMode, tsHost: target.host, tsPort: target.port, tsPasswordEncrypted: encryptedPassword, }; } private toInviteView(record: ManagedInviteRecord): ManagedInviteView { const now = Date.now(); const expired = Date.parse(record.expiresAt) <= now; const exhausted = record.maxUses > 0 && record.useCount >= record.maxUses; return { id: record.id, target: formatTeamSpeakTarget({ host: record.targetHost, port: record.targetPort }), channel: record.channel, expiresAt: record.expiresAt, maxUses: record.maxUses, useCount: record.useCount, createdAt: record.createdAt, revokedAt: record.revokedAt, status: record.revokedAt ? "revoked" : expired ? "expired" : exhausted ? "exhausted" : "active", }; } private toSettingsUpdate(settings: ReturnType): SettingsUpdate { return { siteName: settings.siteName, welcomeText: settings.welcomeText, accessMode: settings.accessMode, tsHost: settings.tsHost, tsPort: settings.tsPort, tsPasswordEncrypted: settings.tsPasswordEncrypted, }; } private importLegacyConfigOnce(): void { if (this.database.getMeta("legacy_config_checked") === "1") return; if (existsSync(this.legacyConfigPath)) { const legacy = loadConfig(this.legacyConfigPath); const current = this.database.getSettings(); this.database.updateSettings({ siteName: current.siteName, welcomeText: current.welcomeText, accessMode: current.accessMode, tsHost: legacy.tsHost, tsPort: legacy.tsPort, tsPasswordEncrypted: legacy.tsServerPassword ? encryptSecret(legacy.tsServerPassword, this.masterSecret) : null, }, "LEGACY_CONFIG_IMPORTED"); this.database.setMeta("legacy_config_imported", "1"); this.database.setMeta("legacy_import_notice_pending", "1"); this.logger.info("Legacy config imported; WebSpeak settings are now managed from /admin"); } this.database.setMeta("legacy_config_checked", "1"); } } function hashInviteToken(token: string): string { return createHash("sha256").update(token).digest("hex"); } export class AdminInputError extends Error { constructor(readonly code: string, message: string) { super(message); this.name = "AdminInputError"; } }