[PR #6540] [MERGED] fix(amneziawg): stop a disabled inbound's relay slot from being taken #6340

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6540
Author: @BlindMaster24
Created: 9/15/2026
Status: ✅ Merged
Merged: 9/15/2026
Merged by: @BlindMaster24

Base: main ← Head: fix/amneziawg-disabled-relay-slot


📝 Commits (3)

  • 538232c test(amneziawg): pin that a disabled row still owns its relay slot
  • 2553889 fix(amneziawg): count a disabled inbound as owning its relay slot
  • dbb04d2 test(amneziawg): drop the disabled-row case that asserts the reversed rule

📊 Changes

3 files changed (+31 additions, -25 deletions)

View changed files

📝 internal/web/service/inbound_amneziawg_relay_window_test.go (+27 -0)
📝 internal/web/service/port_conflict.go (+4 -2)
📝 internal/web/service/port_conflict_test.go (+0 -23)

📄 Description

Summary

A local AmneziaWG inbound owns the loopback relay port its id derives for as long as the row exists — but the forward port check only counted its enabled rows as owners. An ordinary inbound could therefore be saved onto that port while the AmneziaWG row was off, and turning that row back on 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 #6539; this is the second half of the same invariant that change wrote down in the other direction.

Why it happens

checkAmneziawgnetSocksConflict filters its candidates with enable = true (internal/web/service/port_conflict.go:290), so a disabled AmneziaWG row is invisible when a new inbound's configured port is validated. Nothing else covers the gap: the relay is not a database row, so the general port = ? query cannot see it, and SetInboundEnable (internal/web/service/inbound.go:1566-1588) flips the column inside a transaction that runs no port check at all.

Reproduces entirely through the panel:

  1. AmneziaWG inbound #1 exists and is toggled off. injectAmneziawgnetSocks skips it (internal/web/service/xray.go:739 requires inbound.Enable), so no relay is in the generated config — but the row still owns slot 65101.
  2. Any inbound is created on port 65101. The forward check drops the disabled row, the DB query finds nothing, the save succeeds.
  3. #1 is toggled back on. No port check runs.
  4. At the next Xray start both inbounds are appended on 127.0.0.1:65101; Xray fails the second bind and refuses the entire config.

Why this rule, and not a check on the enable path

Two designs close that sequence, and the review asked for the choice to be deliberate rather than inherited from a shared helper:

  • A (this PR): a row owns the slot its id derives for as long as the row exists, disabled included, so the port is never handed out.
  • B: a row owns it only while enabled, and SetInboundEnable validates the relay port when it switches the row on.

A is taken because the rule already exists in three of the four AmneziaWG checks in this file, including checkAmneziawgnetSocksRelayCollision, which shipped in #6539 and treats a disabled owner as an owner for exactly the reason that decides this: re-enabling runs no port check. B would make those three agree only by relaxing the merged one, and it moves a resource conflict onto a toggle that users flip as a routine on/off switch, where a refusal is far more surprising than at save time. The cost of A is the narrow case in Breaking changes below, which B would avoid.

The change

One predicate: the forward check now counts every local AmneziaWG row, disabled ones included.

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_DisabledAmneziawgStillOwnsItsRelaySlot — an AmneziaWG inbound is created disabled, and an ordinary inbound is then validated on that row's derived relay port; the save must be refused with the owner named.

It fails without the fix, on a test-only head whose CI run failed on exactly that one test:

--- FAIL: TestCheckPortConflict_DisabledAmneziawgStillOwnsItsRelaySlot (0.11s)
    inbound_amneziawg_relay_window_test.go:163: inbound #1 is disabled but still owns relay port 65101; the save must be refused
FAIL	github.com/mhsanaei/3x-ui/v3/internal/web/service	74.514s

TestCheckPortConflict_AmneziawgnetSocksRelayIgnoredWhenDisabled (internal/web/service/port_conflict_test.go) is deleted by this PR. It asserted the rule this change reverses, and it never reached the predicate it named: its fixture seeds Settings: {}, which amneziawg.InstanceFromInbound rejects on parsed.Server == nil (internal/amneziawg/instance.go:32) one statement before the enable column is looked at, so it passed with or without the filter. Leaving it would have had the package documenting both rules for the same operator state with nothing failing to flag the contradiction.

  • 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, apart from the case below.

The one behaviour change: a database that already holds a disabled AmneziaWG row and an ordinary inbound configured on that row's derived relay port (65101–65535) can no longer save that ordinary inbound while it keeps that port — not just a port change, but any save through that inbound's own update path, such as a remark or a TLS setting (adding a client to it goes through a different path that runs no port check). That state exists today and its config starts, because the disabled row injects no relay. The escape is the port change itself, which succeeds: the check compares the incoming port, so moving that inbound off the relay slot clears the refusal, and deleting the AmneziaWG row clears it too. Ports in 65101–65535 are panel-internal and never suggested, so reaching this state takes a deliberate choice of a very high port; the state is also the only one in which the AmneziaWG row can never be switched on.


🔄 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/6540 **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-disabled-relay-slot` --- ### 📝 Commits (3) - [`538232c`](https://github.com/MHSanaei/3x-ui/commit/538232cafa48a04060068ac35a42b407e2fbbe0a) test(amneziawg): pin that a disabled row still owns its relay slot - [`2553889`](https://github.com/MHSanaei/3x-ui/commit/255388972d15bae2a44f955d9c2374cc22a5eef9) fix(amneziawg): count a disabled inbound as owning its relay slot - [`dbb04d2`](https://github.com/MHSanaei/3x-ui/commit/dbb04d219949657646666e2bd0e73376afa1e48f) test(amneziawg): drop the disabled-row case that asserts the reversed rule ### 📊 Changes **3 files changed** (+31 additions, -25 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/inbound_amneziawg_relay_window_test.go` (+27 -0) 📝 `internal/web/service/port_conflict.go` (+4 -2) 📝 `internal/web/service/port_conflict_test.go` (+0 -23) </details> ### 📄 Description ## Summary A local AmneziaWG inbound owns the loopback relay port its id derives for as long as the row exists — but the forward port check only counted its *enabled* rows as owners. An ordinary inbound could therefore be saved onto that port while the AmneziaWG row was off, and turning that row back on 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 #6539; this is the second half of the same invariant that change wrote down in the other direction. ## Why it happens `checkAmneziawgnetSocksConflict` filters its candidates with `enable = true` (`internal/web/service/port_conflict.go:290`), so a disabled AmneziaWG row is invisible when a new inbound's configured port is validated. Nothing else covers the gap: the relay is not a database row, so the general `port = ?` query cannot see it, and `SetInboundEnable` (`internal/web/service/inbound.go:1566-1588`) flips the column inside a transaction that runs no port check at all. Reproduces entirely through the panel: 1. AmneziaWG inbound `#1` exists and is toggled off. `injectAmneziawgnetSocks` skips it (`internal/web/service/xray.go:739` requires `inbound.Enable`), so no relay is in the generated config — but the row still owns slot `65101`. 2. Any inbound is created on port `65101`. The forward check drops the disabled row, the DB query finds nothing, the save succeeds. 3. `#1` is toggled back on. No port check runs. 4. At the next Xray start both inbounds are appended on `127.0.0.1:65101`; Xray fails the second bind and refuses the entire config. ## Why this rule, and not a check on the enable path Two designs close that sequence, and the review asked for the choice to be deliberate rather than inherited from a shared helper: - **A (this PR):** a row owns the slot its id derives for as long as the row exists, disabled included, so the port is never handed out. - **B:** a row owns it only while enabled, and `SetInboundEnable` validates the relay port when it switches the row on. A is taken because the rule already exists in three of the four AmneziaWG checks in this file, including `checkAmneziawgnetSocksRelayCollision`, which shipped in #6539 and treats a disabled owner as an owner for exactly the reason that decides this: re-enabling runs no port check. B would make those three agree only by relaxing the merged one, and it moves a resource conflict onto a toggle that users flip as a routine on/off switch, where a refusal is far more surprising than at save time. The cost of A is the narrow case in **Breaking changes** below, which B would avoid. ## The change One predicate: the forward check now counts every local AmneziaWG row, disabled ones included. ## 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_DisabledAmneziawgStillOwnsItsRelaySlot` — an AmneziaWG inbound is created disabled, and an ordinary inbound is then validated on that row's derived relay port; the save must be refused with the owner named. It fails without the fix, on a test-only head whose CI run failed on exactly that one test: ``` --- FAIL: TestCheckPortConflict_DisabledAmneziawgStillOwnsItsRelaySlot (0.11s) inbound_amneziawg_relay_window_test.go:163: inbound #1 is disabled but still owns relay port 65101; the save must be refused FAIL github.com/mhsanaei/3x-ui/v3/internal/web/service 74.514s ``` `TestCheckPortConflict_AmneziawgnetSocksRelayIgnoredWhenDisabled` (`internal/web/service/port_conflict_test.go`) is deleted by this PR. It asserted the rule this change reverses, and it never reached the predicate it named: its fixture seeds `Settings: {}`, which `amneziawg.InstanceFromInbound` rejects on `parsed.Server == nil` (`internal/amneziawg/instance.go:32`) one statement before the `enable` column is looked at, so it passed with or without the filter. Leaving it would have had the package documenting both rules for the same operator state with nothing failing to flag the contradiction. - [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, apart from the case below. **The one behaviour change:** a database that already holds a *disabled* AmneziaWG row **and** an ordinary inbound configured on that row's derived relay port (`65101`–`65535`) can no longer save that ordinary inbound while it keeps that port — not just a port change, but any save through that inbound's own update path, such as a remark or a TLS setting (adding a client to it goes through a different path that runs no port check). That state exists today and its config starts, because the disabled row injects no relay. The escape is the port change itself, which succeeds: the check compares the incoming port, so moving that inbound off the relay slot clears the refusal, and deleting the AmneziaWG row clears it too. Ports in `65101`–`65535` are panel-internal and never suggested, so reaching this state takes a deliberate choice of a very high port; the state is also the only one in which the AmneziaWG row can never be switched on. --- <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:03 +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#6340
No description provided.