[PR #5648] [MERGED] fix(xray): clean stale routing references when a balancer or outbound is deleted #5947

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5648
Author: @nima1024m
Created: 6/29/2026
Status: ✅ Merged
Merged: 6/29/2026
Merged by: @MHSanaei

Base: main ← Head: fix/routing-rule-reference-cleanup


📝 Commits (6)

  • 6e6ed8f feat(xray): reference-cleanup helpers for entity deletion
  • a20ec5d fix(balancers): clean routing rules referencing a deleted balancer
  • 9fc3fae fix(outbounds): clean rules, balancer selectors and dialerProxy on outbound delete
  • 4677bdb Merge remote-tracking branch 'origin/main' into fix/routing-rule-reference-cleanup
  • 9e95365 refactor(xray): share one rule classifier across preview and apply
  • c30f7b4 fix(xray): null-guard reference cleanup against unvalidated configs

📊 Changes

20 files changed (+634 additions, -75 deletions)

View changed files

➕ frontend/src/pages/xray/DeletionImpactList.tsx (+40 -0)
📝 frontend/src/pages/xray/balancers/BalancersTab.tsx (+8 -14)
📝 frontend/src/pages/xray/balancers/balancer-helpers.ts (+0 -16)
📝 frontend/src/pages/xray/outbounds/OutboundsTab.tsx (+7 -5)
➕ frontend/src/pages/xray/reference-cleanup.ts (+236 -0)
📝 frontend/src/test/balancer-observatory-sync.test.ts (+1 -40)
➕ frontend/src/test/routing-reference-cleanup.test.ts (+264 -0)
📝 internal/web/translation/ar-EG.json (+6 -0)
📝 internal/web/translation/en-US.json (+6 -0)
📝 internal/web/translation/es-ES.json (+6 -0)
📝 internal/web/translation/fa-IR.json (+6 -0)
📝 internal/web/translation/id-ID.json (+6 -0)
📝 internal/web/translation/ja-JP.json (+6 -0)
📝 internal/web/translation/pt-BR.json (+6 -0)
📝 internal/web/translation/ru-RU.json (+6 -0)
📝 internal/web/translation/tr-TR.json (+6 -0)
📝 internal/web/translation/uk-UA.json (+6 -0)
📝 internal/web/translation/vi-VN.json (+6 -0)
📝 internal/web/translation/zh-CN.json (+6 -0)
📝 internal/web/translation/zh-TW.json (+6 -0)

📄 Description

Problem

Deleting a balancer or outbound on the Xray Settings page left routing rules pointing at the removed entity. A dangling balancerTag is fatal — xray-core's Router.Init returns "balancer <tag> not found", so the whole core fails to (re)start and every inbound drops. A dangling outboundTag black-holes matched traffic at the dispatcher. CheckXrayConfig only validates JSON shape, so the broken config saved cleanly and only detonated on the next core restart.

Fix

When an outbound or balancer is deleted, the rules/balancers that reference it are repaired in the same edit, and the delete confirmation lists exactly what will change (modified vs removed):

  • A rule is kept (dead reference dropped) if it still has a destination (outboundTag or balancerTag); it is removed if none remains, since a destination-less rule black-holes its matched traffic.
  • Deleting an outbound cascades: removing it from a balancer's selector can empty the selector, which removes that balancer too — and then its rules are repaired the same way. fallbackTag and other outbounds' dialerProxy references are cleared, and observatories are re-synced.

Pure logic lives in frontend/src/pages/xray/reference-cleanup.ts (plan* for the preview, apply* for the mutation, sharing one classifyRule so the preview can never disagree with the mutation), wired into the balancer/outbound delete modals via a shared DeletionImpactList.

Scope

  • ✅ Balancer + outbound deletion (this PR, frontend — they live only in the client-side xray config blob, staged until Save).
  • ✅ Inbound deletion is already handled on the backend (RemoveInboundTagReferences).
  • ⏳ Client user[] pruning is deferred to a follow-up: a dangling user entry is inert in xray-core, and client records are deleted from several scattered backend paths with no single chokepoint.

Testing

  • 17 unit tests: keep/drop logic, the outbound→balancer cascade, fallbackTag/dialerProxy cleanup, disabled rules, plan/apply parity, and the tagless/edge cases.
  • Gate green: typecheck, lint, 628 frontend tests, Go build, frontend build. Verified manually in the panel across all scenarios.
  • A multi-angle review (correctness / cleanup / altitude / conventions) found no correctness bugs in valid configs; its top suggestions — share the rule classifier, guard tagless balancers, drop an orphaned helper — are applied.
  • i18n for the impact modal added across all 13 locales.
  • Manual testing

Screenshots

{62E5F3BD-DFFA-4A28-BA22-7F2CD387BB47} {1D7CA87F-CD11-4520-88FD-70F58EFDC8E9} {54924F75-A7B3-483A-B3E9-D9D270E86711}

🔄 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/5648 **Author:** [@nima1024m](https://github.com/nima1024m) **Created:** 6/29/2026 **Status:** ✅ Merged **Merged:** 6/29/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/routing-rule-reference-cleanup` --- ### 📝 Commits (6) - [`6e6ed8f`](https://github.com/MHSanaei/3x-ui/commit/6e6ed8f7a3771ed05586a00745e82237b8747c3d) feat(xray): reference-cleanup helpers for entity deletion - [`a20ec5d`](https://github.com/MHSanaei/3x-ui/commit/a20ec5dd7692ce1eeb494f29b2a5b5e3fc645c38) fix(balancers): clean routing rules referencing a deleted balancer - [`9fc3fae`](https://github.com/MHSanaei/3x-ui/commit/9fc3faecf96ce9d315688b1da774eb58087d6455) fix(outbounds): clean rules, balancer selectors and dialerProxy on outbound delete - [`4677bdb`](https://github.com/MHSanaei/3x-ui/commit/4677bdb58902ad4f27c1f5623261d2306fbcffaa) Merge remote-tracking branch 'origin/main' into fix/routing-rule-reference-cleanup - [`9e95365`](https://github.com/MHSanaei/3x-ui/commit/9e95365d5530fbc9b87bc87cf9ebc05e3e7b856d) refactor(xray): share one rule classifier across preview and apply - [`c30f7b4`](https://github.com/MHSanaei/3x-ui/commit/c30f7b4d23d662b1c3ed75f093a029eb357ceb5c) fix(xray): null-guard reference cleanup against unvalidated configs ### 📊 Changes **20 files changed** (+634 additions, -75 deletions) <details> <summary>View changed files</summary> ➕ `frontend/src/pages/xray/DeletionImpactList.tsx` (+40 -0) 📝 `frontend/src/pages/xray/balancers/BalancersTab.tsx` (+8 -14) 📝 `frontend/src/pages/xray/balancers/balancer-helpers.ts` (+0 -16) 📝 `frontend/src/pages/xray/outbounds/OutboundsTab.tsx` (+7 -5) ➕ `frontend/src/pages/xray/reference-cleanup.ts` (+236 -0) 📝 `frontend/src/test/balancer-observatory-sync.test.ts` (+1 -40) ➕ `frontend/src/test/routing-reference-cleanup.test.ts` (+264 -0) 📝 `internal/web/translation/ar-EG.json` (+6 -0) 📝 `internal/web/translation/en-US.json` (+6 -0) 📝 `internal/web/translation/es-ES.json` (+6 -0) 📝 `internal/web/translation/fa-IR.json` (+6 -0) 📝 `internal/web/translation/id-ID.json` (+6 -0) 📝 `internal/web/translation/ja-JP.json` (+6 -0) 📝 `internal/web/translation/pt-BR.json` (+6 -0) 📝 `internal/web/translation/ru-RU.json` (+6 -0) 📝 `internal/web/translation/tr-TR.json` (+6 -0) 📝 `internal/web/translation/uk-UA.json` (+6 -0) 📝 `internal/web/translation/vi-VN.json` (+6 -0) 📝 `internal/web/translation/zh-CN.json` (+6 -0) 📝 `internal/web/translation/zh-TW.json` (+6 -0) </details> ### 📄 Description ## Problem Deleting a balancer or outbound on the **Xray Settings** page left routing rules pointing at the removed entity. A dangling `balancerTag` is fatal — xray-core's `Router.Init` returns `"balancer <tag> not found"`, so the whole core fails to (re)start and **every inbound drops**. A dangling `outboundTag` black-holes matched traffic at the dispatcher. `CheckXrayConfig` only validates JSON shape, so the broken config saved cleanly and only detonated on the next core restart. ## Fix When an outbound or balancer is deleted, the rules/balancers that reference it are repaired in the same edit, and the delete confirmation lists exactly what will change (modified vs removed): - A rule is **kept** (dead reference dropped) if it still has a destination (`outboundTag` or `balancerTag`); it is **removed** if none remains, since a destination-less rule black-holes its matched traffic. - Deleting an outbound **cascades**: removing it from a balancer's `selector` can empty the selector, which removes that balancer too — and then its rules are repaired the same way. `fallbackTag` and other outbounds' `dialerProxy` references are cleared, and observatories are re-synced. Pure logic lives in `frontend/src/pages/xray/reference-cleanup.ts` (`plan*` for the preview, `apply*` for the mutation, sharing one `classifyRule` so the preview can never disagree with the mutation), wired into the balancer/outbound delete modals via a shared `DeletionImpactList`. ## Scope - ✅ **Balancer + outbound** deletion (this PR, frontend — they live only in the client-side xray config blob, staged until Save). - ✅ **Inbound** deletion is already handled on the backend (`RemoveInboundTagReferences`). - ⏳ **Client `user[]`** pruning is deferred to a follow-up: a dangling `user` entry is inert in xray-core, and client records are deleted from several scattered backend paths with no single chokepoint. ## Testing - 17 unit tests: keep/drop logic, the outbound→balancer cascade, `fallbackTag`/`dialerProxy` cleanup, disabled rules, plan/apply parity, and the tagless/edge cases. - Gate green: typecheck, lint, 628 frontend tests, Go build, frontend build. Verified manually in the panel across all scenarios. - A multi-angle review (correctness / cleanup / altitude / conventions) found no correctness bugs in valid configs; its top suggestions — share the rule classifier, guard tagless balancers, drop an orphaned helper — are applied. - i18n for the impact modal added across all 13 locales. - Manual testing ## Screenshots <img width="200" height="100" alt="{62E5F3BD-DFFA-4A28-BA22-7F2CD387BB47}" src="https://github.com/user-attachments/assets/1e77c2e4-dc63-4582-96b8-d6b7ec6fff2c" /> <img width="200" height="100" alt="{1D7CA87F-CD11-4520-88FD-70F58EFDC8E9}" src="https://github.com/user-attachments/assets/b2679303-0fae-4a83-85c6-5150ee9586ab" /> <img width="200" height="100" alt="{54924F75-A7B3-483A-B3E9-D9D270E86711}" src="https://github.com/user-attachments/assets/b1a98b76-385e-4eaf-8fef-55b463f75cc9" /> --- <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:14 +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#5947
No description provided.