feat: report live voice status and harden target handling

This commit is contained in:
illusia committed 2026-09-28 22:28:16 +08:00
1 parent e0d5916a86
commit 0a4fff6f6f
19 files changed
+1003 -101

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
import assert from "node:assert/strict";
import test from "node:test";
import { isSafeOpenTargetForPrefill } from "./open-target-policy.js";
test("open-mode target prefill follows the same allow/deny result as the join policy", async () => {
assert.equal(await isSafeOpenTargetForPrefill({ host: "198.51.100.24", port: 9987 }, {
resolveTarget: async (target) => target,
}), true);
assert.equal(await isSafeOpenTargetForPrefill({ host: "teamspeak", port: 9987 }, {
resolveTarget: async () => { throw new Error("private or unresolved target"); },
}), false);
});
test("open-mode target prefill fails closed when DNS validation stalls", async () => {
const result = await isSafeOpenTargetForPrefill({ host: "internal.example", port: 9987 }, {
timeoutMs: 10,
resolveTarget: async () => new Promise(() => undefined),
});
assert.equal(result, false);
});
+20
View File
@@ -27,6 +27,26 @@ export async function resolveSafeOpenTarget(target: TeamSpeakTarget): Promise<Te
return { host: addresses[0]!.address, port: target.port };
}
/** A bounded, non-authoritative check used only to avoid pre-filling a target that open mode will reject. */
export async function isSafeOpenTargetForPrefill(
target: TeamSpeakTarget,
options: { timeoutMs?: number; resolveTarget?: (target: TeamSpeakTarget) => Promise<TeamSpeakTarget> } = {},
): Promise<boolean> {
const timeoutMs = options.timeoutMs ?? 1_000;
const resolveTarget = options.resolveTarget ?? resolveSafeOpenTarget;
let timer: ReturnType<typeof setTimeout> | undefined;
try {
return await Promise.race([
Promise.resolve().then(() => resolveTarget(target)).then(() => true, () => false),
new Promise<boolean>((resolve) => {
timer = setTimeout(() => resolve(false), timeoutMs);
}),
]);
} finally {
if (timer !== undefined) clearTimeout(timer);
}
}
export function isRestrictedAddress(address: string): boolean {
const version = isIP(address);
if (version === 4) return isRestrictedIpv4(address);