mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
feat: add relay deployment mode and multiple relays
This commit is contained in:
1 parent
097c72bc5d
commit
73313f9e2d
14 files changed
+464
-114
No files matched your search
@@ -26,6 +26,11 @@ export interface AccelerationRelayOptions {
|
||||
timeoutMs?: number;
|
||||
}
|
||||
|
||||
export interface ConfiguredAccelerationRelay extends AccelerationRelayOptions {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface AccelerationTarget {
|
||||
host: string;
|
||||
port: number;
|
||||
|
||||
@@ -9,6 +9,7 @@ export interface JoinTicketPayload {
|
||||
identity?: string;
|
||||
rememberIdentity?: boolean;
|
||||
accelerated?: boolean;
|
||||
accelerationRelayId?: string;
|
||||
}
|
||||
|
||||
interface StoredTicket {
|
||||
|
||||
+16
-16
@@ -12,7 +12,7 @@ import { AdminSessionStore } from "../admin/admin-session.js";
|
||||
import { resolveSafeOpenTarget } from "../security/open-target-policy.js";
|
||||
import { identityFromString } from "@echosixhiya/teamspeak-client";
|
||||
import { JoinRateLimiter } from "./join-rate-limit.js";
|
||||
import type { AccelerationRelayOptions } from "./acceleration-relay.js";
|
||||
import type { ConfiguredAccelerationRelay } from "./acceleration-relay.js";
|
||||
|
||||
export interface WebServerOptions {
|
||||
port: number;
|
||||
@@ -61,11 +61,10 @@ export function createWebServer(options: WebServerOptions): WebServer {
|
||||
app.get("/api/public-config", (_request, response) => {
|
||||
response.setHeader("Cache-Control", "no-store");
|
||||
const acceleration = resolveAccelerationOptions(options.voiceBridgeOptions.acceleration);
|
||||
const accelerationName = resolveAccelerationName(options.voiceBridgeOptions.accelerationName);
|
||||
response.json({
|
||||
...options.adminService.getPublicConfig(),
|
||||
accelerationAvailable: Boolean(acceleration),
|
||||
accelerationName: acceleration ? (accelerationName || "中继加速") : "",
|
||||
accelerationAvailable: acceleration.length > 0,
|
||||
accelerationRelays: acceleration.map((relay) => ({ id: relay.id, name: relay.name })),
|
||||
});
|
||||
});
|
||||
|
||||
@@ -111,9 +110,14 @@ export function createWebServer(options: WebServerOptions): WebServer {
|
||||
let target = managedInvite?.target ?? policy.defaultTarget;
|
||||
let serverPassword = managedInvite?.serverPassword ?? policy.serverPassword;
|
||||
const channel = requestedChannel || managedInvite?.channel || "";
|
||||
const accelerationRequested = body.accelerated === true;
|
||||
const requestedRelayId = typeof body.accelerationRelayId === "string" ? body.accelerationRelayId.trim().slice(0, 110) : "";
|
||||
const accelerationRequested = body.accelerated === true || Boolean(requestedRelayId);
|
||||
const acceleration = resolveAccelerationOptions(options.voiceBridgeOptions.acceleration);
|
||||
if (accelerationRequested && !acceleration) {
|
||||
if (accelerationRequested && acceleration.length === 0) {
|
||||
response.status(400).json({ ok: false, code: "ACCELERATION_UNAVAILABLE" });
|
||||
return;
|
||||
}
|
||||
if (requestedRelayId && !acceleration.some((relay) => relay.id === requestedRelayId)) {
|
||||
response.status(400).json({ ok: false, code: "ACCELERATION_UNAVAILABLE" });
|
||||
return;
|
||||
}
|
||||
@@ -147,7 +151,7 @@ export function createWebServer(options: WebServerOptions): WebServer {
|
||||
nickname,
|
||||
...(channel ? { channel } : {}),
|
||||
...(identity ? { identity, rememberIdentity: true } : body.rememberIdentity === true ? { rememberIdentity: true } : {}),
|
||||
...(accelerationRequested ? { accelerated: true } : {}),
|
||||
...(accelerationRequested ? { accelerated: true, ...(requestedRelayId ? { accelerationRelayId: requestedRelayId } : {}) } : {}),
|
||||
});
|
||||
response.status(201).json({ ok: true, ticket });
|
||||
});
|
||||
@@ -196,15 +200,11 @@ export function createWebServer(options: WebServerOptions): WebServer {
|
||||
}
|
||||
|
||||
function resolveAccelerationOptions(
|
||||
configured: AccelerationRelayOptions | (() => AccelerationRelayOptions | undefined) | undefined,
|
||||
): AccelerationRelayOptions | undefined {
|
||||
return typeof configured === "function" ? configured() : configured;
|
||||
}
|
||||
|
||||
function resolveAccelerationName(
|
||||
configured: VoiceBridgeOptions["accelerationName"],
|
||||
): string | undefined {
|
||||
return typeof configured === "function" ? configured() : configured;
|
||||
configured: ConfiguredAccelerationRelay[] | (() => ConfiguredAccelerationRelay[]) | undefined,
|
||||
): ConfiguredAccelerationRelay[] {
|
||||
const value = typeof configured === "function" ? configured() : configured;
|
||||
if (!value) return [];
|
||||
return Array.isArray(value) ? value : [value];
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
|
||||
@@ -14,7 +14,7 @@ import { parseClientCommand, type ClientCommand } from "./voice-protocol.js";
|
||||
import { isRecoverable, reconnectDelayMs, reconnectWindowOpen } from "./reconnect-policy.js";
|
||||
import { WebRtcAudioSession, type WebRtcAudioOptions, type WebRtcAudioStats, type WebRtcSessionDescription } from "./webrtc-audio.js";
|
||||
import { pingTeamSpeakSession } from "./network-probe.js";
|
||||
import type { AccelerationRelayOptions } from "./acceleration-relay.js";
|
||||
import type { AccelerationRelayOptions, ConfiguredAccelerationRelay } from "./acceleration-relay.js";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const { OpusEncoder } = require("@discordjs/opus") as {
|
||||
@@ -34,7 +34,7 @@ const MAX_SERVER_AUDIO_BUFFERED_BYTES = 4_096;
|
||||
export interface VoiceBridgeOptions {
|
||||
joinTickets: JoinTicketStore;
|
||||
webRtc?: WebRtcAudioOptions | (() => WebRtcAudioOptions);
|
||||
acceleration?: AccelerationRelayOptions | (() => AccelerationRelayOptions | undefined);
|
||||
acceleration?: ConfiguredAccelerationRelay[] | (() => ConfiguredAccelerationRelay[]);
|
||||
accelerationName?: string | (() => string | undefined);
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ export class VoiceBridge {
|
||||
|
||||
const { target, serverPassword, nickname } = connection;
|
||||
const channelName = connection.channel;
|
||||
const acceleration = connection.accelerated ? this.getAccelerationOptions() : undefined;
|
||||
const acceleration = connection.accelerated ? this.getAccelerationOptions(connection.accelerationRelayId) : undefined;
|
||||
if (connection.accelerated && !acceleration) {
|
||||
ws.close(4006, "ACCELERATION_UNAVAILABLE");
|
||||
return;
|
||||
@@ -794,9 +794,13 @@ export class VoiceBridge {
|
||||
return typeof configured === "function" ? configured() : configured;
|
||||
}
|
||||
|
||||
private getAccelerationOptions(): AccelerationRelayOptions | undefined {
|
||||
private getAccelerationOptions(relayId = ""): AccelerationRelayOptions | undefined {
|
||||
const configured = this.options.acceleration;
|
||||
return typeof configured === "function" ? configured() : configured;
|
||||
const relays = typeof configured === "function" ? configured() : configured;
|
||||
if (!relays?.length) return undefined;
|
||||
const selected = relayId ? relays.find((relay) => relay.id === relayId) : relays[0];
|
||||
if (!selected) return undefined;
|
||||
return { relayHost: selected.relayHost, relayPort: selected.relayPort, token: selected.token };
|
||||
}
|
||||
|
||||
private async handleWebRtcOffer(
|
||||
|
||||
Reference in new issue
Block a user