mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6542] [MERGED] fix(amneziawg): reserve the relay port before an AmneziaWG inbound has a peer #6341
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#6341
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/6542
Author: @BlindMaster24
Created: 9/15/2026
Status: ✅ Merged
Merged: 9/15/2026
Merged by: @BlindMaster24
Base:
main← Head:fix/amneziawg-relay-slot-before-first-peer📝 Commits (3)
f85f8dftest(amneziawg): pin that a peerless inbound still owns its relay portc2d70cdfix(amneziawg): reserve the relay port before the first peer is addedb41118cdocs(amneziawg): stop the forward check's doc block claiming every row gets a relay📊 Changes
2 files changed (+19 additions, -23 deletions)
View changed files
📝
internal/web/service/port_conflict.go(+4 -16)📝
internal/web/service/port_conflict_test.go(+15 -7)📄 Description
Summary
A local AmneziaWG inbound reserved its automatic loopback relay port only once it had a qualifying peer, so a freshly created AmneziaWG inbound owned nothing. An ordinary inbound could be saved onto the port that row derives, and adding the inbound's first client then put two inbounds on
127.0.0.1:65101, which makes Xray refuse its whole config and takes every other protocol on the host down with it.Found as a pre-existing MEDIUM during the review of #6540. That change removes the other gate on ownership,
enable; this one removes the peer gate, and the two are independent.Why it happens
checkAmneziawgnetSocksConflictskips a candidate whenamneziawg.InstanceFromInboundreturnsok=false(internal/web/service/port_conflict.go:300), andInstanceFromInboundrequires at least one enabled peer with a public key and allowed IPs (internal/amneziawg/instance.go:38-52). A fresh AmneziaWG inbound has none:normalizeAmneziaWGSettingswritesClients: []model.Client{}for empty settings (internal/web/service/inbound_amneziawg.go:206-217), andinjectAmneziawgnetSocksskips a row with no peer emails (internal/web/service/xray.go:756-760).Nothing else covers the gap, and nothing re-checks it later: only two call sites run
checkPortConflictTxat all —AddInbound(internal/web/service/inbound.go:1229) andUpdateInbound(:1721) — while the client paths that can create the first peer,ClientService.AddInboundClient(internal/web/service/client_inbound_apply.go:315) among them, run no port check.Reproduces entirely through the panel, with the ordinary workflow rather than a deliberate toggle:
InstanceFromInboundisfalse, so no relay is generated.65101–65535). The forward check drops the AmneziaWG row, the DB query cannot see a relay that is not a row, and the save succeeds.Where the guard belongs
Two shapes close this, and this PR takes the first:
checkAmneziawgnetSocksRelayCollisionandcheckAmneziawgnetSocksReverseConflictkey onprotocolandnode_id IS NULLalone (internal/web/service/port_conflict.go:320-327,:347-354).AddInboundClient,UpdateInboundClient, both client-enable setters andSetInboundEnable. That is five new validation sites, each able to refuse a client operation because of a loopback port the operator cannot see or act on from where the refusal appears.The first is also the only shape consistent with what already ships: #6539's collision check has no peer gate.
The change
The peer gate is removed from the forward check's candidate loop, and the now-unused
amneziawgimport goes with it.Type of change
Areas affected
How was this tested?
TestCheckPortConflict_AmneziawgnetSocksRelayReservedBeforeTheFirstPeer(internal/web/service/port_conflict_test.go) replacesTestCheckPortConflict_AmneziawgnetSocksRelayIgnoredWhenNoQualifyingPeer, which asserted the rule this change removes. The rewrite keeps the test's position and shape and changes three things: its name, its doc comment, and the direction of its assertion. Its fixture is also corrected — the old one seededSettings:{}``, a shape the service never writes, and the new one seeds whatnormalizeAmneziaWGSettingswrites for a fresh inbound: a server block with"clients":[].It fails without the fix, on the test-only head:
Executed in run 34944524141, job
go-test— the only--- FAIL:line in that job, andgolangcipassed alongside it.go build ./...and the test suite pass locally — cannot run on this machine (no C toolchain:CGO_ENABLED=0, sogo-sqlite3is its stub). The DB-backed tests compile here and CI exercises them.npm run lint,npm run typecheck, andnpm run buildpass. — N/Adocs/, the translations and the frontend.Breaking changes
No schema change, no migration, and no row's stored data moves. Xray's generated config is unchanged for every setup that starts today.
The one behaviour change: saving an inbound onto the derived relay port of a local AmneziaWG row that has no qualifying peer is now refused, where it used to be accepted. That is the bug above, so the refusal is the fix; it also means a row whose settings cannot produce a relay at all reserves its port until the row is deleted or its settings are made valid — the edit that makes them valid runs the port check, so a squatter is caught there rather than silently surviving.
Residual, not fixed here: a database that already reached the broken state before this change — a peerless AmneziaWG row plus an ordinary inbound on its derived port — still breaks Xray when that row's first client is added, because the client paths run no port check. Closing that needs a decision (refuse the client operation, or re-check the relay on the peer transitions), so it is reported rather than folded in. Ports in
65101–65535are panel-internal and never suggested, so reaching this state takes a deliberately chosen high port.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.