[PR #6539] [MERGED] fix(amneziawg): wrap the relay port window instead of refusing ids past it #6339

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/6539
Author: @BlindMaster24
Created: 9/15/2026
Status: ✅ Merged
Merged: 9/15/2026
Merged by: @BlindMaster24

Base: main ← Head: fix/amneziawg-relay-port-window


📝 Commits (5)

  • 58df4d9 fix(amneziawg): wrap the relay port window instead of refusing ids past it
  • 9ce17d1 test(amneziawg): give each relay-window fixture its own client email
  • 3a3f82c fix(amneziawg): say relay port in the relay conflict message
  • df6295f test(amneziawg): pin that a node-assigned inbound owns no relay slot
  • 0b8758d fix(amneziawg): skip the relay guards for node-assigned inbounds

📊 Changes

5 files changed (+266 additions, -21 deletions)

View changed files

📝 internal/amneziawgnet/socks_config.go (+5 -2)
➕ internal/amneziawgnet/socks_config_test.go (+26 -0)
📝 internal/web/service/inbound.go (+10 -8)
➕ internal/web/service/inbound_amneziawg_relay_window_test.go (+173 -0)
📝 internal/web/service/port_conflict.go (+52 -11)

📄 Description

Summary

An AmneziaWG inbound's loopback relay port is derived from its row id, and the id is refused once that port would pass 65535 — so a long-lived database runs out of AmneziaWG inbounds it can ever create. The id now wraps into the same port window instead, with a guard for the collision that wrapping allows.

Fixes #6537.

Why

SOCKSPortForInbound returns SOCKSBasePort + inboundID (internal/amneziawgnet/socks_config.go:19, SOCKSBasePort = 65100), and AddInbound refused the save when that exceeded 65535 (internal/web/service/inbound.go:1241). That is a lifetime budget of 435, not a concurrency limit: inbounds is AUTOINCREMENT, so an id is handed out once and never reused, and the counter is reset in exactly one place — when the table becomes completely empty (DelInbound, internal/web/service/inbound.go:1445, SQLite only). The reporter's counter is at 70350, so AmneziaWG has never worked on that database at all, and no amount of deleting inbounds fixes 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?

  • TestSOCKSPortForInboundKeepsEverySlotInsideTheWindow — no id derives a port outside 65101..65535, and every id up to 435 keeps SOCKSBasePort + id byte for byte. Fails before the change (id 436 derives relay port 65536).
  • TestAddInbound_AmneziawgPastTheRelayPortWindowStillCreates — the reported case: with the inbounds counter pushed to 70001, creating an AmneziaWG inbound succeeds and lands inside the window. Fails before the change with the exceeds the relay port window error.
  • TestAddInbound_AmneziawgRefusesAClaimedRelayPort — a second AmneziaWG inbound whose id is one slot-window away now shares the first one's relay port; the create must be refused with the owner named. Sub-tests cover an enabled and a disabled blocker.
  • TestCheckPortConflict_AmneziawgRelayCollisionBlocksAnAdoptedInbound — the update path, which is the only place an inbound adopted from a node can have its slot re-checked.

All four fail without the fix.

go test ./internal/amneziawgnet/ is green here except four egress tests (TestEgressConnectDomainResolvesThroughTunnel, TestEgressConnectDomainIPv6OnlyTunnelResolvesThroughTunnel, TestEgressUDPDatagramDomainForwardedIntoTunnel, TestEgressUDPDatagramDomainInterleavedClients), which also fail on the base commit on this machine — they resolve domain targets through the tunnel and need working DNS.

The four DB-backed tests cannot run on this machine at all: it has CGO_ENABLED=0, so go-sqlite3 is its stub (InitDB: go-sqlite3 requires cgo to work). They compile, and CI exercises them.

Screenshots / recordings

N/A — no UI change.

Breaking changes

None for existing rows: every id up to 435 keeps the exact relay port it had, so no stored config, running relay or generated Xray config moves, and nothing migrates. Two consequences worth knowing:

  • The ceiling changes meaning — 435 AmneziaWG relays at once instead of 435 inbounds ever created. A create that lands on a live slot is refused with a message naming the inbound that owns it, instead of silently killing Xray's start.
  • Rolling the binary back after creating an AmneziaWG inbound with an id above 435 reinstates the old formula, which derives an out-of-range port for that row and makes Xray refuse the config. Nothing can guard that from this side (the old build is frozen), so it belongs in the release notes if it is felt to matter.

Checklist

  • 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); see above, CI covers it.
  • 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.

Repairs from the first review round

  • The fixtures each built the same client email, which an email being panel-unique rejected before either new guard ran; each now derives its email from its own tag (9ce17d11).
  • A refusal that reported an inbound's automatic relay port read as if that inbound listened on an unrelated port; portConflictDetail.Relay makes those messages say relay port N, and configured-port messages are unchanged (3a3f82c1).
  • The sequence-push fixtures now skip under PostgreSQL instead of hard-failing, matching the repository's other dialect-bound tests (3a3f82c1).

