mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6547] [MERGED] fix(xray): refuse a config the running core cannot bind #6345
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#6345
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/6547
Author: @BlindMaster24
Created: 9/15/2026
Status: ✅ Merged
Merged: 9/15/2026
Merged by: @BlindMaster24
Base:
main← Head:fix/xray-refuse-config-with-duplicate-binds📝 Commits (2)
8e50b2cfix(xray): refuse a config the running core cannot binda4f32cafix(xray): surface a refused config and re-key the bind excuse set📊 Changes
5 files changed (+431 additions, -2 deletions)
View changed files
📝
frontend/src/pages/index/OverviewActionBar.tsx(+7 -1)📝
internal/web/service/server.go(+3 -1)📝
internal/web/service/xray.go(+37 -0)➕
internal/web/service/xray_bind_conflict.go(+147 -0)➕
internal/web/service/xray_bind_conflict_test.go(+237 -0)📄 Description
What breaks today
XrayService.RestartXrayhands the newly generated config to the core after stopping the working one:If any inbound in that config cannot bind its listen, the core does not skip that inbound — it exits the whole process (
main/run.go:94→os.Exit(-1)afterFailed to start).CheckXrayRunningJobthen runs every second and restarts Xray on the same config, so the panel settles into a ~3-second spawn-crash loop: every protocol on the host is dead (not just the colliding one) while the cause stays in the logs.The save-time port guards cannot cover this. They run on inbound save, and several paths reach a colliding config without one:
SetInboundEnableflipsenablewith no port check at all;This is the same family as #6537/#6539/#6540/#6542/#6544, which each closed one path. This one closes the funnel: every state-changing op goes through
RestartXray(runtime/local.go:336), andprocess.Start()exists nowhere else in the service layer (verified by grep).The rule
bindConflicts(newCfg, runningCfg)reports two inbounds that would bind one socket: same port, overlapping listens (listenOverlaps, so0.0.0.0/empty/::count as wildcard) and a shared transport.vless/tcp+hysteria2/udpon one port is a supported deployment and is already pinned by tests — so the check is transport-aware and reusesinboundTransports, the exact rule the save-time guards use.Two decisions worth your eye:
1. It refuses only when a working core is being replaced. With nothing running, the config is started and the core's own error names the port (
logger.Warning). Refusing there would let a wrong static read leave the panel with no core at all, which is worse than the loop it removes.2. A collision the running core already serves is excused. The running config is proof of what the core can bind: if two tags collide in the new config and collided in the config this process is serving right now, they demonstrably bind and the pair is not reported. This is what makes the guard safe to put on the synchronization path — a config that works today can never be refused by a static analysis being wrong about it. Only a collision the running config does not have is refused.
Rejected: making the "restart Xray" button bypass the check. That would re-open the exact loop the change removes, and the error message already names both tags and the port, which is what an operator needs to fix it.
Speed on the sync path
The probe buckets inbounds by port first, so transports are parsed only for a port two inbounds share — a clean config does zero JSON unmarshals beyond the listen fields, and the running-config pass only runs when the new config actually produced a collision.
Measured on a 500-inbound config (ad-hoc benchmark, not committed):
~44 µs against
GetXrayConfig()'s own per-restart work (template parse + DB rows + injectors), and the excuse rule costs nothing on a config that has no collision.Red first, with the exact output
1. The transport rule — probe: treat any same-port overlap as a clash. Exactly the three coexistence cases fail, nothing else:
2. The excuse rule — probe: report raw collisions. Exactly the two "already running" cases fail:
3. The relay's own shape. The relay is emitted as
protocol: "socks"withsettings.udp = true(xray.go:768-775,SocksInboundSettings), whichinboundTransportsdoes not model for a bare"socks"— the probe maps it onto the mixed shape so its UDP bit is not lost. Probe: drop that mapping and only this case fails:4. The probe reads real generated configs, not fixtures.
TestBindConflicts_GeneratedConfigseeds an AmneziaWG row with one qualifying peer plus a user inbound written straight to the DB on the derived relay port — the path the save-time guards cannot see — then callsGetXrayConfig()and asserts the refusal namesawg-1,userand65101, that the same config is excused when it is the running one, and that a free port stays clean. This one pins the real emitted shape and the derived port; the two probes above are what prove the rule it applies.Gate
go test ./internal/web/service -shuffle=on— green, 196 s, all DB-backed tests included (local run with a pure-Go SQLite driver via-overlay; the tree's owngo.mod/go.sumare unchanged).gofmt -landgo veton the package — clean.go-test,golangci,raceon this head.Unverified here: PostgreSQL and the Xray gRPC end-to-end tests (
XUI_TEST_PG_DSN,XRAY_E2E_BINARYare unset). The guard's call site inRestartXrayis one line at a single funnel and is not covered by a test — it cannot be exercised without a real core process, so it is verified by reading; the decision it makes is what the tests above cover.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.