mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6544] [MERGED] fix(amneziawg): refuse a row's own relay port and keep a disabled row's slot reserved #6343
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#6343
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/6544
Author: @BlindMaster24
Created: 9/15/2026
Status: ✅ Merged
Merged: 9/15/2026
Merged by: @BlindMaster24
Base:
main← Head:fix/amneziawg-relay-port-self-and-containment📝 Commits (4)
bf08e1afix(amneziawg): refuse a WireGuard port that is the row's own relay port35ffba1fix(amneziawg): keep a disabled row's relay port reserved for port forwards80eb571fix(amneziawg): re-run the forward guard once a new row has its own ports0f90bc6fix(amneziawg): stop blocking stored forward specs on a disabled row's slot📊 Changes
4 files changed (+157 additions, -22 deletions)
View changed files
📝
internal/web/service/inbound.go(+9 -1)📝
internal/web/service/inbound_amneziawg.go(+37 -21)📝
internal/web/service/inbound_amneziawg_relay_window_test.go(+94 -0)📝
internal/web/service/port_conflict.go(+17 -0)📄 Description
Closes two relay-slot gaps in the same guard family, both of which the #6542 review had left
pre-existing. Both had the same shape: every guard that reserves an AmneziaWG row's own ports skipped a row it should have counted — the row being saved.1. A row could claim its own relay port
All three relay checks exclude the id whose slot they compute (
id != ignoreId), so nothing compared an AmneziaWG row's own WireGuard listen port with the relay port its own id derives. Saving a row on that exact port puts two UDP listeners on the same port:inbound.Listen:inbound.Port(internal/amneziawgnet/device.go:137,ListenPort: ib.Portatinternal/amneziawg/instance.go:65);127.0.0.1:port(internal/web/service/xray.go:770,udp:trueatinternal/amneziawgnet/relay.go:47-61).Whichever loses the bind race dies — and if it is the relay, Xray refuses the whole config and every other protocol on the host goes down with it. It took nothing exotic to reach: the first AmneziaWG inbound on port
65101is id 1, whose derived relay port is exactly65101.amneziawgnetSocksSelfConflictnow states the rule the three siblings already state — a local row owns the slot its id derives — from both call sites that know the id:AddInbound's post-Save block andcheckPortConflictTx'signoreId > 0block. A node-hosted row is untouched: it binds no relay on this host, so its WireGuard port may legitimately be the port its id would derive.2. A new row's own clients' forwards were validated before it existed
normalizeAmneziaWGSettingsvalidates every client'sForwardedPortsbefore the row is saved, so the new row was never a candidate for itself in that pass — a client could forward exactly the relay port the row's own id derives, or its own WireGuard listen port, and the create was accepted. At runtime the panel's wildcard forward listener and Xray's loopback relay then race for the same port.AddInbound's post-Saveblock, the only frame where the id exists, now re-runs the guard over the settings submitted in that same request; both callers share one copy of the collision message viaamneziaWGForwardedPortsConflict.This one reads only what the request submitted, so it can never refuse a previously-stored spec.
Reverted after round 2: reserving a disabled row's relay slot
An earlier commit here dropped
enable = truefromloadPortConflictContextso that a disabled AmneziaWG row's relay slot could not be claimed by a client'sForwardedPorts. Round 2 showed what that costs, and the code confirms it:UpdateInboundClientcarries a stored spec forward for a partial edit (client_inbound_apply.go:763-765) and re-validates it (:772,:909), so after an in-place upgrade an operation that never submitted the field — a bot enable/expiry toggle — is refused over a slot the operator did not touch, for a relay that is not even emitted while the row is disabled. The inbound-save path re-validates every stored spec the same way (inbound_amneziawg.go:281).The slot that protects is claimable only by a spec an operator authors onto
65101-65535; the cost lands on unrelated operations. Nothing forces that trade here, so the widening is undone and the slot returns to the residual list below. The precise fix — refuse a newly claimed spec rather than a stored one, and check the enable transition inSetInboundEnable, which is where the conflict is actually created — is larger than the hole and belongs in its own change.Red-first
Every fix here was watched red before it was written, on the same tree, by running the real paths against a throwaway SQLite database:
The two self-collision tests pin two genuinely different call sites:
AddInboundreaches the checks afterSave(the id only exists then),UpdateInboundthroughcheckPortConflictTxwithignoreId > 0. A fix in either place alone leaves the other red.TestCheckPortConflict_NodeAssignedAmneziawgOwnsNoRelaySlotgains the same rule for a node-hosted row's own port. That assertion cannot be red on the unfixed tree (nothing refused it before), so it is a regression guard against the new refusal firing on rows that own no slot, not a fix verification. The test that pinned the reverted disabled-row rule was deleted with it.Gate
go test ./internal/web/service/ -count=1 -shuffle=on— green on every head (whole package, DB-backed tests included). It runs here through an overlay that swaps the cgo SQLite driver for a pure-Go one, since this host has no C compiler; no test or production file is substituted.gofmtclean,go vetclean oninternal/web/service,internal/amneziawgnet,internal/amneziawg.raceincluded.Analyze (actions)failed once with "job was not started because it repeatedly failed to be acquired" and is green on re-run — infra, not a code signal.XRAY_E2E_BINARYunset).Residual, left alone
SetInboundEnable(inbound.go:1569-1590) flips the column with no port check. Closing it means refusing the enable — the operation that actually collides — rather than widening the forward guard, which would stop specs that work today on a port that is genuinely free. Needs the maintainer's call.inbound_amneziawg.go:281), so moving an AmneziaWG inbound onto a port one of its own clients already forwards is accepted on the update path; the post-Savere-run in this PR closes it on create only. The update-path half needs the in-memory row overlaid onto the guard's context, which is its own change.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.