[GH-ISSUE #6731] [Bug]: WireGuard peer gets the AmneziaWG public key when one client is on both inbounds (v3.9.0, incomplete #6328 fix) #4487

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

Originally created by @unger2007-beep on GitHub (Oct 4, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6731

Describe the bug

A client attached to both a WireGuard inbound and an AmneziaWG inbound gets a WireGuard peer in the generated Xray config with the AmneziaWG identity's public key. The subscription (after #6653) correctly hands the client the WireGuard inbound's own private key, so the two never match: the WireGuard handshake never completes. AmneziaWG keeps working.

This looks like the unfinished half of #6328. Its fix overlays the inbound's own AllowedIPs and PreSharedKey onto the shared clients row, but not PublicKey:

// internal/web/service/xray.go (v3.9.0 and current main), GetXrayConfig
case model.WireGuard:
    if inboundClient, ok := wireguardClientsByEmail[strings.ToLower(strings.TrimSpace(c.Email))]; ok {
        c.AllowedIPs = inboundClient.AllowedIPs
        c.PreSharedKey = inboundClient.PreSharedKey
    }
    wgPeers = append(wgPeers, model.WireguardPeerFromClient(c))

c comes from ListForInbound (the shared clients row), whose key columns hold whichever tunnel inbound was synced last — here the AmneziaWG one. On v3.8.5 the mismatch appeared in the subscription instead (#6641). Since #6653 the subscription reads per-inbound identity, so the Xray config is now the inconsistent side, and it is wrong every time.

How to reproduce the problem

Fresh ghcr.io/mhsanaei/3x-ui:v3.9.0 container, API token from x-ui setting -getApiToken:

  1. Add a WireGuard inbound (port 20051, address: ["10.1.0.1/24"]).
  2. Add an AmneziaWG inbound (port 20052, subnet 10.9.1.0/24).
  3. Add one client to both in a single call:
    POST /panel/api/clients/add {"client":{"email":"carol","subId":"carolsub","enable":true},"inboundIds":[<wg>,<awg>]}
  4. Compare the public key derived from the WireGuard link's private key (subscription /sub/carolsub, wireguard://<privkey>@…) with the peer the panel wrote for Xray:
    jq '.inbounds[] | select(.protocol=="wireguard") | .settings.peers' /app/bin/config.json

Expected behavior

The Xray WireGuard peer for carol carries the WireGuard inbound's own public key, the one matching the private key in her WireGuard link and in the inbound's settings.

Actual behavior

v3.9.0, one run (public keys, xray wg -i <privkey> for link keys):

Source public key address
WireGuard link in subscription EJcNa+/36dh… 10.0.0.2/32
WireGuard inbound settings (/panel/api/inbounds/list) EJcNa+/36dh… 10.0.0.2/32
Running Xray WireGuard peer (bin/config.json) g7TRyaDMyjG… 10.0.0.2/32
AmneziaWG link / AmneziaWG inbound settings g7TRyaDMyjG… 10.9.1.2/32

The WireGuard peer has the right address (fixed by #6328) but the AmneziaWG key. The client's handshake initiations go unanswered. Xray client debug log: Handshake did not complete after 5 seconds, retrying.

The same happens for inbounds pushed to a multi-node node; it is the same code path on the node. A client attached to the WireGuard inbound only passes traffic normally on the same inbound.

Relevant logs

[Debug] peer(…) - Sending handshake initiation
[Debug] peer(…) - Handshake did not complete after 5 seconds, retrying (try 2)

Screenshots

No response

3x-ui version

v3.9.0 (the WireGuard branch above is unchanged on main as of 2026-10-04)

Xray-core version

26.9.30

How did you install 3x-ui?

Docker / Docker Compose

Operating system

Docker Engine 29 on Ubuntu 24.04

Which parts of the panel are affected?

Xray config generation

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

No — direct access

Suggested fix

In GetXrayConfig's WireGuard branch, also take PublicKey (and KeepAlive) from the inbound's own settings entry, the same tunnel-identity set #6653 overlays for subscriptions. The workaround until then is one client (email) per tunnel inbound.

Related: #6328, #6641, #6653.

Before submitting

  • I searched existing issues and this bug has not been reported. (#6328 is the closed, partially fixed predecessor.)
  • 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 @unger2007-beep on GitHub (Oct 4, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6731 ### Describe the bug A client attached to **both** a WireGuard inbound and an AmneziaWG inbound gets a WireGuard peer in the generated Xray config with the **AmneziaWG identity's public key**. The subscription (after #6653) correctly hands the client the WireGuard inbound's own private key, so the two never match: the WireGuard handshake never completes. AmneziaWG keeps working. This looks like the unfinished half of #6328. Its fix overlays the inbound's own `AllowedIPs` and `PreSharedKey` onto the shared `clients` row, but not `PublicKey`: ```go // internal/web/service/xray.go (v3.9.0 and current main), GetXrayConfig case model.WireGuard: if inboundClient, ok := wireguardClientsByEmail[strings.ToLower(strings.TrimSpace(c.Email))]; ok { c.AllowedIPs = inboundClient.AllowedIPs c.PreSharedKey = inboundClient.PreSharedKey } wgPeers = append(wgPeers, model.WireguardPeerFromClient(c)) ``` `c` comes from `ListForInbound` (the shared `clients` row), whose key columns hold whichever tunnel inbound was synced last — here the AmneziaWG one. On v3.8.5 the mismatch appeared in the subscription instead (#6641). Since #6653 the subscription reads per-inbound identity, so the Xray config is now the inconsistent side, and it is wrong every time. ### How to reproduce the problem Fresh `ghcr.io/mhsanaei/3x-ui:v3.9.0` container, API token from `x-ui setting -getApiToken`: 1. Add a WireGuard inbound (port 20051, `address: ["10.1.0.1/24"]`). 2. Add an AmneziaWG inbound (port 20052, subnet `10.9.1.0/24`). 3. Add one client to both in a single call: `POST /panel/api/clients/add` `{"client":{"email":"carol","subId":"carolsub","enable":true},"inboundIds":[<wg>,<awg>]}` 4. Compare the public key derived from the WireGuard link's private key (subscription `/sub/carolsub`, `wireguard://<privkey>@…`) with the peer the panel wrote for Xray: `jq '.inbounds[] | select(.protocol=="wireguard") | .settings.peers' /app/bin/config.json` ### Expected behavior The Xray WireGuard peer for `carol` carries the WireGuard inbound's own public key, the one matching the private key in her WireGuard link and in the inbound's settings. ### Actual behavior v3.9.0, one run (public keys, `xray wg -i <privkey>` for link keys): | Source | public key | address | |---|---|---| | WireGuard link in subscription | `EJcNa+/36dh…` | `10.0.0.2/32` | | WireGuard inbound settings (`/panel/api/inbounds/list`) | `EJcNa+/36dh…` | `10.0.0.2/32` | | **Running Xray WireGuard peer (`bin/config.json`)** | **`g7TRyaDMyjG…`** | `10.0.0.2/32` | | AmneziaWG link / AmneziaWG inbound settings | `g7TRyaDMyjG…` | `10.9.1.2/32` | The WireGuard peer has the right address (fixed by #6328) but the AmneziaWG key. The client's handshake initiations go unanswered. Xray client debug log: `Handshake did not complete after 5 seconds, retrying`. The same happens for inbounds pushed to a multi-node node; it is the same code path on the node. A client attached to the WireGuard inbound only passes traffic normally on the same inbound. ### Relevant logs ```shell [Debug] peer(…) - Sending handshake initiation [Debug] peer(…) - Handshake did not complete after 5 seconds, retrying (try 2) ``` ### Screenshots _No response_ ### 3x-ui version v3.9.0 (the WireGuard branch above is unchanged on `main` as of 2026-10-04) ### Xray-core version 26.9.30 ### How did you install 3x-ui? Docker / Docker Compose ### Operating system Docker Engine 29 on Ubuntu 24.04 ### Which parts of the panel are affected? Xray config generation ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? No — direct access ### Suggested fix In `GetXrayConfig`'s WireGuard branch, also take `PublicKey` (and `KeepAlive`) from the inbound's own settings entry, the same tunnel-identity set #6653 overlays for subscriptions. The workaround until then is one client (email) per tunnel inbound. Related: #6328, #6641, #6653. ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. (#6328 is the closed, partially fixed predecessor.) - [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).
Author
Owner

@github-actions[bot] commented on GitHub (Oct 4, 2026):

You're right: this is a real bug and it is still on the default branch. When GetXrayConfig builds a WireGuard peer, it copies AllowedIPs and PreSharedKey from the inbound's own settings entry but takes PublicKey and KeepAlive from the shared clients row. That row holds the tunnel identity of whichever WireGuard/AmneziaWG inbound was synced last.

How it happens

  1. ListForInbound reads clients.* joined through client_inbounds (internal/web/service/client_link.go:369-372). That gives one row per client, and its key lives in a single column: PublicKey string ... gorm:"column:wg_public_key" (internal/database/model/model.go:930).
  2. GetXrayConfig parses the inbound's own settings into wireguardClientsByEmail (internal/web/service/xray.go:211-220). It then overlays only part of that entry:
    // internal/web/service/xray.go:285-290
    case model.WireGuard:
        if inboundClient, ok := wireguardClientsByEmail[strings.ToLower(strings.TrimSpace(c.Email))]; ok {
            c.AllowedIPs = inboundClient.AllowedIPs
            c.PreSharedKey = inboundClient.PreSharedKey
        }
        wgPeers = append(wgPeers, model.WireguardPeerFromClient(c))
    
  3. WireguardPeerFromClient writes peer["publicKey"] = c.PublicKey and keepAlive from c.KeepAliveSeconds() (internal/database/model/model.go:437-448). Those values come from the shared row, not from this inbound.

The commit message of #6653 (0dec3d65) describes the same shared row: "the shared clients rows, whose wg_* columns hold whichever WireGuard/AmneziaWG inbound synced last". That fix only touched internal/sub/service.go, where overlayInboundTunnelIdentity now copies PrivateKey, PublicKey, PreSharedKey, AllowedIPs and KeepAlive. The Xray-config side still copies only two of those fields. That explains your table: the address is correct (from the #6344 overlay) and the key is the AmneziaWG one.

The live gRPC path does read the right key. WireguardClientsToPeers (internal/database/model/model.go:464-493) builds peers straight from the inbound's settings clients. So right after an inbound add or edit the running peer may be correct, and it goes wrong on the next full config generation (restart, config.json write). I worked that out from the code and did not run it.

Your workaround holds until a fix lands: use one client (email) per WireGuard/AmneziaWG inbound.

Verdict: Confirmed bug
Severity: Medium (only a client attached to two or more WireGuard/AmneziaWG inbounds on the same panel, but for that client the WireGuard handshake fails every time)
Confidence: High
Root cause: internal/web/service/xray.go:286-289 (XrayService.GetXrayConfig, WireGuard branch) copies only AllowedIPs/PreSharedKey from the inbound's settings entry. PublicKey/KeepAlive stay as the shared clients.wg_* values, which belong to the last-synced tunnel inbound. The partial overlay came from f6445304 "fix: preserve per-inbound WireGuard peer addresses (#6344)".
Already fixed: No. Still present on the default branch (815c9c57) and in v3.9.0.
Duplicate of: None. Related: #6328 (address half, fixed by f6445304), #6641 / #6653 (0dec3d65, the same shared-row problem fixed for subscriptions only)
Evidence: internal/web/service/xray.go:285-290; internal/database/model/model.go:435-449 (WireguardPeerFromClient), :930 (wg_public_key column); internal/web/service/client_link.go:360-386 (ListForInbound returns clients.*); internal/sub/service.go overlayInboundTunnelIdentity from 0dec3d65 (the full overlay done for subscriptions).
Not determined: Whether a multi-node node can reach this with a client on only one inbound there. It depends on the node's local clients row, and testing a node holding a single tunnel inbound would settle it. Inferred from the code: two plain WireGuard inbounds sharing one client should fail the same way, because the shared row only ever holds one key.

Suggested fix: In GetXrayConfig's WireGuard branch, take the whole tunnel identity (PublicKey, PreSharedKey, AllowedIPs, KeepAlive) from this inbound's settings entry, the same set overlayInboundTunnelIdentity copies for subscriptions. A test with one client on two tunnel inbounds that have different keys, asserting each peer's publicKey, would pin it.

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

<!-- gh-comment-id:5978924125 --> @github-actions[bot] commented on GitHub (Oct 4, 2026): You're right: this is a real bug and it is still on the default branch. When `GetXrayConfig` builds a WireGuard peer, it copies `AllowedIPs` and `PreSharedKey` from the inbound's own settings entry but takes `PublicKey` and `KeepAlive` from the shared `clients` row. That row holds the tunnel identity of whichever WireGuard/AmneziaWG inbound was synced last. **How it happens** 1. `ListForInbound` reads `clients.*` joined through `client_inbounds` (`internal/web/service/client_link.go:369-372`). That gives one row per client, and its key lives in a single column: `PublicKey string ... gorm:"column:wg_public_key"` (`internal/database/model/model.go:930`). 2. `GetXrayConfig` parses the inbound's own settings into `wireguardClientsByEmail` (`internal/web/service/xray.go:211-220`). It then overlays only part of that entry: ```go // internal/web/service/xray.go:285-290 case model.WireGuard: if inboundClient, ok := wireguardClientsByEmail[strings.ToLower(strings.TrimSpace(c.Email))]; ok { c.AllowedIPs = inboundClient.AllowedIPs c.PreSharedKey = inboundClient.PreSharedKey } wgPeers = append(wgPeers, model.WireguardPeerFromClient(c)) ``` 3. `WireguardPeerFromClient` writes `peer["publicKey"] = c.PublicKey` and `keepAlive` from `c.KeepAliveSeconds()` (`internal/database/model/model.go:437-448`). Those values come from the shared row, not from this inbound. The commit message of #6653 (`0dec3d65`) describes the same shared row: "the shared clients rows, whose wg_* columns hold whichever WireGuard/AmneziaWG inbound synced last". That fix only touched `internal/sub/service.go`, where `overlayInboundTunnelIdentity` now copies `PrivateKey`, `PublicKey`, `PreSharedKey`, `AllowedIPs` and `KeepAlive`. The Xray-config side still copies only two of those fields. That explains your table: the address is correct (from the #6344 overlay) and the key is the AmneziaWG one. The live gRPC path does read the right key. `WireguardClientsToPeers` (`internal/database/model/model.go:464-493`) builds peers straight from the inbound's settings `clients`. So right after an inbound add or edit the running peer may be correct, and it goes wrong on the next full config generation (restart, `config.json` write). I worked that out from the code and did not run it. Your workaround holds until a fix lands: use one client (email) per WireGuard/AmneziaWG inbound. Verdict: Confirmed bug Severity: Medium (only a client attached to two or more WireGuard/AmneziaWG inbounds on the same panel, but for that client the WireGuard handshake fails every time) Confidence: High Root cause: `internal/web/service/xray.go:286-289` (`XrayService.GetXrayConfig`, WireGuard branch) copies only `AllowedIPs`/`PreSharedKey` from the inbound's settings entry. `PublicKey`/`KeepAlive` stay as the shared `clients.wg_*` values, which belong to the last-synced tunnel inbound. The partial overlay came from `f6445304` "fix: preserve per-inbound WireGuard peer addresses (#6344)". Already fixed: No. Still present on the default branch (`815c9c57`) and in v3.9.0. Duplicate of: None. Related: #6328 (address half, fixed by `f6445304`), #6641 / #6653 (`0dec3d65`, the same shared-row problem fixed for subscriptions only) Evidence: `internal/web/service/xray.go:285-290`; `internal/database/model/model.go:435-449` (`WireguardPeerFromClient`), `:930` (`wg_public_key` column); `internal/web/service/client_link.go:360-386` (`ListForInbound` returns `clients.*`); `internal/sub/service.go` `overlayInboundTunnelIdentity` from `0dec3d65` (the full overlay done for subscriptions). Not determined: Whether a multi-node node can reach this with a client on only one inbound there. It depends on the node's local `clients` row, and testing a node holding a single tunnel inbound would settle it. Inferred from the code: two plain WireGuard inbounds sharing one client should fail the same way, because the shared row only ever holds one key. Suggested fix: In `GetXrayConfig`'s WireGuard branch, take the whole tunnel identity (`PublicKey`, `PreSharedKey`, `AllowedIPs`, `KeepAlive`) from this inbound's settings entry, the same set `overlayInboundTunnelIdentity` copies for subscriptions. A test with one client on two tunnel inbounds that have different keys, asserting each peer's `publicKey`, would pin it. *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
Author
Owner

@mvanhorn commented on GitHub (Oct 6, 2026):

GetXrayConfig builds the WireGuard peer from the shared clients row and only overwrites AllowedIPs and PreSharedKey from the inbound settings entry for that email. PublicKey and KeepAlive stay as stored on the shared row, which holds the tunnel identity of whichever WireGuard or AmneziaWG inbound was synced last, and WireguardPeerFromClient emits those leftover fields.

The WireGuard branch now copies PublicKey from the matching inbound settings client. When that entry's KeepAlive is nil the peer keepalive is cleared, and when it is set the integer is copied into a new pointer.

<!-- gh-comment-id:6011683400 --> @mvanhorn commented on GitHub (Oct 6, 2026): GetXrayConfig builds the WireGuard peer from the shared clients row and only overwrites AllowedIPs and PreSharedKey from the inbound settings entry for that email. PublicKey and KeepAlive stay as stored on the shared row, which holds the tunnel identity of whichever WireGuard or AmneziaWG inbound was synced last, and WireguardPeerFromClient emits those leftover fields. The WireGuard branch now copies PublicKey from the matching inbound settings client. When that entry's KeepAlive is nil the peer keepalive is cleared, and when it is set the integer is copied into a new pointer.
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#4487
No description provided.