mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 20:12:52 +08:00
Adopt the pinned published TeamSpeak SDK and direct relay routing. Add optional TeamSpeak Query administration with encrypted credentials and separate WebSpeak access, site, operations and skin management. Distribute Discord Voice through the GitHub skin index, preserve skin state during updates, and correct KAAK scaling and screen fullscreen. Validation: 831 tests; backend and frontend builds; five-language UI checks from narrow screens through 4K and local Query fixture actions.
37 lines
1.6 KiB
TypeScript
37 lines
1.6 KiB
TypeScript
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
|
|
const ALGORITHM = "aes-256-gcm";
|
|
const VERSION = "v1";
|
|
const DEFAULT_AAD = "webspeak:teamspeak-password:v1";
|
|
|
|
export function encryptSecret(value: string, masterKey: Buffer, purpose = DEFAULT_AAD): string {
|
|
if (!value) return "";
|
|
assertMasterKey(masterKey);
|
|
const nonce = randomBytes(12);
|
|
const cipher = createCipheriv(ALGORITHM, masterKey, nonce);
|
|
cipher.setAAD(Buffer.from(purpose, "utf8"));
|
|
const ciphertext = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]);
|
|
const tag = cipher.getAuthTag();
|
|
return [VERSION, nonce.toString("base64url"), tag.toString("base64url"), ciphertext.toString("base64url")].join(":");
|
|
}
|
|
|
|
export function decryptSecret(payload: string | null | undefined, masterKey: Buffer, purpose = DEFAULT_AAD): string {
|
|
if (!payload) return "";
|
|
assertMasterKey(masterKey);
|
|
const [version, nonceText, tagText, ciphertextText, ...extra] = payload.split(":");
|
|
if (version !== VERSION || !nonceText || !tagText || ciphertextText === undefined || extra.length) {
|
|
throw new Error("Unsupported encrypted secret format");
|
|
}
|
|
const decipher = createDecipheriv(ALGORITHM, masterKey, Buffer.from(nonceText, "base64url"));
|
|
decipher.setAAD(Buffer.from(purpose, "utf8"));
|
|
decipher.setAuthTag(Buffer.from(tagText, "base64url"));
|
|
return Buffer.concat([
|
|
decipher.update(Buffer.from(ciphertextText, "base64url")),
|
|
decipher.final(),
|
|
]).toString("utf8");
|
|
}
|
|
|
|
function assertMasterKey(masterKey: Buffer): void {
|
|
if (masterKey.length !== 32) throw new Error("Invalid master key length");
|
|
}
|