[GH-ISSUE #6537] [Bug]: Error during creating amneziawg #4387

Closed
opened 2026-10-31 20:39:19 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @romdegtyarev on GitHub (Sep 15, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6537

Describe the bug

Something went wrong (amneziawg: inbound id 70350 exceeds the relay port window (ids above 435 are not supported))

How to reproduce the problem

  1. Open Inbound
  2. Create amneziawg

Expected behavior

No response

Actual behavior

No response

Relevant logs


Screenshots

No response

3x-ui version

3.8.0

Xray-core version

No response

How did you install 3x-ui?

Docker / Docker Compose

Operating system

Debian 12

Which parts of the panel are affected?

No response

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

None

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @romdegtyarev on GitHub (Sep 15, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6537 ### Describe the bug Something went wrong (amneziawg: inbound id 70350 exceeds the relay port window (ids above 435 are not supported)) ### How to reproduce the problem 1. Open Inbound 2. Create amneziawg ### Expected behavior _No response_ ### Actual behavior _No response_ ### Relevant logs ```shell ``` ### Screenshots _No response_ ### 3x-ui version 3.8.0 ### Xray-core version _No response_ ### How did you install 3x-ui? Docker / Docker Compose ### Operating system Debian 12 ### Which parts of the panel are affected? _No response_ ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? None ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:39:19 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Sep 15, 2026):

Confirmed bug — this is a hard design limit, not something wrong with your input. An AmneziaWG inbound's internal loopback relay port is derived by adding the database row id to a fixed base port, so only inbound ids 0–435 can ever produce a valid port. Your panel's inbounds id counter has reached 70350, which means AmneziaWG inbounds can no longer be created on that database at all.

Where it happens

The relay port is a pure function of the primary key:

// internal/amneziawgnet/socks_config.go:10-18
// SOCKSBasePort is the first loopback port used for an AmneziaWG inbound's
// own Xray SOCKS5 relay inbound (see relay.go/SocksInboundSettings).
const SOCKSBasePort = 65100

// SOCKSPortForInbound derives one inbound's loopback SOCKS5 relay port from
// its id, so config generation and the dialing relay never need to negotiate.
func SOCKSPortForInbound(inboundID int) int {
	return SOCKSBasePort + inboundID
}

InboundService.AddInbound saves the row, reads back the id the database assigned, and rejects it when the derived port would overflow the port range:

// internal/web/service/inbound.go:1243-1247
if inbound.Protocol == model.AmneziaWG {
	if amneziawgnet.SOCKSPortForInbound(inbound.Id) > 65535 {
		return common.NewErrorf("amneziawg: inbound id %d exceeds the relay port window (ids above %d are not supported)",
			inbound.Id, 65535-amneziawgnet.SOCKSBasePort)
	}

65535 - 65100 = 435, which is exactly the number in your message. 65100 + 435 = 65535 is the last usable port; id 436 already overflows. The id is not something you sent — AddInbound zeroes it first (inbound.Id = 0, internal/web/service/inbound.go:1096), so 70350 came from the database's auto-increment counter. The Something went wrong (…) wrapper is internal/web/controller/inbound.go:176.

Why your counter is at 70350

The inbounds table uses SQLite AUTOINCREMENT (id integer PRIMARY KEY AUTOINCREMENT — the DDL literal is pinned in internal/database/inbound_port_unique_migration_test.go:13), so ids are never reused after a delete. The panel resets that counter in exactly one place, and only when the table becomes completely empty:

// internal/web/service/inbound.go:1445-1455
if !database.IsPostgres() {
	var count int64
	if err := db.Model(&model.Inbound{}).Count(&count).Error; err != nil {
		return needRestart, err
	}
	if count == 0 {
		if err := db.Exec("DELETE FROM sqlite_sequence WHERE name = ?", "inbounds").Error; err != nil {

So any panel that has created and deleted more than ~436 inbounds over its lifetime without ever emptying the table crosses this line permanently. I could not determine from the report how yours reached 70350 — the panel-side counter is the only input, so the create/delete history of that database would settle it.

Workaround while this stands

This only helps if your existing inbound rows all have low ids: SQLite picks max(sqlite_sequence value, current max rowid) + 1, so lowering the stored counter does nothing if a live row already has an id above 435.

  1. Check both numbers. With the compose setup the database is on the host at ./db/x-ui.db:

    sqlite3 ./db/x-ui.db \
      "SELECT COALESCE(MAX(id),0) FROM inbounds; SELECT seq FROM sqlite_sequence WHERE name='inbounds';"
    
  2. If the first number is 434 or below, stop the panel, take a backup (panel overview has a download button, or just copy the file), and lower the counter:

    docker compose down
    cp ./db/x-ui.db ./db/x-ui.db.bak
    sqlite3 ./db/x-ui.db "UPDATE sqlite_sequence SET seq = (SELECT COALESCE(MAX(id),0) FROM inbounds) WHERE name='inbounds';"
    docker compose up -d
    

If the first number is already above 435, there is no safe workaround — renumbering existing inbounds would break every row that references them by id (client traffic, client IPs, fallbacks, balancers).

Verdict: Confirmed bug
Severity: High
Confidence: High
Root cause: SOCKSPortForInbound (internal/amneziawgnet/socks_config.go:16-18) derives the per-inbound loopback SOCKS5 relay port as SOCKSBasePort + inboundID, using a monotonically increasing, never-reused database primary key as a port offset. That gives a lifetime budget of 436 ids (0–435) per database; past it the guard at internal/web/service/inbound.go:1244 rejects every AmneziaWG create permanently. Introduced with the feature itself in effcccce — "feat(amneziawg): add native AmneziaWG protocol support (#6105)"; internal/amneziawgnet/socks_config.go has had no other commit since.
Already fixed: Still present on the default branch (tip a810f497, after the v3.8.0 tag 837addf6).
Duplicate of: None
Evidence: const SOCKSBasePort = 65100 (internal/amneziawgnet/socks_config.go:12); return SOCKSBasePort + inboundID (internal/amneziawgnet/socks_config.go:17); if amneziawgnet.SOCKSPortForInbound(inbound.Id) > 65535 { (internal/web/service/inbound.go:1244) with 65535-amneziawgnet.SOCKSBasePort = 435 (internal/web/service/inbound.go:1246); inbound.Id = 0 (internal/web/service/inbound.go:1096) proves the id is database-assigned; DELETE FROM sqlite_sequence WHERE name = ? gated on count == 0 (internal/web/service/inbound.go:1445-1455) is the only counter reset, and it is SQLite-only; AUTOINCREMENT in the pinned inbounds DDL (internal/database/inbound_port_unique_migration_test.go:13). No test covers the > 65535 branch — grepping *_test.go for the guard or its message returns nothing, so this path is untested.
Not determined: What drove this database's inbounds counter to 70350 (bulk API create/delete, a node-sync churn loop, or restored data). The SELECT COALESCE(MAX(id),0) FROM inbounds / SELECT seq FROM sqlite_sequence WHERE name='inbounds' pair above would settle both that and whether the workaround applies.

Suggested fix: Stop deriving the relay port from the primary key — allocate it from the free ports inside the 65100–65535 window and persist the choice on the inbound row, so the constraint becomes "436 concurrent AmneziaWG inbounds" instead of "436 inbounds ever created". checkAmneziawgnetSocksConflict / checkAmneziawgnetSocksReverseConflict (internal/web/service/port_conflict.go:275-320), injectAmneziawgnetSocks (internal/web/service/xray.go:770) and Manager (internal/amneziawgnet/manager.go:255) would read the stored port instead of recomputing it.

@MHSanaei

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:5674707014 --> @github-actions[bot] commented on GitHub (Sep 15, 2026): **Confirmed bug — this is a hard design limit, not something wrong with your input.** An AmneziaWG inbound's internal loopback relay port is derived by *adding the database row id to a fixed base port*, so only inbound ids `0`–`435` can ever produce a valid port. Your panel's `inbounds` id counter has reached `70350`, which means AmneziaWG inbounds can no longer be created on that database at all. ## Where it happens The relay port is a pure function of the primary key: ```go // internal/amneziawgnet/socks_config.go:10-18 // SOCKSBasePort is the first loopback port used for an AmneziaWG inbound's // own Xray SOCKS5 relay inbound (see relay.go/SocksInboundSettings). const SOCKSBasePort = 65100 // SOCKSPortForInbound derives one inbound's loopback SOCKS5 relay port from // its id, so config generation and the dialing relay never need to negotiate. func SOCKSPortForInbound(inboundID int) int { return SOCKSBasePort + inboundID } ``` `InboundService.AddInbound` saves the row, reads back the id the database assigned, and rejects it when the derived port would overflow the port range: ```go // internal/web/service/inbound.go:1243-1247 if inbound.Protocol == model.AmneziaWG { if amneziawgnet.SOCKSPortForInbound(inbound.Id) > 65535 { return common.NewErrorf("amneziawg: inbound id %d exceeds the relay port window (ids above %d are not supported)", inbound.Id, 65535-amneziawgnet.SOCKSBasePort) } ``` `65535 - 65100 = 435`, which is exactly the number in your message. `65100 + 435 = 65535` is the last usable port; id `436` already overflows. The id is not something you sent — `AddInbound` zeroes it first (`inbound.Id = 0`, `internal/web/service/inbound.go:1096`), so `70350` came from the database's auto-increment counter. The `Something went wrong (…)` wrapper is `internal/web/controller/inbound.go:176`. ## Why your counter is at 70350 The `inbounds` table uses SQLite `AUTOINCREMENT` (`id integer PRIMARY KEY AUTOINCREMENT` — the DDL literal is pinned in `internal/database/inbound_port_unique_migration_test.go:13`), so ids are **never reused** after a delete. The panel resets that counter in exactly one place, and only when the table becomes completely empty: ```go // internal/web/service/inbound.go:1445-1455 if !database.IsPostgres() { var count int64 if err := db.Model(&model.Inbound{}).Count(&count).Error; err != nil { return needRestart, err } if count == 0 { if err := db.Exec("DELETE FROM sqlite_sequence WHERE name = ?", "inbounds").Error; err != nil { ``` So any panel that has created and deleted more than ~436 inbounds over its lifetime without ever emptying the table crosses this line permanently. I could not determine from the report how yours reached 70350 — the panel-side counter is the only input, so the create/delete history of that database would settle it. ## Workaround while this stands This only helps if your **existing** inbound rows all have low ids: SQLite picks `max(sqlite_sequence value, current max rowid) + 1`, so lowering the stored counter does nothing if a live row already has an id above 435. 1. Check both numbers. With the compose setup the database is on the host at `./db/x-ui.db`: ```bash sqlite3 ./db/x-ui.db \ "SELECT COALESCE(MAX(id),0) FROM inbounds; SELECT seq FROM sqlite_sequence WHERE name='inbounds';" ``` 2. If the first number is `434` or below, stop the panel, **take a backup** (panel overview has a download button, or just copy the file), and lower the counter: ```bash docker compose down cp ./db/x-ui.db ./db/x-ui.db.bak sqlite3 ./db/x-ui.db "UPDATE sqlite_sequence SET seq = (SELECT COALESCE(MAX(id),0) FROM inbounds) WHERE name='inbounds';" docker compose up -d ``` If the first number is already above 435, there is no safe workaround — renumbering existing inbounds would break every row that references them by id (client traffic, client IPs, fallbacks, balancers). Verdict: Confirmed bug Severity: High Confidence: High Root cause: `SOCKSPortForInbound` (`internal/amneziawgnet/socks_config.go:16-18`) derives the per-inbound loopback SOCKS5 relay port as `SOCKSBasePort + inboundID`, using a monotonically increasing, never-reused database primary key as a port offset. That gives a lifetime budget of 436 ids (`0`–`435`) per database; past it the guard at `internal/web/service/inbound.go:1244` rejects every AmneziaWG create permanently. Introduced with the feature itself in `effcccce` — "feat(amneziawg): add native AmneziaWG protocol support (#6105)"; `internal/amneziawgnet/socks_config.go` has had no other commit since. Already fixed: Still present on the default branch (tip `a810f497`, after the v3.8.0 tag `837addf6`). Duplicate of: None Evidence: `const SOCKSBasePort = 65100` (internal/amneziawgnet/socks_config.go:12); `return SOCKSBasePort + inboundID` (internal/amneziawgnet/socks_config.go:17); `if amneziawgnet.SOCKSPortForInbound(inbound.Id) > 65535 {` (internal/web/service/inbound.go:1244) with `65535-amneziawgnet.SOCKSBasePort` = 435 (internal/web/service/inbound.go:1246); `inbound.Id = 0` (internal/web/service/inbound.go:1096) proves the id is database-assigned; `DELETE FROM sqlite_sequence WHERE name = ?` gated on `count == 0` (internal/web/service/inbound.go:1445-1455) is the only counter reset, and it is SQLite-only; `AUTOINCREMENT` in the pinned inbounds DDL (internal/database/inbound_port_unique_migration_test.go:13). No test covers the `> 65535` branch — grepping `*_test.go` for the guard or its message returns nothing, so this path is untested. Not determined: What drove this database's `inbounds` counter to 70350 (bulk API create/delete, a node-sync churn loop, or restored data). The `SELECT COALESCE(MAX(id),0) FROM inbounds` / `SELECT seq FROM sqlite_sequence WHERE name='inbounds'` pair above would settle both that and whether the workaround applies. Suggested fix: Stop deriving the relay port from the primary key — allocate it from the free ports inside the `65100`–`65535` window and persist the choice on the inbound row, so the constraint becomes "436 concurrent AmneziaWG inbounds" instead of "436 inbounds ever created". `checkAmneziawgnetSocksConflict` / `checkAmneziawgnetSocksReverseConflict` (`internal/web/service/port_conflict.go:275-320`), `injectAmneziawgnetSocks` (`internal/web/service/xray.go:770`) and `Manager` (`internal/amneziawgnet/manager.go:255`) would read the stored port instead of recomputing it. @MHSanaei *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
Author
Owner

@BlindMaster24 commented on GitHub (Sep 15, 2026):

Confirming the diagnosis, and it is fixed by #6539 — open now, intended for the next release.

The relay port an AmneziaWG inbound uses is derived from its row id, and ids above 435 were refused outright. That made the window a lifetime budget rather than a limit on how many can run at once: inbounds is AUTOINCREMENT, so an id is never reused and the counter only resets when the table becomes completely empty, and a database whose counter has climbed past the window can never create another AmneziaWG inbound. Your counter is at 70350, so the protocol has never worked on that database.

#6539 wraps the id into the same 435 ports instead of refusing it. Every id up to 435 keeps exactly the port it had, so nothing moves on upgrade, and a create or edit whose port another AmneziaWG inbound already holds is refused with that inbound named rather than silently breaking Xray's start. Two consequences worth knowing up front: the limit becomes 435 AmneziaWG inbounds at once instead of 435 ever created, and rolling back to a build from before #6539 after creating an inbound above 435 would derive an out-of-range port for that row, so a downgrade there is not clean.

While you are still on 3.8.0, the workaround the analysis above gives only helps if every live inbound row is at or below id 434, and lowering the counter cannot help past that — SQLite takes max(seq, max(rowid)) + 1, and renumbering existing rows would break everything that references an inbound by id.

<!-- gh-comment-id:5675487257 --> @BlindMaster24 commented on GitHub (Sep 15, 2026): Confirming the diagnosis, and it is fixed by #6539 — open now, intended for the next release. The relay port an AmneziaWG inbound uses is derived from its row id, and ids above 435 were refused outright. That made the window a lifetime budget rather than a limit on how many can run at once: `inbounds` is `AUTOINCREMENT`, so an id is never reused and the counter only resets when the table becomes completely empty, and a database whose counter has climbed past the window can never create another AmneziaWG inbound. Your counter is at 70350, so the protocol has never worked on that database. #6539 wraps the id into the same 435 ports instead of refusing it. Every id up to 435 keeps exactly the port it had, so nothing moves on upgrade, and a create or edit whose port another AmneziaWG inbound already holds is refused with that inbound named rather than silently breaking Xray's start. Two consequences worth knowing up front: the limit becomes 435 AmneziaWG inbounds **at once** instead of 435 ever created, and rolling back to a build from before #6539 after creating an inbound above 435 would derive an out-of-range port for that row, so a downgrade there is not clean. While you are still on 3.8.0, the workaround the analysis above gives only helps if every live inbound row is at or below id 434, and lowering the counter cannot help past that — SQLite takes `max(seq, max(rowid)) + 1`, and renumbering existing rows would break everything that references an inbound by id.
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#4387
No description provided.