[PR #6043] [CLOSED] feat(clients): add per-client download/upload speed limits via tc HTB #6058

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

Base: main ← Head: feat/speed-limit


📝 Commits (10+)

  • c41fb72 feat(clients): add per-client download/upload speed limits via tc HTB
  • 2bdb585 fix(clients): use CommandContext for tc invocations
  • e77f8dc fix(clients): gate tc shaping and share upload police per client
  • 2827384 fix(clients): avoid int overflow in police index allocation
  • 6f0acab docs(clients): document per-client speed limits and opt-in setting
  • 110043c fix(frontend): raise ConfigBlock text contrast for a11y
  • b00743d fix(clients): address Copilot review on tc speed limiting
  • 17ac009 Merge branch 'main' into feat/speed-limit
  • 9ff2a28 Merge branch 'main' into feat/speed-limit
  • 2e426e5 Merge branch 'main' into feat/speed-limit

📊 Changes

42 files changed (+1175 additions, -19 deletions)

View changed files

📝 docs/content/docs/en/config/clients.mdx (+8 -0)
📝 docs/content/docs/fa/config/clients.mdx (+8 -0)
📝 docs/content/docs/ru/config/clients.mdx (+8 -0)
📝 docs/content/docs/zh/config/clients.mdx (+6 -0)
📝 frontend/public/openapi.json (+24 -0)
📝 frontend/src/components/clients/ConfigBlock.css (+1 -0)
📝 frontend/src/generated/examples.ts (+6 -0)
📝 frontend/src/generated/schemas.ts (+24 -0)
📝 frontend/src/generated/types.ts (+6 -0)
📝 frontend/src/generated/zod.ts (+6 -0)
📝 frontend/src/hooks/useClients.ts (+4 -0)
📝 frontend/src/models/setting.ts (+1 -0)
📝 frontend/src/pages/clients/ClientBulkAddModal.tsx (+27 -1)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+44 -0)
📝 frontend/src/pages/clients/ClientsPage.tsx (+2 -1)
📝 frontend/src/pages/settings/GeneralTab.tsx (+5 -0)
📝 frontend/src/schemas/client.ts (+6 -0)
📝 frontend/src/schemas/defaults.ts (+1 -0)
📝 frontend/src/schemas/setting.ts (+1 -0)
📝 internal/database/db.go (+18 -0)

...and 22 more files

📄 Description

Store speedDown/speedUp on clients and shape traffic for observed client IPs on the primary interface so operators can cap bandwidth without Fail2ban.

Summary

Adds optional per-client download/upload speed limits (Mbps), stored on the client record and enforced with Linux tc HTB (egress) plus a shared ingress police action per client on the primary interface. Shaping is driven by the existing online-IP scan job and is disabled by default behind a speedLimitEnable setting.

Why

Operators often need to cap a client’s bandwidth without Fail2ban or IP bans. This gives panel-level speed controls that apply to observed client IPs on the host’s primary NIC, without seizing the host qdisc unless an admin explicitly opts in.

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. Build/compile the changed packages and regenerate OpenAPI/Zod via make gen.
  2. Unit tests: go test ./internal/web/service/ -run 'TestUniqueValidIPs|TestIPMatch|TestTcShaper' (Sync add/update/remove, shared police across multiple IPs).
  3. Settings → General: leave Client Speed Limit off; confirm no tc takeover on panel start/restart. Enable it, restart panel, then proceed.
  4. Panel → Clients → create/edit: set Download / Upload (Mbps), save, reopen and confirm values persist. Confirm fields are hidden for MTProto-only clients and when the setting is off.
  5. Bulk add (with the setting enabled): set speed fields and confirm created clients receive them.
  6. On Linux with tc and root (or CAP_NET_ADMIN): connect a limited client, wait for the IP-scan job (~10s), then check tc class show / tc filter show / tc actions for HTB (down) and a shared police index referenced by each IP (up).
  7. With limitIp > 1 and a nonzero speedUp, confirm multiple observed IPs share one police action (aggregate upload cap).
  8. Set limits to 0 (or disconnect), wait for the next sync, and confirm classes/filters/actions are removed.
  9. Confirm Fail2ban IP limiting still runs independently when enabled.

Screenshots / recordings

Breaking changes

None for API consumers beyond optional new fields (speedDown, speedUp; default 0 = unlimited) and optional setting speedLimitEnable (default false).

