mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
feat: show client IP in connection records
This commit is contained in:
1 parent
8046985574
commit
69043748a1
3 files changed
+45
-5
No files matched your search
@@ -10,6 +10,7 @@ import type { AdminSessionSummary } from "../server/voice-bridge.js";
|
||||
export interface AdminConnectionRecord {
|
||||
id: string;
|
||||
nickname: string;
|
||||
clientIp: string;
|
||||
target: string;
|
||||
startedAt: string;
|
||||
connectedAt: string | null;
|
||||
@@ -430,7 +431,7 @@ function readRecentLogs(logFile: string | undefined, limit: number): AdminLogEnt
|
||||
try {
|
||||
const raw = JSON.parse(line) as Record<string, unknown>;
|
||||
const context: Record<string, string | number | boolean> = {};
|
||||
for (const key of ["component", "entryId", "code", "reason", "attempt", "target", "nickname", "channel", "reconnect", "port"]) {
|
||||
for (const key of ["component", "entryId", "code", "reason", "attempt", "target", "nickname", "clientIp", "channel", "reconnect", "port"]) {
|
||||
const value = raw[key];
|
||||
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") context[key] = value;
|
||||
}
|
||||
@@ -460,6 +461,7 @@ export function readConnectionHistory(logFile: string | undefined, limit: number
|
||||
const records = new Map<string, {
|
||||
id: string;
|
||||
nickname: string;
|
||||
clientIp: string;
|
||||
target: string;
|
||||
startedAt: string | null;
|
||||
connectedAt: string | null;
|
||||
@@ -474,6 +476,7 @@ export function readConnectionHistory(logFile: string | undefined, limit: number
|
||||
const current = records.get(entryId) ?? {
|
||||
id: entryId,
|
||||
nickname: "",
|
||||
clientIp: "",
|
||||
target: "",
|
||||
startedAt: null,
|
||||
connectedAt: null,
|
||||
@@ -483,8 +486,10 @@ export function readConnectionHistory(logFile: string | undefined, limit: number
|
||||
reason: null,
|
||||
};
|
||||
const nickname = typeof log.raw.nickname === "string" ? log.raw.nickname : "";
|
||||
const clientIp = typeof log.raw.clientIp === "string" ? log.raw.clientIp : "";
|
||||
const target = typeof log.raw.target === "string" ? log.raw.target : "";
|
||||
if (nickname) current.nickname = nickname;
|
||||
if (clientIp) current.clientIp = clientIp;
|
||||
if (target) current.target = target;
|
||||
if (log.message === "WebClient connecting") {
|
||||
current.startedAt ??= log.timestamp;
|
||||
@@ -521,6 +526,7 @@ export function readConnectionHistory(logFile: string | undefined, limit: number
|
||||
return {
|
||||
id: record.id,
|
||||
nickname: record.nickname || "—",
|
||||
clientIp: record.clientIp || "—",
|
||||
target: record.target || "—",
|
||||
startedAt: record.startedAt,
|
||||
connectedAt: record.connectedAt,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { WebSocketServer, WebSocket } from "ws";
|
||||
import type { IncomingMessage, Server } from "node:http";
|
||||
import { createRequire } from "node:module";
|
||||
import { isIP } from "node:net";
|
||||
import { identityFromString } from "@echosixhiya/teamspeak-client";
|
||||
import { DirectorySynchronizer } from "./directory-sync.js";
|
||||
import { TSClient, type TSDirectorySnapshot, type TSVoiceData } from "./ts-client.js";
|
||||
@@ -122,6 +123,7 @@ interface WebClientEntry {
|
||||
ws: WebSocket;
|
||||
nickname: string;
|
||||
rememberIdentity: boolean;
|
||||
clientIp: string;
|
||||
target: TeamSpeakTarget;
|
||||
acceleration?: AccelerationRelayOptions;
|
||||
identityLeaseKey?: string;
|
||||
@@ -174,6 +176,7 @@ export class VoiceBridge {
|
||||
ws.close(4006, "ACCELERATION_UNAVAILABLE");
|
||||
return;
|
||||
}
|
||||
const clientIp = resolveClientIp(req);
|
||||
const webrtcPublicHost = resolveWebRtcPublicHost(req);
|
||||
let identity;
|
||||
try {
|
||||
@@ -203,7 +206,7 @@ export class VoiceBridge {
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.info({ entryId, nickname, channel: channelName, target: formatTeamSpeakTarget(target) }, "WebClient connecting");
|
||||
this.logger.info({ entryId, nickname, clientIp, channel: channelName, target: formatTeamSpeakTarget(target) }, "WebClient connecting");
|
||||
let tsClient: TSClient;
|
||||
try {
|
||||
tsClient = new TSClient({ target, nickname, serverPassword, defaultChannel: channelName, identity, ...(acceleration ? { acceleration } : {}) }, this.logger);
|
||||
@@ -221,6 +224,7 @@ export class VoiceBridge {
|
||||
ws,
|
||||
nickname,
|
||||
rememberIdentity: connection.rememberIdentity === true,
|
||||
clientIp,
|
||||
target,
|
||||
...(acceleration ? { acceleration } : {}),
|
||||
...(identityLeaseKey ? { identityLeaseKey } : {}),
|
||||
@@ -321,7 +325,7 @@ export class VoiceBridge {
|
||||
reconnectStartedAt = 0;
|
||||
if (!wasReconnecting) {
|
||||
entry!.eventLog.push({ id: `event-${Date.now().toString(36)}-connected`, kind: "connection", message: "已连接到服务器", timestamp: Date.now() });
|
||||
this.logger.info({ entryId: entry!.id, nickname: entry!.nickname, target: formatTeamSpeakTarget(entry!.target) }, "Web client connected to TeamSpeak");
|
||||
this.logger.info({ entryId: entry!.id, nickname: entry!.nickname, clientIp: entry!.clientIp, target: formatTeamSpeakTarget(entry!.target) }, "Web client connected to TeamSpeak");
|
||||
}
|
||||
session.transition("connected");
|
||||
sendJson({
|
||||
@@ -759,6 +763,7 @@ export class VoiceBridge {
|
||||
this.logger.info({
|
||||
entryId: entry.id,
|
||||
nickname: entry.nickname,
|
||||
clientIp: entry.clientIp,
|
||||
target: formatTeamSpeakTarget(entry.target),
|
||||
reason,
|
||||
...(entry.connectionFailureCode ? { failureCode: entry.connectionFailureCode } : {}),
|
||||
@@ -873,6 +878,30 @@ function resolveWebRtcPublicHost(request: IncomingMessage): string | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function resolveClientIp(request: IncomingMessage): string {
|
||||
const candidates = [
|
||||
firstHeader(request.headers["x-forwarded-for"])?.split(",", 1)[0],
|
||||
firstHeader(request.headers["x-real-ip"]),
|
||||
request.socket.remoteAddress,
|
||||
];
|
||||
for (const candidate of candidates) {
|
||||
const normalized = normalizeClientIp(candidate);
|
||||
if (normalized) return normalized;
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
function normalizeClientIp(value: string | undefined): string | undefined {
|
||||
if (!value) return undefined;
|
||||
let trimmed = value.trim();
|
||||
if (trimmed.startsWith("[") && trimmed.endsWith("]")) trimmed = trimmed.slice(1, -1);
|
||||
if (trimmed.toLowerCase().startsWith("::ffff:")) {
|
||||
const mapped = trimmed.slice("::ffff:".length);
|
||||
if (isIP(mapped) === 4) trimmed = mapped;
|
||||
}
|
||||
return isIP(trimmed) ? trimmed : undefined;
|
||||
}
|
||||
|
||||
function firstHeader(value: string | string[] | undefined): string | undefined {
|
||||
return Array.isArray(value) ? value[0] : value;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user