feat(admin): manage WebRTC settings from console

This commit is contained in:
illusia committed 2026-09-03 07:52:13 +08:00
1 parent ecff06e9a1
commit 5c3d1b7e5d
11 files changed
+163 -58

No files matched your search

+4
View File
@@ -332,6 +332,10 @@ function readSettingsInput(body: Record<string, unknown>): AdminSettingsInput {
accessMode: body.accessMode === "open" ? "open" : body.accessMode === "fixed" ? "fixed" : body.accessMode as never,
siteName: readString(body, "siteName", 80),
welcomeText: readOptionalString(body, "welcomeText", 500),
webRtcEnabled: body.webRtcEnabled === true,
webRtcPublicHost: readOptionalString(body, "webRtcPublicHost", 253),
webRtcUdpStart: readOptionalNumber(body, "webRtcUdpStart", 40000),
webRtcUdpEnd: readOptionalNumber(body, "webRtcUdpEnd", 40099),
};
}
+47
View File
@@ -7,6 +7,9 @@ import { type AccessMode, type ManagedInviteRecord, type SettingsUpdate, WebSpea
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";
import type { WebRtcAudioOptions } from "../server/webrtc-audio.js";
const MAX_WEBRTC_UDP_PORTS = 100;
export interface AdminSettingsInput {
target: string;
@@ -15,6 +18,10 @@ export interface AdminSettingsInput {
accessMode: AccessMode;
siteName: string;
welcomeText: string;
webRtcEnabled: boolean;
webRtcPublicHost: string;
webRtcUdpStart: number;
webRtcUdpEnd: number;
}
export interface ConnectionPolicy {
@@ -108,11 +115,24 @@ export class AdminService {
lastTestAt: settings.lastTestAt,
lastTestLatencyMs: settings.lastTestLatencyMs,
lastTestError: settings.lastTestError,
webRtcEnabled: settings.webRtcEnabled,
webRtcPublicHost: settings.webRtcPublicHost,
webRtcUdpStart: settings.webRtcUdpStart,
webRtcUdpEnd: settings.webRtcUdpEnd,
internalPort: 3040,
updatedAt: settings.updatedAt,
};
}
getWebRtcAudioOptions(): WebRtcAudioOptions {
const settings = this.database.getSettings();
return {
enabled: settings.webRtcEnabled,
...(settings.webRtcPublicHost ? { publicHost: settings.webRtcPublicHost } : {}),
icePortRange: [settings.webRtcUdpStart, settings.webRtcUdpEnd],
};
}
updateSettings(input: AdminSettingsInput): void {
const current = this.database.getSettings();
const settings = this.normalizeSettings(input, current);
@@ -252,6 +272,21 @@ export class AdminService {
if (input.accessMode !== "fixed" && input.accessMode !== "open") {
throw new AdminInputError("INVALID_ACCESS_MODE", "Access mode is invalid");
}
if (typeof input.webRtcEnabled !== "boolean") {
throw new AdminInputError("INVALID_WEBRTC_ENABLED", "WebRTC enabled value is invalid");
}
const webRtcPublicHost = input.webRtcPublicHost.trim();
if (webRtcPublicHost.length > 253 || /[\s/\\]/.test(webRtcPublicHost)) {
throw new AdminInputError("INVALID_WEBRTC_HOST", "WebRTC public host is invalid");
}
if (
!Number.isInteger(input.webRtcUdpStart) || input.webRtcUdpStart < 1 || input.webRtcUdpStart > 65_535
|| !Number.isInteger(input.webRtcUdpEnd) || input.webRtcUdpEnd < 1 || input.webRtcUdpEnd > 65_535
|| input.webRtcUdpStart > input.webRtcUdpEnd
|| input.webRtcUdpEnd - input.webRtcUdpStart + 1 > MAX_WEBRTC_UDP_PORTS
) {
throw new AdminInputError("INVALID_WEBRTC_PORTS", "WebRTC UDP port range is invalid");
}
let encryptedPassword = current.tsPasswordEncrypted;
const action = input.passwordAction ?? (input.serverPassword === undefined ? "keep" : "replace");
@@ -264,6 +299,10 @@ export class AdminService {
tsHost: target.host,
tsPort: target.port,
tsPasswordEncrypted: encryptedPassword,
webRtcEnabled: input.webRtcEnabled,
webRtcPublicHost,
webRtcUdpStart: input.webRtcUdpStart,
webRtcUdpEnd: input.webRtcUdpEnd,
};
}
@@ -292,6 +331,10 @@ export class AdminService {
tsHost: settings.tsHost,
tsPort: settings.tsPort,
tsPasswordEncrypted: settings.tsPasswordEncrypted,
webRtcEnabled: settings.webRtcEnabled,
webRtcPublicHost: settings.webRtcPublicHost,
webRtcUdpStart: settings.webRtcUdpStart,
webRtcUdpEnd: settings.webRtcUdpEnd,
};
}
@@ -307,6 +350,10 @@ export class AdminService {
tsHost: legacy.tsHost,
tsPort: legacy.tsPort,
tsPasswordEncrypted: legacy.tsServerPassword ? encryptSecret(legacy.tsServerPassword, this.masterSecret) : null,
webRtcEnabled: current.webRtcEnabled,
webRtcPublicHost: current.webRtcPublicHost,
webRtcUdpStart: current.webRtcUdpStart,
webRtcUdpEnd: current.webRtcUdpEnd,
}, "LEGACY_CONFIG_IMPORTED");
this.database.setMeta("legacy_config_imported", "1");
this.database.setMeta("legacy_import_notice_pending", "1");
+1 -20
View File
@@ -8,7 +8,6 @@ import { WebSpeakDatabase } from "./persistence/database.js";
import { loadOrCreateMasterSecret } from "./security/master-secret.js";
import { AdminService } from "./admin/admin-service.js";
import { JoinTicketStore } from "./server/join-ticket.js";
import type { WebRtcAudioOptions } from "./server/webrtc-audio.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT_DIR = path.resolve(__dirname, "..");
@@ -20,7 +19,6 @@ const DATA_DIR = process.env.WEBSPEAK_DATA_DIR?.trim() || path.join(ROOT_DIR, "d
const LOG_DIR = path.join(DATA_DIR, "logs");
const STATIC_DIR = path.join(ROOT_DIR, "web", "dist");
const APP_VERSION = readPackageVersion();
const WEBRTC_OPTIONS = readWebRtcOptions();
async function main() {
const logger = createLogger(LOG_DIR);
@@ -50,7 +48,7 @@ async function main() {
certDir: hasCert ? CERT_DIR : undefined,
voiceBridgeOptions: {
joinTickets,
webRtc: WEBRTC_OPTIONS,
webRtc: () => adminService.getWebRtcAudioOptions(),
},
adminService,
logger,
@@ -86,23 +84,6 @@ function removeObsoleteBootstrapFile(): void {
}
}
function readWebRtcOptions(): WebRtcAudioOptions {
const enabled = /^(1|true|yes)$/i.test(process.env.WEBSPEAK_WEBRTC?.trim() ?? "");
const publicHost = process.env.WEBSPEAK_WEBRTC_PUBLIC_HOST?.trim() || undefined;
const start = parsePort(process.env.WEBSPEAK_WEBRTC_UDP_START, 40_000);
const end = parsePort(process.env.WEBSPEAK_WEBRTC_UDP_END, 40_099);
return {
enabled,
...(publicHost ? { publicHost } : {}),
...(start <= end ? { icePortRange: [start, end] as [number, number] } : {}),
};
}
function parsePort(value: string | undefined, fallback: number): number {
const port = Number(value);
return Number.isInteger(port) && port >= 1 && port <= 65_535 ? port : fallback;
}
main().catch((err) => {
console.error("Fatal error:", err);
process.exit(1);
+35 -2
View File
@@ -5,7 +5,7 @@ import { DatabaseSync } from "node:sqlite";
import type { AdminCredential } from "../security/admin-password.js";
import type { TeamSpeakProtocol } from "../server/teamspeak-adapter.js";
export const DATABASE_SCHEMA_VERSION = 2;
export const DATABASE_SCHEMA_VERSION = 3;
export type AccessMode = "fixed" | "open";
export interface PersistedSettings {
@@ -19,6 +19,10 @@ export interface PersistedSettings {
lastTestAt: string | null;
lastTestLatencyMs: number | null;
lastTestError: string | null;
webRtcEnabled: boolean;
webRtcPublicHost: string;
webRtcUdpStart: number;
webRtcUdpEnd: number;
updatedAt: string;
}
@@ -29,6 +33,10 @@ export interface SettingsUpdate {
tsHost: string;
tsPort: number;
tsPasswordEncrypted: string | null;
webRtcEnabled: boolean;
webRtcPublicHost: string;
webRtcUdpStart: number;
webRtcUdpEnd: number;
}
export interface ManagedInviteRecord {
@@ -70,6 +78,10 @@ interface SettingsRow extends Record<string, unknown> {
last_test_at: string | null;
last_test_latency_ms: number | null;
last_test_error: string | null;
webrtc_enabled: number;
webrtc_public_host: string;
webrtc_udp_start: number;
webrtc_udp_end: number;
updated_at: string;
}
@@ -140,6 +152,10 @@ export class WebSpeakDatabase {
lastTestAt: row.last_test_at,
lastTestLatencyMs: row.last_test_latency_ms,
lastTestError: row.last_test_error,
webRtcEnabled: row.webrtc_enabled === 1,
webRtcPublicHost: row.webrtc_public_host,
webRtcUdpStart: row.webrtc_udp_start,
webRtcUdpEnd: row.webrtc_udp_end,
updatedAt: row.updated_at,
};
}
@@ -349,6 +365,18 @@ export class WebSpeakDatabase {
`);
this.database.exec("PRAGMA user_version = 2");
});
version = 2;
}
if (version === 2) {
this.transaction(() => {
this.database.exec(`
ALTER TABLE settings ADD COLUMN webrtc_enabled INTEGER NOT NULL DEFAULT 0 CHECK (webrtc_enabled IN (0, 1));
ALTER TABLE settings ADD COLUMN webrtc_public_host TEXT NOT NULL DEFAULT '';
ALTER TABLE settings ADD COLUMN webrtc_udp_start INTEGER NOT NULL DEFAULT 40000 CHECK (webrtc_udp_start BETWEEN 1 AND 65535);
ALTER TABLE settings ADD COLUMN webrtc_udp_end INTEGER NOT NULL DEFAULT 40099 CHECK (webrtc_udp_end BETWEEN 1 AND 65535);
`);
this.database.exec("PRAGMA user_version = 3");
});
}
}
@@ -356,7 +384,8 @@ export class WebSpeakDatabase {
this.database.prepare(
`UPDATE settings SET
site_name = ?, welcome_text = ?, access_mode = ?, ts_host = ?, ts_port = ?,
ts_password_encrypted = ?, updated_at = ?
ts_password_encrypted = ?, webrtc_enabled = ?, webrtc_public_host = ?,
webrtc_udp_start = ?, webrtc_udp_end = ?, updated_at = ?
WHERE id = 1`,
).run(
settings.siteName,
@@ -365,6 +394,10 @@ export class WebSpeakDatabase {
settings.tsHost,
settings.tsPort,
settings.tsPasswordEncrypted,
settings.webRtcEnabled ? 1 : 0,
settings.webRtcPublicHost,
settings.webRtcUdpStart,
settings.webRtcUdpEnd,
now,
);
}
+9 -4
View File
@@ -30,7 +30,7 @@ const MAX_SERVER_AUDIO_BUFFERED_BYTES = 4_096;
export interface VoiceBridgeOptions {
joinTickets: JoinTicketStore;
webRtc?: WebRtcAudioOptions;
webRtc?: WebRtcAudioOptions | (() => WebRtcAudioOptions);
}
export interface AdminSessionSummary {
@@ -267,7 +267,7 @@ export class VoiceBridge {
serverEventLog: entry!.eventLog,
whisperTargetIds: [...entry!.whisperTargetIds],
whisperActive: entry!.whisperActive,
webrtcAvailable: this.options.webRtc?.enabled === true,
webrtcAvailable: this.getWebRtcOptions()?.enabled === true,
...(entry!.rememberIdentity ? { identity: tsClient.getIdentityString() } : {}),
});
sendJson({ type: "channelList", channels: entry!.channelTree });
@@ -515,7 +515,7 @@ export class VoiceBridge {
const rawMessage = typeof data === "string" ? data : data.toString("utf-8");
const webRtcOffer = parseWebRtcOffer(rawMessage);
if (webRtcOffer) {
if (this.options.webRtc?.enabled !== true) {
if (this.getWebRtcOptions()?.enabled !== true) {
sendProtocolError(sendJson, "WEBRTC_DISABLED", "WebRTC 音频传输未启用");
return;
}
@@ -683,6 +683,11 @@ export class VoiceBridge {
return token ? this.options.joinTickets.consume(token) : null;
}
private getWebRtcOptions(): WebRtcAudioOptions | undefined {
const configured = this.options.webRtc;
return typeof configured === "function" ? configured() : configured;
}
private async handleWebRtcOffer(
entry: WebClientEntry,
offer: WebRtcSessionDescription,
@@ -692,7 +697,7 @@ export class VoiceBridge {
try { await entry.webrtc.close(); } catch { /* replace a retried offer */ }
entry.webrtc = null;
}
const config = this.options.webRtc;
const config = this.getWebRtcOptions();
if (!config?.enabled) return;
const peer = new WebRtcAudioSession({
connectionId: entry.id,