[PR #6046] [CLOSED] feat(inbounds): multiply client usage by inbound trafficRatio #6060

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/6046
Author: @HamidRezaSZ
Created: 7/19/2026
Status: ❌ Closed

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


📝 Commits (4)

  • f71be2d feat(inbounds): multiply client usage by inbound trafficRatio
  • 1fccce5 test(frontend): refresh inbound-full snapshots for trafficRatio
  • 66da63e fix(inbounds): persist and normalize trafficRatio on update
  • e451abf fix(inbounds): persist and normalize trafficRatio on update

📊 Changes

41 files changed (+1534 additions, -364 deletions)

View changed files

📝 README.md (+1 -0)
📝 docs/architecture.md (+8 -2)
📝 docs/content/docs/en/config/clients.mdx (+4 -1)
📝 docs/content/docs/en/config/inbounds.mdx (+7 -0)
📝 docs/content/docs/en/reference/api/hosts.mdx (+1 -1)
📝 docs/content/docs/fa/reference/api/hosts.mdx (+1 -1)
📝 docs/content/docs/ru/reference/api/hosts.mdx (+1 -1)
📝 docs/content/docs/zh/reference/api/hosts.mdx (+1 -1)
📝 docs/public/openapi.json (+1136 -344)
📝 frontend/public/openapi.json (+8 -0)
📝 frontend/src/generated/examples.ts (+1 -0)
📝 frontend/src/generated/schemas.ts (+7 -0)
📝 frontend/src/generated/types.ts (+1 -0)
📝 frontend/src/generated/zod.ts (+1 -0)
📝 frontend/src/lib/xray/inbound-form-adapter.ts (+4 -0)
📝 frontend/src/models/dbinbound.ts (+3 -0)
📝 frontend/src/pages/inbounds/form/InboundFormModal.tsx (+10 -0)
📝 frontend/src/schemas/api/inbound.ts (+1 -0)
📝 frontend/src/schemas/forms/inbound-form.ts (+1 -0)
📝 frontend/src/test/__snapshots__/inbound-full.test.ts.snap (+8 -0)

...and 21 more files

📄 Description

Summary

Add a per-inbound trafficRatio (default 1) that scales attached clients' recorded traffic deltas so billed usage and quota use the multiplier, while inbound aggregate counters stay raw. Create and update paths both persist and normalize the field (≤0 / invalid → 1), with a DB backfill for existing rows.

Multi-attach note: Xray reports client traffic per email only. A client on several inbounds with different ratios is billed at the ratio of the attached inbound with the lowest id (local polls). Prefer matching ratios across shared inbounds, or a single attachment, when billing must be exact. Node sync applies the reporting inbound’s ratio once per email delta.

Why

Operators often need to bill or quota clients at a different rate than actual bytes transferred (e.g. ratio 2 so 10 MB real becomes 20 MB recorded). Putting the multiplier on the inbound keeps one setting for all clients on that inbound and avoids per-client configuration drift.

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 an inbound with Traffic ratio 2, then edit it to another value and confirm the change is persisted (not stuck at create-time).
  2. Attach clients and generate traffic (local panel and, if available, a sub-node reporting to that inbound).
  3. Confirm client up/down and quota progress reflect the multiplier; inbound total counters remain unscaled.
  4. Confirm invalid/zero/empty ratio normalizes to 1 on add, update, and after migration backfill.
  5. Unit tests: TestAddClientTraffic_AppliesTrafficRatio, TestScaleTrafficBytes, TestNormalizeTrafficRatio, TestUpdateInbound_PersistsTrafficRatio, TestUpdateInbound_TrafficRatioClampedToOne, TestAddInbound_TrafficRatioClampedToOne.

Screenshots / recordings

Breaking changes

None. Existing inbounds get trafficRatio = 1 via the DB default and normalizeInboundTrafficRatio backfill, so recorded usage is unchanged until an operator sets a different ratio.

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/6046 **Author:** [@HamidRezaSZ](https://github.com/HamidRezaSZ) **Created:** 7/19/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/inbound-traffic-ratio` --- ### 📝 Commits (4) - [`f71be2d`](https://github.com/MHSanaei/3x-ui/commit/f71be2dac41829e0262edcc701bccf50415759b6) feat(inbounds): multiply client usage by inbound trafficRatio - [`1fccce5`](https://github.com/MHSanaei/3x-ui/commit/1fccce518d3000380de899ff01ec907da343cd6a) test(frontend): refresh inbound-full snapshots for trafficRatio - [`66da63e`](https://github.com/MHSanaei/3x-ui/commit/66da63edc696ffb1a1660e25ee215c03a9e462c4) fix(inbounds): persist and normalize trafficRatio on update - [`e451abf`](https://github.com/MHSanaei/3x-ui/commit/e451abfd56655e7e314fb2bca6574e5707ad771d) fix(inbounds): persist and normalize trafficRatio on update ### 📊 Changes **41 files changed** (+1534 additions, -364 deletions) <details> <summary>View changed files</summary> 📝 `README.md` (+1 -0) 📝 `docs/architecture.md` (+8 -2) 📝 `docs/content/docs/en/config/clients.mdx` (+4 -1) 📝 `docs/content/docs/en/config/inbounds.mdx` (+7 -0) 📝 `docs/content/docs/en/reference/api/hosts.mdx` (+1 -1) 📝 `docs/content/docs/fa/reference/api/hosts.mdx` (+1 -1) 📝 `docs/content/docs/ru/reference/api/hosts.mdx` (+1 -1) 📝 `docs/content/docs/zh/reference/api/hosts.mdx` (+1 -1) 📝 `docs/public/openapi.json` (+1136 -344) 📝 `frontend/public/openapi.json` (+8 -0) 📝 `frontend/src/generated/examples.ts` (+1 -0) 📝 `frontend/src/generated/schemas.ts` (+7 -0) 📝 `frontend/src/generated/types.ts` (+1 -0) 📝 `frontend/src/generated/zod.ts` (+1 -0) 📝 `frontend/src/lib/xray/inbound-form-adapter.ts` (+4 -0) 📝 `frontend/src/models/dbinbound.ts` (+3 -0) 📝 `frontend/src/pages/inbounds/form/InboundFormModal.tsx` (+10 -0) 📝 `frontend/src/schemas/api/inbound.ts` (+1 -0) 📝 `frontend/src/schemas/forms/inbound-form.ts` (+1 -0) 📝 `frontend/src/test/__snapshots__/inbound-full.test.ts.snap` (+8 -0) _...and 21 more files_ </details> ### 📄 Description ## Summary Add a per-inbound `trafficRatio` (default `1`) that scales attached clients' recorded traffic deltas so billed usage and quota use the multiplier, while inbound aggregate counters stay raw. Create and update paths both persist and normalize the field (`≤0` / invalid → `1`), with a DB backfill for existing rows. **Multi-attach note:** Xray reports client traffic per email only. A client on several inbounds with different ratios is billed at the ratio of the attached inbound with the lowest `id` (local polls). Prefer matching ratios across shared inbounds, or a single attachment, when billing must be exact. Node sync applies the reporting inbound’s ratio once per email delta. ## Why Operators often need to bill or quota clients at a different rate than actual bytes transferred (e.g. ratio `2` so 10 MB real becomes 20 MB recorded). Putting the multiplier on the inbound keeps one setting for all clients on that inbound and avoids per-client configuration drift. ## 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 an inbound with **Traffic ratio** `2`, then edit it to another value and confirm the change is persisted (not stuck at create-time). 2. Attach clients and generate traffic (local panel and, if available, a sub-node reporting to that inbound). 3. Confirm client up/down and quota progress reflect the multiplier; inbound total counters remain unscaled. 4. Confirm invalid/zero/empty ratio normalizes to `1` on add, update, and after migration backfill. 5. Unit tests: `TestAddClientTraffic_AppliesTrafficRatio`, `TestScaleTrafficBytes`, `TestNormalizeTrafficRatio`, `TestUpdateInbound_PersistsTrafficRatio`, `TestUpdateInbound_TrafficRatioClampedToOne`, `TestAddInbound_TrafficRatioClampedToOne`. ## Screenshots / recordings <!-- Attach inbound create/edit form showing the Traffic ratio field. --> ## Breaking changes None. Existing inbounds get `trafficRatio = 1` via the DB default and `normalizeInboundTrafficRatio` backfill, so recorded usage is unchanged until an operator sets a different ratio. ## 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#6060
No description provided.