mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
feat(kaak): render TeamSpeak server banner
This commit is contained in:
1 parent
766a9b9b5a
commit
398a4d6c7f
24 files changed
+335
-27
No files matched your search
@@ -19,6 +19,7 @@ class SdkStub extends EventEmitter {
|
||||
inputCommands: string[] = [];
|
||||
fileTransfers: Array<{ channelId: bigint; path: string; password: string }> = [];
|
||||
serverInfo: { name: string; iconId: number | null } | null = { name: "兄弟们的teamspeak", iconId: 123456 };
|
||||
serverPresentationRows: Array<Record<string, string>> = [];
|
||||
inputUpdate = async (_command: string): Promise<void> => {};
|
||||
async execCommand(command: string) { this.inputCommands.push(command); await this.inputUpdate(command); }
|
||||
initialized = 0;
|
||||
@@ -26,7 +27,10 @@ class SdkStub extends EventEmitter {
|
||||
metadata = async () => rows;
|
||||
transfer = async () => transfer;
|
||||
download = async (destination: Writable) => { destination.write(Buffer.from("GIF89a")); };
|
||||
execCommandWithResponse(command = "") { this.inputCommands.push(command); return this.metadata(); }
|
||||
execCommandWithResponse(command = "") {
|
||||
this.inputCommands.push(command);
|
||||
return command === "serverinfo" ? Promise.resolve(this.serverPresentationRows) : this.metadata();
|
||||
}
|
||||
async fileTransferInitDownload(channelId: bigint, path: string, password: string) {
|
||||
this.initialized++;
|
||||
this.fileTransfers.push({ channelId, path, password });
|
||||
@@ -67,6 +71,27 @@ test("server info uses the name and icon id captured by the SDK handshake", asyn
|
||||
assert.deepEqual(await f.client.getServerInfo(), { name: "No icon", iconId: null });
|
||||
});
|
||||
|
||||
test("server presentation reads bounded public banner and welcome fields", async () => {
|
||||
const f = fixture();
|
||||
f.sdk.serverPresentationRows = [{
|
||||
virtualserver_hostbanner_gfx_url: "https://assets.example.invalid/teamspeak/banner.png",
|
||||
virtualserver_welcomemessage: "欢迎来到服务器!\n请遵守频道规则。",
|
||||
}];
|
||||
assert.deepEqual(await f.client.getServerPresentation(), {
|
||||
bannerUrl: "https://assets.example.invalid/teamspeak/banner.png",
|
||||
welcomeMessage: "欢迎来到服务器!\n请遵守频道规则。",
|
||||
});
|
||||
assert.deepEqual(f.sdk.inputCommands, ["serverinfo"]);
|
||||
|
||||
f.sdk.serverPresentationRows = [{
|
||||
virtualserver_hostbanner_gfx_url: "javascript:alert(1)",
|
||||
virtualserver_welcomemessage: "x".repeat(5_000),
|
||||
}];
|
||||
const sanitized = await f.client.getServerPresentation();
|
||||
assert.equal(sanitized.bannerUrl, null);
|
||||
assert.equal(sanitized.welcomeMessage.length, 4_096);
|
||||
});
|
||||
|
||||
test("server icons use the TeamSpeak file transfer path and enforce the download limit", async () => {
|
||||
const f = fixture();
|
||||
assert.equal((await f.client.getServerIcon(123456))?.toString(), "GIF89a");
|
||||
|
||||
@@ -49,11 +49,18 @@ export interface TSClientServerInfo {
|
||||
iconId: number | null;
|
||||
}
|
||||
|
||||
export interface TSClientServerPresentation {
|
||||
bannerUrl: string | null;
|
||||
welcomeMessage: string;
|
||||
}
|
||||
|
||||
// Keep enough room for avatars produced by the native client. The previous
|
||||
// 120 KiB limit rejected the 134 KiB PNG used by the Shanghai test account,
|
||||
// so every avatar silently fell back to its initial letter.
|
||||
const MAX_CLIENT_AVATAR_BYTES = 256 * 1024;
|
||||
const MAX_SERVER_ICON_BYTES = 128 * 1024;
|
||||
const MAX_SERVER_BANNER_URL_LENGTH = 2_048;
|
||||
const MAX_SERVER_WELCOME_MESSAGE_LENGTH = 4_096;
|
||||
|
||||
export type TSDirectoryChannel = DirectorySnapshot["channels"][number] & Partial<ChannelCapacity>;
|
||||
export type TSDirectorySnapshot = Omit<DirectorySnapshot, "channels"> & { channels: TSDirectoryChannel[] };
|
||||
@@ -291,6 +298,29 @@ export class TSClient extends EventEmitter {
|
||||
return client.serverInfo ?? { name: "", iconId: null };
|
||||
}
|
||||
|
||||
/** Read only the public banner and welcome message from the active virtual server. */
|
||||
async getServerPresentation(): Promise<TSClientServerPresentation> {
|
||||
const client = this.client;
|
||||
if (!client || !this.connected) throw new Error("TeamSpeak session is not ready");
|
||||
const generation = this.connectionGeneration;
|
||||
const isCurrent = () => this.client === client && this.connected && this.connectionGeneration === generation;
|
||||
const empty: TSClientServerPresentation = { bannerUrl: null, welcomeMessage: "" };
|
||||
try {
|
||||
const rows = await client.execCommandWithResponse("serverinfo", 5_000);
|
||||
if (!isCurrent()) throw new Error("TeamSpeak session changed while reading server presentation");
|
||||
const row = rows[0] ?? {};
|
||||
return {
|
||||
bannerUrl: normalizeTeamSpeakBannerUrl(row.virtualserver_hostbanner_gfx_url),
|
||||
welcomeMessage: normalizeTeamSpeakWelcomeMessage(row.virtualserver_welcomemessage),
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
if (!isCurrent()) throw error;
|
||||
// Some servers restrict serverinfo. Keep the voice connection usable and
|
||||
// omit the optional presentation when the fields cannot be read.
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
/** Download the public virtual-server icon using TeamSpeak's normal file-transfer port. */
|
||||
async getServerIcon(iconId: number): Promise<Buffer | null> {
|
||||
const client = this.client;
|
||||
@@ -569,6 +599,24 @@ export class TSClient extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeTeamSpeakBannerUrl(value: unknown): string | null {
|
||||
if (typeof value !== "string") return null;
|
||||
const candidate = value.trim();
|
||||
if (!candidate || candidate.length > MAX_SERVER_BANNER_URL_LENGTH) return null;
|
||||
try {
|
||||
const url = new URL(candidate);
|
||||
if ((url.protocol !== "https:" && url.protocol !== "http:") || url.username || url.password) return null;
|
||||
return url.toString();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeTeamSpeakWelcomeMessage(value: unknown): string {
|
||||
if (typeof value !== "string") return "";
|
||||
return value.replace(/\0/g, "").replace(/\r\n?/g, "\n").slice(0, MAX_SERVER_WELCOME_MESSAGE_LENGTH).trim();
|
||||
}
|
||||
|
||||
function escapeTeamSpeakValue(value: string): string {
|
||||
return value
|
||||
.replace(/\\/g, "\\\\")
|
||||
|
||||
@@ -23,12 +23,17 @@ class TeamSpeakStub extends EventEmitter {
|
||||
emitDirectorySnapshotOnConnect = true;
|
||||
connectCalls = 0;
|
||||
serverInfoCalls = 0;
|
||||
serverPresentationCalls = 0;
|
||||
async connect() { this.connectCalls++; this.disconnected = false; if (this.emitDirectorySnapshotOnConnect) this.emit("directorySnapshot", this.directory); }
|
||||
async disconnect() { this.disconnected = true; }
|
||||
getClientId() { return 1; }
|
||||
getChannelId() { return 1n; }
|
||||
isConnected() { return !this.disconnected; }
|
||||
async getServerInfo() { this.serverInfoCalls++; return { name: "兄弟们的teamspeak", iconId: null }; }
|
||||
async getServerPresentation() {
|
||||
this.serverPresentationCalls++;
|
||||
return { bannerUrl: "https://assets.example.invalid/teamspeak/banner.png", welcomeMessage: "欢迎来到服务器!\n请遵守频道规则。" };
|
||||
}
|
||||
async getServerIcon() { return null; }
|
||||
async sendProtocolCommand() {}
|
||||
async setAccompanimentActive() {}
|
||||
@@ -98,15 +103,27 @@ test("server identity lookup waits until the directory sync publishes the connec
|
||||
f.socket.on("message", (data, binary) => {
|
||||
if (binary) return;
|
||||
const message = JSON.parse(data.toString()) as Record<string, unknown>;
|
||||
if (message.type === "serverInfo") resolve(message);
|
||||
if (message.type === "serverInfo" && message.bannerUrl === null) resolve(message);
|
||||
});
|
||||
});
|
||||
const serverPresentation = new Promise<Record<string, unknown>>(resolve => {
|
||||
f.socket.on("message", (data, binary) => {
|
||||
if (binary) return;
|
||||
const message = JSON.parse(data.toString()) as Record<string, unknown>;
|
||||
if (message.type === "serverInfo" && message.bannerUrl === "https://assets.example.invalid/teamspeak/banner.png") resolve(message);
|
||||
});
|
||||
});
|
||||
f.sdk.emit("directorySnapshot", f.sdk.directory);
|
||||
assert.deepEqual(await serverInfo, { type: "serverInfo", name: "兄弟们的teamspeak", icon: null });
|
||||
assert.deepEqual(await serverInfo, { type: "serverInfo", name: "兄弟们的teamspeak", icon: null, bannerUrl: null, welcomeMessage: "" });
|
||||
assert.deepEqual(await serverPresentation, {
|
||||
type: "serverInfo", name: "兄弟们的teamspeak", icon: null,
|
||||
bannerUrl: "https://assets.example.invalid/teamspeak/banner.png", welcomeMessage: "欢迎来到服务器!\n请遵守频道规则。",
|
||||
});
|
||||
const publishedTypes = f.messages.filter(message => !message.binary).map(message => (JSON.parse(message.data.toString()) as { type: string }).type);
|
||||
assert.ok(publishedTypes.indexOf("connected") >= 0);
|
||||
assert.ok(publishedTypes.indexOf("serverInfo") > publishedTypes.indexOf("connected"));
|
||||
assert.equal(f.sdk.serverInfoCalls, 1);
|
||||
assert.equal(f.sdk.serverPresentationCalls, 1);
|
||||
assert.ok(f.bridge.getActiveCount() > 0);
|
||||
});
|
||||
|
||||
|
||||
@@ -310,12 +310,20 @@ export class VoiceBridge {
|
||||
const info = await tsClient.getServerInfo();
|
||||
if (!isCurrent()) return;
|
||||
const name = info.name.trim().slice(0, 256);
|
||||
sendJson({ type: "serverInfo", name, icon: null });
|
||||
if (!info.iconId) return;
|
||||
const icon = await tsClient.getServerIcon(info.iconId);
|
||||
if (!icon || !isCurrent()) return;
|
||||
const dataUrl = avatarDataUrl(icon);
|
||||
if (dataUrl) sendJson({ type: "serverInfo", name, icon: dataUrl });
|
||||
const publicInfo = { name, icon: null as string | null, bannerUrl: null as string | null, welcomeMessage: "" };
|
||||
sendJson({ type: "serverInfo", ...publicInfo });
|
||||
const [presentation, icon] = await Promise.all([
|
||||
tsClient.getServerPresentation().catch((error: unknown) => {
|
||||
if (isCurrent()) this.logger.debug({ entryId, err: error instanceof Error ? error.message : String(error) }, "TeamSpeak server presentation unavailable");
|
||||
return { bannerUrl: null, welcomeMessage: "" };
|
||||
}),
|
||||
info.iconId ? tsClient.getServerIcon(info.iconId).catch(() => null) : Promise.resolve(null),
|
||||
]);
|
||||
if (!isCurrent()) return;
|
||||
publicInfo.bannerUrl = presentation.bannerUrl;
|
||||
publicInfo.welcomeMessage = presentation.welcomeMessage;
|
||||
publicInfo.icon = icon ? avatarDataUrl(icon) : null;
|
||||
sendJson({ type: "serverInfo", ...publicInfo });
|
||||
} catch (error: unknown) {
|
||||
if (isCurrent()) this.logger.debug({ entryId, err: error instanceof Error ? error.message : String(error) }, "TeamSpeak server icon unavailable");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user