[GH-ISSUE #6367] [Bug]: AmneziaWG inbound ignores the "listen" field and always binds all addresses #4323

Closed
opened 2026-10-31 20:38:49 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @IvanSavoskin on GitHub (Aug 30, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6367

Describe the bug

An inbound with protocol: amneziawg and a non-empty listen value binds to all addresses
instead of the configured one. Inbounds backed by Xray honour listen normally on the same
panel.

The panel stores the value correctly — GET /panel/api/inbounds/get/:id returns
"listen": "SECONDARY_IP" — but the embedded device listens on the wildcard address.

On a host with two public IPv4 addresses this is not cosmetic. Because the socket is bound
to the wildcard address, the source address of every reply is chosen by the routing table
instead of the address the request arrived on. Clients that connect to the secondary IP get
answers from the primary one, discard them, and never complete a handshake.

How to reproduce the problem

  1. Have a host with two public IPv4 addresses on one interface (PRIMARY_IP as the primary
    address and the default route source, SECONDARY_IP as an additional /32).
  2. Create an inbound with protocol: amneziawg, port: 1235, listen: SECONDARY_IP.
  3. Add at least one client (the device does not open its socket until an inbound has peers).
  4. ss -lunp | grep 1235 — the socket is bound to 0.0.0.0 and [::], not to SECONDARY_IP.
  5. Connect a client to SECONDARY_IP:1235 and capture traffic on the server.

Expected behavior

An AmneziaWG inbound with a non-empty listen binds only to that address, the same way
Xray-backed inbounds do, and replies leave from the address the client connected to.

If per-address binding is not supported by the embedded device, the field should be rejected
on save or documented as ignored for this protocol, so that multi-IP setups do not fail
silently.

Actual behavior

The socket is bound to all addresses, replies are sourced from the primary IP, and the
handshake never completes. The client retries every ~5 seconds indefinitely. Nothing is
logged by the panel.

Workaround that makes the second inbound usable — note that SNAT alone is not enough,
because the incoming packet has already created a conntrack entry whose reply tuple is
exactly what SNAT produces, so the translated packet clashes on insert and is dropped
silently. The incoming flow has to be left untracked as well:

iptables -t raw -A PREROUTING -p udp -d SECONDARY_IP --dport 1235 -j NOTRACK
iptables -t nat -A POSTROUTING -p udp --sport 1235 -o IFACE -j SNAT --to-source SECONDARY_IP

Relevant logs

The panel logs nothing at all for this case. Evidence is on the socket and on the wire.

Configured value read back from the API:


"listen": "SECONDARY_IP", "port": 1235, "protocol": "amneziawg"


Actual binding:


# ss -lunp | grep 1235
UNCONN 0 0    0.0.0.0:1235    0.0.0.0:*   users:(("x-ui",pid=632053,fd=21))
UNCONN 0 0       [::]:1235       [::]:*   users:(("x-ui",pid=632053,fd=22))


Client connecting to the secondary IP, server answering from the primary one:


IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 47
IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 149
IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 243
IP PRIMARY_IP.1235 > CLIENT_IP.1345: UDP, length 425
IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 47
IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 94
IP PRIMARY_IP.1235 > CLIENT_IP.1345: UDP, length 260

Screenshots

No response

3x-ui version

3.7.0

Xray-core version

26.7.28

How did you install 3x-ui?

install.sh script

Operating system

Ubuntu 24.04 LTS, x86_64

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?

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 @IvanSavoskin on GitHub (Aug 30, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6367 ### Describe the bug An inbound with `protocol: amneziawg` and a non-empty `listen` value binds to all addresses instead of the configured one. Inbounds backed by Xray honour `listen` normally on the same panel. The panel stores the value correctly — `GET /panel/api/inbounds/get/:id` returns `"listen": "SECONDARY_IP"` — but the embedded device listens on the wildcard address. On a host with two public IPv4 addresses this is not cosmetic. Because the socket is bound to the wildcard address, the source address of every reply is chosen by the routing table instead of the address the request arrived on. Clients that connect to the secondary IP get answers from the primary one, discard them, and never complete a handshake. ### How to reproduce the problem 1. Have a host with two public IPv4 addresses on one interface (`PRIMARY_IP` as the primary address and the default route source, `SECONDARY_IP` as an additional `/32`). 2. Create an inbound with `protocol: amneziawg`, `port: 1235`, `listen: SECONDARY_IP`. 3. Add at least one client (the device does not open its socket until an inbound has peers). 4. `ss -lunp | grep 1235` — the socket is bound to `0.0.0.0` and `[::]`, not to `SECONDARY_IP`. 5. Connect a client to `SECONDARY_IP:1235` and capture traffic on the server. ### Expected behavior An AmneziaWG inbound with a non-empty `listen` binds only to that address, the same way Xray-backed inbounds do, and replies leave from the address the client connected to. If per-address binding is not supported by the embedded device, the field should be rejected on save or documented as ignored for this protocol, so that multi-IP setups do not fail silently. ### Actual behavior The socket is bound to all addresses, replies are sourced from the primary IP, and the handshake never completes. The client retries every ~5 seconds indefinitely. Nothing is logged by the panel. Workaround that makes the second inbound usable — note that `SNAT` alone is not enough, because the incoming packet has already created a conntrack entry whose reply tuple is exactly what `SNAT` produces, so the translated packet clashes on insert and is dropped silently. The incoming flow has to be left untracked as well: ```bash iptables -t raw -A PREROUTING -p udp -d SECONDARY_IP --dport 1235 -j NOTRACK iptables -t nat -A POSTROUTING -p udp --sport 1235 -o IFACE -j SNAT --to-source SECONDARY_IP ``` ### Relevant logs ```shell The panel logs nothing at all for this case. Evidence is on the socket and on the wire. Configured value read back from the API: "listen": "SECONDARY_IP", "port": 1235, "protocol": "amneziawg" Actual binding: # ss -lunp | grep 1235 UNCONN 0 0 0.0.0.0:1235 0.0.0.0:* users:(("x-ui",pid=632053,fd=21)) UNCONN 0 0 [::]:1235 [::]:* users:(("x-ui",pid=632053,fd=22)) Client connecting to the secondary IP, server answering from the primary one: IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 47 IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 149 IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 243 IP PRIMARY_IP.1235 > CLIENT_IP.1345: UDP, length 425 IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 47 IP CLIENT_IP.1345 > SECONDARY_IP.1235: UDP, length 94 IP PRIMARY_IP.1235 > CLIENT_IP.1345: UDP, length 260 ``` ### Screenshots _No response_ ### 3x-ui version 3.7.0 ### Xray-core version 26.7.28 ### How did you install 3x-ui? install.sh script ### Operating system Ubuntu 24.04 LTS, x86_64 ### 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? 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:38:49 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Aug 30, 2026):

Confirmed — the panel never passes an AmneziaWG inbound's listen value into the embedded device at all, so the wildcard bind you captured with ss is exactly what the current code produces. Your reading of the symptom is right; the only correction is the location: AmneziaWG inbounds are deliberately excluded from the generated Xray config, so this is not in "Xray config generation".

Here is the whole path the value would have to travel, and where it stops:

  1. InstanceFromInbound builds the desired instance from the inbound row and copies only the port — ib.Listen is never read:
// internal/amneziawg/instance.go:61-66
return Instance{
    Id:                    ib.Id,
    Tag:                   ib.Tag,
    InterfaceName:         interfaceNameForID(ib.Id),
    ListenPort:            ib.Port,
  1. amneziawg.Instance (internal/amneziawg/types.go:73-124) has no bind-address field of any kind — ListenPort int at types.go:77 is the only listener-related member, so there is nothing for the runtime to carry even if it wanted to.

  2. The one socket-creating call site takes no address:

// internal/amneziawgnet/device.go:139
dev := device.NewDevice(tun, awgconn.NewDefaultBind(), logger)

and the UAPI config the device is configured with emits only a port:

// internal/amneziawgnet/device.go:197
fmt.Fprintf(&b, "listen_port=%d\n", inst.ListenPort)
  1. All three Desired{} construction sites (internal/web/job/amneziawg_job.go:39, internal/web/runtime/local.go:63 and :196) pass Instance plus DeviceOptions, and DeviceOptions (device.go:30-68) holds only obfuscation/timing fields. grep -rn "\.Listen" internal/amneziawg internal/amneziawgnet returns nothing outside tests.

For contrast, both of the other listener families do honour it. Xray-backed inbounds (including the native WireGuard protocol) get it written into the config:

// internal/database/model/model.go:344-348
listen := i.Listen
if listen == "" {
    listen = "0.0.0.0"
}

and the other non-Xray sidecar, MTProto, maps the same field onto its child process (internal/mtproto/manager.go:220 Listen: ib.Listen, consumed by bindTo() at manager.go:74-80). AmneziaWG is the only protocol that drops it silently — and internal/web/service/xray.go:177 (if inbound.Protocol == model.MTProto || inbound.Protocol == model.AmneziaWG { continue }) is why nothing further downstream ever sees it either.

Two things make the silence worse than a plain unimplemented field:

  • A routable listen is used as the advertised address elsewhere: internal/sub/service.go:1298-1316 puts it into the candidate list (and puts it first under the listen share-address strategy), so generated client configs get Endpoint = SECONDARY_IP:1235 for a server that never sources replies from that address — precisely the asymmetry in your tcpdump.
  • The port-conflict check treats two same-port inbounds with different non-empty listen values as non-conflicting (internal/web/service/port_conflict.go:94-99: return a == b), which for AmneziaWG is not true, since both bind the wildcard. I did not test that pairing, so treat the consequence as an inference rather than a confirmed second defect.

The per-client port-forward listeners have the same gap by construction — internal/amneziawgnet/portfwd.go:288 and portfwd_udp.go:49 both format ":%d".

Your iptables workaround is sound and is currently the only way to pin the reply source for this protocol; nothing in the panel can do it for you today.

Verdict: Confirmed bug
Severity: Medium
Confidence: High
Root cause: InstanceFromInbound in internal/amneziawg/instance.go:61-66 copies ib.Port into Instance.ListenPort and never reads ib.Listen; amneziawg.Instance (internal/amneziawg/types.go:73-124) has no bind-address field, so the single socket-creating call site internal/amneziawgnet/device.go:139 uses awgconn.NewDefaultBind() and buildUAPIConfig (device.go:197) emits only listen_port. Triggered by any AmneziaWG inbound with a non-empty listen; no input reaches a per-address bind. Introduced by effcccce "feat(amneziawg): add native AmneziaWG protocol support (#6105)" (2026-08-24), the only commit that has ever touched internal/amneziawgnet/device.go.
Already fixed: Still present on the default branch (HEAD f727d04f, v3.7.0 — the first release containing effcccce, per git tag --contains).
Duplicate of: None. Related: #6323 (AmneziaWG session dies after handshake) shares neither the bind path nor the root cause, so it is not the same defect.
Evidence: internal/amneziawg/instance.go:65 ListenPort: ib.Port,; internal/amneziawg/types.go:77 ListenPort int; internal/amneziawgnet/device.go:139 dev := device.NewDevice(tun, awgconn.NewDefaultBind(), logger); internal/amneziawgnet/device.go:197 fmt.Fprintf(&b, "listen_port=%d\n", inst.ListenPort); internal/database/model/model.go:344 listen := i.Listen (Xray path, for contrast); internal/mtproto/manager.go:220 Listen: ib.Listen (MTProto sidecar, for contrast); internal/web/service/xray.go:177 excludes AmneziaWG from the Xray config; internal/web/service/port_conflict.go:94-99 listenOverlaps; internal/sub/service.go:1306-1313. No comment or test anywhere in internal/amneziawg / internal/amneziawgnet documents or pins the wildcard bind as intended, and the frontend offers the field for every protocol without a caveat (frontend/src/pages/inbounds/form/InboundFormModal.tsx:639-644).
Not determined: Whether amneziawg-go v3.1.20260814's default bind implements sticky source addresses (IP_PKTINFO), which would decide whether a wildcard-bound socket could ever reply from the right address — I could not read the dependency's source in this environment, and your capture already shows it does not in practice. Reading conn/bind_std.go in that module would settle it; it does not change the verdict, since the panel supplies no address either way.

Suggested fix: carry the inbound's listen on amneziawg.Instance and open the device with a bind pinned to it instead of awgconn.NewDefaultBind() at internal/amneziawgnet/device.go:139, adding it to addressFingerprint (internal/amneziawgnet/manager.go:258) so an edit rebuilds the Device rather than trying to hot-apply it. If a per-address bind is not wanted for this protocol, the cheaper alternative is to reject a non-empty listen for model.AmneziaWG at save time so it fails loudly instead of silently.

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

<!-- gh-comment-id:5468829247 --> @github-actions[bot] commented on GitHub (Aug 30, 2026): Confirmed — the panel never passes an AmneziaWG inbound's `listen` value into the embedded device at all, so the wildcard bind you captured with `ss` is exactly what the current code produces. Your reading of the symptom is right; the only correction is the location: AmneziaWG inbounds are deliberately excluded from the generated Xray config, so this is not in "Xray config generation". Here is the whole path the value would have to travel, and where it stops: 1. `InstanceFromInbound` builds the desired instance from the inbound row and copies only the port — `ib.Listen` is never read: ```go // internal/amneziawg/instance.go:61-66 return Instance{ Id: ib.Id, Tag: ib.Tag, InterfaceName: interfaceNameForID(ib.Id), ListenPort: ib.Port, ``` 2. `amneziawg.Instance` (`internal/amneziawg/types.go:73-124`) has no bind-address field of any kind — `ListenPort int` at `types.go:77` is the only listener-related member, so there is nothing for the runtime to carry even if it wanted to. 3. The one socket-creating call site takes no address: ```go // internal/amneziawgnet/device.go:139 dev := device.NewDevice(tun, awgconn.NewDefaultBind(), logger) ``` and the UAPI config the device is configured with emits only a port: ```go // internal/amneziawgnet/device.go:197 fmt.Fprintf(&b, "listen_port=%d\n", inst.ListenPort) ``` 4. All three `Desired{}` construction sites (`internal/web/job/amneziawg_job.go:39`, `internal/web/runtime/local.go:63` and `:196`) pass `Instance` plus `DeviceOptions`, and `DeviceOptions` (`device.go:30-68`) holds only obfuscation/timing fields. `grep -rn "\.Listen" internal/amneziawg internal/amneziawgnet` returns nothing outside tests. For contrast, both of the other listener families do honour it. Xray-backed inbounds (including the native WireGuard protocol) get it written into the config: ```go // internal/database/model/model.go:344-348 listen := i.Listen if listen == "" { listen = "0.0.0.0" } ``` and the other non-Xray sidecar, MTProto, maps the same field onto its child process (`internal/mtproto/manager.go:220` `Listen: ib.Listen`, consumed by `bindTo()` at `manager.go:74-80`). AmneziaWG is the only protocol that drops it silently — and `internal/web/service/xray.go:177` (`if inbound.Protocol == model.MTProto || inbound.Protocol == model.AmneziaWG { continue }`) is why nothing further downstream ever sees it either. Two things make the silence worse than a plain unimplemented field: - A routable `listen` **is** used as the advertised address elsewhere: `internal/sub/service.go:1298-1316` puts it into the candidate list (and puts it first under the `listen` share-address strategy), so generated client configs get `Endpoint = SECONDARY_IP:1235` for a server that never sources replies from that address — precisely the asymmetry in your `tcpdump`. - The port-conflict check treats two same-port inbounds with different non-empty `listen` values as non-conflicting (`internal/web/service/port_conflict.go:94-99`: `return a == b`), which for AmneziaWG is not true, since both bind the wildcard. I did not test that pairing, so treat the consequence as an inference rather than a confirmed second defect. The per-client port-forward listeners have the same gap by construction — `internal/amneziawgnet/portfwd.go:288` and `portfwd_udp.go:49` both format `":%d"`. Your `iptables` workaround is sound and is currently the only way to pin the reply source for this protocol; nothing in the panel can do it for you today. Verdict: Confirmed bug Severity: Medium Confidence: High Root cause: `InstanceFromInbound` in `internal/amneziawg/instance.go:61-66` copies `ib.Port` into `Instance.ListenPort` and never reads `ib.Listen`; `amneziawg.Instance` (`internal/amneziawg/types.go:73-124`) has no bind-address field, so the single socket-creating call site `internal/amneziawgnet/device.go:139` uses `awgconn.NewDefaultBind()` and `buildUAPIConfig` (`device.go:197`) emits only `listen_port`. Triggered by any AmneziaWG inbound with a non-empty `listen`; no input reaches a per-address bind. Introduced by effcccce "feat(amneziawg): add native AmneziaWG protocol support (#6105)" (2026-08-24), the only commit that has ever touched `internal/amneziawgnet/device.go`. Already fixed: Still present on the default branch (HEAD f727d04f, v3.7.0 — the first release containing effcccce, per `git tag --contains`). Duplicate of: None. Related: #6323 (AmneziaWG session dies after handshake) shares neither the bind path nor the root cause, so it is not the same defect. Evidence: `internal/amneziawg/instance.go:65` `ListenPort: ib.Port,`; `internal/amneziawg/types.go:77` `ListenPort int`; `internal/amneziawgnet/device.go:139` `dev := device.NewDevice(tun, awgconn.NewDefaultBind(), logger)`; `internal/amneziawgnet/device.go:197` `fmt.Fprintf(&b, "listen_port=%d\n", inst.ListenPort)`; `internal/database/model/model.go:344` `listen := i.Listen` (Xray path, for contrast); `internal/mtproto/manager.go:220` `Listen: ib.Listen` (MTProto sidecar, for contrast); `internal/web/service/xray.go:177` excludes AmneziaWG from the Xray config; `internal/web/service/port_conflict.go:94-99` `listenOverlaps`; `internal/sub/service.go:1306-1313`. No comment or test anywhere in `internal/amneziawg` / `internal/amneziawgnet` documents or pins the wildcard bind as intended, and the frontend offers the field for every protocol without a caveat (`frontend/src/pages/inbounds/form/InboundFormModal.tsx:639-644`). Not determined: Whether `amneziawg-go` v3.1.20260814's default bind implements sticky source addresses (`IP_PKTINFO`), which would decide whether a wildcard-bound socket could ever reply from the right address — I could not read the dependency's source in this environment, and your capture already shows it does not in practice. Reading `conn/bind_std.go` in that module would settle it; it does not change the verdict, since the panel supplies no address either way. Suggested fix: carry the inbound's `listen` on `amneziawg.Instance` and open the device with a bind pinned to it instead of `awgconn.NewDefaultBind()` at `internal/amneziawgnet/device.go:139`, adding it to `addressFingerprint` (`internal/amneziawgnet/manager.go:258`) so an edit rebuilds the Device rather than trying to hot-apply it. If a per-address bind is not wanted for this protocol, the cheaper alternative is to reject a non-empty `listen` for `model.AmneziaWG` at save time so it fails loudly instead of silently. *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
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#4323
No description provided.