[PR #7088] fix(clients): re-add WireGuard peers with their keys instead of restarting Xray #6640

Open
opened 2026-11-07 14:34:20 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/7088
Author: @Dv2Corn
Created: 11/6/2026
Status: 🔄 Open

Base: main ← Head: fix/wireguard-readd-keys


📝 Commits (5)

  • 7e61c74 fix(traffic): re-add a quota-disabled WireGuard client on reset without restarting Xray
  • cbb530b fix(traffic): keep Xray running when auto-renew re-adds a WireGuard peer with keepAlive
  • 3f27a77 fix(clients): re-add WireGuard peers with their keys on bulk enable
  • 44d1b9e fix(inbounds): validate client emails and subIds on inbound create
  • 8837a4a fix(clients): push sidecar config after bulk enable flips the traffic rows

📊 Changes

7 files changed (+379 additions, -61 deletions)

View changed files

📝 internal/web/service/client_bulk.go (+28 -13)
📝 internal/web/service/client_crud.go (+12 -0)
📝 internal/web/service/client_inbound_apply.go (+2 -28)
➕ internal/web/service/client_reset_core_readd_test.go (+262 -0)
📝 internal/web/service/inbound.go (+6 -0)
➕ internal/web/service/inbound_add_client_email_test.go (+42 -0)
📝 internal/web/service/inbound_traffic.go (+27 -20)

📄 Description

Summary

Three paths re-add a client to the running core with an account map the core's WireGuard builder refuses. Each failed add falls back to a full Xray restart, which drops every connected user on the panel. This PR builds that map in one place, so the keys always go with it, and fixes a related gap where /inbounds/add and /inbounds/import store client emails and subIds that the client API refuses.

Why

All three restarts happen on a plain WireGuard inbound with default settings:

  1. Traffic reset of a quota-disabled client. resetClientTrafficLocked builds the map by hand without publicKey, allowedIPs, preSharedKey or keepAlive (inbound_traffic.go#L766-L775). AddUser fails with missing required user field "publicKey" (xray/api.go#L915), and the reset sets needRestart (inbound_traffic.go#L841-L843). The same call is also made for AmneziaWG, MTProto and TUIC inbounds, where AddUser changes nothing, so the re-enabled peer never reaches the running device.
  2. Auto-renew of a WireGuard peer with a keepalive. The renew path passes the stored client JSON through apiUserFromClient (inbound_traffic.go#L369). In that JSON keepAlive is a number, but the builder only accepts a string (xray/api.go#L938): invalid type for user field "keepAlive": float64. Every renewal of such a peer restarts Xray.
  3. Bulk enable. bulkSetEnableInboundClients builds its own map without the WireGuard keys (client_bulk.go#L1813-L1822), with the same result as (1). On AmneziaWG, MTProto and TUIC inbounds the bulk toggle has the same no-op problem as (1).

The single-client add and update paths already send the full map (client_inbound_apply.go#L694-L707, #L1157-L1170). The three paths above had simply fallen behind.

Separately, AddInbound (inbound.go#L1297) creates client records from the inbound's settings without running validateClientEmail / validateClientSubID (client_crud.go#L39-L54). As a result /panel/api/inbounds/add and /inbounds/import still accept the names . and .. that #7062 rejects on the client API, as well as emails and subIds containing / or whitespace. A subscription URL for such a subId can never be reached.

What changes:

  • New helper coreUserFromClient builds the account map, including publicKey, allowedIPs, preSharedKey and keepAlive. The reset, bulk enable and single-client add/update paths all use it.
  • The reset re-enable goes through applyTrafficMutationBatch, and bulk enable/disable calls applyLocalMtproto / applyLocalAmneziaWG / applyLocalTuic. Both already route the sidecar protocols to their reconcilers elsewhere, and the single-client update path does the same.
  • Bulk enable/disable pushes the sidecar inbounds only after the client_traffics rows are switched. buildInboundForLocalRuntime leaves out every client whose traffic row is off, so a push from the per-inbound fan-out, before BulkSetEnable flips those rows, still left a bulk-enabled client off the device until the 10-second reconcile job. The fan-out now only records the sidecar protocol, and BulkSetEnable pushes each such inbound under lockInbound after the traffic-row transaction.
  • apiUserFromClient converts a numeric keepAlive to the string the builder takes.
  • AddInbound validates every client's email and subId with the client API's rules. A push from the master (FromNodeSync) is exempt: the node takes what its master already holds, and refusing a legacy name there would only leave the node out of sync.

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?

The new tests go through the real xray.XrayAPI.AddUser account builder, using a stub HandlerService that accepts every call. AmneziaWG/MTProto/TUIC behave like the local runtime, where only UpdateInbound reaches the device.

  • internal/web/service/client_reset_core_readd_test.go
    • TestResetTrafficOfQuotaDisabledWireGuardPeerNeedsNoRestart: on main it fails with missing required user field "publicKey", and passes with the fix.
    • TestResetTrafficOfQuotaDisabledAmneziaWGPeerReachesTheDevice: on main no inbound reconcile is pushed; with the fix it passes.
    • TestAutoRenewOfWireGuardPeerWithKeepAliveNeedsNoRestart: on main it fails with invalid type for user field "keepAlive": float64, and passes with the fix.
    • TestBulkEnableOfWireGuardPeerNeedsNoRestart: on main it fails with missing required user field "publicKey", and passes with the fix.
    • TestBulkSetEnableOfSidecarClientReachesTheDevice: AmneziaWG, MTProto and TUIC subtests. Each switches an enabled client off in bulk and back on, so the enable starts from the state bulk disable leaves (traffic row off), and checks that both reach the device. On main nothing is pushed. With the push still in the per-inbound fan-out, all three fail with bulk enable=true never reached the <protocol> sidecar; pushed 1 updates; with the push after the traffic rows, all three pass.
  • internal/web/service/inbound_add_client_email_test.go
    • TestAddInboundRefusesClientNamesTheClientAPIRefuses covers ., .., a/b and the subId sub one. On main all four are accepted; with the fix all four are refused.
    • TestAddInboundFromMasterPushKeepsALegacyDotName: a master push with . is still stored. It passes both before and after.

Run with Go 1.27.3 and the internal/web/dist stub the CI uses:

  • go build ./..., go vet ./... and gofmt -l are clean.
  • go test -shuffle=on -count=1 ./... passes, apart from TestResetTrafficByEmail_ReenablesEveryInboundOfTheClient (from #6987). That test is flaky on main too: it failed 11 of 200 runs on main and 19 of 200 on this branch. It runs the two inbound resets side by side without the serial traffic writer the panel uses. The second reset can then read the record as still off after the first one has zeroed the counters, and it keeps the client off as if it had been disabled by hand. This PR does not touch it; starting the traffic writer in that test (resetTrafficWriterForTest(t) + StartTrafficWriter()) makes it stable.
  • go test -race -count=1 ./internal/web/service ./internal/web/controller passes.
  • After the bulk-enable ordering commit: go test -shuffle=on -count=1 ./internal/web/service and go test -race -count=1 ./internal/web/service pass; go vet and gofmt -l on the package are clean.

Screenshots / recordings

N/A

Breaking changes

None for clients the panel already accepts. /inbounds/add and /inbounds/import now refuse an inbound whose settings contain a client email or subId that /clients/add would refuse.

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/7088 **Author:** [@Dv2Corn](https://github.com/Dv2Corn) **Created:** 11/6/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/wireguard-readd-keys` --- ### 📝 Commits (5) - [`7e61c74`](https://github.com/MHSanaei/3x-ui/commit/7e61c74cade72d211e4a71ef603750b721a14209) fix(traffic): re-add a quota-disabled WireGuard client on reset without restarting Xray - [`cbb530b`](https://github.com/MHSanaei/3x-ui/commit/cbb530b3d7e353faec8d0241c63efbd3a2b34438) fix(traffic): keep Xray running when auto-renew re-adds a WireGuard peer with keepAlive - [`3f27a77`](https://github.com/MHSanaei/3x-ui/commit/3f27a77fc83d7860ffe8b2f762e87c519cf5857c) fix(clients): re-add WireGuard peers with their keys on bulk enable - [`44d1b9e`](https://github.com/MHSanaei/3x-ui/commit/44d1b9e8795939a8f3720e050e02a2f5c4e28d58) fix(inbounds): validate client emails and subIds on inbound create - [`8837a4a`](https://github.com/MHSanaei/3x-ui/commit/8837a4a493463da4491b83b8e21bcd70ddf22787) fix(clients): push sidecar config after bulk enable flips the traffic rows ### 📊 Changes **7 files changed** (+379 additions, -61 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/client_bulk.go` (+28 -13) 📝 `internal/web/service/client_crud.go` (+12 -0) 📝 `internal/web/service/client_inbound_apply.go` (+2 -28) ➕ `internal/web/service/client_reset_core_readd_test.go` (+262 -0) 📝 `internal/web/service/inbound.go` (+6 -0) ➕ `internal/web/service/inbound_add_client_email_test.go` (+42 -0) 📝 `internal/web/service/inbound_traffic.go` (+27 -20) </details> ### 📄 Description ## Summary Three paths re-add a client to the running core with an account map the core's WireGuard builder refuses. Each failed add falls back to a full Xray restart, which drops every connected user on the panel. This PR builds that map in one place, so the keys always go with it, and fixes a related gap where `/inbounds/add` and `/inbounds/import` store client emails and subIds that the client API refuses. ## Why All three restarts happen on a plain WireGuard inbound with default settings: 1. **Traffic reset of a quota-disabled client.** `resetClientTrafficLocked` builds the map by hand without `publicKey`, `allowedIPs`, `preSharedKey` or `keepAlive` ([inbound_traffic.go#L766-L775](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/inbound_traffic.go#L766-L775)). `AddUser` fails with `missing required user field "publicKey"` ([xray/api.go#L915](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/xray/api.go#L915)), and the reset sets `needRestart` ([inbound_traffic.go#L841-L843](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/inbound_traffic.go#L841-L843)). The same call is also made for AmneziaWG, MTProto and TUIC inbounds, where `AddUser` changes nothing, so the re-enabled peer never reaches the running device. 2. **Auto-renew of a WireGuard peer with a keepalive.** The renew path passes the stored client JSON through `apiUserFromClient` ([inbound_traffic.go#L369](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/inbound_traffic.go#L369)). In that JSON `keepAlive` is a number, but the builder only accepts a string ([xray/api.go#L938](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/xray/api.go#L938)): `invalid type for user field "keepAlive": float64`. Every renewal of such a peer restarts Xray. 3. **Bulk enable.** `bulkSetEnableInboundClients` builds its own map without the WireGuard keys ([client_bulk.go#L1813-L1822](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/client_bulk.go#L1813-L1822)), with the same result as (1). On AmneziaWG, MTProto and TUIC inbounds the bulk toggle has the same no-op problem as (1). The single-client add and update paths already send the full map ([client_inbound_apply.go#L694-L707](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/client_inbound_apply.go#L694-L707), [#L1157-L1170](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/client_inbound_apply.go#L1157-L1170)). The three paths above had simply fallen behind. Separately, `AddInbound` ([inbound.go#L1297](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/inbound.go#L1297)) creates client records from the inbound's settings without running `validateClientEmail` / `validateClientSubID` ([client_crud.go#L39-L54](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/client_crud.go#L39-L54)). As a result `/panel/api/inbounds/add` and `/inbounds/import` still accept the names `.` and `..` that #7062 rejects on the client API, as well as emails and subIds containing `/` or whitespace. A subscription URL for such a subId can never be reached. What changes: - New helper `coreUserFromClient` builds the account map, including `publicKey`, `allowedIPs`, `preSharedKey` and `keepAlive`. The reset, bulk enable and single-client add/update paths all use it. - The reset re-enable goes through `applyTrafficMutationBatch`, and bulk enable/disable calls `applyLocalMtproto` / `applyLocalAmneziaWG` / `applyLocalTuic`. Both already route the sidecar protocols to their reconcilers elsewhere, and the single-client update path does the same. - Bulk enable/disable pushes the sidecar inbounds only after the `client_traffics` rows are switched. `buildInboundForLocalRuntime` leaves out every client whose traffic row is off, so a push from the per-inbound fan-out, before `BulkSetEnable` flips those rows, still left a bulk-enabled client off the device until the 10-second reconcile job. The fan-out now only records the sidecar protocol, and `BulkSetEnable` pushes each such inbound under `lockInbound` after the traffic-row transaction. - `apiUserFromClient` converts a numeric `keepAlive` to the string the builder takes. - `AddInbound` validates every client's email and subId with the client API's rules. A push from the master (`FromNodeSync`) is exempt: the node takes what its master already holds, and refusing a legacy name there would only leave the node out of sync. ## 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) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [x] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? The new tests go through the real `xray.XrayAPI.AddUser` account builder, using a stub HandlerService that accepts every call. AmneziaWG/MTProto/TUIC behave like the local runtime, where only `UpdateInbound` reaches the device. - `internal/web/service/client_reset_core_readd_test.go` - `TestResetTrafficOfQuotaDisabledWireGuardPeerNeedsNoRestart`: on `main` it fails with `missing required user field "publicKey"`, and passes with the fix. - `TestResetTrafficOfQuotaDisabledAmneziaWGPeerReachesTheDevice`: on `main` no inbound reconcile is pushed; with the fix it passes. - `TestAutoRenewOfWireGuardPeerWithKeepAliveNeedsNoRestart`: on `main` it fails with `invalid type for user field "keepAlive": float64`, and passes with the fix. - `TestBulkEnableOfWireGuardPeerNeedsNoRestart`: on `main` it fails with `missing required user field "publicKey"`, and passes with the fix. - `TestBulkSetEnableOfSidecarClientReachesTheDevice`: AmneziaWG, MTProto and TUIC subtests. Each switches an enabled client off in bulk and back on, so the enable starts from the state bulk disable leaves (traffic row off), and checks that both reach the device. On `main` nothing is pushed. With the push still in the per-inbound fan-out, all three fail with `bulk enable=true never reached the <protocol> sidecar; pushed 1 updates`; with the push after the traffic rows, all three pass. - `internal/web/service/inbound_add_client_email_test.go` - `TestAddInboundRefusesClientNamesTheClientAPIRefuses` covers `.`, `..`, `a/b` and the subId `sub one`. On `main` all four are accepted; with the fix all four are refused. - `TestAddInboundFromMasterPushKeepsALegacyDotName`: a master push with `.` is still stored. It passes both before and after. Run with Go 1.27.3 and the `internal/web/dist` stub the CI uses: - `go build ./...`, `go vet ./...` and `gofmt -l` are clean. - `go test -shuffle=on -count=1 ./...` passes, apart from `TestResetTrafficByEmail_ReenablesEveryInboundOfTheClient` (from #6987). That test is flaky on `main` too: it failed 11 of 200 runs on `main` and 19 of 200 on this branch. It runs the two inbound resets side by side without the serial traffic writer the panel uses. The second reset can then read the record as still off after the first one has zeroed the counters, and it keeps the client off as if it had been disabled by hand. This PR does not touch it; starting the traffic writer in that test (`resetTrafficWriterForTest(t)` + `StartTrafficWriter()`) makes it stable. - `go test -race -count=1 ./internal/web/service ./internal/web/controller` passes. - After the bulk-enable ordering commit: `go test -shuffle=on -count=1 ./internal/web/service` and `go test -race -count=1 ./internal/web/service` pass; `go vet` and `gofmt -l` on the package are clean. ## Screenshots / recordings N/A ## Breaking changes None for clients the panel already accepts. `/inbounds/add` and `/inbounds/import` now refuse an inbound whose settings contain a client email or subId that `/clients/add` would refuse. ## 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>
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#6640
No description provided.