release: refresh v0.1.7 diagnostics

This commit is contained in:
illusia committed 2026-09-06 13:47:37 +08:00
1 parent dc3ad6a598
commit dcc52c4530
11 files changed
+628 -45

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
import { createConnection } from "node:net";
import type { TeamSpeakTarget } from "../domain/teamspeak-target.js";
export type TeamSpeakPingErrorCode = "HOST_NOT_FOUND" | "UNREACHABLE" | "TIMEOUT";
export interface TeamSpeakPingAttempt {
ok: boolean;
latencyMs: number | null;
errorCode?: TeamSpeakPingErrorCode;
}
export interface TeamSpeakPingResult {
ok: boolean;
latencyMs: number | null;
packetLossPercent: number;
attempts: number;
successfulAttempts: number;
samples: number[];
errorCode?: TeamSpeakPingErrorCode;
}
/**
* Probe the TeamSpeak TCP endpoint without creating a TeamSpeak client.
* Browsers cannot send ICMP, and TeamSpeak does not expose an ICMP-style UDP
* echo, so a short TCP connect is the portable reachability signal available
* to the gateway. The socket is destroyed immediately after the handshake.
*/
export function pingTeamSpeakPort(
target: TeamSpeakTarget,
timeoutMs = 1_500,
): Promise<TeamSpeakPingAttempt> {
return new Promise((resolve) => {
const startedAt = Date.now();
const socket = createConnection({ host: target.host, port: target.port });
let settled = false;
const finish = (result: TeamSpeakPingAttempt) => {
if (settled) return;
settled = true;
socket.destroy();
resolve(result);
};
socket.setTimeout(timeoutMs, () => finish({ ok: false, latencyMs: null, errorCode: "TIMEOUT" }));
socket.once("connect", () => finish({ ok: true, latencyMs: Math.max(0, Date.now() - startedAt) }));
socket.once("error", (error: NodeJS.ErrnoException) => finish({
ok: false,
latencyMs: null,
errorCode: error.code === "ENOTFOUND" || error.code === "EAI_AGAIN" ? "HOST_NOT_FOUND" : "UNREACHABLE",
}));
});
}
export async function pingTeamSpeak(
target: TeamSpeakTarget,
options: { attempts?: number; timeoutMs?: number } = {},
): Promise<TeamSpeakPingResult> {
const attempts = Math.max(1, Math.min(8, Math.floor(options.attempts ?? 4)));
const timeoutMs = Math.max(250, Math.min(5_000, Math.floor(options.timeoutMs ?? 1_500)));
const results = await Promise.all(Array.from({ length: attempts }, () => pingTeamSpeakPort(target, timeoutMs)));
const samples = results
.filter((result): result is TeamSpeakPingAttempt & { latencyMs: number } => result.ok && result.latencyMs !== null)
.map((result) => result.latencyMs)
.sort((left, right) => left - right);
const errorCode = results.find((result) => result.errorCode)?.errorCode;
return {
ok: samples.length > 0,
latencyMs: samples.length ? samples[Math.floor(samples.length / 2)] : null,
packetLossPercent: Math.round(((attempts - samples.length) / attempts) * 100),
attempts,
successfulAttempts: samples.length,
samples,
...(errorCode ? { errorCode } : {}),
};
}
+19
View File
@@ -13,6 +13,7 @@ import { SessionManager, type ManagedSession, type SessionTeardownReason } from
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 { pingTeamSpeakPort } from "./network-probe.js";
const require = createRequire(import.meta.url);
const { OpusEncoder } = require("@discordjs/opus") as {
@@ -131,6 +132,7 @@ interface WebClientEntry {
reconnectTimer: ReturnType<typeof setTimeout> | null;
audio: AudioFlowStats;
webrtc: WebRtcAudioSession | null;
lastLatencyProbeAt: number;
}
export class VoiceBridge {
@@ -222,6 +224,7 @@ export class VoiceBridge {
reconnectTimer: null,
audio: createAudioFlowStats(),
webrtc: null,
lastLatencyProbeAt: 0,
};
this.entries.set(entryId, entry!);
try {
@@ -865,6 +868,22 @@ async function handleCommand(
command: ClientCommand,
sendJson: (message: Record<string, unknown>) => void,
): Promise<void> {
if (command.type === "latencyProbe") {
const sequence = command.payload.sequence as string;
const now = Date.now();
if (now - entry.lastLatencyProbeAt < 150) return;
entry.lastLatencyProbeAt = now;
const result = await pingTeamSpeakPort(entry.target);
sendJson({
type: "latencyPong",
sequence,
teamSpeakLatencyMs: result.latencyMs,
teamSpeakReachable: result.ok,
teamSpeakErrorCode: result.errorCode ?? null,
});
return;
}
if (command.type === "switchChannel") {
const rawId = command.payload.channelId as string;
const channelPassword = typeof command.payload.password === "string" ? command.payload.password : "";
+5 -2
View File
@@ -1,5 +1,5 @@
export interface ClientCommand {
type: "switchChannel" | "sendTextMessage" | "sendServerMessage" | "sendPrivateMessage" | "poke" | "setAway" | "setWhisperTargets" | "setWhisperActive" | "setMicrophoneMuted" | "setAccompanimentActive" | "setMemberVolume";
type: "switchChannel" | "sendTextMessage" | "sendServerMessage" | "sendPrivateMessage" | "poke" | "setAway" | "setWhisperTargets" | "setWhisperActive" | "setMicrophoneMuted" | "setAccompanimentActive" | "setMemberVolume" | "latencyProbe";
requestId?: string;
payload: Record<string, unknown>;
}
@@ -19,7 +19,7 @@ export function parseClientCommand(raw: string): ClientCommandResult {
if (value.requestId !== undefined && (typeof value.requestId !== "string" || value.requestId.length > 64)) {
return { error: { code: "INVALID_REQUEST_ID", message: "请求标识无效" } };
}
const supportedTypes = new Set(["switchChannel", "sendTextMessage", "sendServerMessage", "sendPrivateMessage", "poke", "setAway", "setWhisperTargets", "setWhisperActive", "setMicrophoneMuted", "setAccompanimentActive", "setMemberVolume"]);
const supportedTypes = new Set(["switchChannel", "sendTextMessage", "sendServerMessage", "sendPrivateMessage", "poke", "setAway", "setWhisperTargets", "setWhisperActive", "setMicrophoneMuted", "setAccompanimentActive", "setMemberVolume", "latencyProbe"]);
if (!supportedTypes.has(value.type)) {
return { error: { code: "UNKNOWN_MESSAGE_TYPE", message: "不支持的消息类型" } };
}
@@ -65,6 +65,9 @@ export function parseClientCommand(raw: string): ClientCommandResult {
if (value.type === "setMemberVolume" && (typeof value.payload.clientId !== "number" || !Number.isInteger(value.payload.clientId) || value.payload.clientId <= 0 || value.payload.clientId > 65535 || typeof value.payload.volume !== "number" || !Number.isFinite(value.payload.volume) || value.payload.volume < 0 || value.payload.volume > 4)) {
return { error: { code: "INVALID_MEMBER_VOLUME", message: "成员音量无效" } };
}
if (value.type === "latencyProbe" && (typeof value.payload.sequence !== "string" || value.payload.sequence.length > 64)) {
return { error: { code: "INVALID_LATENCY_PROBE", message: "延迟探测标识无效" } };
}
return {
type: value.type as ClientCommand["type"],
...(typeof value.requestId === "string" ? { requestId: value.requestId } : {}),