[PR #6544] [MERGED] fix(amneziawg): refuse a row's own relay port and keep a disabled row's slot reserved #6343

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

📋 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)

  • bf08e1a fix(amneziawg): refuse a WireGuard port that is the row's own relay port
  • 35ffba1 fix(amneziawg): keep a disabled row's relay port reserved for port forwards
  • 80eb571 fix(amneziawg): re-run the forward guard once a new row has its own ports
  • 0f90bc6 fix(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:

  • the embedded device binds UDP on inbound.Listen:inbound.Port (internal/amneziawgnet/device.go:137, ListenPort: ib.Port at internal/amneziawg/instance.go:65);
  • its injected relay binds TCP and UDP on 127.0.0.1:port (internal/web/service/xray.go:770, udp:true at internal/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 65101 is id 1, whose derived relay port is exactly 65101.

amneziawgnetSocksSelfConflict now 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 and checkPortConflictTx's ignoreId > 0 block. 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

normalizeAmneziaWGSettings validates every client's ForwardedPorts before 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-Save block, 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 via amneziaWGForwardedPortsConflict.

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 = true from loadPortConflictContext so that a disabled AmneziaWG row's relay slot could not be claimed by a client's ForwardedPorts. Round 2 showed what that costs, and the code confirms it: UpdateInboundClient carries 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 in SetInboundEnable, 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:

--- FAIL: TestAddInbound_AmneziawgRefusesItsOwnRelayPort (0.21s)
    inbound_amneziawg_relay_window_test.go:190: WireGuard port 65102 is inbound #2's own relay port; the create must be refused
--- FAIL: TestUpdateInbound_AmneziawgRefusesItsOwnRelayPort (0.20s)
    inbound_amneziawg_relay_window_test.go:212: WireGuard port 65101 is inbound #1's own relay port; the save must be refused
--- FAIL: TestAddInbound_AmneziawgRefusesAClientForwardingItsOwnRelayPort (0.30s)
    inbound_amneziawg_relay_window_test.go:196: inbound #2 derives relay port 65102 and its own client forwards that port; the create must be refused

The two self-collision tests pin two genuinely different call sites: AddInbound reaches the checks after Save (the id only exists then), UpdateInbound through checkPortConflictTx with ignoreId > 0. A fix in either place alone leaves the other red.

TestCheckPortConflict_NodeAssignedAmneziawgOwnsNoRelaySlot gains 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.
  • gofmt clean, go vet clean on internal/web/service, internal/amneziawgnet, internal/amneziawg.
  • CI on the final head: 21 check runs green, race included. 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.
  • Not covered by any local run: PostgreSQL (the new tests' fixtures are SQLite-only, like their siblings in this file) and the Xray gRPC end-to-end path (XRAY_E2E_BINARY unset).

Residual, left alone

  • A disabled row's own listen port stays claimable, and 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.
  • The forward guard compares a client's spec with the inbound's stored listen port (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-Save re-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.
  • Client operations still run no port check, so a database already in a broken state is not repaired — it is refused on the next save of the offending row, which is the point.

🔄 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/6544 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/15/2026 **Status:** ✅ Merged **Merged:** 9/15/2026 **Merged by:** [@BlindMaster24](https://github.com/BlindMaster24) **Base:** `main` ← **Head:** `fix/amneziawg-relay-port-self-and-containment` --- ### 📝 Commits (4) - [`bf08e1a`](https://github.com/MHSanaei/3x-ui/commit/bf08e1a2fe9e66fa128914463b61bfd3df8564b9) fix(amneziawg): refuse a WireGuard port that is the row's own relay port - [`35ffba1`](https://github.com/MHSanaei/3x-ui/commit/35ffba1eeaba784c390d7b603eb60325a53c4d11) fix(amneziawg): keep a disabled row's relay port reserved for port forwards - [`80eb571`](https://github.com/MHSanaei/3x-ui/commit/80eb5712b69199dc09eefe732c89ad38b405dbc0) fix(amneziawg): re-run the forward guard once a new row has its own ports - [`0f90bc6`](https://github.com/MHSanaei/3x-ui/commit/0f90bc6b0d7f3279cbf85b65ce5996d918952e8b) fix(amneziawg): stop blocking stored forward specs on a disabled row's slot ### 📊 Changes **4 files changed** (+157 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 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: - the embedded device binds UDP on `inbound.Listen:inbound.Port` (`internal/amneziawgnet/device.go:137`, `ListenPort: ib.Port` at `internal/amneziawg/instance.go:65`); - its injected relay binds TCP **and** UDP on `127.0.0.1:port` (`internal/web/service/xray.go:770`, `udp:true` at `internal/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 `65101` is id 1, whose derived relay port is exactly `65101`. `amneziawgnetSocksSelfConflict` now 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 and `checkPortConflictTx`'s `ignoreId > 0` block. 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 `normalizeAmneziaWGSettings` validates every client's `ForwardedPorts` before 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-`Save` block, 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 via `amneziaWGForwardedPortsConflict`. 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 = true` from `loadPortConflictContext` so that a disabled AmneziaWG row's relay slot could not be claimed by a client's `ForwardedPorts`. Round 2 showed what that costs, and the code confirms it: `UpdateInboundClient` carries 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 in `SetInboundEnable`, 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: ``` --- FAIL: TestAddInbound_AmneziawgRefusesItsOwnRelayPort (0.21s) inbound_amneziawg_relay_window_test.go:190: WireGuard port 65102 is inbound #2's own relay port; the create must be refused --- FAIL: TestUpdateInbound_AmneziawgRefusesItsOwnRelayPort (0.20s) inbound_amneziawg_relay_window_test.go:212: WireGuard port 65101 is inbound #1's own relay port; the save must be refused --- FAIL: TestAddInbound_AmneziawgRefusesAClientForwardingItsOwnRelayPort (0.30s) inbound_amneziawg_relay_window_test.go:196: inbound #2 derives relay port 65102 and its own client forwards that port; the create must be refused ``` The two self-collision tests pin two genuinely different call sites: `AddInbound` reaches the checks after `Save` (the id only exists then), `UpdateInbound` through `checkPortConflictTx` with `ignoreId > 0`. A fix in either place alone leaves the other red. `TestCheckPortConflict_NodeAssignedAmneziawgOwnsNoRelaySlot` gains 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. - `gofmt` clean, `go vet` clean on `internal/web/service`, `internal/amneziawgnet`, `internal/amneziawg`. - CI on the final head: 21 check runs green, `race` included. `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. - Not covered by any local run: PostgreSQL (the new tests' fixtures are SQLite-only, like their siblings in this file) and the Xray gRPC end-to-end path (`XRAY_E2E_BINARY` unset). ## Residual, left alone - **A disabled row's own listen port stays claimable**, and `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. - **The forward guard compares a client's spec with the inbound's stored listen port** (`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-`Save` re-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. - **Client operations still run no port check**, so a database already in a broken state is not repaired — it is refused on the next save of the offending row, which is the point. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:05 +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#6343
No description provided.