[GH-ISSUE #6512] [Feature]: Add abilty to attach subscription to balancer selector #4379

Closed
opened 2026-10-31 20:39:15 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @lifufkd on GitHub (Sep 13, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6512

Hi! It would be great if it can be possible to chose entire subscription (all outbounds from it) in balancer selector. This can eliminate the need to manually change outbounds (on unstable subscriptions, they change very often)

Describe the solution you'd like

On my opinion it can be made like this: when subscriptions are automatically renewed, you need to check to replace outbounds with new ones in each corresponding balancer selector

Alternatives you've considered

No response

Concrete use case

No response

Mockups, screenshots, or examples

No response

Which parts of the panel would this affect?

No response

Are you willing to help?

  • I'd like to implement this feature myself and open a PR.
  • I can help test once a PR is open.

Before submitting

  • I searched existing issues and this feature has not been requested.
  • This feature request is written in English.
Originally created by @lifufkd on GitHub (Sep 13, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6512 ### Is your feature request related to a problem? Hi! It would be great if it can be possible to chose entire subscription (all outbounds from it) in balancer selector. This can eliminate the need to manually change outbounds (on unstable subscriptions, they change very often) ### Describe the solution you'd like On my opinion it can be made like this: when subscriptions are automatically renewed, you need to check to replace outbounds with new ones in each corresponding balancer selector ### Alternatives you've considered _No response_ ### Concrete use case _No response_ ### Mockups, screenshots, or examples _No response_ ### Which parts of the panel would this affect? _No response_ ### Are you willing to help? - [ ] I'd like to implement this feature myself and open a PR. - [ ] I can help test once a PR is open. ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this feature has not been requested. - [x] This feature request is written in English.
bsakaguchi 2026-10-31 20:39:15 +07:00
Author
Owner

@github-actions[bot] commented on GitHub (Sep 13, 2026):

This already works today — no code change is needed. A balancer selector entry is a tag prefix, and every outbound from one subscription already shares a prefix, so putting that prefix in the selector is "attach the whole subscription to the balancer": servers the provider adds on the next refresh join the balancer automatically, and servers it drops fall out.

How to do it

  1. Open Xray → Outbounds → subscription, and read (or set) the Tag prefix field. Leave it blank and the panel assigns sub1-, sub2-, … automatically:

    // internal/web/service/outbound_subscription.go:166
    return fmt.Sprintf("sub%d-", defaultPrefixNumber(subs, excludeId)), nil
    

    The prefix is also shown under the subscription in the list (frontend/src/pages/xray/outbounds/OutboundsTab.tsx:910-911).

  2. Open Xray → Balancers → your balancer, and type that prefix — e.g. sub1-, or hk- if you named it — as a single selector entry, instead of listing the individual outbounds. The field accepts free text; it is an Ant Design tags input, not a closed dropdown:

    // frontend/src/pages/xray/balancers/BalancerFormModal.tsx:250-256
    <FormField name="selector" label={t('pages.xray.balancer.selector')} required>
      <Select
        mode="tags"
        tokenSeparators={[',']}
        options={outboundTags.map((tg) => ({ value: tg, label: tg }))}
      />
    
  3. Save. Nothing else to maintain.

Use one prefix per subscription and keep them distinct — matching is a plain string prefix, so sub1- would also swallow a sub1-extra-… tag. Setting a fallbackTag on the balancer is worth it too: if the provider ever returns an empty list, the balancer has no members left.

Why this holds across refreshes

The refresh loop you describe is already wired end to end:

  • internal/web/job/outbound_subscription_job.go → RefreshAllEnabled(), then j.xraySvc.SetToNeedRestart() when anything changed, so Xray reloads with the new outbound set. It runs on cadenceOutboundSub = "@every 5m" (internal/web/web.go:305), gated per subscription by its own updateInterval (default 600 seconds, internal/web/service/outbound_subscription.go:177-179).
  • The fetched outbounds are merged into the generated config at internal/web/service/xray.go:376-379, never into your saved template.
  • Fresh tags are always built from the subscription's prefix (outbound_subscription.go:501-509, via link.SuggestTag(prefix, remark, i)), which is what makes the prefix selector pick up new servers.

This is the documented, intended design rather than an accident — the strategy note at the bottom of internal/web/service/outbound_subscription.go:642-645 says so directly:

  • If you use a prefix/wildcard selector (e.g. "hk-", "sg-."), then any
    new servers that the subscription later returns will automatically be
    eligible for that balancer on the next Xray reload — this is the recommended
    way to "subscribe to a pool".

The panel relies on the same prefix semantics for its own subscription balancers — internal/sub/json_service.go:537 emits "selector": []string{prefix}, and internal/sub/sub_balancer_test.go:87 pins the generated selector to the bare prefix "bal-1-". One caveat on the comment quoted above: everything the panel itself generates uses the bare prefix with no * (see also the golden fixture "selector": []any{"proxy-"} at internal/web/service/golden_fixtures_xray_test.go:301), so use sub1-, not sub1-*. I could not check the * / regex form against xray-core's router from this checkout, so I can only vouch for the bare-prefix form.

Docs: Outbound subscriptions & balancers — docs/content/docs/en/operations/outbounds-routing.mdx:127-132 covers exactly this.

On the duplicate

#5316 ("Subscription LoadBalancer") asked for the same thing, and its "Alternatives you've considered" section proposed the same prefix-expansion idea. It was closed as completed on 2026-06-14 because the mechanism above already covers it. Closing this one the same way — the remaining gap is UX (the panel never points you at the prefix workflow from the balancer form), so if you want to push for that specifically, a request framed as "surface the subscription prefix as a pickable balancer member in the UI" is the one that is still open ground.

Verdict: Duplicate
Duplicate of: #5316 — same request (attach a whole outbound subscription to a balancer), same resolution: a prefix selector entry already expands to the subscription's outbound pool at every Xray reload
Evidence: internal/web/service/outbound_subscription.go:642-645 (design note naming prefix selectors "the recommended way to subscribe to a pool"); outbound_subscription.go:166 + :501-509 (every tag from a subscription carries its prefix); frontend/src/pages/xray/balancers/BalancerFormModal.tsx:250-256 (mode="tags" — arbitrary selector text accepted); internal/sub/json_service.go:537 and internal/sub/sub_balancer_test.go:87 (the panel emits a bare-prefix selector itself); internal/web/job/outbound_subscription_job.go + internal/web/web.go:305 + internal/web/service/xray.go:376-379 (refresh → reload → re-merge)
Not determined: whether xray-core treats a trailing * or a regex in a selector entry as a wildcard — the xray-core module source is not present in this checkout. Grepping app/router/balancing.go upstream would settle it; it does not change the advice above, which uses the bare prefix.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:5656203244 --> @github-actions[bot] commented on GitHub (Sep 13, 2026): This already works today — no code change is needed. A balancer selector entry is a **tag prefix**, and every outbound from one subscription already shares a prefix, so putting that prefix in the selector *is* "attach the whole subscription to the balancer": servers the provider adds on the next refresh join the balancer automatically, and servers it drops fall out. ## How to do it 1. Open **Xray → Outbounds → subscription**, and read (or set) the **Tag prefix** field. Leave it blank and the panel assigns `sub1-`, `sub2-`, … automatically: ```go // internal/web/service/outbound_subscription.go:166 return fmt.Sprintf("sub%d-", defaultPrefixNumber(subs, excludeId)), nil ``` The prefix is also shown under the subscription in the list (`frontend/src/pages/xray/outbounds/OutboundsTab.tsx:910-911`). 2. Open **Xray → Balancers → your balancer**, and type that prefix — e.g. `sub1-`, or `hk-` if you named it — as a **single** selector entry, instead of listing the individual outbounds. The field accepts free text; it is an Ant Design tags input, not a closed dropdown: ```tsx // frontend/src/pages/xray/balancers/BalancerFormModal.tsx:250-256 <FormField name="selector" label={t('pages.xray.balancer.selector')} required> <Select mode="tags" tokenSeparators={[',']} options={outboundTags.map((tg) => ({ value: tg, label: tg }))} /> ``` 3. Save. Nothing else to maintain. Use one prefix per subscription and keep them distinct — matching is a plain string prefix, so `sub1-` would also swallow a `sub1-extra-…` tag. Setting a `fallbackTag` on the balancer is worth it too: if the provider ever returns an empty list, the balancer has no members left. ## Why this holds across refreshes The refresh loop you describe is already wired end to end: - `internal/web/job/outbound_subscription_job.go` → `RefreshAllEnabled()`, then `j.xraySvc.SetToNeedRestart()` when anything changed, so Xray reloads with the new outbound set. It runs on `cadenceOutboundSub = "@every 5m"` (`internal/web/web.go:305`), gated per subscription by its own `updateInterval` (default `600` seconds, `internal/web/service/outbound_subscription.go:177-179`). - The fetched outbounds are merged into the generated config at `internal/web/service/xray.go:376-379`, never into your saved template. - Fresh tags are always built from the subscription's prefix (`outbound_subscription.go:501-509`, via `link.SuggestTag(prefix, remark, i)`), which is what makes the prefix selector pick up new servers. This is the documented, intended design rather than an accident — the strategy note at the bottom of `internal/web/service/outbound_subscription.go:642-645` says so directly: > - If you use a *prefix/wildcard* selector (e.g. "hk-*", "sg-.*"), then any > *new* servers that the subscription later returns will automatically be > eligible for that balancer on the next Xray reload — this is the recommended > way to "subscribe to a pool". The panel relies on the same prefix semantics for its own subscription balancers — `internal/sub/json_service.go:537` emits `"selector": []string{prefix}`, and `internal/sub/sub_balancer_test.go:87` pins the generated selector to the bare prefix `"bal-1-"`. One caveat on the comment quoted above: everything the panel itself generates uses the **bare** prefix with no `*` (see also the golden fixture `"selector": []any{"proxy-"}` at `internal/web/service/golden_fixtures_xray_test.go:301`), so use `sub1-`, not `sub1-*`. I could not check the `*` / regex form against xray-core's router from this checkout, so I can only vouch for the bare-prefix form. Docs: [Outbound subscriptions & balancers](https://docs.sanaei.dev/docs/operations/outbounds-routing) — `docs/content/docs/en/operations/outbounds-routing.mdx:127-132` covers exactly this. ## On the duplicate #5316 ("Subscription LoadBalancer") asked for the same thing, and its "Alternatives you've considered" section proposed the same prefix-expansion idea. It was closed as completed on 2026-06-14 because the mechanism above already covers it. Closing this one the same way — the remaining gap is UX (the panel never points you at the prefix workflow from the balancer form), so if you want to push for that specifically, a request framed as "surface the subscription prefix as a pickable balancer member in the UI" is the one that is still open ground. Verdict: Duplicate Duplicate of: #5316 — same request (attach a whole outbound subscription to a balancer), same resolution: a prefix selector entry already expands to the subscription's outbound pool at every Xray reload Evidence: `internal/web/service/outbound_subscription.go:642-645` (design note naming prefix selectors "the recommended way to subscribe to a pool"); `outbound_subscription.go:166` + `:501-509` (every tag from a subscription carries its prefix); `frontend/src/pages/xray/balancers/BalancerFormModal.tsx:250-256` (`mode="tags"` — arbitrary selector text accepted); `internal/sub/json_service.go:537` and `internal/sub/sub_balancer_test.go:87` (the panel emits a bare-prefix selector itself); `internal/web/job/outbound_subscription_job.go` + `internal/web/web.go:305` + `internal/web/service/xray.go:376-379` (refresh → reload → re-merge) Not determined: whether xray-core treats a trailing `*` or a regex in a selector entry as a wildcard — the xray-core module source is not present in this checkout. Grepping `app/router/balancing.go` upstream would settle it; it does not change the advice above, which uses the bare prefix. *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
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#4379
No description provided.