A modern and elegant dashboard for network traffic visualization and analysis.
fix(collector): normalize Stash cumulative traffic counters
Hobson12770f1dfcd4
diff --git a/apps/collector/src/modules/collector/gateway.collector.test.ts b/apps/collector/src/modules/collector/gateway.collector.test.tsindex e37e2d5..6ff2ced 100644--- a/apps/collector/src/modules/collector/gateway.collector.test.ts+++ b/apps/collector/src/modules/collector/gateway.collector.test.ts@@ -3,7 +3,7 @@ import { WebSocketServer } from "ws";import type { AddressInfo } from "node:net";import { GatewayCollector } from "./gateway.collector.js";-describe("GatewayCollector heartbeat watchdog", () => {+describe("GatewayCollector", () => {let servers: WebSocketServer[] = [];let collectors: GatewayCollector[] = [];@@ -31,6 +31,67 @@ describe("GatewayCollector heartbeat watchdog", () => {servers = [];});+ it.each([+ {+ gateway: "Stash",+ id: 648256,+ upload: { current: 0, max: 10, total: 5157 },+ download: { current: 0, max: 20, total: 6681 },+ },+ { gateway: "Clash", id: "648256", upload: 5157, download: 6681 },+ ])("delivers cumulative $gateway counters to the traffic callback", async (fixture) => {+ const wss = await makeServer();+ const port = (wss.address() as AddressInfo).port;+ const onData = vi.fn();+ const metadata = { host: "example.com", destinationIP: "203.0.113.1" };+ const connection = { ...fixture, metadata, chains: ["DIRECT"], rule: "Match" };+ wss.on("connection", (socket) => {+ socket.send(JSON.stringify({+ uploadTotal: 5157,+ downloadTotal: 6681,+ connections: [connection],+ }));+ socket.send(JSON.stringify({+ uploadTotal: 5177,+ downloadTotal: 6731,+ connections: [{+ ...connection,+ upload: fixture.gateway === "Stash" ? { total: 5177, current: 0 } : 5177,+ download: fixture.gateway === "Stash" ? { total: 6731, current: 0 } : 6731,+ }],+ }));+ });+ const collector = new GatewayCollector(3, { url: `ws://127.0.0.1:${port}`, onData });+ collectors.push(collector);+ collector.connect();++ await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));+ const first = onData.mock.calls[0][0].connections[0];+ const second = onData.mock.calls[1][0].connections[0];+ expect(first).toMatchObject({ id: "648256", upload: 5157, download: 6681, metadata });+ expect(second.id).toBe(first.id);+ expect(second.upload - first.upload).toBe(20);+ expect(second.download - first.download).toBe(50);+ });++ it("ignores invalid counter totals and preserves keepalive messages", async () => {+ const wss = await makeServer();+ const port = (wss.address() as AddressInfo).port;+ const onData = vi.fn();+ wss.on("connection", (socket) => {+ socket.send(JSON.stringify({ connections: [{ id: "invalid", upload: { total: "123" }, download: { total: -1 } }] }));+ socket.send(JSON.stringify({ connections: [] }));+ socket.send(JSON.stringify({ memory: 1024 }));+ });+ const collector = new GatewayCollector(4, { url: `ws://127.0.0.1:${port}`, onData });+ collectors.push(collector);+ collector.connect();+ await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));+ expect(onData.mock.calls[0][0].connections[0]).toMatchObject({ upload: 0, download: 0 });+ expect(onData.mock.calls[1][0]).toEqual({ connections: [] });+ expect(onData.mock.calls[2][0]).toEqual({ memory: 1024 });+ });+it("terminates and reconnects when the link goes silent (no data, no pong)", async () => {const wss = await makeServer();const port = (wss.address() as AddressInfo).port;diff --git a/apps/collector/src/modules/collector/gateway.collector.ts b/apps/collector/src/modules/collector/gateway.collector.tsindex c791d80..93df761 100644--- a/apps/collector/src/modules/collector/gateway.collector.ts+++ b/apps/collector/src/modules/collector/gateway.collector.ts@@ -21,6 +21,34 @@ const CLEANUP_INTERVAL = 2 * 60 * 1000; // 2 minutesconst DEFAULT_HEARTBEAT_INTERVAL = 10_000; // ping + liveness check cadence (ms)const DEFAULT_HEARTBEAT_TIMEOUT = 30_000; // terminate after this much silence (ms)+// Stash uses { current, max, total, last } counters instead of Clash's numbers.+// Normalize at the gateway boundary so batching and realtime deltas share the+// same cumulative byte counts.+function normalizeCounter(value: unknown): number {+ const total = value && typeof value === "object" && "total" in value+ ? value.total+ : value;+ return typeof total === "number" && Number.isFinite(total) && total >= 0+ ? total+ : 0;+}++function normalizeConnections(data: ConnectionsData): ConnectionsData {+ if (!Array.isArray(data?.connections)) return data;+ return {+ ...data,+ connections: data.connections.map((connection) => {+ if (!connection || typeof connection !== "object") return connection;+ return {+ ...connection,+ id: typeof connection.id === "number" ? String(connection.id) : connection.id,+ upload: normalizeCounter(connection.upload),+ download: normalizeCounter(connection.download),+ };+ }),+ };+}+export interface CollectorOptions {url: string;token?: string;@@ -102,7 +130,7 @@ export class GatewayCollector {this.lastActivity = Date.now();try {const json = JSON.parse(data.toString()) as ConnectionsData;- this.onData?.(json);+ this.onData?.(normalizeConnections(json));} catch (err) {console.error(`[Collector:${this.backendId}] Failed to parse message:`,