[PR #5586] [MERGED] feat(balancer): add balancer-to-balancer fallback support #5920

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5586
Author: @Ssentiago
Created: 6/26/2026
Status: ✅ Merged
Merged: 7/8/2026
Merged by: @MHSanaei

Base: main ← Head: feat/balancer-nested-fallback


📝 Commits (4)

  • 71831de feat(balancer): add balancer-to-balancer fallback support
  • 0be0f21 fix(review): override regression, save payload sync, i18n completeness
  • 3d69efe fix(test): update balancer form modal tests after cycle-detection guard
  • c5e3545 test(balancer): cover loopback dedup/cycle/rename/orphan paths

📊 Changes

24 files changed (+932 additions, -60 deletions)

View changed files

📝 frontend/src/pages/xray/XrayPage.tsx (+15 -0)
📝 frontend/src/pages/xray/balancers/BalancerFormModal.tsx (+107 -7)
📝 frontend/src/pages/xray/balancers/BalancersTab.tsx (+107 -12)
➕ frontend/src/pages/xray/balancers/balancer-loopback.ts (+198 -0)
📝 frontend/src/pages/xray/outbounds/OutboundFormModal.tsx (+4 -0)
📝 frontend/src/pages/xray/outbounds/OutboundsTab.tsx (+8 -1)
📝 frontend/src/pages/xray/routing/RoutingTab.tsx (+27 -21)
📝 frontend/src/schemas/xray.ts (+9 -2)
📝 frontend/src/test/balancer-form-modal.test.tsx (+34 -4)
➕ frontend/src/test/balancer-loopback.test.ts (+285 -0)
📝 internal/web/service/xray.go (+48 -1)
📝 internal/web/translation/ar-EG.json (+7 -1)
📝 internal/web/translation/en-US.json (+6 -0)
📝 internal/web/translation/es-ES.json (+7 -1)
📝 internal/web/translation/fa-IR.json (+7 -1)
📝 internal/web/translation/id-ID.json (+7 -1)
📝 internal/web/translation/ja-JP.json (+7 -1)
📝 internal/web/translation/pt-BR.json (+7 -1)
📝 internal/web/translation/ru-RU.json (+7 -1)
📝 internal/web/translation/tr-TR.json (+7 -1)

...and 4 more files

📄 Description

Summary

Add seamless balancer-to-balancer fallback support. Users can now select another balancer as a fallback directly from the balancer form dropdown — the panel automatically generates the required loopback infrastructure (loopback outbound + routing rule) under the hood.

Why

Xray does not natively support using a balancer as a fallbackTag for another balancer. Before this feature, setting up balancer-to-balancer fallback required the following manual steps:

Assume balancers A and B exist, and we want B as the fallback for A:

  1. Create an outbound with protocol loopback and a unique tag (e.g. B_loopback), setting inboundTag to the same unique tag
  2. Create a routing rule: set inboundTag to the loopback tag from step 1, set balancerTag to the target balancer B
  3. Edit balancer A — set its fallbackTag to the loopback outbound tag from step 1

This process is:

  • Tedious: 3 manual steps across 3 different tabs (Outbounds, Routing, Balancers)
  • Error-prone: typos in tags silently break routing; wrong tags produce no traffic
  • Undocumented: no UI hints, no validation — users must know the xray loopback internals
  • Hard to maintain: renaming a balancer requires manually updating the loopback outbound tag, the routing rule, and other balancers' fallbackTag references

This feature automates all of it: the user just picks a fallback balancer from a dropdown, and the panel handles the loopback plumbing, deduplication, rename propagation, and cleanup.

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 it works

When a user selects balancer B as fallback for balancer A:

  1. Panel creates two objects in the xray template:

    • Loopback outbound _bl_B — re-injects traffic back into xray routing (settings.inboundTag: "_bl_B")
    • Routing rule — sends traffic from _bl_B inbound tag to balancer B
  2. Balancer A's fallbackTag is set to _bl_B (the loopback outbound tag)

  3. Traffic flow:

    Balancer A (fallback) → loopback outbound _bl_B → routing rule → Balancer B → outbound
    

Deduplication: Multiple balancers sharing the same fallback target (e.g., A→B and C→B) reuse a single _bl_B loopback. The loopback is only removed when no balancer references it.

Self-healing: On save, ensureMissingBalancerLoopbacks creates any missing loopback objects for balancers with _bl_ fallback tags. cleanupOrphanedBalancerLoopbacks removes orphaned loopback objects. The _bl_ prefix is reserved — any outbound with this prefix is overwritten with the correct loopback outbound.

Files changed

Frontend

File Change
frontend/src/pages/xray/balancers/balancer-loopback.ts New. Core helper module: create/remove/resolve/rename/dedup/cleanup loopback objects, DFS cycle detection, self-healing
frontend/src/pages/xray/balancers/BalancerFormModal.tsx Fallback dropdown shows other balancers with indicator. Info alert. Per-tag DFS cycle detection with disabled options + tooltip. Self-reference guard
frontend/src/pages/xray/balancers/BalancersTab.tsx Auto-generate/remove loopback on fallback change. Delete protection. Live status resolve _bl_ tags. Override dropdown includes fallback balancer
frontend/src/pages/xray/outbounds/OutboundsTab.tsx Filters _bl_ loopback outbounds from UI
frontend/src/pages/xray/routing/RoutingTab.tsx Filters _bl_ routing rules and _bl_ inbound tags from UI
frontend/src/pages/xray/XrayPage.tsx Self-healing + orphan cleanup + cycle detection on save (order: heal → cleanup → check)
frontend/src/schemas/xray.ts Zod .refine() rejects _bl_ prefix in balancer tag and outbound tag schemas

Backend

File Change
internal/web/service/xray.go OverrideBalancer resolves balancer tags to loopback outbound tags via routing rules. Returns error if resolution fails or target is unresolved

i18n (13 locale files, 6 keys)

Key Purpose
pages.xray.balancer.fallbackBalancerHint Dropdown hint text
pages.xray.balancer.balancerFallbackInfo Info alert explaining traffic routing path and latency
pages.xray.balancer.balancerFallbackCycle Cycle detection error message
pages.xray.balancer.balancerDeleteInUse Delete protection error with dependent balancer names
pages.xray.balancer.reservedPrefix _bl_ prefix rejection message
pages.xray.balancer.cycleTooltip Disabled dropdown option tooltip showing cycle path

Safety guards

Guard Where Behavior
Self-reference BalancerFormModal Cannot select own balancer as fallback (excluded from dropdown)
Cycle detection (edit) BalancerFormModal DFS Per-tag: disabled options with tooltip showing cycle path (e.g., "Cycle: A → B → (back to A)")
Cycle detection (save) XrayPage onSaveAll Detects existing cycles in config and blocks save
Delete protection BalancersTab confirmDelete Blocks deletion if balancer is used as fallback by others, shows which ones
Routing rule cleanup BalancersTab confirmDelete Deletes routing rules referencing a removed balancer
Self-healing XrayPage onSaveAll Creates missing loopback objects for broken references
Orphan cleanup XrayPage onSaveAll Removes loopback objects no longer referenced by any balancer
Override resolution xray.go OverrideBalancer Resolves balancer tags to loopback outbound tags; returns error if resolution fails
_bl_ prefix validation Zod schemas (xray.ts) Panel rejects creation of outbounds and balancers with tags starting with _bl_ — this prefix is reserved for internal loopback objects
_bl_ UI filtering OutboundsTab, RoutingTab, BalancersTab _bl_ objects hidden from tables and dropdowns

How was this tested?

  1. Create two balancers (A and B), set B as fallback for A — verify traffic flows through loopback
  2. Create third balancer C, set B as fallback — verify deduplication (shared _bl_B)
  3. Try to set A as fallback for B when A→B exists — verify cycle detection blocks selection
  4. Delete balancer B while A uses it as fallback — verify delete is blocked with error
  5. Rename balancer B — verify loopback objects and references are updated
  6. Override balancer A target to B — verify override works through loopback mechanism
  7. Edit Advanced JSON to create cycle (X→Y→Z→X) — verify save is blocked
  8. Edit Advanced JSON to remove loopback objects — verify self-healing recreates them on save
  9. Verify _bl_ tags are rejected when creating outbounds/balancers via form
  10. Verify internal _bl_ objects are hidden from Outbounds, Routing, and Balancers tabs

Screenshots / recordings

Section with balancers (one used as fallback for another):
image

Balancers in the Fallback list:
image

Breaking changes

None. Internal _bl_ objects are filtered from the UI and stored in the xray template alongside existing config. The feature is additive — existing setups without nested balancers are unaffected.

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/5586 **Author:** [@Ssentiago](https://github.com/Ssentiago) **Created:** 6/26/2026 **Status:** ✅ Merged **Merged:** 7/8/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/balancer-nested-fallback` --- ### 📝 Commits (4) - [`71831de`](https://github.com/MHSanaei/3x-ui/commit/71831deae860ffe223834da2f50874278103f0ed) feat(balancer): add balancer-to-balancer fallback support - [`0be0f21`](https://github.com/MHSanaei/3x-ui/commit/0be0f215972c32fec7e5998d1f45421fd21013e2) fix(review): override regression, save payload sync, i18n completeness - [`3d69efe`](https://github.com/MHSanaei/3x-ui/commit/3d69efe21d861c97dbd6dc667c65691205640594) fix(test): update balancer form modal tests after cycle-detection guard - [`c5e3545`](https://github.com/MHSanaei/3x-ui/commit/c5e35454a30667699b4808158b145049d0ca39ca) test(balancer): cover loopback dedup/cycle/rename/orphan paths ### 📊 Changes **24 files changed** (+932 additions, -60 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/pages/xray/XrayPage.tsx` (+15 -0) 📝 `frontend/src/pages/xray/balancers/BalancerFormModal.tsx` (+107 -7) 📝 `frontend/src/pages/xray/balancers/BalancersTab.tsx` (+107 -12) ➕ `frontend/src/pages/xray/balancers/balancer-loopback.ts` (+198 -0) 📝 `frontend/src/pages/xray/outbounds/OutboundFormModal.tsx` (+4 -0) 📝 `frontend/src/pages/xray/outbounds/OutboundsTab.tsx` (+8 -1) 📝 `frontend/src/pages/xray/routing/RoutingTab.tsx` (+27 -21) 📝 `frontend/src/schemas/xray.ts` (+9 -2) 📝 `frontend/src/test/balancer-form-modal.test.tsx` (+34 -4) ➕ `frontend/src/test/balancer-loopback.test.ts` (+285 -0) 📝 `internal/web/service/xray.go` (+48 -1) 📝 `internal/web/translation/ar-EG.json` (+7 -1) 📝 `internal/web/translation/en-US.json` (+6 -0) 📝 `internal/web/translation/es-ES.json` (+7 -1) 📝 `internal/web/translation/fa-IR.json` (+7 -1) 📝 `internal/web/translation/id-ID.json` (+7 -1) 📝 `internal/web/translation/ja-JP.json` (+7 -1) 📝 `internal/web/translation/pt-BR.json` (+7 -1) 📝 `internal/web/translation/ru-RU.json` (+7 -1) 📝 `internal/web/translation/tr-TR.json` (+7 -1) _...and 4 more files_ </details> ### 📄 Description ## Summary Add seamless balancer-to-balancer fallback support. Users can now select another balancer as a fallback directly from the balancer form dropdown — the panel automatically generates the required loopback infrastructure (loopback outbound + routing rule) under the hood. ## Why Xray does not natively support using a balancer as a `fallbackTag` for another balancer. Before this feature, setting up balancer-to-balancer fallback required the following manual steps: Assume balancers **A** and **B** exist, and we want **B** as the fallback for **A**: 1. Create an **outbound** with protocol `loopback` and a unique tag (e.g. `B_loopback`), setting `inboundTag` to the same unique tag 2. Create a **routing rule**: set `inboundTag` to the loopback tag from step 1, set `balancerTag` to the target balancer **B** 3. Edit balancer **A** — set its `fallbackTag` to the loopback outbound tag from step 1 This process is: - **Tedious**: 3 manual steps across 3 different tabs (Outbounds, Routing, Balancers) - **Error-prone**: typos in tags silently break routing; wrong tags produce no traffic - **Undocumented**: no UI hints, no validation — users must know the xray loopback internals - **Hard to maintain**: renaming a balancer requires manually updating the loopback outbound tag, the routing rule, and other balancers' fallbackTag references This feature automates all of it: the user just picks a fallback balancer from a dropdown, and the panel handles the loopback plumbing, deduplication, rename propagation, and cleanup. ## 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) - [x] 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 it works When a user selects balancer **B** as fallback for balancer **A**: 1. Panel creates two objects in the xray template: - **Loopback outbound** `_bl_B` — re-injects traffic back into xray routing (`settings.inboundTag: "_bl_B"`) - **Routing rule** — sends traffic from `_bl_B` inbound tag to balancer **B** 2. Balancer **A**'s `fallbackTag` is set to `_bl_B` (the loopback outbound tag) 3. Traffic flow: ``` Balancer A (fallback) → loopback outbound _bl_B → routing rule → Balancer B → outbound ``` **Deduplication**: Multiple balancers sharing the same fallback target (e.g., A→B and C→B) reuse a single `_bl_B` loopback. The loopback is only removed when no balancer references it. **Self-healing**: On save, `ensureMissingBalancerLoopbacks` creates any missing loopback objects for balancers with `_bl_` fallback tags. `cleanupOrphanedBalancerLoopbacks` removes orphaned loopback objects. The `_bl_` prefix is reserved — any outbound with this prefix is overwritten with the correct loopback outbound. ## Files changed ### Frontend | File | Change | |------|--------| | `frontend/src/pages/xray/balancers/balancer-loopback.ts` | **New.** Core helper module: create/remove/resolve/rename/dedup/cleanup loopback objects, DFS cycle detection, self-healing | | `frontend/src/pages/xray/balancers/BalancerFormModal.tsx` | Fallback dropdown shows other balancers with indicator. Info alert. Per-tag DFS cycle detection with disabled options + tooltip. Self-reference guard | | `frontend/src/pages/xray/balancers/BalancersTab.tsx` | Auto-generate/remove loopback on fallback change. Delete protection. Live status resolve `_bl_` tags. Override dropdown includes fallback balancer | | `frontend/src/pages/xray/outbounds/OutboundsTab.tsx` | Filters `_bl_` loopback outbounds from UI | | `frontend/src/pages/xray/routing/RoutingTab.tsx` | Filters `_bl_` routing rules and `_bl_` inbound tags from UI | | `frontend/src/pages/xray/XrayPage.tsx` | Self-healing + orphan cleanup + cycle detection on save (order: heal → cleanup → check) | | `frontend/src/schemas/xray.ts` | Zod `.refine()` rejects `_bl_` prefix in balancer tag and outbound tag schemas | ### Backend | File | Change | |------|--------| | `internal/web/service/xray.go` | `OverrideBalancer` resolves balancer tags to loopback outbound tags via routing rules. Returns error if resolution fails or target is unresolved | ### i18n (13 locale files, 6 keys) | Key | Purpose | |-----|---------| | `pages.xray.balancer.fallbackBalancerHint` | Dropdown hint text | | `pages.xray.balancer.balancerFallbackInfo` | Info alert explaining traffic routing path and latency | | `pages.xray.balancer.balancerFallbackCycle` | Cycle detection error message | | `pages.xray.balancer.balancerDeleteInUse` | Delete protection error with dependent balancer names | | `pages.xray.balancer.reservedPrefix` | `_bl_` prefix rejection message | | `pages.xray.balancer.cycleTooltip` | Disabled dropdown option tooltip showing cycle path | ## Safety guards | Guard | Where | Behavior | |-------|-------|----------| | **Self-reference** | BalancerFormModal | Cannot select own balancer as fallback (excluded from dropdown) | | **Cycle detection (edit)** | BalancerFormModal DFS | Per-tag: disabled options with tooltip showing cycle path (e.g., "Cycle: A → B → (back to A)") | | **Cycle detection (save)** | XrayPage onSaveAll | Detects existing cycles in config and blocks save | | **Delete protection** | BalancersTab confirmDelete | Blocks deletion if balancer is used as fallback by others, shows which ones | | **Routing rule cleanup** | BalancersTab confirmDelete | Deletes routing rules referencing a removed balancer | | **Self-healing** | XrayPage onSaveAll | Creates missing loopback objects for broken references | | **Orphan cleanup** | XrayPage onSaveAll | Removes loopback objects no longer referenced by any balancer | | **Override resolution** | xray.go OverrideBalancer | Resolves balancer tags to loopback outbound tags; returns error if resolution fails | | **`_bl_` prefix validation** | Zod schemas (xray.ts) | Panel rejects creation of outbounds and balancers with tags starting with `_bl_` — this prefix is reserved for internal loopback objects | | **`_bl_` UI filtering** | OutboundsTab, RoutingTab, BalancersTab | `_bl_` objects hidden from tables and dropdowns | ## How was this tested? 1. Create two balancers (A and B), set B as fallback for A — verify traffic flows through loopback 2. Create third balancer C, set B as fallback — verify deduplication (shared `_bl_B`) 3. Try to set A as fallback for B when A→B exists — verify cycle detection blocks selection 4. Delete balancer B while A uses it as fallback — verify delete is blocked with error 5. Rename balancer B — verify loopback objects and references are updated 6. Override balancer A target to B — verify override works through loopback mechanism 7. Edit Advanced JSON to create cycle (X→Y→Z→X) — verify save is blocked 8. Edit Advanced JSON to remove loopback objects — verify self-healing recreates them on save 9. Verify `_bl_` tags are rejected when creating outbounds/balancers via form 10. Verify internal `_bl_` objects are hidden from Outbounds, Routing, and Balancers tabs ## Screenshots / recordings Section with balancers (one used as fallback for another): <img width="1492" height="378" alt="image" src="https://github.com/user-attachments/assets/c81bd0f2-e00b-4ecd-b106-fefbe95fea1e" /> Balancers in the Fallback list: <img width="1280" height="864" alt="image" src="https://github.com/user-attachments/assets/4109edba-4296-4946-88b9-cd3d4ea44936" /> ## Breaking changes None. Internal `_bl_` objects are filtered from the UI and stored in the xray template alongside existing config. The feature is additive — existing setups without nested balancers are unaffected. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [ ] 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. - [ ] 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:05:01 +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#5920
No description provided.