mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6243] [MERGED] feat(sub): client-side balancers for the JSON subscription #6198
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6198
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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+)
c4f72eafeat(sub): add SubBalancer model and migratione686a29feat(sub): add SubBalancer service8574cd3feat(sub): add SubBalancer API controller and routesa9d070afeat(sub): emit client-side balancers in JSON subscription6875224test(sub): cover SubBalancer service and JSON outputbf7b9cffeat(sub): add sub-balancers i18n keyse688b49feat(sub): add SubBalancer schema and API queries6e8318bfeat(sub): add subscription balancers settings tab151d688test(sub): add SubBalancer form modal testb79bfbdfeat(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.balancersentry 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
balancerrouting rule + aburstObservatoryto probe and pick the best member outbound.The same client-side injection pattern is used by Remnawave's
injectHostsfeature — emitting panel-defined routing into the subscription rather than relying on the client app to choose.Refs: #5825
Design notes
subJsonObservatory(JSON string), consistent with the existingsubJsonMux/subJsonRules/subJsonFinalMasksettings. Per-balancer probe URLs are a rare need and not worth the model/form bloat.burstObservatoryis emitted only forleastPing/leastLoad.random/roundRobinhave 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).sub_balancersrows per request from its own DB.SubBalancerrows 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-widesubJsonObservatorysetting, which is persisted via the regular Settings Save button.Type of change
Areas affected
How was this tested?
New unit tests
internal/sub/sub_balancer_test.go— emitted balancer document shape (member retagging underbal-<id>-…,routing.balancersselector,balancerTagrules,burstObservatoryforleastLoad), ordering/interleaving with inbounds by sort order, disabled balancers and balancers with no matching members skipped, manual inbound docs keep their plainproxyrouting (shared routing tree is cloned per doc),burstObservatoryemitted only forleastPing/leastLoad(notrandom/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),roundRobinaccepted.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 newsubJsonObservatoryfield and theSubBalancerschema;frontend/public/openapi.jsonupdated; re-runningmake genproduces 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+ (forleastPing/leastLoad)burstObservatory.Screenshots / recordings
Breaking changes
None. New
sub_balancerstable created via AutoMigrate on startup (additive); also registered inmigrationModels()so SQLite→Postgres cross-DB migration (x-ui migrate-db) copies it. New/panel/api/sub-balancersendpoints are additive. NewsubJsonObservatorypanel 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
go build ./...and the test suite pass locally.npm run lint,npm run typecheck, andnpm run buildpass.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.