🔄 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/6539 **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-window` --- ### 📝 Commits (5) - [`58df4d9`](https://github.com/MHSanaei/3x-ui/commit/58df4d9045ce105e5b84483687f01d653a78e53d) fix(amneziawg): wrap the relay port window instead of refusing ids past it - [`9ce17d1`](https://github.com/MHSanaei/3x-ui/commit/9ce17d11d9b7148e98a95dba089f93faf84c639a) test(amneziawg): give each relay-window fixture its own client email - [`3a3f82c`](https://github.com/MHSanaei/3x-ui/commit/3a3f82c1f589724e81943b60a808d4cb5b19dd0e) fix(amneziawg): say relay port in the relay conflict message - [`df6295f`](https://github.com/MHSanaei/3x-ui/commit/df6295f998f1677fd88415af4541bb4844cd43a0) test(amneziawg): pin that a node-assigned inbound owns no relay slot - [`0b8758d`](https://github.com/MHSanaei/3x-ui/commit/0b8758d3982e463e41ffa310f5dad905e14aba74) fix(amneziawg): skip the relay guards for node-assigned inbounds ### 📊 Changes **5 files changed** (+266 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `internal/amneziawgnet/socks_config.go` (+5 -2) ➕ `internal/amneziawgnet/socks_config_test.go` (+26 -0) 📝 `internal/web/service/inbound.go` (+10 -8) ➕ `internal/web/service/inbound_amneziawg_relay_window_test.go` (+173 -0) 📝 `internal/web/service/port_conflict.go` (+52 -11) </details> ### 📄 Description ## Summary An AmneziaWG inbound's loopback relay port is derived from its row id, and the id is refused once that port would pass 65535 — so a long-lived database runs out of AmneziaWG inbounds it can ever create. The id now wraps into the same port window instead, with a guard for the collision that wrapping allows. Fixes #6537. ## Why `SOCKSPortForInbound` returns `SOCKSBasePort + inboundID` (`internal/amneziawgnet/socks_config.go:19`, `SOCKSBasePort = 65100`), and `AddInbound` refused the save when that exceeded 65535 (`internal/web/service/inbound.go:1241`). That is a lifetime budget of 435, not a concurrency limit: `inbounds` is `AUTOINCREMENT`, so an id is handed out once and never reused, and the counter is reset in exactly one place — when the table becomes completely empty (`DelInbound`, `internal/web/service/inbound.go:1445`, SQLite only). The reporter's counter is at 70350, so AmneziaWG has never worked on that database at all, and no amount of deleting inbounds fixes 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? - `TestSOCKSPortForInboundKeepsEverySlotInsideTheWindow` — no id derives a port outside `65101..65535`, and every id up to 435 keeps `SOCKSBasePort + id` byte for byte. Fails before the change (`id 436 derives relay port 65536`). - `TestAddInbound_AmneziawgPastTheRelayPortWindowStillCreates` — the reported case: with the `inbounds` counter pushed to 70001, creating an AmneziaWG inbound succeeds and lands inside the window. Fails before the change with the `exceeds the relay port window` error. - `TestAddInbound_AmneziawgRefusesAClaimedRelayPort` — a second AmneziaWG inbound whose id is one slot-window away now shares the first one's relay port; the create must be refused with the owner named. Sub-tests cover an enabled and a disabled blocker. - `TestCheckPortConflict_AmneziawgRelayCollisionBlocksAnAdoptedInbound` — the update path, which is the only place an inbound adopted from a node can have its slot re-checked. All four fail without the fix. `go test ./internal/amneziawgnet/` is green here except four egress tests (`TestEgressConnectDomainResolvesThroughTunnel`, `TestEgressConnectDomainIPv6OnlyTunnelResolvesThroughTunnel`, `TestEgressUDPDatagramDomainForwardedIntoTunnel`, `TestEgressUDPDatagramDomainInterleavedClients`), which also fail on the base commit on this machine — they resolve domain targets through the tunnel and need working DNS. The four DB-backed tests cannot run on this machine at all: it has `CGO_ENABLED=0`, so `go-sqlite3` is its stub (`InitDB: go-sqlite3 requires cgo to work`). They compile, and CI exercises them. ## Screenshots / recordings N/A — no UI change. ## Breaking changes None for existing rows: every id up to 435 keeps the exact relay port it had, so no stored config, running relay or generated Xray config moves, and nothing migrates. Two consequences worth knowing: - The ceiling changes meaning — 435 AmneziaWG relays **at once** instead of 435 inbounds ever created. A create that lands on a live slot is refused with a message naming the inbound that owns it, instead of silently killing Xray's start. - Rolling the binary back after creating an AmneziaWG inbound with an id above 435 reinstates the old formula, which derives an out-of-range port for that row and makes Xray refuse the config. Nothing can guard that from this side (the old build is frozen), so it belongs in the release notes if it is felt to matter. ## Checklist - [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`); see above, CI covers it. - [ ] 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. ## Repairs from the first review round - The fixtures each built the same client email, which an email being panel-unique rejected before either new guard ran; each now derives its email from its own tag (`9ce17d11`). - A refusal that reported an inbound's automatic relay port read as if that inbound listened on an unrelated port; `portConflictDetail.Relay` makes those messages say `relay port N`, and configured-port messages are unchanged (`3a3f82c1`). - The sequence-push fixtures now skip under PostgreSQL instead of hard-failing, matching the repository's other dialect-bound tests (`3a3f82c1`). --- <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#6339
No description provided.