mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 20:12:52 +08:00
fix: align connection diagnostics and localize announcement
This commit is contained in:
1 parent
62e41dbc51
commit
a5e5dd6592
11 files changed
+87
-27
No files matched your search
@@ -14,7 +14,7 @@ export interface AdminConnectionRecord {
|
||||
startedAt: string;
|
||||
connectedAt: string | null;
|
||||
disconnectedAt: string | null;
|
||||
durationSeconds: number;
|
||||
durationSeconds: number | null;
|
||||
status: "active" | "connecting" | "disconnected" | "failed";
|
||||
reason: string | null;
|
||||
}
|
||||
@@ -349,6 +349,7 @@ function readSettingsInput(body: Record<string, unknown>): AdminSettingsInput {
|
||||
accessMode: body.accessMode === "open" ? "open" : body.accessMode === "fixed" ? "fixed" : body.accessMode as never,
|
||||
siteName: readString(body, "siteName", 80),
|
||||
welcomeText: readOptionalString(body, "welcomeText", 500),
|
||||
welcomeTextEn: typeof body.welcomeTextEn === "string" ? body.welcomeTextEn.slice(0, 500) : undefined,
|
||||
webRtcEnabled: body.webRtcEnabled === true,
|
||||
webRtcUdpStart: readOptionalInteger(body, "webRtcUdpStart"),
|
||||
webRtcUdpEnd: readOptionalInteger(body, "webRtcUdpEnd"),
|
||||
@@ -475,7 +476,7 @@ export function readConnectionHistory(logFile: string | undefined, limit: number
|
||||
? Math.max(0, Math.floor(log.raw.durationSeconds))
|
||||
: current.connectedAt
|
||||
? Math.max(0, Math.floor((Date.parse(log.timestamp) - Date.parse(current.connectedAt)) / 1000))
|
||||
: 0;
|
||||
: null;
|
||||
current.reason = typeof log.raw.reason === "string" ? log.raw.reason : null;
|
||||
current.status = current.connectedAt ? "disconnected" : "failed";
|
||||
}
|
||||
@@ -494,7 +495,9 @@ export function readConnectionHistory(logFile: string | undefined, limit: number
|
||||
startedAt: record.startedAt,
|
||||
connectedAt: record.connectedAt,
|
||||
disconnectedAt: record.disconnectedAt,
|
||||
durationSeconds: record.durationSeconds ?? Math.max(0, Math.floor((end - Date.parse(start)) / 1000)),
|
||||
durationSeconds: record.durationSeconds ?? (record.status === "active" || record.status === "connecting"
|
||||
? Math.max(0, Math.floor((end - Date.parse(start)) / 1000))
|
||||
: null),
|
||||
status: record.status,
|
||||
reason: record.reason,
|
||||
};
|
||||
|
||||
@@ -17,6 +17,7 @@ export interface AdminSettingsInput {
|
||||
accessMode: AccessMode;
|
||||
siteName: string;
|
||||
welcomeText: string;
|
||||
welcomeTextEn?: string;
|
||||
webRtcEnabled: boolean;
|
||||
webRtcUdpStart?: number;
|
||||
webRtcUdpEnd?: number;
|
||||
@@ -97,6 +98,7 @@ export class AdminService {
|
||||
initialized: this.isInitialized(),
|
||||
siteName: settings.siteName,
|
||||
welcomeText: settings.welcomeText,
|
||||
welcomeTextEn: settings.welcomeTextEn,
|
||||
accessMode: settings.accessMode,
|
||||
target: formatTeamSpeakTarget({ host: settings.tsHost, port: settings.tsPort }),
|
||||
};
|
||||
@@ -110,6 +112,7 @@ export class AdminService {
|
||||
accessMode: settings.accessMode,
|
||||
siteName: settings.siteName,
|
||||
welcomeText: settings.welcomeText,
|
||||
welcomeTextEn: settings.welcomeTextEn,
|
||||
lastTestAt: settings.lastTestAt,
|
||||
lastTestLatencyMs: settings.lastTestLatencyMs,
|
||||
lastTestError: settings.lastTestError,
|
||||
@@ -263,8 +266,10 @@ export class AdminService {
|
||||
}
|
||||
const siteName = input.siteName.trim();
|
||||
const welcomeText = input.welcomeText.trim();
|
||||
const welcomeTextEn = typeof input.welcomeTextEn === "string" ? input.welcomeTextEn.trim() : current.welcomeTextEn;
|
||||
if (!siteName || siteName.length > 80) throw new AdminInputError("INVALID_SITE_NAME", "Site name must contain 1 to 80 characters");
|
||||
if (welcomeText.length > 500) throw new AdminInputError("INVALID_WELCOME_TEXT", "Welcome text cannot exceed 500 characters");
|
||||
if (welcomeTextEn.length > 500) throw new AdminInputError("INVALID_WELCOME_TEXT_EN", "English welcome text cannot exceed 500 characters");
|
||||
if (input.accessMode !== "fixed" && input.accessMode !== "open") {
|
||||
throw new AdminInputError("INVALID_ACCESS_MODE", "Access mode is invalid");
|
||||
}
|
||||
@@ -293,6 +298,7 @@ export class AdminService {
|
||||
return {
|
||||
siteName,
|
||||
welcomeText,
|
||||
welcomeTextEn,
|
||||
accessMode: input.accessMode,
|
||||
tsHost: target.host,
|
||||
tsPort: target.port,
|
||||
@@ -324,6 +330,7 @@ export class AdminService {
|
||||
return {
|
||||
siteName: settings.siteName,
|
||||
welcomeText: settings.welcomeText,
|
||||
welcomeTextEn: settings.welcomeTextEn,
|
||||
accessMode: settings.accessMode,
|
||||
tsHost: settings.tsHost,
|
||||
tsPort: settings.tsPort,
|
||||
@@ -342,6 +349,7 @@ export class AdminService {
|
||||
this.database.updateSettings({
|
||||
siteName: current.siteName,
|
||||
welcomeText: current.welcomeText,
|
||||
welcomeTextEn: current.welcomeTextEn,
|
||||
accessMode: current.accessMode,
|
||||
tsHost: legacy.tsHost,
|
||||
tsPort: legacy.tsPort,
|
||||
|
||||
@@ -6,12 +6,13 @@ import type { AdminCredential } from "../security/admin-password.js";
|
||||
import type { TeamSpeakProtocol } from "../server/teamspeak-adapter.js";
|
||||
import { DEFAULT_WEBRTC_UDP_PORT_RANGE } from "../server/webrtc-config.js";
|
||||
|
||||
export const DATABASE_SCHEMA_VERSION = 3;
|
||||
export const DATABASE_SCHEMA_VERSION = 4;
|
||||
export type AccessMode = "fixed" | "open";
|
||||
|
||||
export interface PersistedSettings {
|
||||
siteName: string;
|
||||
welcomeText: string;
|
||||
welcomeTextEn: string;
|
||||
accessMode: AccessMode;
|
||||
tsHost: string;
|
||||
tsPort: number;
|
||||
@@ -29,6 +30,7 @@ export interface PersistedSettings {
|
||||
export interface SettingsUpdate {
|
||||
siteName: string;
|
||||
welcomeText: string;
|
||||
welcomeTextEn?: string;
|
||||
accessMode: AccessMode;
|
||||
tsHost: string;
|
||||
tsPort: number;
|
||||
@@ -69,6 +71,7 @@ interface ManagedInviteRow extends Record<string, unknown> {
|
||||
interface SettingsRow extends Record<string, unknown> {
|
||||
site_name: string;
|
||||
welcome_text: string;
|
||||
welcome_text_en: string;
|
||||
access_mode: string;
|
||||
ts_host: string;
|
||||
ts_port: number;
|
||||
@@ -143,6 +146,7 @@ export class WebSpeakDatabase {
|
||||
return {
|
||||
siteName: row.site_name,
|
||||
welcomeText: row.welcome_text,
|
||||
welcomeTextEn: row.welcome_text_en,
|
||||
accessMode: row.access_mode === "open" ? "open" : "fixed",
|
||||
tsHost: row.ts_host,
|
||||
tsPort: row.ts_port,
|
||||
@@ -377,19 +381,29 @@ export class WebSpeakDatabase {
|
||||
`);
|
||||
this.database.exec("PRAGMA user_version = 3");
|
||||
});
|
||||
version = 3;
|
||||
}
|
||||
if (version === 3) {
|
||||
this.transaction(() => {
|
||||
this.database.exec(`
|
||||
ALTER TABLE settings ADD COLUMN welcome_text_en TEXT NOT NULL DEFAULT '';
|
||||
`);
|
||||
this.database.exec("PRAGMA user_version = 4");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private writeSettings(settings: SettingsUpdate, now: string): void {
|
||||
this.database.prepare(
|
||||
`UPDATE settings SET
|
||||
site_name = ?, welcome_text = ?, access_mode = ?, ts_host = ?, ts_port = ?,
|
||||
site_name = ?, welcome_text = ?, welcome_text_en = ?, access_mode = ?, ts_host = ?, ts_port = ?,
|
||||
ts_password_encrypted = ?, webrtc_enabled = ?, webrtc_udp_start = ?,
|
||||
webrtc_udp_end = ?, updated_at = ?
|
||||
WHERE id = 1`,
|
||||
).run(
|
||||
settings.siteName,
|
||||
settings.welcomeText,
|
||||
settings.welcomeTextEn ?? "",
|
||||
settings.accessMode,
|
||||
settings.tsHost,
|
||||
settings.tsPort,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import type { Logger } from "../logger.js";
|
||||
import { normalizeTeamSpeakError, type WebSpeakErrorCode } from "../errors.js";
|
||||
import type { TeamSpeakTarget } from "../domain/teamspeak-target.js";
|
||||
import { TeamSpeakAdapter, type TeamSpeakProtocol } from "./teamspeak-adapter.js";
|
||||
import { TSClient } from "./ts-client.js";
|
||||
import type { TeamSpeakProtocol } from "./teamspeak-adapter.js";
|
||||
|
||||
export type ProbeErrorCode =
|
||||
| "HOST_NOT_FOUND"
|
||||
@@ -30,8 +31,10 @@ export class TeamSpeakProbeError extends Error {
|
||||
interface ProbeAdapter {
|
||||
connect(): Promise<void>;
|
||||
disconnect(): Promise<void>;
|
||||
readonly protocol: TeamSpeakProtocol | null;
|
||||
readonly client: {
|
||||
readonly protocol?: TeamSpeakProtocol | null;
|
||||
getProtocol?(): TeamSpeakProtocol | null;
|
||||
execCommandWithResponse?(command: string, timeoutMs?: number): Promise<Record<string, string>[]>;
|
||||
readonly client?: {
|
||||
execCommandWithResponse(command: string, timeoutMs?: number): Promise<Record<string, string>[]>;
|
||||
};
|
||||
}
|
||||
@@ -47,7 +50,15 @@ export async function probeTeamSpeak(
|
||||
password: string,
|
||||
logger: Logger,
|
||||
factory: ProbeAdapterFactory = ({ target: probeTarget, password: probePassword, logger: probeLogger }) =>
|
||||
new TeamSpeakAdapter({ target: probeTarget, nickname: "WebSpeak Probe", serverPassword: probePassword }, probeLogger),
|
||||
(() => {
|
||||
const client = new TSClient({ target: probeTarget, nickname: "WebSpeak Probe", serverPassword: probePassword }, probeLogger);
|
||||
return {
|
||||
connect: () => client.connect(),
|
||||
disconnect: () => client.disconnect(),
|
||||
getProtocol: () => client.getProtocol(),
|
||||
execCommandWithResponse: (command: string, timeoutMs?: number) => client.execCommandWithResponse(command, timeoutMs),
|
||||
};
|
||||
})(),
|
||||
): Promise<TeamSpeakProbeResult> {
|
||||
const startedAt = Date.now();
|
||||
const adapter = factory({ target, password, logger: logger.child({ component: "teamspeak-probe" }) });
|
||||
@@ -55,7 +66,9 @@ export async function probeTeamSpeak(
|
||||
await adapter.connect();
|
||||
let serverName: string | null = null;
|
||||
try {
|
||||
const rows = await adapter.client.execCommandWithResponse("serverinfo", 3000);
|
||||
const execute = adapter.execCommandWithResponse?.bind(adapter) ?? adapter.client?.execCommandWithResponse.bind(adapter.client);
|
||||
if (!execute) throw new Error("Probe adapter does not support commands");
|
||||
const rows = await execute("serverinfo", 3000);
|
||||
const first = rows[0] ?? {};
|
||||
serverName = first.virtualserver_name ?? first.server_name ?? first.name ?? null;
|
||||
} catch {
|
||||
@@ -63,7 +76,7 @@ export async function probeTeamSpeak(
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
protocol: adapter.protocol,
|
||||
protocol: adapter.protocol ?? adapter.getProtocol?.() ?? null,
|
||||
latencyMs: Math.max(0, Date.now() - startedAt),
|
||||
serverName,
|
||||
requiresPassword: Boolean(password),
|
||||
|
||||
@@ -247,6 +247,11 @@ export class TSClient extends EventEmitter {
|
||||
return this.adapter?.protocol ?? null;
|
||||
}
|
||||
|
||||
async execCommandWithResponse(command: string, timeoutMs = 3000): Promise<Record<string, string>[]> {
|
||||
if (!this.client || !this.connected) throw new Error("TeamSpeak session is not ready");
|
||||
return this.client.execCommandWithResponse(command, timeoutMs);
|
||||
}
|
||||
|
||||
getIdentityString(): string {
|
||||
return this.identity.toString();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user