[PR #6972] feat(monitoring): allow selecting network interface for traffic statistics #6555

Open
opened 2026-11-02 04:09:36 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6972
Author: @HamedSanaei
Created: 10/27/2026
Status: 🔄 Open

Base: main ← Head: feat/network-traffic-interface


📝 Commits (3)

  • 9fa66c0 feat(monitoring): add configurable traffic interface selection
  • 4d5f74d fix(monitoring): handle unavailable interfaces consistently
  • 1b6ee40 chore(merge): update network monitoring with upstream main

📊 Changes

63 files changed (+2770 additions, -179 deletions)

View changed files

📝 docs/content/docs/en/config/panel.mdx (+81 -0)
📝 docs/content/docs/en/reference/api/server.mdx (+10 -1)
➕ docs/public/images/network-traffic-automatic.png (+0 -0)
➕ docs/public/images/network-traffic-interface.png (+0 -0)
📝 docs/public/openapi.json (+266 -29)
📝 frontend/public/openapi.json (+266 -29)
📝 frontend/src/api/queries/useAllSettings.ts (+2 -2)
➕ frontend/src/api/queries/useNetworkInterfaces.ts (+21 -0)
📝 frontend/src/api/queryKeys.ts (+1 -0)
📝 frontend/src/generated/examples.ts (+54 -0)
📝 frontend/src/generated/schemas.ts (+189 -0)
📝 frontend/src/generated/types.ts (+49 -4)
📝 frontend/src/generated/zod.ts (+53 -12)
📝 frontend/src/models/setting.ts (+3 -1)
📝 frontend/src/models/status.ts (+3 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+9 -3)
📝 frontend/src/pages/index/ThroughputCard.tsx (+29 -7)
📝 frontend/src/pages/index/useOverviewHistory.ts (+3 -4)
📝 frontend/src/pages/settings/GeneralTab.tsx (+3 -0)
➕ frontend/src/pages/settings/NetworkTrafficSettings.tsx (+101 -0)

...and 43 more files

📄 Description

Summary

Add legacy, automatic, and manual network interface selection to system traffic monitoring, with persisted settings, an authenticated interface picker, and consistent dashboard, history, Telegram, Discord, status API, WebSocket, and node-history behavior. Rebase throughput and packet-rate samples when their source changes or counters reset.

Why

System traffic currently sums interfaces using hard-coded exclusions. On stacked-network setups, including Azure Accelerated Networking, a synthetic interface and its SR-IOV interface can represent the same traffic and be counted twice. Administrators can now opt into one appropriate interface while existing installations and new installations keep legacy aggregation by default.

Closes #6906

Automatic route selection was inspired by IPv6Monitor. The implementation uses the existing Go netlink dependency; it does not copy that project's daemon, database, or nftables accounting.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

Discord system reports are updated as well. Client quota, billing, inbound traffic accounting, and Xray configuration are unchanged.

How was this tested?

Development followed red → green → refactor. Regression tests reject the original doubled aggregation in single-interface modes and the original throughput subtraction on incompatible samples.

  • make verify: Go tests/lint/build, frontend lint/format/typecheck, 2,033 frontend tests, production and Storybook builds, generated API checks, and published OpenAPI consistency.
  • make race: passed across Go packages.
  • go vet -p=2 ./...: passed.
  • make node-e2e: passed with real master/node processes, including global traffic heartbeat chains.
  • Documentation: pnpm typecheck, pnpm lint, pnpm format:check, pnpm test (112 tests), and pnpm build passed.
  • Windows amd64 CGo cross-build passed using LLVM-MinGW.
  • Chromium component preview: manual selection, automatic resolution, and 390px mobile layout; screenshots below.
  • The repository's staged frontend pre-commit hook passed.

Coverage includes the 30 GiB + 30 GiB stacked-interface fixture; exact physical/virtual manual selection; IPv4/IPv6 routes, metrics, ECMP, unusable routes and unsigned priorities; mode/route/device changes; byte and packet counter resets; disappearing/reappearing interfaces; invalid elapsed time; settings persistence after reopening the database; invalid input rejection; unchanged missing manual selections; interactive Discord status distinguishing unavailable traffic from measured zero; omitted-field compatibility under an interleaved settings update; authenticated endpoint scope; and unavailable versus measured-zero values in history and reports.

To exercise the feature:

  1. Open Settings → General → Panel settings → Network Traffic Interface.
  2. Save Manual with an interface from the dynamic list; reload settings and confirm the dashboard identifies that interface.
  3. Save Automatic; the settings page shows the default-route interface. Linux selection prefers usable IPv4 defaults, then lowest metric and interface-name ties; IPv6 defaults are used when no usable IPv4 default exists.
  4. Query GET /panel/api/server/networkInterfaces using an admin session/token. It returns the existing envelope with names/up state and the automatic result or error. Anonymous, monitor-scope, and node-sync tokens cannot list interfaces.
  5. When collection fails, status retains numeric fields for compatibility but marks network metrics unavailable; the dashboard and reports display that state and histories omit the sample. Recovery establishes a fresh baseline.

Screenshots / recordings

Local Chromium previews of the actual settings component with example interface names:

Manual interface selection

Automatic route resolution

Breaking changes

None. Legacy aggregation remains the default, using the same interface exclusions. Two settings use the existing key/value table; no tables, columns, or migrations are added. Older API clients omitting the new pair preserve the saved selection; explicit networkTrafficMode: "legacy" restores aggregation. Status metadata and the admin-only interface-list route are additive, and older responses remain accepted by the frontend.

New manual selections must exist when saved. An unchanged saved manual selection may remain unavailable, allowing unrelated settings to be edited without replacing that selection.

Network interface selection is machine-bound for the existing keep-host-settings database import option.

Automatic selection is a Linux main-routing-table heuristic, not a full policy-routing lookup. It selects one usable ECMP hop, prefers IPv4 when IPv4/IPv6 defaults differ, and never silently falls back to aggregation. Other supported platforms retain manual/legacy modes and show an explicit automatic-detection error. Docker observes its own network namespace; host-wide visibility requires sharing the host namespace. No monitoring-specific capabilities, firewall changes, shell commands, Python, or extra daemon are introduced.

Totals are cumulative kernel counters, not monthly billing. Previous history remains intact when the selection changes. Physical Azure/SR-IOV hardware was unavailable locally; the double-counting regression uses deterministic counters. Separate IPv4/IPv6 traffic accounting is intentionally outside this PR.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6972 **Author:** [@HamedSanaei](https://github.com/HamedSanaei) **Created:** 10/27/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/network-traffic-interface` --- ### 📝 Commits (3) - [`9fa66c0`](https://github.com/MHSanaei/3x-ui/commit/9fa66c0a58bfa37e8579cb536fe796c513e0b98b) feat(monitoring): add configurable traffic interface selection - [`4d5f74d`](https://github.com/MHSanaei/3x-ui/commit/4d5f74da2dce9a3f5810541d23a154c56d3a1bc1) fix(monitoring): handle unavailable interfaces consistently - [`1b6ee40`](https://github.com/MHSanaei/3x-ui/commit/1b6ee4029ee6186df31e71cf22089430d26347cd) chore(merge): update network monitoring with upstream main ### 📊 Changes **63 files changed** (+2770 additions, -179 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/config/panel.mdx` (+81 -0) 📝 `docs/content/docs/en/reference/api/server.mdx` (+10 -1) ➕ `docs/public/images/network-traffic-automatic.png` (+0 -0) ➕ `docs/public/images/network-traffic-interface.png` (+0 -0) 📝 `docs/public/openapi.json` (+266 -29) 📝 `frontend/public/openapi.json` (+266 -29) 📝 `frontend/src/api/queries/useAllSettings.ts` (+2 -2) ➕ `frontend/src/api/queries/useNetworkInterfaces.ts` (+21 -0) 📝 `frontend/src/api/queryKeys.ts` (+1 -0) 📝 `frontend/src/generated/examples.ts` (+54 -0) 📝 `frontend/src/generated/schemas.ts` (+189 -0) 📝 `frontend/src/generated/types.ts` (+49 -4) 📝 `frontend/src/generated/zod.ts` (+53 -12) 📝 `frontend/src/models/setting.ts` (+3 -1) 📝 `frontend/src/models/status.ts` (+3 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+9 -3) 📝 `frontend/src/pages/index/ThroughputCard.tsx` (+29 -7) 📝 `frontend/src/pages/index/useOverviewHistory.ts` (+3 -4) 📝 `frontend/src/pages/settings/GeneralTab.tsx` (+3 -0) ➕ `frontend/src/pages/settings/NetworkTrafficSettings.tsx` (+101 -0) _...and 43 more files_ </details> ### 📄 Description ## Summary Add legacy, automatic, and manual network interface selection to system traffic monitoring, with persisted settings, an authenticated interface picker, and consistent dashboard, history, Telegram, Discord, status API, WebSocket, and node-history behavior. Rebase throughput and packet-rate samples when their source changes or counters reset. ## Why System traffic currently sums interfaces using hard-coded exclusions. On stacked-network setups, including Azure Accelerated Networking, a synthetic interface and its SR-IOV interface can represent the same traffic and be counted twice. Administrators can now opt into one appropriate interface while existing installations and new installations keep legacy aggregation by default. Closes #6906 Automatic route selection was inspired by [IPv6Monitor](https://github.com/HamedSanaei/ipv6monitor). The implementation uses the existing Go netlink dependency; it does not copy that project's daemon, database, or nftables accounting. ## Type of change - [x] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [x] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [x] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [x] Multi-node (sub-nodes) - [x] Telegram bot Discord system reports are updated as well. Client quota, billing, inbound traffic accounting, and Xray configuration are unchanged. ## How was this tested? Development followed red → green → refactor. Regression tests reject the original doubled aggregation in single-interface modes and the original throughput subtraction on incompatible samples. - `make verify`: Go tests/lint/build, frontend lint/format/typecheck, 2,033 frontend tests, production and Storybook builds, generated API checks, and published OpenAPI consistency. - `make race`: passed across Go packages. - `go vet -p=2 ./...`: passed. - `make node-e2e`: passed with real master/node processes, including global traffic heartbeat chains. - Documentation: `pnpm typecheck`, `pnpm lint`, `pnpm format:check`, `pnpm test` (112 tests), and `pnpm build` passed. - Windows amd64 CGo cross-build passed using LLVM-MinGW. - Chromium component preview: manual selection, automatic resolution, and 390px mobile layout; screenshots below. - The repository's staged frontend pre-commit hook passed. Coverage includes the 30 GiB + 30 GiB stacked-interface fixture; exact physical/virtual manual selection; IPv4/IPv6 routes, metrics, ECMP, unusable routes and unsigned priorities; mode/route/device changes; byte and packet counter resets; disappearing/reappearing interfaces; invalid elapsed time; settings persistence after reopening the database; invalid input rejection; unchanged missing manual selections; interactive Discord status distinguishing unavailable traffic from measured zero; omitted-field compatibility under an interleaved settings update; authenticated endpoint scope; and unavailable versus measured-zero values in history and reports. To exercise the feature: 1. Open Settings → General → Panel settings → Network Traffic Interface. 2. Save Manual with an interface from the dynamic list; reload settings and confirm the dashboard identifies that interface. 3. Save Automatic; the settings page shows the default-route interface. Linux selection prefers usable IPv4 defaults, then lowest metric and interface-name ties; IPv6 defaults are used when no usable IPv4 default exists. 4. Query `GET /panel/api/server/networkInterfaces` using an admin session/token. It returns the existing envelope with names/up state and the automatic result or error. Anonymous, monitor-scope, and node-sync tokens cannot list interfaces. 5. When collection fails, status retains numeric fields for compatibility but marks network metrics unavailable; the dashboard and reports display that state and histories omit the sample. Recovery establishes a fresh baseline. ## Screenshots / recordings Local Chromium previews of the actual settings component with example interface names: ![Manual interface selection](https://raw.githubusercontent.com/HamedSanaei/3x-ui/feat/network-traffic-interface/docs/public/images/network-traffic-interface.png) ![Automatic route resolution](https://raw.githubusercontent.com/HamedSanaei/3x-ui/feat/network-traffic-interface/docs/public/images/network-traffic-automatic.png) ## Breaking changes None. Legacy aggregation remains the default, using the same interface exclusions. Two settings use the existing key/value table; no tables, columns, or migrations are added. Older API clients omitting the new pair preserve the saved selection; explicit `networkTrafficMode: "legacy"` restores aggregation. Status metadata and the admin-only interface-list route are additive, and older responses remain accepted by the frontend. New manual selections must exist when saved. An unchanged saved manual selection may remain unavailable, allowing unrelated settings to be edited without replacing that selection. Network interface selection is machine-bound for the existing keep-host-settings database import option. Automatic selection is a Linux main-routing-table heuristic, not a full policy-routing lookup. It selects one usable ECMP hop, prefers IPv4 when IPv4/IPv6 defaults differ, and never silently falls back to aggregation. Other supported platforms retain manual/legacy modes and show an explicit automatic-detection error. Docker observes its own network namespace; host-wide visibility requires sharing the host namespace. No monitoring-specific capabilities, firewall changes, shell commands, Python, or extra daemon are introduced. Totals are cumulative kernel counters, not monthly billing. Previous history remains intact when the selection changes. Physical Azure/SR-IOV hardware was unavailable locally; the double-counting regression uses deterministic counters. Separate IPv4/IPv6 traffic accounting is intentionally outside this PR. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6555
No description provided.