[PR #5684] [MERGED] fix(wireguard): build peers in GenXrayInboundConfig so node reconcile keeps clients #5960

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5684
Author: @STRENCH0
Created: 6/30/2026
Status: ✅ Merged
Merged: 7/8/2026
Merged by: @MHSanaei

Base: main ← Head: fix/wireguard-node-reconcile-peers


📝 Commits (2)

  • 0072f8a fix(wireguard): build peers in GenXrayInboundConfig so node reconcile keeps clients
  • 34119e0 Merge branch 'main' into fix/wireguard-node-reconcile-peers

📊 Changes

3 files changed (+177 additions, -14 deletions)

View changed files

📝 internal/database/model/model.go (+79 -0)
➕ internal/database/model/model_wireguard_peers_test.go (+97 -0)
📝 internal/web/service/xray.go (+1 -14)

📄 Description

Adding a WireGuard client on the master broke every WireGuard connection on the sub-node until Xray was manually restarted on the node. Adding the same client directly on the node worked.

Root cause: the panel stores WireGuard clients under the settings key clients (the shape every other protocol uses), but xray-core's wireguard inbound is configured with peers. The clients->peers conversion lived only in the full-config generation path (XrayService.GetXrayConfig), which runs on a full Xray restart. The live gRPC AddInbound path goes through (*Inbound).GenXrayInboundConfig, which passed the WireGuard settings verbatim

  • with clients and no peers.

Why the master path broke it and the node path did not:

  • Adding on the node is a single safe operation: AddInboundClient -> AddUser -> AlterInbound{AddUser} -> wireguard.Server.AddUser, which appends one peer via IPC without touching the others. The inbound is local (NodeID == nil), so nothing is marked dirty and no reconcile runs.
  • Adding on the master does two things: it pushes the client to the node (the same safe hot-add, which succeeds), and it marks the node dirty. The reconcile then pushes panel/api/inbounds/update/:id to the node, whose InboundService.UpdateInbound applies it live via DelInbound + AddInbound (buildRuntimeInboundForAPI -> Local.AddInbound -> GenXrayInboundConfig). That re-adds the wireguard inbound with zero peers, wiping the device and dropping every connected client. A manual restart regenerated the full config, converted clients to peers, and restored them - hence "only a restart fixes it".

Fix: Convert WireGuard clients to peers in GenXrayInboundConfig itself, the single
chokepoint for every live AddInbound (create, edit, node reconcile). Clients are the
source of truth: WireguardClientsToPeers always rebuilds peers from clients
(matching GetXrayConfig field for field) and drops the clients key.

Summary

WireGuard inbounds hosted on a sub-node now keep working after a client is added
from the master. Previously, adding a WireGuard client on the master dropped every
WireGuard connection on the sub-node (across all clients of the inbound) until Xray
was manually restarted on the node. Adding the same client directly on the node worked.

Why

The panel stores WireGuard clients under settings.clients (the shape every other protocol uses), but xray-core's wireguard inbound is configured with settings.peers. The clients→peers conversion lived only in the full-config path (XrayService.GetXrayConfig), which runs on a full Xray restart. The live gRPC AddInbound path goes through (*Inbound).GenXrayInboundConfig, which passed the WireGuard settings verbatim — with clients and no peers.

Adding a client on the master does two things: it pushes the client to the node (a safe single-peer hot-add via AlterInbound{AddUser}, which succeeds) and it marks the node dirty. The reconcile then pushes panel/api/inbounds/update/:id to the node, whose InboundService.UpdateInbound (the inbound is local there, NodeID == nil) applies it live via DelInbound + AddInbound (buildRuntimeInboundForAPI → Local.AddInbound → GenXrayInboundConfig). That re-adds the wireguard inbound with zero peers, wiping the device and dropping every connected client — hence "only a restart fixes it".

Adding the same client directly on the node worked, because that path only does the safe single-peer hot-add (the inbound is not marked dirty, so no reconcile and no peerless rebuild). The same peerless rebuild also affected plain WireGuard inbound edits on a standalone panel.

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?

New unit tests in internal/database/model, mirroring the existing settings-transform helper tests (StripVlessInboundEncryption, HealShadowsocksClientMethods):

  • TestWireguardClientsToPeers — asserts the conversion drops clients, emits peers with the correct fields (publicKey, allowedIPs, preSharedKey, keepAlive), preserves secretKey, and skips disabled clients.
  • TestWireguardClientsToPeersIdempotent — asserts the no-op paths (peers already present / clients absent), so the full-config path that already populated peers is unaffected.
  • TestGenXrayInboundConfigWireguardConvertsPeers — end-to-end through GenXrayInboundConfig; verified red without the fix (clients persisted, peers was nil).

The emitted peer shape was verified field-for-field against the peers map built by GetXrayConfig.

Full local gate passed:

  • make verify (gen-check, lint, typecheck, test, build) — OK
  • golangci-lint run ./internal/database/model/ — 0 issues

Manual testing — two-panel master/sub-node with a real WireGuard client:

  • Built the panel image from this branch and ran two isolated instances via
    docker compose: a master and a sub-node on one bridge network (each
    container in its own netns, so no Xray API / sub-port collisions), plus a third
    container as a pure WireGuard client (wireguard-tools, /dev/net/tun).
  • Linked the node to the master over its API token, created a WireGuard inbound
    hosted on the node, and added client A from the master.
  • Before the fix (peers-present guard): client A never completed a handshake — the
    node's live device had zero peers (0 B received). A client added directly on the
    node
    worked, but re-adding any client from the master then dropped it to 100%
    packet loss until Xray was restarted on the node — exactly the reported bug.
  • After the fix: brought up client A's tunnel from the client container and
    confirmed handshake, ping to the tunnel address 10.0.0.1, and egress
    (curl routed through the node). Then added a second client B
    from the master to trigger the node reconcile while pinging continuously: the tunnel
    showed only a brief blip during the DelInbound+AddInbound device rebuild
    (~15 dropped packets) and recovered on its own — existing peers preserved. On the
    unfixed build the same step dropped the tunnel permanently.

Screenshots / recordings

N/A — no UI changes.

Breaking changes

None.

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.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/5684 **Author:** [@STRENCH0](https://github.com/STRENCH0) **Created:** 6/30/2026 **Status:** ✅ Merged **Merged:** 7/8/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/wireguard-node-reconcile-peers` --- ### 📝 Commits (2) - [`0072f8a`](https://github.com/MHSanaei/3x-ui/commit/0072f8a3ac2ce9eb2b858d1623f0d4666c7e0353) fix(wireguard): build peers in GenXrayInboundConfig so node reconcile keeps clients - [`34119e0`](https://github.com/MHSanaei/3x-ui/commit/34119e08260f4a4898aab9fc8acebe9559fa1376) Merge branch 'main' into fix/wireguard-node-reconcile-peers ### 📊 Changes **3 files changed** (+177 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `internal/database/model/model.go` (+79 -0) ➕ `internal/database/model/model_wireguard_peers_test.go` (+97 -0) 📝 `internal/web/service/xray.go` (+1 -14) </details> ### 📄 Description Adding a WireGuard client on the master broke every WireGuard connection on the sub-node until Xray was manually restarted on the node. Adding the same client directly on the node worked. Root cause: the panel stores WireGuard clients under the settings key `clients` (the shape every other protocol uses), but xray-core's wireguard inbound is configured with `peers`. The `clients`->`peers` conversion lived only in the full-config generation path (XrayService.GetXrayConfig), which runs on a full Xray restart. The live gRPC AddInbound path goes through (*Inbound).GenXrayInboundConfig, which passed the WireGuard settings verbatim - with `clients` and no `peers`. Why the master path broke it and the node path did not: - Adding on the node is a single safe operation: AddInboundClient -> AddUser -> AlterInbound{AddUser} -> wireguard.Server.AddUser, which appends one peer via IPC without touching the others. The inbound is local (NodeID == nil), so nothing is marked dirty and no reconcile runs. - Adding on the master does two things: it pushes the client to the node (the same safe hot-add, which succeeds), and it marks the node dirty. The reconcile then pushes panel/api/inbounds/update/:id to the node, whose InboundService.UpdateInbound applies it live via DelInbound + AddInbound (buildRuntimeInboundForAPI -> Local.AddInbound -> GenXrayInboundConfig). That re-adds the wireguard inbound with zero peers, wiping the device and dropping every connected client. A manual restart regenerated the full config, converted clients to peers, and restored them - hence "only a restart fixes it". Fix: Convert WireGuard `clients` to `peers` in `GenXrayInboundConfig` itself, the single chokepoint for every live AddInbound (create, edit, node reconcile). Clients are the source of truth: `WireguardClientsToPeers` always rebuilds `peers` from `clients` (matching `GetXrayConfig` field for field) and drops the `clients` key. ## Summary WireGuard inbounds hosted on a sub-node now keep working after a client is added from the master. Previously, adding a WireGuard client on the master dropped every WireGuard connection on the sub-node (across all clients of the inbound) until Xray was manually restarted on the node. Adding the same client directly on the node worked. ## Why The panel stores WireGuard clients under `settings.clients` (the shape every other protocol uses), but xray-core's wireguard inbound is configured with `settings.peers`. The `clients`→`peers` conversion lived **only** in the full-config path (`XrayService.GetXrayConfig`), which runs on a full Xray restart. The live gRPC `AddInbound` path goes through `(*Inbound).GenXrayInboundConfig`, which passed the WireGuard settings verbatim — with `clients` and no `peers`. Adding a client on the master does two things: it pushes the client to the node (a safe single-peer hot-add via `AlterInbound{AddUser}`, which succeeds) **and** it marks the node dirty. The reconcile then pushes `panel/api/inbounds/update/:id` to the node, whose `InboundService.UpdateInbound` (the inbound is local there, `NodeID == nil`) applies it live via `DelInbound` + `AddInbound` (`buildRuntimeInboundForAPI` → `Local.AddInbound` → `GenXrayInboundConfig`). That re-adds the wireguard inbound with **zero peers**, wiping the device and dropping every connected client — hence "only a restart fixes it". Adding the same client **directly on the node** worked, because that path only does the safe single-peer hot-add (the inbound is not marked dirty, so no reconcile and no peerless rebuild). The same peerless rebuild also affected plain WireGuard inbound **edits** on a standalone panel. ## 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) - [ ] Backend (API endpoints, login, settings) - [x] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [x] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? New unit tests in `internal/database/model`, mirroring the existing settings-transform helper tests (`StripVlessInboundEncryption`, `HealShadowsocksClientMethods`): - `TestWireguardClientsToPeers` — asserts the conversion drops `clients`, emits `peers` with the correct fields (`publicKey`, `allowedIPs`, `preSharedKey`, `keepAlive`), preserves `secretKey`, and **skips disabled clients**. - `TestWireguardClientsToPeersIdempotent` — asserts the no-op paths (peers already present / clients absent), so the full-config path that already populated peers is unaffected. - `TestGenXrayInboundConfigWireguardConvertsPeers` — end-to-end through `GenXrayInboundConfig`; verified **red without the fix** (`clients` persisted, `peers` was `nil`). The emitted peer shape was verified field-for-field against the `peers` map built by `GetXrayConfig`. Full local gate passed: - `make verify` (gen-check, lint, typecheck, test, build) — OK - `golangci-lint run ./internal/database/model/` — 0 issues **Manual testing — two-panel master/sub-node with a real WireGuard client:** - Built the panel image from this branch and ran two isolated instances via `docker compose`: a **master** and a **sub-node** on one bridge network (each container in its own netns, so no Xray API / sub-port collisions), plus a third container as a **pure WireGuard client** (`wireguard-tools`, `/dev/net/tun`). - Linked the node to the master over its API token, created a WireGuard inbound **hosted on the node**, and added client **A** from the master. - **Before the fix** (peers-present guard): client A never completed a handshake — the node's live device had zero peers (`0 B received`). A client added *directly on the node* worked, but re-adding any client *from the master* then dropped it to 100% packet loss until Xray was restarted on the node — exactly the reported bug. - **After the fix:** brought up client A's tunnel from the client container and confirmed handshake, ping to the tunnel address `10.0.0.1`, and egress (`curl` routed through the node). Then added a second client **B** from the master to trigger the node reconcile while pinging continuously: the tunnel showed only a brief blip during the `DelInbound`+`AddInbound` device rebuild (~15 dropped packets) and recovered on its own — existing peers preserved. On the unfixed build the same step dropped the tunnel permanently. ## Screenshots / recordings N/A — no UI changes. ## Breaking changes None. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:05:18 +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#5960
No description provided.