[PR #6243] [MERGED] feat(sub): client-side balancers for the JSON subscription #6198

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6243
Author: @DIMFLIX
Created: 8/17/2026
Status: ✅ Merged
Merged: 8/23/2026
Merged by: @MHSanaei

Base: main ← Head: main


📝 Commits (10+)

  • c4f72ea feat(sub): add SubBalancer model and migration
  • e686a29 feat(sub): add SubBalancer service
  • 8574cd3 feat(sub): add SubBalancer API controller and routes
  • a9d070a feat(sub): emit client-side balancers in JSON subscription
  • 6875224 test(sub): cover SubBalancer service and JSON output
  • bf7b9cf feat(sub): add sub-balancers i18n keys
  • e688b49 feat(sub): add SubBalancer schema and API queries
  • 6e8318b feat(sub): add subscription balancers settings tab
  • 151d688 test(sub): add SubBalancer form modal test
  • b79bfbd feat(sub): register sub-balancers in API docs and OpenAPI

📊 Changes

65 files changed (+6885 additions, -1090 deletions)

View changed files

📝 CLAUDE.md (+2 -3)
📝 docs/content/docs/en/reference/api/api-tokens.mdx (+3 -7)
📝 docs/content/docs/en/reference/api/clients.mdx (+90 -57)
📝 docs/content/docs/en/reference/api/hosts.mdx (+42 -43)
📝 docs/content/docs/en/reference/api/inbounds.mdx (+25 -8)
📝 docs/content/docs/en/reference/api/meta.json (+1 -0)
📝 docs/content/docs/en/reference/api/nodes.mdx (+26 -20)
📝 docs/content/docs/en/reference/api/server.mdx (+67 -12)
📝 docs/content/docs/en/reference/api/settings.mdx (+19 -1)
➕ docs/content/docs/en/reference/api/subscription-balancers.mdx (+60 -0)
📝 docs/content/docs/en/reference/api/subscription-server.mdx (+8 -6)
📝 docs/content/docs/en/reference/api/xray-settings.mdx (+47 -13)
📝 docs/lib/xray/subscription.ts (+2 -0)
📝 docs/public/openapi.json (+3076 -901)
📝 frontend/public/openapi.json (+351 -0)
➕ frontend/src/api/queries/useSubBalancerMutations.ts (+41 -0)
➕ frontend/src/api/queries/useSubBalancersQuery.ts (+31 -0)
📝 frontend/src/api/queryKeys.ts (+4 -0)
📝 frontend/src/generated/examples.ts (+15 -0)
📝 frontend/src/generated/schemas.ts (+73 -0)

...and 45 more files

📄 Description

Summary

Adds Subscription Balancers — admin-defined balancers emitted into the JSON subscription as extra config documents, so a client profile can auto-pick the best of several inbounds' endpoints (Xray routing.balancers + burstObservatory) instead of listing them one by one. Managed in Settings → Sub Balancers, with an Observatory sub-tab for probe parameters, mirroring the Xray Balancers page layout.

Supported strategies: random, roundRobin, leastPing, leastLoad (Xray routing balancer strategies).

Why

Today a JSON-sub client gets one config document per inbound, in the inbound's own order. When several inbounds are meant to be one logical "best of" profile, the only option is to ship them all separately and let the client (or the user) pick — there's no panel-side way to emit a single routing.balancers entry that auto-selects the fastest/least-loaded member outbound.

This gives admins that lever: define a balancer (remark, strategy, member inbounds, sort order), and every client sitting on at least one of those inbounds gets one extra subscription document that uses a balancer routing rule + a burstObservatory to probe and pick the best member outbound.

The same client-side injection pattern is used by Remnawave's injectHosts feature — emitting panel-defined routing into the subscription rather than relying on the client app to choose.

Refs: #5825

Design notes

  • Observatory config is panel-wide, stored as subJsonObservatory (JSON string), consistent with the existing subJsonMux / subJsonRules / subJsonFinalMask settings. Per-balancer probe URLs are a rare need and not worth the model/form bloat.
  • Conditional emit: burstObservatory is emitted only for leastPing / leastLoad. random / roundRobin have no fallback in the client-side model, so an observatory would only make the client probe every outbound for nothing — these strategies emit no observatory (per Xray routing docs).
  • Balancers vs inbounds: member inbounds are selected by id; an admin can pick inbounds hosted on another node as long as they are in the subscriber's subscription. The sub server reads sub_balancers rows per request from its own DB.
  • SubBalancer rows are read per request by the subscription builder, so balancer mutations (create/update/delete) are incremental — no Xray restart, no panel restart needed to apply them. The Observatory sub-tab edits the panel-wide subJsonObservatory setting, which is persisted via the regular Settings Save button.

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?

New unit tests

  • internal/sub/sub_balancer_test.go — emitted balancer document shape (member retagging under bal-<id>-…, routing.balancers selector, balancerTag rules, burstObservatory for leastLoad), ordering/interleaving with inbounds by sort order, disabled balancers and balancers with no matching members skipped, manual inbound docs keep their plain proxy routing (shared routing tree is cloned per doc), burstObservatory emitted only for leastPing/leastLoad (not random/roundRobin) with the panel-wide probe config overriding defaults.
  • internal/web/service/sub_balancer_test.go — CRUD + validation (remark required, valid strategy, ≥1 inbound, sort order ≥1), roundRobin accepted.
  • frontend/src/test/sub-balancer-form-modal.test.tsx — form modal validation + submit payload.

Ran locally

  • go test ./internal/sub/... ./internal/web/... — green.
  • make gen — frontend/src/generated/* regenerated to include the new subJsonObservatory field and the SubBalancer schema; frontend/public/openapi.json updated; re-running make gen produces no further diff (gen-check clean).
  • make typecheck (tsc --noEmit), make lint-fe (eslint), npm test (vitest incl. sub-balancer / observatory / i18n-dead-keys) — green.
  • make verify (gen-check + lint + typecheck + test + build + build-storybook) — passes.

Manual (UI): Settings → Sub Balancers (appears when the JSON subscription is enabled): add/edit/delete a balancer, toggle enabled, reorder via sort order; inbounds from other nodes that are part of the subscription can be selected as members; Observatory sub-tab toggles probe config on/off and edits destination/connectivity/interval/timeout/sampling/httpMethod, persisted via the Settings Save button. A client on ≥1 selected inbound receives one extra JSON-sub document with routing.balancers + (for leastPing/leastLoad) burstObservatory.

Screenshots / recordings

image image image

Breaking changes

None. New sub_balancers table created via AutoMigrate on startup (additive); also registered in migrationModels() so SQLite→Postgres cross-DB migration (x-ui migrate-db) copies it. New /panel/api/sub-balancers endpoints are additive. New subJsonObservatory panel setting defaults to empty → safe built-in probe defaults, so the sub server behaves as before until an admin enables it. No existing API, config field, or subscription output changes for users who don't create a balancer.

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/6243 **Author:** [@DIMFLIX](https://github.com/DIMFLIX) **Created:** 8/17/2026 **Status:** ✅ Merged **Merged:** 8/23/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (10+) - [`c4f72ea`](https://github.com/MHSanaei/3x-ui/commit/c4f72ea8621ac45c5d609d1ea5235958a861ed3b) feat(sub): add SubBalancer model and migration - [`e686a29`](https://github.com/MHSanaei/3x-ui/commit/e686a292cf170bd22e7711a28337c3b21ba2731a) feat(sub): add SubBalancer service - [`8574cd3`](https://github.com/MHSanaei/3x-ui/commit/8574cd33745b55c28c1da06d05775a0e254780b5) feat(sub): add SubBalancer API controller and routes - [`a9d070a`](https://github.com/MHSanaei/3x-ui/commit/a9d070a7b57d75ad54a233c8eb4965078a782813) feat(sub): emit client-side balancers in JSON subscription - [`6875224`](https://github.com/MHSanaei/3x-ui/commit/6875224bffa8eb949333ec1ecec4d9479e491d21) test(sub): cover SubBalancer service and JSON output - [`bf7b9cf`](https://github.com/MHSanaei/3x-ui/commit/bf7b9cf93d2d8071cec61de2ef1d411248e8df30) feat(sub): add sub-balancers i18n keys - [`e688b49`](https://github.com/MHSanaei/3x-ui/commit/e688b49f8ca7e21d77952ae149b00c21bc5870bc) feat(sub): add SubBalancer schema and API queries - [`6e8318b`](https://github.com/MHSanaei/3x-ui/commit/6e8318b6bc71a2c0c69331ab476290a6d0c01a87) feat(sub): add subscription balancers settings tab - [`151d688`](https://github.com/MHSanaei/3x-ui/commit/151d6880dfe62523701d76a674c7093e6b6ebd41) test(sub): add SubBalancer form modal test - [`b79bfbd`](https://github.com/MHSanaei/3x-ui/commit/b79bfbd67cd2bc5e04cc1a4071c607c84401dd49) feat(sub): register sub-balancers in API docs and OpenAPI ### 📊 Changes **65 files changed** (+6885 additions, -1090 deletions) <details> <summary>View changed files</summary> 📝 `CLAUDE.md` (+2 -3) 📝 `docs/content/docs/en/reference/api/api-tokens.mdx` (+3 -7) 📝 `docs/content/docs/en/reference/api/clients.mdx` (+90 -57) 📝 `docs/content/docs/en/reference/api/hosts.mdx` (+42 -43) 📝 `docs/content/docs/en/reference/api/inbounds.mdx` (+25 -8) 📝 `docs/content/docs/en/reference/api/meta.json` (+1 -0) 📝 `docs/content/docs/en/reference/api/nodes.mdx` (+26 -20) 📝 `docs/content/docs/en/reference/api/server.mdx` (+67 -12) 📝 `docs/content/docs/en/reference/api/settings.mdx` (+19 -1) ➕ `docs/content/docs/en/reference/api/subscription-balancers.mdx` (+60 -0) 📝 `docs/content/docs/en/reference/api/subscription-server.mdx` (+8 -6) 📝 `docs/content/docs/en/reference/api/xray-settings.mdx` (+47 -13) 📝 `docs/lib/xray/subscription.ts` (+2 -0) 📝 `docs/public/openapi.json` (+3076 -901) 📝 `frontend/public/openapi.json` (+351 -0) ➕ `frontend/src/api/queries/useSubBalancerMutations.ts` (+41 -0) ➕ `frontend/src/api/queries/useSubBalancersQuery.ts` (+31 -0) 📝 `frontend/src/api/queryKeys.ts` (+4 -0) 📝 `frontend/src/generated/examples.ts` (+15 -0) 📝 `frontend/src/generated/schemas.ts` (+73 -0) _...and 45 more files_ </details> ### 📄 Description ## Summary Adds **Subscription Balancers** — admin-defined balancers emitted into the JSON subscription as extra config documents, so a client profile can auto-pick the best of several inbounds' endpoints (Xray `routing.balancers` + `burstObservatory`) instead of listing them one by one. Managed in **Settings → Sub Balancers**, with an **Observatory** sub-tab for probe parameters, mirroring the Xray Balancers page layout. Supported strategies: `random`, `roundRobin`, `leastPing`, `leastLoad` (Xray routing balancer strategies). ## Why Today a JSON-sub client gets one config document per inbound, in the inbound's own order. When several inbounds are meant to be one logical "best of" profile, the only option is to ship them all separately and let the client (or the user) pick — there's no panel-side way to emit a single `routing.balancers` entry that auto-selects the fastest/least-loaded member outbound. This gives admins that lever: define a balancer (remark, strategy, member inbounds, sort order), and every client sitting on at least one of those inbounds gets one extra subscription document that uses a `balancer` routing rule + a `burstObservatory` to probe and pick the best member outbound. The same client-side injection pattern is used by Remnawave's `injectHosts` feature — emitting panel-defined routing into the subscription rather than relying on the client app to choose. Refs: #5825 ## Design notes - **Observatory config is panel-wide**, stored as `subJsonObservatory` (JSON string), consistent with the existing `subJsonMux` / `subJsonRules` / `subJsonFinalMask` settings. Per-balancer probe URLs are a rare need and not worth the model/form bloat. - **Conditional emit**: `burstObservatory` is emitted only for `leastPing` / `leastLoad`. `random` / `roundRobin` have no fallback in the client-side model, so an observatory would only make the client probe every outbound for nothing — these strategies emit no observatory (per Xray routing docs). - **Balancers vs inbounds**: member inbounds are selected by id; an admin can pick inbounds hosted on another node as long as they are in the subscriber's subscription. The sub server reads `sub_balancers` rows per request from its own DB. - `SubBalancer` rows are read per request by the subscription builder, so balancer mutations (create/update/delete) are incremental — no Xray restart, no panel restart needed to apply them. The Observatory sub-tab edits the panel-wide `subJsonObservatory` setting, which is persisted via the regular Settings **Save** button. ## 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 - [x] 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? **New unit tests** - `internal/sub/sub_balancer_test.go` — emitted balancer document shape (member retagging under `bal-<id>-…`, `routing.balancers` selector, `balancerTag` rules, `burstObservatory` for `leastLoad`), ordering/interleaving with inbounds by sort order, disabled balancers and balancers with no matching members skipped, manual inbound docs keep their plain `proxy` routing (shared routing tree is cloned per doc), `burstObservatory` emitted only for `leastPing`/`leastLoad` (not `random`/`roundRobin`) with the panel-wide probe config overriding defaults. - `internal/web/service/sub_balancer_test.go` — CRUD + validation (remark required, valid strategy, ≥1 inbound, sort order ≥1), `roundRobin` accepted. - `frontend/src/test/sub-balancer-form-modal.test.tsx` — form modal validation + submit payload. **Ran locally** - `go test ./internal/sub/... ./internal/web/...` — green. - `make gen` — `frontend/src/generated/*` regenerated to include the new `subJsonObservatory` field and the `SubBalancer` schema; `frontend/public/openapi.json` updated; re-running `make gen` produces no further diff (gen-check clean). - `make typecheck` (`tsc --noEmit`), `make lint-fe` (eslint), `npm test` (vitest incl. sub-balancer / observatory / i18n-dead-keys) — green. - `make verify` (gen-check + lint + typecheck + test + build + build-storybook) — passes. **Manual (UI):** Settings → Sub Balancers (appears when the JSON subscription is enabled): add/edit/delete a balancer, toggle enabled, reorder via sort order; inbounds from other nodes that are part of the subscription can be selected as members; Observatory sub-tab toggles probe config on/off and edits destination/connectivity/interval/timeout/sampling/httpMethod, persisted via the Settings **Save** button. A client on ≥1 selected inbound receives one extra JSON-sub document with `routing.balancers` + (for `leastPing`/`leastLoad`) `burstObservatory`. ## Screenshots / recordings <img width="1889" height="894" alt="image" src="https://github.com/user-attachments/assets/7b4fea36-ed8e-428f-84ca-1c894fe2f2d8" /> <img width="670" height="605" alt="image" src="https://github.com/user-attachments/assets/3a1423aa-630a-4ffd-a20d-cceeefadcaaa" /> <img width="1884" height="660" alt="image" src="https://github.com/user-attachments/assets/76bd6d4a-c2c8-492e-8378-8a02fb1e051c" /> ## Breaking changes None. New `sub_balancers` table created via AutoMigrate on startup (additive); also registered in `migrationModels()` so SQLite→Postgres cross-DB migration (`x-ui migrate-db`) copies it. New `/panel/api/sub-balancers` endpoints are additive. New `subJsonObservatory` panel setting defaults to empty → safe built-in probe defaults, so the sub server behaves as before until an admin enables it. No existing API, config field, or subscription output changes for users who don't create a balancer. ## 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:07:03 +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#6198
No description provided.