mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
feat: add managed invites and admin diagnostics backend
This commit is contained in:
1 parent
96ef77c789
commit
f5bd06fdaf
10 files changed
+562
-29
No files matched your search
@@ -1,8 +1,9 @@
|
||||
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 SettingsUpdate, WebSpeakDatabase } from "../persistence/database.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";
|
||||
@@ -22,6 +23,24 @@ export interface ConnectionPolicy {
|
||||
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 {
|
||||
@@ -164,6 +183,56 @@ export class AdminService {
|
||||
};
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
@@ -197,6 +266,23 @@ export class AdminService {
|
||||
};
|
||||
}
|
||||
|
||||
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<WebSpeakDatabase["getSettings"]>): SettingsUpdate {
|
||||
return {
|
||||
siteName: settings.siteName,
|
||||
@@ -229,6 +315,10 @@ export class AdminService {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user