[PR #6044] [CLOSED] feat(clients): multiply recorded traffic by per-client ratio #6059

Closed
opened 2026-11-02 04:06:04 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6044
Author: @HamidRezaSZ
Created: 7/19/2026
Status: ❌ Closed

Base: main ← Head: feat/client-traffic-ratio


📝 Commits (5)

  • 2d2d2e3 feat(clients): multiply recorded traffic by per-client ratio
  • e2112e1 fix(clients): normalize trafficRatio across create, update, and API reads
  • 82d2049 docs(clients): document trafficRatio in wiki, README, and OpenAPI
  • e5911a4 fix(docs): align hosts API pages with groupId OpenAPI paths
  • 057334d fix(clients): address Copilot review on trafficRatio helpers

📊 Changes

42 files changed (+1536 additions, -441 deletions)

View changed files

📝 README.md (+1 -1)
📝 docs/architecture.md (+4 -2)
📝 docs/content/docs/en/config/clients.mdx (+4 -1)
📝 docs/content/docs/en/reference/api/hosts.mdx (+1 -1)
📝 docs/content/docs/fa/config/clients.mdx (+4 -1)
📝 docs/content/docs/fa/reference/api/hosts.mdx (+1 -1)
📝 docs/content/docs/ru/config/clients.mdx (+4 -1)
📝 docs/content/docs/ru/reference/api/hosts.mdx (+1 -1)
📝 docs/content/docs/zh/config/clients.mdx (+2 -1)
📝 docs/content/docs/zh/reference/api/hosts.mdx (+1 -1)
📝 docs/lib/site-i18n.ts (+1 -1)
📝 docs/public/openapi.json (+1141 -345)
📝 frontend/public/mockServiceWorker.js (+21 -9)
📝 frontend/public/openapi.json (+11 -1)
📝 frontend/src/generated/examples.ts (+2 -0)
📝 frontend/src/generated/schemas.ts (+11 -1)
📝 frontend/src/generated/types.ts (+2 -0)
📝 frontend/src/generated/zod.ts (+2 -0)
📝 frontend/src/pages/clients/ClientBulkAddModal.tsx (+11 -0)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+16 -2)

...and 22 more files

📄 Description

Summary

Add a per-client trafficRatio (default 1) that multiplies real traffic deltas before they are stored, so ratio 2 turns 10MB of real use into 20MB in the DB and panel while quota enforcement stays consistent. Unset or non-positive ratios are normalized to 1 on write and API read so the field never persists or returns as 0.

Why

Operators often want billed usage to differ from raw network usage (e.g. sell 1:2 traffic). Without a multiplier, the panel can only show real bytes. This lets each client set a ratio so recorded up/down (and remaining quota) reflect billed usage without changing how Xray reports stats.

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

How was this tested?

  1. Create or edit a client, set Traffic Ratio to 2, save.
  2. Let the client generate traffic (or feed a known delta via the local traffic path).
  3. Confirm client_traffics up/down and the clients table show ~2× the real usage; quota depletes against the inflated counters.
  4. Node path: deltas from SetRemoteTraffic are scaled the same way; node baselines stay raw.
  5. Create/update a client via API without trafficRatio and confirm GET returns 1 (not 0); omit the field on update and confirm an existing custom ratio is preserved.
  6. Unit tests:
    • TestAddClientTraffic_AppliesTrafficRatio, TestScaleTrafficBytes (internal/web/service)
    • TestNormalizeClientTrafficRatio, TestClientToRecordNormalizesTrafficRatio, TestMergeClientRecordTrafficRatioResetToOne (internal/database/model)

Screenshots / recordings

Breaking changes

