[PR #6542] [MERGED] fix(amneziawg): reserve the relay port before an AmneziaWG inbound has a peer #6341

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

  • f85f8df test(amneziawg): pin that a peerless inbound still owns its relay port
  • c2d70cd fix(amneziawg): reserve the relay port before the first peer is added
  • b41118c docs(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.

Red-first head. On this machine no DB-backed test can run at all — no C toolchain (go-sqlite3 needs cgo), no container runtime — so the failing half of red-first can only be executed by CI. The first commit here is deliberately the test alone; go-test is red on it, and the fix is the commit after. This pull request is opened as a draft for exactly that window, so the review job does not read a head that is red by construction.

Why it happens

checkAmneziawgnetSocksConflict skips a candidate when amneziawg.InstanceFromInbound returns ok=false (internal/web/service/port_conflict.go:300), and InstanceFromInbound requires at least one enabled peer with a public key and allowed IPs (internal/amneziawg/instance.go:38-52). A fresh AmneziaWG inbound has none: normalizeAmneziaWGSettings writes Clients: []model.Client{} for empty settings (internal/web/service/inbound_amneziawg.go:206-217), and injectAmneziawgnetSocks skips 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 checkPortConflictTx at all — AddInbound (internal/web/service/inbound.go:1229) and UpdateInbound (: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:

  1. Create an AmneziaWG inbound and give it no client yet. The row owns no slot, and InstanceFromInbound is false, so no relay is generated.
  2. Save any inbound on that row's derived port (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.
  3. Add the inbound's first client. No port check runs.
  4. At the next Xray start the relay is generated next to the inbound from step 2; Xray fails the second bind and refuses the entire config.

Where the guard belongs

Two shapes close this, and this PR takes the first:

  • Ownership follows the row (this PR), so a local AmneziaWG row reserves its derived port from creation. One predicate removed, and the check then states the same rule as its two siblings — checkAmneziawgnetSocksRelayCollision and checkAmneziawgnetSocksReverseConflict key on protocol and node_id IS NULL alone (internal/web/service/port_conflict.go:320-327, :347-354).
  • Ownership follows what is injected, which needs a port check added to every path that can create the first peer or re-enable the row — at least AddInboundClient, UpdateInboundClient, both client-enable setters and SetInboundEnable. 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 amneziawg import goes with it.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

TestCheckPortConflict_AmneziawgnetSocksRelayReservedBeforeTheFirstPeer (internal/web/service/port_conflict_test.go) replaces TestCheckPortConflict_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 seeded Settings: {}``, a shape the service never writes, and the new one seeds what normalizeAmneziaWGSettings writes for a fresh inbound: a server block with "clients":[].

It fails without the fix, on the test-only head:

--- FAIL: TestCheckPortConflict_AmneziawgnetSocksRelayReservedBeforeTheFirstPeer (0.11s)
    port_conflict_test.go:898: an AmneziaWG inbound with no peer yet still owns relay port 65101; the save must be refused

Executed in run 34944524141, job go-test — the only --- FAIL: line in that job, and golangci passed alongside it.

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally — cannot run on this machine (no C toolchain: CGO_ENABLED=0, so go-sqlite3 is its stub). The DB-backed tests compile here and CI exercises them.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass. — N/A
  • I updated the Wiki / README / API docs if user-facing behavior changed. — nothing documents the relay port or its window; checked docs/, the translations and the frontend.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

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–65535 are 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.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6542 **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-slot-before-first-peer` --- ### 📝 Commits (3) - [`f85f8df`](https://github.com/MHSanaei/3x-ui/commit/f85f8dff1aec77cee620acfd4d27f4799c81c4f4) test(amneziawg): pin that a peerless inbound still owns its relay port - [`c2d70cd`](https://github.com/MHSanaei/3x-ui/commit/c2d70cd0f519d7db228e8110a5b19630f891823f) fix(amneziawg): reserve the relay port before the first peer is added - [`b41118c`](https://github.com/MHSanaei/3x-ui/commit/b41118cb6e8546c6898178c5763813134c9bcc45) docs(amneziawg): stop the forward check's doc block claiming every row gets a relay ### 📊 Changes **2 files changed** (+19 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/port_conflict.go` (+4 -16) 📝 `internal/web/service/port_conflict_test.go` (+15 -7) </details> ### 📄 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. > **Red-first head.** On this machine no DB-backed test can run at all — no C toolchain (`go-sqlite3` needs cgo), no container runtime — so the failing half of red-first can only be executed by CI. The first commit here is deliberately the test alone; `go-test` is red on it, and the fix is the commit after. This pull request is opened as a draft for exactly that window, so the review job does not read a head that is red by construction. ## Why it happens `checkAmneziawgnetSocksConflict` skips a candidate when `amneziawg.InstanceFromInbound` returns `ok=false` (`internal/web/service/port_conflict.go:300`), and `InstanceFromInbound` requires at least one enabled peer with a public key and allowed IPs (`internal/amneziawg/instance.go:38-52`). A fresh AmneziaWG inbound has none: `normalizeAmneziaWGSettings` writes `Clients: []model.Client{}` for empty settings (`internal/web/service/inbound_amneziawg.go:206-217`), and `injectAmneziawgnetSocks` skips 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 `checkPortConflictTx` at all — `AddInbound` (`internal/web/service/inbound.go:1229`) and `UpdateInbound` (`: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: 1. Create an AmneziaWG inbound and give it no client yet. The row owns no slot, and `InstanceFromInbound` is `false`, so no relay is generated. 2. Save any inbound on that row's derived port (`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. 3. Add the inbound's first client. No port check runs. 4. At the next Xray start the relay is generated next to the inbound from step 2; Xray fails the second bind and refuses the entire config. ## Where the guard belongs Two shapes close this, and this PR takes the first: - **Ownership follows the row (this PR)**, so a local AmneziaWG row reserves its derived port from creation. One predicate removed, and the check then states the same rule as its two siblings — `checkAmneziawgnetSocksRelayCollision` and `checkAmneziawgnetSocksReverseConflict` key on `protocol` and `node_id IS NULL` alone (`internal/web/service/port_conflict.go:320-327`, `:347-354`). - **Ownership follows what is injected**, which needs a port check added to every path that can create the first peer or re-enable the row — at least `AddInboundClient`, `UpdateInboundClient`, both client-enable setters and `SetInboundEnable`. 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 `amneziawg` import goes with it. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [x] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? `TestCheckPortConflict_AmneziawgnetSocksRelayReservedBeforeTheFirstPeer` (`internal/web/service/port_conflict_test.go`) replaces `TestCheckPortConflict_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 seeded `Settings: `{}``, a shape the service never writes, and the new one seeds what `normalizeAmneziaWGSettings` writes for a fresh inbound: a server block with `"clients":[]`. It fails without the fix, on the test-only head: ``` --- FAIL: TestCheckPortConflict_AmneziawgnetSocksRelayReservedBeforeTheFirstPeer (0.11s) port_conflict_test.go:898: an AmneziaWG inbound with no peer yet still owns relay port 65101; the save must be refused ``` Executed in [run 34944524141, job `go-test`](https://github.com/MHSanaei/3x-ui/actions/runs/34944524141/job/104300664280) — the only `--- FAIL:` line in that job, and `golangci` passed alongside it. - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [ ] `go build ./...` and the test suite pass locally — cannot run on this machine (no C toolchain: `CGO_ENABLED=0`, so `go-sqlite3` is its stub). The DB-backed tests compile here and CI exercises them. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. — N/A - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. — nothing documents the relay port or its window; checked `docs/`, the translations and the frontend. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. ## 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`–`65535` are panel-internal and never suggested, so reaching this state takes a deliberately chosen high port. --- <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#6341
No description provided.