[PR #4124] [MERGED] xray-setting: pin api routing rule to index 0 on save (#4113) #5607

Closed
opened 2026-10-31 20:48:08 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/4124
Author: @pwnnex
Created: 4/28/2026
Status: ✅ Merged
Merged: 4/28/2026
Merged by: @MHSanaei

Base: main ← Head: fix/api-routing-rule-hoist-v2


📝 Commits (1)

  • 0acf86a xray-setting: pin api routing rule to index 0 on save

📊 Changes

2 files changed (+235 additions, -0 deletions)

View changed files

📝 web/service/xray_setting.go (+120 -0)
📝 web/service/xray_setting_test.go (+115 -0)

📄 Description

closes #4113. probably also closes #2818, where the long-running documented workaround is "manually move the api rule to the top in xray configs > routing".

what users see

admin adds a cascade outbound (eg vless to a second server) via xray configs > outbounds, sets up a routing rule pointing inbound traffic to it, and then everyone goes "offline" in the panel and traffic counters show zero. the actual vpn keeps working fine.

screenshot from #4113

root cause

xray's stats are queried over a local grpc inbound (tag api, listening on 127.0.0.1:62789). a default routing rule in the template sends inboundTag: [api] -> outboundTag: api so that traffic stays internal.

when the admin adds their own routing rule and it ends up before the api rule (which happens easily — eg appending a catch-all that matches all inbound traffic and sends it to vless-cascade), the api inbound's grpc traffic gets routed to the cascade outbound first match wins. the panel's GetTraffic call then either fails or gets routed off-server, returns no inbound/user counters, and every client appears offline with zero traffic.

#2818 is the same bug from a different angle. its top reply is "navigate to routing rules and move the row where outbound = api to the top". this PR just makes that automatic on save.

fix

web/service/xray_setting.go::SaveXraySetting now runs the saved template through a new EnsureStatsRouting helper before persisting. it:

  • finds the routing rule that matches inboundTag containing api and outboundTag = api
  • if it's already at index 0, returns the input unchanged (no churn)
  • if it's elsewhere in the list, moves it to index 0 keeping all other rules in their relative order
  • if it's missing entirely, inserts the default {type: field, inboundTag: [api], outboundTag: api} at the front

other routing rules are not touched.

tests

6 unit tests in xray_setting_test.go:

  • HoistsApiRuleFromMiddle — direct #4113 repro: catch-all rule before api rule, after EnsureStatsRouting api is at index 0
  • NoOpWhenAlreadyFirst — well-formed config returns identical string, no diff churn
  • InsertsDefaultWhenMissing — admin who deleted the api rule gets it back
  • NoRoutingBlock — empty config or one without routing section is handled
  • InvalidJsonReturnsAsIs — malformed input returns raw + error, caller can skip
  • AcceptsInboundTagAsString — handles inboundTag: "api" (string) as well as inboundTag: ["api"] (array)

go build ./... clean, go test ./... clean.


🔄 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/4124 **Author:** [@pwnnex](https://github.com/pwnnex) **Created:** 4/28/2026 **Status:** ✅ Merged **Merged:** 4/28/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/api-routing-rule-hoist-v2` --- ### 📝 Commits (1) - [`0acf86a`](https://github.com/MHSanaei/3x-ui/commit/0acf86a689b28355d969129a06810d71965be22a) xray-setting: pin api routing rule to index 0 on save ### 📊 Changes **2 files changed** (+235 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `web/service/xray_setting.go` (+120 -0) 📝 `web/service/xray_setting_test.go` (+115 -0) </details> ### 📄 Description closes #4113. probably also closes #2818, where the long-running documented workaround is "manually move the api rule to the top in xray configs > routing". ## what users see admin adds a cascade outbound (eg vless to a second server) via xray configs > outbounds, sets up a routing rule pointing inbound traffic to it, and then everyone goes "offline" in the panel and traffic counters show zero. the actual vpn keeps working fine. [screenshot from #4113](https://github.com/user-attachments/assets/8c734825-0ef5-45ce-abaa-2a969c3ece39) ## root cause xray's stats are queried over a local grpc inbound (tag `api`, listening on 127.0.0.1:62789). a default routing rule in the template sends `inboundTag: [api] -> outboundTag: api` so that traffic stays internal. when the admin adds their own routing rule and it ends up before the api rule (which happens easily — eg appending a catch-all that matches all inbound traffic and sends it to `vless-cascade`), the api inbound's grpc traffic gets routed to the cascade outbound first match wins. the panel's `GetTraffic` call then either fails or gets routed off-server, returns no inbound/user counters, and every client appears offline with zero traffic. #2818 is the same bug from a different angle. its top reply is "navigate to routing rules and move the row where outbound = api to the top". this PR just makes that automatic on save. ## fix `web/service/xray_setting.go::SaveXraySetting` now runs the saved template through a new `EnsureStatsRouting` helper before persisting. it: - finds the routing rule that matches `inboundTag` containing `api` and `outboundTag = api` - if it's already at index 0, returns the input unchanged (no churn) - if it's elsewhere in the list, moves it to index 0 keeping all other rules in their relative order - if it's missing entirely, inserts the default `{type: field, inboundTag: [api], outboundTag: api}` at the front other routing rules are not touched. ## tests 6 unit tests in `xray_setting_test.go`: - `HoistsApiRuleFromMiddle` — direct #4113 repro: catch-all rule before api rule, after EnsureStatsRouting api is at index 0 - `NoOpWhenAlreadyFirst` — well-formed config returns identical string, no diff churn - `InsertsDefaultWhenMissing` — admin who deleted the api rule gets it back - `NoRoutingBlock` — empty config or one without routing section is handled - `InvalidJsonReturnsAsIs` — malformed input returns raw + error, caller can skip - `AcceptsInboundTagAsString` — handles `inboundTag: "api"` (string) as well as `inboundTag: ["api"]` (array) `go build ./...` clean, `go test ./...` clean. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-31 20:48:08 +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#5607
No description provided.