None. Existing clients get traffic_ratio = 1 via AutoMigrate default (usage unchanged). Ratio only affects future deltas; historical counters are not rewritten. Inbound aggregate counters stay raw. Legacy rows that somehow stored 0 are normalized to 1 on API read and on the next write through ToRecord / create-update defaults.

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/6044 **Author:** [@HamidRezaSZ](https://github.com/HamidRezaSZ) **Created:** 7/19/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/client-traffic-ratio` --- ### 📝 Commits (5) - [`2d2d2e3`](https://github.com/MHSanaei/3x-ui/commit/2d2d2e37b56dc9904df2bfc73865450b88962aed) feat(clients): multiply recorded traffic by per-client ratio - [`e2112e1`](https://github.com/MHSanaei/3x-ui/commit/e2112e171faa3374e3484432221faab6d3440cd6) fix(clients): normalize trafficRatio across create, update, and API reads - [`82d2049`](https://github.com/MHSanaei/3x-ui/commit/82d2049ff9d61610bed1b2e9c7e13e8f7d8543ce) docs(clients): document trafficRatio in wiki, README, and OpenAPI - [`e5911a4`](https://github.com/MHSanaei/3x-ui/commit/e5911a431c789070fa35feb9c5e8240a93a62769) fix(docs): align hosts API pages with groupId OpenAPI paths - [`057334d`](https://github.com/MHSanaei/3x-ui/commit/057334d64e7aee063adb2619df1953f66a206a4d) fix(clients): address Copilot review on trafficRatio helpers ### 📊 Changes **42 files changed** (+1536 additions, -441 deletions) <details> <summary>View changed files</summary> 📝 `README.md` (+1 -1) 📝 `docs/architecture.md` (+4 -2) 📝 `docs/content/docs/en/config/clients.mdx` (+4 -1) 📝 `docs/content/docs/en/reference/api/hosts.mdx` (+1 -1) 📝 `docs/content/docs/fa/config/clients.mdx` (+4 -1) 📝 `docs/content/docs/fa/reference/api/hosts.mdx` (+1 -1) 📝 `docs/content/docs/ru/config/clients.mdx` (+4 -1) 📝 `docs/content/docs/ru/reference/api/hosts.mdx` (+1 -1) 📝 `docs/content/docs/zh/config/clients.mdx` (+2 -1) 📝 `docs/content/docs/zh/reference/api/hosts.mdx` (+1 -1) 📝 `docs/lib/site-i18n.ts` (+1 -1) 📝 `docs/public/openapi.json` (+1141 -345) 📝 `frontend/public/mockServiceWorker.js` (+21 -9) 📝 `frontend/public/openapi.json` (+11 -1) 📝 `frontend/src/generated/examples.ts` (+2 -0) 📝 `frontend/src/generated/schemas.ts` (+11 -1) 📝 `frontend/src/generated/types.ts` (+2 -0) 📝 `frontend/src/generated/zod.ts` (+2 -0) 📝 `frontend/src/pages/clients/ClientBulkAddModal.tsx` (+11 -0) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+16 -2) _...and 22 more files_ </details> ### 📄 Description ## Summary Add a per-client `trafficRatio` (default `1`) that multiplies real traffic deltas before they are stored, so ratio `2` turns 10MB of real use into 20MB in the DB and panel while quota enforcement stays consistent. Unset or non-positive ratios are normalized to `1` on write and API read so the field never persists or returns as `0`. ## Why Operators often want billed usage to differ from raw network usage (e.g. sell 1:2 traffic). Without a multiplier, the panel can only show real bytes. This lets each client set a ratio so recorded up/down (and remaining quota) reflect billed usage without changing how Xray reports stats. ## Type of change - [ ] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [ ] 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 - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [x] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? 1. Create or edit a client, set **Traffic Ratio** to `2`, save. 2. Let the client generate traffic (or feed a known delta via the local traffic path). 3. Confirm `client_traffics` up/down and the clients table show ~2× the real usage; quota depletes against the inflated counters. 4. Node path: deltas from `SetRemoteTraffic` are scaled the same way; node baselines stay raw. 5. Create/update a client via API **without** `trafficRatio` and confirm GET returns `1` (not `0`); omit the field on update and confirm an existing custom ratio is preserved. 6. Unit tests: - `TestAddClientTraffic_AppliesTrafficRatio`, `TestScaleTrafficBytes` (`internal/web/service`) - `TestNormalizeClientTrafficRatio`, `TestClientToRecordNormalizesTrafficRatio`, `TestMergeClientRecordTrafficRatioResetToOne` (`internal/database/model`) ## Screenshots / recordings <!-- Add a screenshot of Client Form / Bulk Add with the Traffic Ratio field. --> ## Breaking changes None. Existing clients get `traffic_ratio = 1` via AutoMigrate default (usage unchanged). Ratio only affects **future** deltas; historical counters are not rewritten. Inbound aggregate counters stay raw. Legacy rows that somehow stored `0` are normalized to `1` on API read and on the next write through `ToRecord` / create-update defaults. ## 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>
bsakaguchi 2026-11-02 04:06:04 +07:00
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#6059
No description provided.