mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
feat: complete runtime and KAAK skin updates
This commit is contained in:
1 parent
c994722016
commit
7fd1ea96fe
40 files changed
+3436
-1088
No files matched your search
@@ -1,6 +1,74 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { isSafeOpenTargetForPrefill } from "./open-target-policy.js";
|
||||
import {
|
||||
isRestrictedAddress,
|
||||
isSafeOpenTargetForPrefill,
|
||||
resolveSafeOpenTarget,
|
||||
TeamSpeakTargetResolutionError,
|
||||
} from "./open-target-policy.js";
|
||||
|
||||
const resolved = (...addresses: string[]) => async () => addresses.map((address) => ({
|
||||
address,
|
||||
family: address.includes(":") ? 6 : 4,
|
||||
}));
|
||||
|
||||
test("Mihomo fake-IP DNS answers remain connectable without allowing literal or mixed fake-IP targets", async () => {
|
||||
const target = { host: "ts.twilightly.top", port: 9987 };
|
||||
|
||||
assert.equal(isRestrictedAddress("198.18.0.32"), true);
|
||||
assert.deepEqual(await resolveSafeOpenTarget(target, { lookup: resolved("198.18.0.32"), resolveSrv: async () => [] }), {
|
||||
host: "198.18.0.32",
|
||||
port: 9987,
|
||||
});
|
||||
await assert.rejects(resolveSafeOpenTarget({ host: "198.18.0.32", port: 9987 }));
|
||||
await assert.rejects(resolveSafeOpenTarget(target, { lookup: resolved("198.19.0.32"), resolveSrv: async () => [] }));
|
||||
await assert.rejects(resolveSafeOpenTarget(target, { lookup: resolved("198.18.0.32", "192.168.1.10"), resolveSrv: async () => [] }));
|
||||
await assert.rejects(resolveSafeOpenTarget(target, { lookup: resolved("198.18.0.32", "8.8.8.8"), resolveSrv: async () => [] }));
|
||||
});
|
||||
|
||||
test("TeamSpeak SRV records resolve before the input hostname and pin the advertised endpoint", async () => {
|
||||
const lookups: string[] = [];
|
||||
const target = await resolveSafeOpenTarget({ host: "ts.twilightly.top", port: 9987 }, {
|
||||
resolveSrv: async (service) => {
|
||||
assert.equal(service, "_ts3._udp.ts.twilightly.top");
|
||||
return [{ name: "routerstun.speedtest.twilightly.top.", port: 18967, priority: 0, weight: 5 }];
|
||||
},
|
||||
lookup: async (hostname) => {
|
||||
lookups.push(hostname);
|
||||
assert.equal(hostname, "routerstun.speedtest.twilightly.top");
|
||||
return [{ address: "183.200.23.34", family: 4 }];
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(target, { host: "183.200.23.34", port: 18967 });
|
||||
assert.deepEqual(lookups, ["routerstun.speedtest.twilightly.top"]);
|
||||
});
|
||||
|
||||
test("all TeamSpeak SRV destinations are checked and failed hostname resolution is classified", async () => {
|
||||
await assert.rejects(resolveSafeOpenTarget({ host: "voice.example.com", port: 9987 }, {
|
||||
resolveSrv: async () => [
|
||||
{ name: "public.example.net", port: 18967, priority: 0, weight: 5 },
|
||||
{ name: "private.example.net", port: 18967, priority: 0, weight: 5 },
|
||||
],
|
||||
lookup: async (hostname) => [{ address: hostname.startsWith("private") ? "10.0.0.8" : "8.8.8.8", family: 4 }],
|
||||
}));
|
||||
|
||||
await assert.rejects(resolveSafeOpenTarget({ host: "missing.example.com", port: 9987 }, {
|
||||
resolveSrv: async () => [],
|
||||
lookup: async () => { throw Object.assign(new Error("no records"), { code: "ENOTFOUND" }); },
|
||||
}), TeamSpeakTargetResolutionError);
|
||||
});
|
||||
|
||||
test("open-mode hostname checks still pin public DNS results and block ordinary private answers", async () => {
|
||||
assert.deepEqual(await resolveSafeOpenTarget({ host: "voice.example.com", port: 9987 }, {
|
||||
lookup: resolved("8.8.8.8", "1.1.1.1"),
|
||||
resolveSrv: async () => [],
|
||||
}), { host: "8.8.8.8", port: 9987 });
|
||||
await assert.rejects(resolveSafeOpenTarget({ host: "voice.example.com", port: 9987 }, {
|
||||
lookup: resolved("8.8.8.8", "10.0.0.5"),
|
||||
resolveSrv: async () => [],
|
||||
}));
|
||||
});
|
||||
|
||||
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 }, {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { isIP } from "node:net";
|
||||
import { lookup } from "node:dns/promises";
|
||||
import { lookup, resolveSrv } from "node:dns/promises";
|
||||
import type { TeamSpeakTarget } from "../domain/teamspeak-target.js";
|
||||
import { parseTeamSpeakTarget } from "../domain/teamspeak-target.js";
|
||||
|
||||
export class UnsafeTeamSpeakTargetError extends Error {
|
||||
constructor(message = "This TeamSpeak target is not allowed in open mode") {
|
||||
@@ -9,17 +10,77 @@ export class UnsafeTeamSpeakTargetError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class TeamSpeakTargetResolutionError extends Error {
|
||||
constructor(message = "TeamSpeak target could not be resolved", options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
this.name = "TeamSpeakTargetResolutionError";
|
||||
}
|
||||
}
|
||||
|
||||
/** Resolve and reject addresses that could turn a public gateway into an internal network probe. */
|
||||
export async function assertSafeOpenTarget(target: TeamSpeakTarget): Promise<void> {
|
||||
await resolveSafeOpenTarget(target);
|
||||
}
|
||||
|
||||
export async function resolveSafeOpenTarget(target: TeamSpeakTarget): Promise<TeamSpeakTarget> {
|
||||
type ResolvedAddress = { address: string; family: number };
|
||||
type AddressLookup = (hostname: string) => Promise<ResolvedAddress[]>;
|
||||
type TeamSpeakSrvRecord = { name: string; port: number; priority: number; weight: number };
|
||||
type TeamSpeakSrvLookup = (service: string) => Promise<TeamSpeakSrvRecord[]>;
|
||||
|
||||
export async function resolveSafeOpenTarget(
|
||||
target: TeamSpeakTarget,
|
||||
options: { lookup?: AddressLookup; resolveSrv?: TeamSpeakSrvLookup } = {},
|
||||
): Promise<TeamSpeakTarget> {
|
||||
const addressLookup = options.lookup ?? ((hostname) => lookup(hostname, { all: true, verbatim: true }));
|
||||
if (!isIP(target.host) && target.host.includes(".") && target.host !== "localhost") {
|
||||
const srvLookup = options.resolveSrv ?? resolveSrv;
|
||||
let srvRecords: TeamSpeakSrvRecord[] = [];
|
||||
try {
|
||||
srvRecords = await srvLookup(`_ts3._udp.${target.host}`);
|
||||
} catch {
|
||||
// No SRV record is normal; use the regular address lookup below.
|
||||
}
|
||||
|
||||
if (srvRecords.length > 0) {
|
||||
const srvTargets = srvRecords.map(({ name, port }) => {
|
||||
const srvHost = name.replace(/\.$/, "");
|
||||
if (!srvHost || !Number.isInteger(port) || port < 1 || port > 65535) {
|
||||
throw new TeamSpeakTargetResolutionError("Invalid TeamSpeak SRV record");
|
||||
}
|
||||
try {
|
||||
return parseTeamSpeakTarget(`${srvHost}:${port}`);
|
||||
} catch (error: unknown) {
|
||||
throw new TeamSpeakTargetResolutionError("Invalid TeamSpeak SRV record", { cause: error });
|
||||
}
|
||||
});
|
||||
const safeTargets = await Promise.all(srvTargets.map((srvTarget) => resolveSafeAddress(srvTarget, addressLookup)));
|
||||
// The SDK uses the first record returned by the system SRV resolver;
|
||||
// keep that ordering while validating every advertised destination.
|
||||
return safeTargets[0]!;
|
||||
}
|
||||
}
|
||||
|
||||
return resolveSafeAddress(target, addressLookup);
|
||||
}
|
||||
|
||||
async function resolveSafeAddress(target: TeamSpeakTarget, addressLookup: AddressLookup): Promise<TeamSpeakTarget> {
|
||||
const literalVersion = isIP(target.host);
|
||||
const addresses = literalVersion
|
||||
? [{ address: target.host, family: literalVersion }]
|
||||
: await lookup(target.host, { all: true, verbatim: true });
|
||||
if (!addresses.length || addresses.some(({ address }) => isRestrictedAddress(address))) {
|
||||
let addresses: ResolvedAddress[];
|
||||
try {
|
||||
addresses = literalVersion
|
||||
? [{ address: target.host, family: literalVersion }]
|
||||
: await addressLookup(target.host);
|
||||
} catch (error: unknown) {
|
||||
throw new TeamSpeakTargetResolutionError(undefined, { cause: error });
|
||||
}
|
||||
// Mihomo's default fake-IP pool is 198.18.0.0/16, allowing its TUN to
|
||||
// route the eventual socket by hostname. Accept this only for DNS names
|
||||
// whose complete answer set is synthetic; literal and mixed answers remain
|
||||
// blocked, preserving the open-mode SSRF boundary.
|
||||
const mihomoFakeIpResolution = !literalVersion
|
||||
&& addresses.length > 0
|
||||
&& addresses.every(({ address }) => isMihomoFakeIpv4(address));
|
||||
if (!addresses.length || (!mihomoFakeIpResolution && addresses.some(({ address }) => isRestrictedAddress(address)))) {
|
||||
throw new UnsafeTeamSpeakTargetError();
|
||||
}
|
||||
// Use the address that was actually validated so a second DNS lookup cannot
|
||||
@@ -68,6 +129,12 @@ export function isRestrictedAddress(address: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
function isMihomoFakeIpv4(address: string): boolean {
|
||||
if (isIP(address) !== 4) return false;
|
||||
const [first, second] = address.split(".").map(Number);
|
||||
return first === 198 && second === 18;
|
||||
}
|
||||
|
||||
function isRestrictedIpv4(address: string): boolean {
|
||||
const parts = address.split(".").map(Number);
|
||||
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return true;
|
||||
|
||||
Reference in new issue
Block a user