mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
- errors: 新增 TeamSpeak 服务器错误 ID 映射表与不可重连错误码集合,归一化 normalizeTeamSpeakError 按 错误ID/关键词/异常类/传输层特征 分级收敛错误 - voice-bridge: 身份无效/客户端不可用时先发结构化 connectionFailed 帧再关闭, 新增 pendingKickReason 机制在 kicked 事件后优先回放踢出原因;Opus 编码器 不可用时限流上报 AUDIO_ENCODER_UNAVAILABLE - ts-client: 记录本端退出 reasonID,转发 kicked 事件并归一化踢出/封禁原因 - network-probe/teamspeak-probe: 收窄关键字匹配、拒绝原因收敛到既有探测码 - session-manager: 新增 teamSpeak-kicked 拆除原因 - WebClient: 麦克风失败不再假成功,音频状态避免自相矛盾,新增降级横幅与 中/英/德三语提示词表 - useVoiceWebSocket: 导出 ensureMicrophone 供开麦前预检,新增音频诊断状态、 CONNECTION_FAILURE_MESSAGES 总表与网关关闭码映射 - 关键新增逻辑补充中文注释说明改动意图
143 lines
5.8 KiB
TypeScript
143 lines
5.8 KiB
TypeScript
import type { Logger } from "../logger.js";
|
|
import { normalizeTeamSpeakError, type WebSpeakErrorCode } from "../errors.js";
|
|
import type { TeamSpeakTarget } from "../domain/teamspeak-target.js";
|
|
import { TSClient } from "./ts-client.js";
|
|
import type { TeamSpeakProtocol } from "./teamspeak-adapter.js";
|
|
|
|
export type ProbeErrorCode =
|
|
| "INVALID_NICKNAME"
|
|
| "HOST_NOT_FOUND"
|
|
| "UNREACHABLE"
|
|
| "CONNECTION_REFUSED"
|
|
| "CONNECTION_RESET"
|
|
| "TIMEOUT"
|
|
| "PASSWORD_REQUIRED"
|
|
| "INVALID_PASSWORD"
|
|
| "PROTOCOL_NEGOTIATION_FAILED"
|
|
| "SERVER_REJECTED"
|
|
| "INTERNAL_ERROR";
|
|
|
|
export interface TeamSpeakProbeResult {
|
|
ok: true;
|
|
protocol: TeamSpeakProtocol | null;
|
|
latencyMs: number;
|
|
serverName: string | null;
|
|
requiresPassword: boolean;
|
|
}
|
|
|
|
export class TeamSpeakProbeError extends Error {
|
|
constructor(readonly code: ProbeErrorCode, message: string, readonly cause?: unknown) {
|
|
super(message);
|
|
this.name = "TeamSpeakProbeError";
|
|
}
|
|
}
|
|
|
|
interface ProbeAdapter {
|
|
connect(): Promise<void>;
|
|
disconnect(): Promise<void>;
|
|
readonly protocol?: TeamSpeakProtocol | null;
|
|
getProtocol?(): TeamSpeakProtocol | null;
|
|
execCommandWithResponse?(command: string, timeoutMs?: number): Promise<Record<string, string>[]>;
|
|
readonly client?: {
|
|
execCommandWithResponse(command: string, timeoutMs?: number): Promise<Record<string, string>[]>;
|
|
};
|
|
}
|
|
|
|
export type ProbeAdapterFactory = (input: {
|
|
target: TeamSpeakTarget;
|
|
password: string;
|
|
logger: Logger;
|
|
}) => ProbeAdapter;
|
|
|
|
export async function probeTeamSpeak(
|
|
target: TeamSpeakTarget,
|
|
password: string,
|
|
logger: Logger,
|
|
factory: ProbeAdapterFactory = ({ target: probeTarget, password: probePassword, logger: probeLogger }) =>
|
|
(() => {
|
|
const client = new TSClient({ target: probeTarget, nickname: "WebSpeak Probe", serverPassword: probePassword }, probeLogger);
|
|
return {
|
|
connect: () => client.connect(),
|
|
disconnect: () => client.disconnect(),
|
|
getProtocol: () => client.getProtocol(),
|
|
execCommandWithResponse: (command: string, timeoutMs?: number) => client.execCommandWithResponse(command, timeoutMs),
|
|
};
|
|
})(),
|
|
): Promise<TeamSpeakProbeResult> {
|
|
const startedAt = Date.now();
|
|
const adapter = factory({ target, password, logger: logger.child({ component: "teamspeak-probe" }) });
|
|
try {
|
|
await adapter.connect();
|
|
let serverName: string | null = null;
|
|
try {
|
|
const execute = adapter.execCommandWithResponse?.bind(adapter) ?? adapter.client?.execCommandWithResponse.bind(adapter.client);
|
|
if (!execute) throw new Error("Probe adapter does not support commands");
|
|
const rows = await execute("serverinfo", 3000);
|
|
const first = rows[0] ?? {};
|
|
serverName = first.virtualserver_name ?? first.server_name ?? first.name ?? null;
|
|
} catch {
|
|
// A connected server can restrict serverinfo; reachability is still proven.
|
|
}
|
|
return {
|
|
ok: true,
|
|
protocol: adapter.protocol ?? adapter.getProtocol?.() ?? null,
|
|
latencyMs: Math.max(0, Date.now() - startedAt),
|
|
serverName,
|
|
requiresPassword: Boolean(password),
|
|
};
|
|
} catch (error: unknown) {
|
|
throw toProbeError(error, password);
|
|
} finally {
|
|
try {
|
|
await adapter.disconnect();
|
|
} catch (error: unknown) {
|
|
logger.warn({ err: extractErrorText(error) }, "TeamSpeak probe cleanup failed");
|
|
}
|
|
}
|
|
}
|
|
|
|
export function toProbeError(error: unknown, password = ""): TeamSpeakProbeError {
|
|
if (error instanceof TeamSpeakProbeError) return error;
|
|
const normalized = normalizeTeamSpeakError(error);
|
|
const raw = `${extractErrorText(error)} ${extractErrorText(normalized.cause)}`.toLocaleLowerCase();
|
|
if (/enotfound|eai_again|getaddrinfo|host not found/.test(raw)) {
|
|
return new TeamSpeakProbeError("HOST_NOT_FOUND", "TeamSpeak host was not found", error);
|
|
}
|
|
// 探测只回答「该地址是否可用」,因此把网关能细分的拒绝原因收敛到既有探测码,
|
|
// 具体原因由连接/重连路径向浏览器上报。
|
|
// The probe only answers "can this address be used at all", so the richer
|
|
// rejection reasons the gateway can now name are bucketed into the existing
|
|
// probe codes; the exact reason is reported by the connect/reconnect path.
|
|
const mapping: Record<WebSpeakErrorCode, ProbeErrorCode> = {
|
|
invalid_target: "INTERNAL_ERROR",
|
|
invalid_nickname: "INVALID_NICKNAME",
|
|
nickname_in_use: "INVALID_NICKNAME",
|
|
invalid_parameter_size: "INVALID_NICKNAME",
|
|
unreachable: "UNREACHABLE",
|
|
host_not_found: "HOST_NOT_FOUND",
|
|
connection_refused: "CONNECTION_REFUSED",
|
|
connection_reset: "CONNECTION_RESET",
|
|
timeout: "TIMEOUT",
|
|
authentication_failed: password.trim() ? "INVALID_PASSWORD" : "PASSWORD_REQUIRED",
|
|
channel_password_required: "PASSWORD_REQUIRED",
|
|
identity_security_level_too_low: "SERVER_REJECTED",
|
|
identity_limit_reached: "SERVER_REJECTED",
|
|
client_version_outdated: "SERVER_REJECTED",
|
|
flooding: "SERVER_REJECTED",
|
|
banned: "SERVER_REJECTED",
|
|
kicked: "SERVER_REJECTED",
|
|
server_shutting_down: "SERVER_REJECTED",
|
|
connection_initialisation_failed: "SERVER_REJECTED",
|
|
protocol_negotiation_failed: "PROTOCOL_NEGOTIATION_FAILED",
|
|
server_full: "SERVER_REJECTED",
|
|
unknown: "SERVER_REJECTED",
|
|
};
|
|
return new TeamSpeakProbeError(mapping[normalized.code] ?? "INTERNAL_ERROR", normalized.message, error);
|
|
}
|
|
|
|
function extractErrorText(error: unknown): string {
|
|
if (!error || typeof error !== "object") return String(error ?? "");
|
|
const candidate = error as { code?: unknown; id?: unknown; serverMessage?: unknown; message?: unknown };
|
|
return `${typeof candidate.code === "string" ? candidate.code : ""} ${typeof candidate.id === "string" || typeof candidate.id === "number" ? candidate.id : ""} ${typeof candidate.serverMessage === "string" ? candidate.serverMessage : ""} ${typeof candidate.message === "string" ? candidate.message : ""}`;
|
|
}
|