A modern and elegant dashboard for network traffic visualization and analysis.
fix: don't count pre-restart traffic of long-lived connections (#50)
luolei707d9152ce5e
diff --git a/apps/agent/internal/agent/runner.go b/apps/agent/internal/agent/runner.goindex 3f9e8c8..cb85751 100644--- a/apps/agent/internal/agent/runner.go+++ b/apps/agent/internal/agent/runner.go@@ -88,8 +88,15 @@ type Runner struct {lastPolicyHash stringgatewayLatencyMs int64serverLatencyMs int64++ // startedAtMs marks when this agent began watching. Flows the gateway+ // opened before it only establish a baseline (issue #50).+ startedAtMs int64}+// preexistingFlowGraceMs absorbs clock skew between gateway and agent.+const preexistingFlowGraceMs = 5_000+func NewRunner(cfg config.Config) *Runner {httpClient := &http.Client{Timeout: cfg.RequestTimeout}hostname, _ := os.Hostname()@@ -104,6 +111,7 @@ func NewRunner(cfg config.Config) *Runner {hostname: hostname,queue: make([]domain.TrafficUpdate, 0, cfg.ReportBatchSize*2),flows: make(map[string]trackedFlow, 2048),+ startedAtMs: time.Now().UnixMilli(),}}@@ -479,7 +487,14 @@ func (r *Runner) ingestSnapshots(snapshots []domain.FlowSnapshot, nowMs int64) {deltaUp := s.UploaddeltaDown := s.Download- if hasPrev {+ if !hasPrev && s.StartMs > 0 && s.StartMs < r.startedAtMs-preexistingFlowGraceMs {+ // Opened before this agent started: the cumulative counters include+ // traffic from before we were watching (likely already reported by+ // the previous process). Baseline only; later deltas count normally.+ deltaUp = 0+ deltaDown = 0+ counted = true+ } else if hasPrev {if s.Upload < prev.LastUpload || s.Download < prev.LastDown {// Counter reset (gateway restart / connection id reuse): the// counter went backwards. Match the direct gateway collector anddiff --git a/apps/agent/internal/agent/runner_test.go b/apps/agent/internal/agent/runner_test.goindex 83d359e..6b86001 100644--- a/apps/agent/internal/agent/runner_test.go+++ b/apps/agent/internal/agent/runner_test.go@@ -134,3 +134,51 @@ func TestIngestSnapshotsFirstTrafficAfterZeroCarriesConnection(t *testing.T) {t.Fatalf("expected connections 1 for first non-zero traffic, got %d", second[0].Connections)}}++func TestIngestSnapshotsBaselinesFlowsOpenedBeforeStart(t *testing.T) {+ runner := NewRunner(config.Config{+ ServerAPIBase: "http://localhost:3000/api",+ BackendID: 1,+ BackendToken: "token",+ AgentID: "agent-test",+ GatewayType: "clash",+ GatewayEndpoint: "http://127.0.0.1:9090",+ ReportInterval: time.Second,+ HeartbeatInterval: time.Second,+ GatewayPollInterval: time.Second,+ RequestTimeout: time.Second,+ ReportBatchSize: 100,+ MaxPendingUpdates: 1000,+ StaleFlowTimeout: time.Minute,+ })+ started := runner.startedAtMs++ // Issue #50: after a restart the first snapshot still carries the full+ // cumulative counters of long-lived connections.+ runner.ingestSnapshots([]domain.FlowSnapshot{+ {ID: "old", Upload: 17_000_000_000, Download: 1, StartMs: started - 3_600_000, Chains: []string{"Proxy"}},+ {ID: "new", Upload: 300, Download: 400, StartMs: started + 1_000, Chains: []string{"Proxy"}},+ {ID: "unknown-start", Upload: 7, Download: 8, Chains: []string{"Proxy"}},+ }, started+2_000)++ first := runner.takeBatch(10)+ var total int64+ for _, u := range first {+ total += u.Upload + u.Download+ }+ if len(first) != 2 || total != 300+400+7+8 {+ t.Fatalf("expected only new and unknown-start flows counted (715 bytes in 2 updates), got %d updates / %d bytes", len(first), total)+ }++ runner.ingestSnapshots([]domain.FlowSnapshot{+ {ID: "old", Upload: 17_000_001_000, Download: 1, StartMs: started - 3_600_000, Chains: []string{"Proxy"}},+ }, started+3_000)++ second := runner.takeBatch(10)+ if len(second) != 1 || second[0].Upload != 1000 || second[0].Download != 0 {+ t.Fatalf("expected baseline flow delta 1000/0, got %+v", second)+ }+ if second[0].Connections != 0 {+ t.Fatalf("expected baselined flow not re-counted as a connection, got %d", second[0].Connections)+ }+}diff --git a/apps/agent/internal/domain/types.go b/apps/agent/internal/domain/types.goindex 32a6dec..36a594a 100644--- a/apps/agent/internal/domain/types.go+++ b/apps/agent/internal/domain/types.go@@ -25,4 +25,7 @@ type FlowSnapshot struct {Upload int64Download int64TimestampMs int64+ // StartMs is when the gateway opened the connection (epoch ms, 0 when the+ // gateway did not report it). Internal only; never sent to the server.+ StartMs int64}diff --git a/apps/agent/internal/gateway/client.go b/apps/agent/internal/gateway/client.goindex 7ca5cab..c3ea59e 100644--- a/apps/agent/internal/gateway/client.go+++ b/apps/agent/internal/gateway/client.go@@ -6,6 +6,7 @@ import ("encoding/json""fmt""io"+ "math""net""net/http""regexp"@@ -46,6 +47,7 @@ func (c *Client) Collect(ctx context.Context) ([]domain.FlowSnapshot, error) {type clashConnectionsResponse struct {Connections []struct {ID string `json:"id"`+ Start string `json:"start"`Upload float64 `json:"upload"`Download float64 `json:"download"`Rule string `json:"rule"`@@ -164,6 +166,7 @@ type surgeRequestsResponse struct {OutBytes flexibleFloat64 `json:"outBytes"`InBytes flexibleFloat64 `json:"inBytes"`Time flexibleFloat64 `json:"time"`+ StartDate flexibleFloat64 `json:"startDate"`} `json:"requests"`}@@ -215,6 +218,7 @@ func (c *Client) collectClash(ctx context.Context) ([]domain.FlowSnapshot, errorUpload: toInt64(item.Upload),Download: toInt64(item.Download),TimestampMs: nowMs,+ StartMs: parseClashStart(item.Start),})}@@ -296,12 +300,38 @@ func (c *Client) collectSurge(ctx context.Context) ([]domain.FlowSnapshot, errorUpload: toInt64(float64(reqItem.OutBytes)),Download: toInt64(float64(reqItem.InBytes)),TimestampMs: timestampMs,+ StartMs: epochToMs(float64(reqItem.StartDate)),})}return snapshots, nil}+// parseClashStart converts mihomo's RFC 3339 connection start time to epoch+// ms, returning 0 when missing or unparseable.+func parseClashStart(value string) int64 {+ value = strings.TrimSpace(value)+ if value == "" {+ return 0+ }+ t, err := time.Parse(time.RFC3339Nano, value)+ if err != nil {+ return 0+ }+ return t.UnixMilli()+}++// epochToMs accepts epoch seconds (Surge startDate) or milliseconds.+func epochToMs(value float64) int64 {+ if value <= 0 || math.IsNaN(value) || math.IsInf(value, 0) {+ return 0+ }+ if value < 1e12 {+ return toInt64(value * 1000)+ }+ return toInt64(value)+}+func normalizeChains(chains []string) []string {if len(chains) == 0 {return []string{"DIRECT"}diff --git a/apps/agent/internal/gateway/start_time_test.go b/apps/agent/internal/gateway/start_time_test.gonew file mode 100644index 0000000..b8e10d0--- /dev/null+++ b/apps/agent/internal/gateway/start_time_test.go@@ -0,0 +1,26 @@+package gateway++import "testing"++func TestConnectionStartParsing(t *testing.T) {+ if got := parseClashStart("2026-10-09T08:00:00.123456789Z"); got != 1_791_532_800_123 {+ t.Fatalf("parseClashStart RFC3339Nano: got %d", got)+ }+ if got := parseClashStart("2026-10-09T16:00:00+08:00"); got != 1_791_532_800_000 {+ t.Fatalf("parseClashStart offset: got %d", got)+ }+ for _, bad := range []string{"", " ", "yesterday"} {+ if got := parseClashStart(bad); got != 0 {+ t.Fatalf("parseClashStart(%q): expected 0, got %d", bad, got)+ }+ }+ if got := epochToMs(1_791_532_800.5); got != 1_791_532_800_500 {+ t.Fatalf("epochToMs seconds: got %d", got)+ }+ if got := epochToMs(1_791_532_800_500); got != 1_791_532_800_500 {+ t.Fatalf("epochToMs millis: got %d", got)+ }+ if got := epochToMs(0); got != 0 {+ t.Fatalf("epochToMs zero: got %d", got)+ }+}diff --git a/apps/collector/src/modules/collector/connection-baseline.test.ts b/apps/collector/src/modules/collector/connection-baseline.test.tsnew file mode 100644index 0000000..872fcbc--- /dev/null+++ b/apps/collector/src/modules/collector/connection-baseline.test.ts@@ -0,0 +1,212 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { WebSocketServer, type WebSocket as WsSocket } from "ws";+import type { AddressInfo } from "node:net";+import type { StatsDatabase } from "../db/db.js";+import { realtimeStore } from "../realtime/realtime.store.js";+import {+ PREEXISTING_GRACE_MS,+ isPreexistingConnection,+ parseConnectionStartMs,+} from "./connection-baseline.js";+import { createCollector } from "./gateway.collector.js";+import { createSurgeCollector } from "./surge.collector.js";++// Every DB method is a no-op; these tests only observe what reaches the+// realtime store.+const fakeDb = new Proxy(+ {},+ { get: () => vi.fn(() => undefined) },+) as unknown as StatsDatabase;++const recordedBytes = (spy: { mock: { calls: unknown[][] } }, backendId: number) =>+ spy.mock.calls+ .filter((call) => call[0] === backendId)+ .map((call) => {+ const traffic = call[1] as { upload: number; download: number };+ return { up: traffic.upload, down: traffic.download, conns: call[2] };+ });++describe("connection start parsing", () => {+ it("parses ISO strings, epoch seconds and epoch milliseconds", () => {+ expect(parseConnectionStartMs("2026-10-09T08:00:00.000Z")).toBe(+ Date.parse("2026-10-09T08:00:00.000Z"),+ );+ expect(parseConnectionStartMs(1_760_000_000.5)).toBe(1_760_000_000_500);+ expect(parseConnectionStartMs(1_760_000_000_500)).toBe(1_760_000_000_500);+ });++ it("returns null for missing or invalid values", () => {+ expect(parseConnectionStartMs(undefined)).toBeNull();+ expect(parseConnectionStartMs("")).toBeNull();+ expect(parseConnectionStartMs("not a date")).toBeNull();+ expect(parseConnectionStartMs(0)).toBeNull();+ expect(parseConnectionStartMs(Number.NaN)).toBeNull();+ });++ it("only treats clearly older connections as preexisting", () => {+ const startedAt = Date.parse("2026-10-09T08:00:00.000Z");+ expect(isPreexistingConnection("2026-10-09T07:00:00.000Z", startedAt)).toBe(true);+ // Within the clock-skew grace window: counted as new.+ expect(+ isPreexistingConnection(new Date(startedAt - PREEXISTING_GRACE_MS + 1).toISOString(), startedAt),+ ).toBe(false);+ expect(isPreexistingConnection("2026-10-09T08:00:01.000Z", startedAt)).toBe(false);+ // Unknown start time never drops traffic.+ expect(isPreexistingConnection(undefined, startedAt)).toBe(false);+ });+});++describe("gateway collector restart baseline (issue #50)", () => {+ let wss: WebSocketServer | null = null;+ let stop: (() => void) | null = null;++ afterEach(async () => {+ stop?.();+ stop = null;+ vi.restoreAllMocks();+ if (wss) {+ for (const client of wss.clients) client.terminate();+ await new Promise<void>((r) => wss!.close(() => r()));+ wss = null;+ }+ });++ it("does not count cumulative bytes of connections opened before start", async () => {+ const spy = vi.spyOn(realtimeStore, "recordTraffic");+ wss = new WebSocketServer({ port: 0 });+ await new Promise<void>((r) => wss!.on("listening", () => r()));+ const port = (wss.address() as AddressInfo).port;+ const socketReady = new Promise<WsSocket>((r) => wss!.on("connection", r));++ const backendId = 9050;+ const collector = createCollector(+ fakeDb,+ `ws://127.0.0.1:${port}`,+ undefined,+ undefined,+ undefined,+ backendId,+ );+ stop = () => collector.disconnect();+ collector.connect();+ const socket = await socketReady;++ const oldStart = new Date(Date.now() - 3_600_000).toISOString();+ const newStart = new Date(Date.now() + 1_000).toISOString();+ const conn = (id: string, start: string, upload: number, download: number) => ({+ id,+ start,+ upload,+ download,+ chains: ["Proxy"],+ rule: "Match",+ rulePayload: "",+ metadata: { host: `${id}.example.com`, destinationIP: "1.1.1.1", sourceIP: "10.0.0.2" },+ });++ // First snapshot after (re)start: a long-lived connection carrying 17 GB+ // of history, plus a connection opened after the collector started.+ socket.send(+ JSON.stringify({+ connections: [+ conn("old", oldStart, 17e9, 17e9),+ conn("new", newStart, 300, 400),+ ],+ }),+ );+ await vi.waitFor(() => expect(recordedBytes(spy, backendId)).toHaveLength(1), {+ timeout: 2000,+ interval: 20,+ });+ expect(recordedBytes(spy, backendId)).toEqual([{ up: 300, down: 400, conns: 1 }]);++ // Second snapshot: the old connection moves 1000 bytes, which is counted+ // as a delta; it was already counted as a connection before the restart.+ socket.send(+ JSON.stringify({+ connections: [+ conn("old", oldStart, 17e9 + 1000, 17e9),+ conn("new", newStart, 300, 400),+ ],+ }),+ );+ await vi.waitFor(() => expect(recordedBytes(spy, backendId)).toHaveLength(2), {+ timeout: 2000,+ interval: 20,+ });+ expect(recordedBytes(spy, backendId)[1]).toEqual({ up: 1000, down: 0, conns: 0 });+ });+});++describe("surge collector restart baseline (issue #50)", () => {+ let stop: (() => void) | null = null;++ afterEach(() => {+ stop?.();+ stop = null;+ vi.restoreAllMocks();+ vi.unstubAllGlobals();+ });++ it("does not count requests that started before the collector", async () => {+ const spy = vi.spyOn(realtimeStore, "recordTraffic");+ const nowSec = Date.now() / 1000;+ const request = (id: string, startDate: number, outBytes: number, inBytes: number) => ({+ id,+ time: Date.now(),+ startDate,+ policyName: "Proxy",+ originalPolicyName: "Proxy",+ rule: "FINAL",+ processPath: "",+ remoteHost: `${id}.example.com:443`,+ remoteAddress: "1.1.1.1",+ sourceAddress: "10.0.0.2",+ outBytes,+ inBytes,+ });++ let poll = 0;+ vi.stubGlobal(+ "fetch",+ vi.fn(async (input: unknown) => {+ const url = String(input);+ if (!url.includes("/v1/requests/recent")) {+ return new Response(JSON.stringify({}), { status: 200 });+ }+ poll++;+ const oldBytes = poll === 1 ? 17e9 : 17e9 + 500;+ return new Response(+ JSON.stringify({+ requests: [+ request("old", nowSec - 3600, oldBytes, 0),+ request("new", nowSec + 1, 300, 400),+ ],+ }),+ { status: 200, headers: { "content-type": "application/json" } },+ );+ }),+ );++ const backendId = 9051;+ const collector = createSurgeCollector(+ fakeDb,+ "http://surge.test:6171",+ undefined,+ undefined,+ undefined,+ backendId,+ );+ stop = () => collector.stop();+ collector.start();++ await vi.waitFor(() => expect(recordedBytes(spy, backendId)).toHaveLength(2), {+ timeout: 6000,+ interval: 50,+ });+ expect(recordedBytes(spy, backendId)).toEqual([+ { up: 300, down: 400, conns: 1 },+ { up: 500, down: 0, conns: 0 },+ ]);+ }, 10_000);+});diff --git a/apps/collector/src/modules/collector/connection-baseline.ts b/apps/collector/src/modules/collector/connection-baseline.tsnew file mode 100644index 0000000..77b3ee9--- /dev/null+++ b/apps/collector/src/modules/collector/connection-baseline.ts@@ -0,0 +1,42 @@+// Gateways report per-connection traffic as cumulative counters. When a+// collector first sees a connection that was already open before it started+// (collector restart, container upgrade, backend added at runtime), those+// counters include traffic accumulated before we were watching — often already+// recorded by the previous process. Counting them as fresh traffic produces a+// huge spike at restart (issue #50). Such connections only establish a+// baseline; later deltas are counted normally.++// Allow for clock skew between the gateway and the collector host.+export const PREEXISTING_GRACE_MS = 5_000;++/**+ * Normalize a gateway start timestamp to epoch milliseconds.+ * Accepts ISO strings (mihomo `start`) and epoch seconds or milliseconds+ * (Surge `startDate`). Returns null when missing or unparseable.+ */+export function parseConnectionStartMs(value: unknown): number | null {+ if (typeof value === "number") {+ if (!Number.isFinite(value) || value <= 0) return null;+ // Epoch seconds are < 1e12 until the year 33658.+ return value < 1e12 ? Math.round(value * 1000) : Math.round(value);+ }+ if (typeof value === "string" && value) {+ const ms = Date.parse(value);+ return Number.isNaN(ms) || ms <= 0 ? null : ms;+ }+ return null;+}++/**+ * True when the connection demonstrably started before the collector began+ * watching. Unknown start times return false so traffic is never dropped on+ * backends that omit the field.+ */+export function isPreexistingConnection(+ startValue: unknown,+ collectorStartedAtMs: number,+): boolean {+ const startMs = parseConnectionStartMs(startValue);+ if (startMs === null) return false;+ return startMs < collectorStartedAtMs - PREEXISTING_GRACE_MS;+}diff --git a/apps/collector/src/modules/collector/gateway.collector.ts b/apps/collector/src/modules/collector/gateway.collector.tsindex e3e6ccd..3f2efa5 100644--- a/apps/collector/src/modules/collector/gateway.collector.ts+++ b/apps/collector/src/modules/collector/gateway.collector.ts@@ -6,6 +6,7 @@ import { TrafficWriteError } from "../clickhouse/clickhouse.writer.js";import { realtimeStore } from "../realtime/realtime.store.js";import { calculateBackoffDelay } from "../../shared/utils/backoff.js";import { BatchBuffer } from "./batch-buffer.js";+import { isPreexistingConnection } from "./connection-baseline.js";// Stale connection cleanup constantsconst STALE_CONNECTION_TIMEOUT = 5 * 60 * 1000; // 5 minutes@@ -228,6 +229,7 @@ export function createCollector() {const id = backendId || 0;const activeConnections = new Map<string, TrackedConnection>();+ const startedAt = Date.now();const batchBuffer = new BatchBuffer();let lastBroadcastTime = 0;const broadcastThrottleMs = 500;@@ -416,6 +418,7 @@ export function createCollector();let hasNewTraffic = false;let counterResets = 0;+ let baselinedConnections = 0;const geoBatchByIp = new Map<string,{ upload: number; download: number; connections: number }@@ -445,8 +448,13 @@ export function createCollector(const existing = activeConnections.get(conn.id);if (!existing) {- // New connection - track it and record initial traffic- const hasInitialTraffic = conn.upload > 0 || conn.download > 0;+ // New connection - track it and record initial traffic. Connections+ // opened before this collector started only set a baseline: their+ // cumulative counters predate us (issue #50).+ const preexisting = isPreexistingConnection(conn.start, startedAt);+ const hasInitialTraffic =+ !preexisting && (conn.upload > 0 || conn.download > 0);+ if (preexisting) baselinedConnections++;activeConnections.set(conn.id, {id: conn.id,domain,@@ -458,7 +466,9 @@ export function createCollector(lastDownload: conn.download,totalUpload: conn.upload,totalDownload: conn.download,- counted: hasInitialTraffic,+ // A preexisting connection was already counted by whoever was+ // watching before us.+ counted: hasInitialTraffic || preexisting,sourceIP,lastSeen: now,});@@ -592,6 +602,12 @@ export function createCollector(}}+ if (baselinedConnections > 0) {+ console.info(+ `[Collector:${id}] Baselined ${baselinedConnections} connection(s) opened before collector start; prior cumulative traffic not counted`,+ );+ }+if (counterResets > 0) {console.info(`[Collector:${id}] Detected ${counterResets} traffic counter reset(s); counted current totals as new traffic`,diff --git a/apps/collector/src/modules/collector/surge.collector.ts b/apps/collector/src/modules/collector/surge.collector.tsindex 1b06d73..207a3d2 100644--- a/apps/collector/src/modules/collector/surge.collector.ts+++ b/apps/collector/src/modules/collector/surge.collector.ts@@ -6,6 +6,7 @@ import { realtimeStore } from "../realtime/realtime.store.js";import type { SurgeRequest, SurgeRequestsData } from "@neko-master/shared";import { calculateBackoffDelay } from "../../shared/utils/backoff.js";import { BatchBuffer } from "./batch-buffer.js";+import { isPreexistingConnection } from "./connection-baseline.js";// Debug configurationconst DEBUG_SURGE = process.env.DEBUG_SURGE === "true";@@ -249,6 +250,7 @@ export function createSurgeCollector(const id = backendId || 0;const activeRequests = new Map<string, TrackedRequest>();const batchBuffer = new BatchBuffer();+ const startedAt = Date.now();// Track recently completed requests to prevent double counting// Key: request ID, Value: completion timestamp@@ -450,6 +452,7 @@ export function createSurgeCollector(data.requests.map((r) => r?.id).filter(Boolean));let hasNewTraffic = false;+ let baselinedRequests = 0;const geoBatchByIp = new Map<string,{ upload: number; download: number; connections: number }@@ -546,7 +549,13 @@ export function createSurgeCollector(// IMPORTANT: We now record initial traffic immediately to prevent data loss// for short-lived connections that disappear before the next poll.// The recentlyCompleted map prevents double counting if the same request reappears.- const hasInitialTraffic = currentUpload > 0 || currentDownload > 0;+ // Requests that started before this collector (including completed+ // ones still listed in /v1/requests/recent) only set a baseline:+ // their cumulative bytes predate us (issue #50).+ const preexisting = isPreexistingConnection(req.startDate, startedAt);+ const hasInitialTraffic =+ !preexisting && (currentUpload > 0 || currentDownload > 0);+ if (preexisting) baselinedRequests++;activeRequests.set(req.id, {id: req.id,domain,@@ -564,7 +573,7 @@ export function createSurgeCollector(completed: isCompleted,disconnected: isDisconnected,lastStatus: req.status,- counted: hasInitialTraffic,+ counted: hasInitialTraffic || preexisting,initialProcessed: true, // Mark as processed});@@ -725,6 +734,12 @@ export function createSurgeCollector(}}+ if (baselinedRequests > 0) {+ console.info(+ `[SurgeCollector:${id}] Baselined ${baselinedRequests} request(s) started before collector start; prior cumulative traffic not counted`,+ );+ }+// Clean up completed/disappeared connectionsfor (const [reqId, trackedReq] of activeRequests) {if (!currentIds.has(reqId)) {