Ops note: Shaping does not run until Client Speed Limit is enabled and the panel is restarted. When enabled on Linux, the panel takes over the primary interface root qdisc with HTB and may install an ingress qdisc—hosts with an existing custom tc/QoS setup should opt in knowingly. Requires tc and sufficient privileges; unavailable platforms (e.g. Windows) skip shaping. New columns are added via GORM AutoMigrate (speed_down, speed_up).

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/6043 **Author:** [@HamidRezaSZ](https://github.com/HamidRezaSZ) **Created:** 7/19/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/speed-limit` --- ### 📝 Commits (10+) - [`c41fb72`](https://github.com/MHSanaei/3x-ui/commit/c41fb7290021e1589050403d2c0fa104617a9021) feat(clients): add per-client download/upload speed limits via tc HTB - [`2bdb585`](https://github.com/MHSanaei/3x-ui/commit/2bdb585e0f493d4b494008154eee64b9dd62e3f2) fix(clients): use CommandContext for tc invocations - [`e77f8dc`](https://github.com/MHSanaei/3x-ui/commit/e77f8dc061f9221cc8cffcd05d29edc867365836) fix(clients): gate tc shaping and share upload police per client - [`2827384`](https://github.com/MHSanaei/3x-ui/commit/2827384888abbae6ee8fd8a72d9838097886559e) fix(clients): avoid int overflow in police index allocation - [`6f0acab`](https://github.com/MHSanaei/3x-ui/commit/6f0acab6b312626718b6ce5f2ee537ea7b65319f) docs(clients): document per-client speed limits and opt-in setting - [`110043c`](https://github.com/MHSanaei/3x-ui/commit/110043ccbf2604d91705f5e08d9050cf3b153961) fix(frontend): raise ConfigBlock text contrast for a11y - [`b00743d`](https://github.com/MHSanaei/3x-ui/commit/b00743dc6c8e01e77f2370eb7fe69e0de7ae413b) fix(clients): address Copilot review on tc speed limiting - [`17ac009`](https://github.com/MHSanaei/3x-ui/commit/17ac00904de3aeb954af0d1c53353cf505c2a23a) Merge branch 'main' into feat/speed-limit - [`9ff2a28`](https://github.com/MHSanaei/3x-ui/commit/9ff2a28ec29a26d5ff2691f53d5fcff304adaca2) Merge branch 'main' into feat/speed-limit - [`2e426e5`](https://github.com/MHSanaei/3x-ui/commit/2e426e5ea5b4af926f1209d38a8d118ea7cff091) Merge branch 'main' into feat/speed-limit ### 📊 Changes **42 files changed** (+1175 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/config/clients.mdx` (+8 -0) 📝 `docs/content/docs/fa/config/clients.mdx` (+8 -0) 📝 `docs/content/docs/ru/config/clients.mdx` (+8 -0) 📝 `docs/content/docs/zh/config/clients.mdx` (+6 -0) 📝 `frontend/public/openapi.json` (+24 -0) 📝 `frontend/src/components/clients/ConfigBlock.css` (+1 -0) 📝 `frontend/src/generated/examples.ts` (+6 -0) 📝 `frontend/src/generated/schemas.ts` (+24 -0) 📝 `frontend/src/generated/types.ts` (+6 -0) 📝 `frontend/src/generated/zod.ts` (+6 -0) 📝 `frontend/src/hooks/useClients.ts` (+4 -0) 📝 `frontend/src/models/setting.ts` (+1 -0) 📝 `frontend/src/pages/clients/ClientBulkAddModal.tsx` (+27 -1) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+44 -0) 📝 `frontend/src/pages/clients/ClientsPage.tsx` (+2 -1) 📝 `frontend/src/pages/settings/GeneralTab.tsx` (+5 -0) 📝 `frontend/src/schemas/client.ts` (+6 -0) 📝 `frontend/src/schemas/defaults.ts` (+1 -0) 📝 `frontend/src/schemas/setting.ts` (+1 -0) 📝 `internal/database/db.go` (+18 -0) _...and 22 more files_ </details> ### 📄 Description Store speedDown/speedUp on clients and shape traffic for observed client IPs on the primary interface so operators can cap bandwidth without Fail2ban. ## Summary Adds optional per-client download/upload speed limits (Mbps), stored on the client record and enforced with Linux `tc` HTB (egress) plus a shared ingress police action per client on the primary interface. Shaping is driven by the existing online-IP scan job and is **disabled by default** behind a `speedLimitEnable` setting. ## Why Operators often need to cap a client’s bandwidth without Fail2ban or IP bans. This gives panel-level speed controls that apply to observed client IPs on the host’s primary NIC, without seizing the host qdisc unless an admin explicitly opts in. ## 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) - [ ] Statistics / traffic counters - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? 1. Build/compile the changed packages and regenerate OpenAPI/Zod via `make gen`. 2. Unit tests: `go test ./internal/web/service/ -run 'TestUniqueValidIPs|TestIPMatch|TestTcShaper'` (Sync add/update/remove, shared police across multiple IPs). 3. Settings → General: leave **Client Speed Limit** off; confirm no `tc` takeover on panel start/restart. Enable it, restart panel, then proceed. 4. Panel → Clients → create/edit: set **Download** / **Upload** (Mbps), save, reopen and confirm values persist. Confirm fields are hidden for MTProto-only clients and when the setting is off. 5. Bulk add (with the setting enabled): set speed fields and confirm created clients receive them. 6. On Linux with `tc` and root (or `CAP_NET_ADMIN`): connect a limited client, wait for the IP-scan job (~10s), then check `tc class show` / `tc filter show` / `tc actions` for HTB (down) and a shared police index referenced by each IP (up). 7. With `limitIp` > 1 and a nonzero `speedUp`, confirm multiple observed IPs share one police action (aggregate upload cap). 8. Set limits to `0` (or disconnect), wait for the next sync, and confirm classes/filters/actions are removed. 9. Confirm Fail2ban IP limiting still runs independently when enabled. ## Screenshots / recordings <!-- Add screenshots of: Settings → speedLimitEnable; Client formModal / Bulk Add speed fields. --> ## Breaking changes None for API consumers beyond optional new fields (`speedDown`, `speedUp`; default `0` = unlimited) and optional setting `speedLimitEnable` (default `false`). **Ops note:** Shaping does **not** run until **Client Speed Limit** is enabled and the panel is restarted. When enabled on Linux, the panel takes over the primary interface root qdisc with HTB and may install an ingress qdisc—hosts with an existing custom `tc`/QoS setup should opt in knowingly. Requires `tc` and sufficient privileges; unavailable platforms (e.g. Windows) skip shaping. New columns are added via GORM AutoMigrate (`speed_down`, `speed_up`). ## 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#6058
No description provided.