[PR #6377] [MERGED] feat(clients): let admins set PersistentKeepalive on tunnel clients #6240

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6377
Author: @YoungReckless4
Created: 8/31/2026
Status: ✅ Merged
Merged: 9/10/2026
Merged by: @MHSanaei

Base: main ← Head: feat/amneziawg-client-keepalive


📝 Commits (4)

  • b70e17e feat(clients): let admins set PersistentKeepalive on tunnel clients
  • 112b19a fix(clients): let an explicit 0 actually disable PersistentKeepalive
  • be229a3 Merge branch 'main' into feat/amneziawg-client-keepalive
  • 5675ba6 fix(clients): repair the keepalive pointer conversion after the main merge

📊 Changes

33 files changed (+220 additions, -34 deletions)

View changed files

📝 docs/public/openapi.json (+2 -0)
📝 frontend/public/openapi.json (+2 -0)
📝 frontend/src/generated/examples.ts (+1 -1)
📝 frontend/src/generated/schemas.ts (+2 -0)
📝 frontend/src/generated/types.ts (+1 -1)
📝 frontend/src/generated/zod.ts (+1 -1)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+12 -0)
📝 internal/database/model/model.go (+26 -5)
📝 internal/database/model/model_wireguard_test.go (+21 -2)
📝 internal/sub/clash_service.go (+4 -4)
📝 internal/sub/clash_service_test.go (+2 -2)
📝 internal/sub/json_service.go (+2 -2)
📝 internal/sub/json_service_test.go (+1 -1)
📝 internal/sub/service.go (+4 -4)
📝 internal/sub/service_amneziawg_test.go (+1 -1)
📝 internal/web/runtime/local.go (+2 -2)
📝 internal/web/service/client_inbound_apply.go (+5 -5)
➕ internal/web/service/client_update_keepalive_test.go (+102 -0)
📝 internal/web/service/client_wireguard.go (+2 -2)
📝 internal/web/service/xray_wireguard_config_test.go (+1 -1)

...and 13 more files

📄 Description

The gap

model.Client already carries KeepAlive (column wg_keep_alive), the frontend
schema already declares it (schemas/protocols/inbound/amneziawg.ts), and every
client-config emitter already writes PersistentKeepalive when it is above zero —
amneziaWGConfigText in Go, buildAmneziaWGClientConfig and genAmneziaWGConfig
in TypeScript.

Nothing in the UI could set it. The client form has inputs for the private key,
public key, preshared key, allowed IPs and forwarded ports, but not this one, so the
value stayed at its 0 default and the line was never emitted for any client created
through the panel.

Why it matters

WireGuard only initiates a handshake when it has data to send. Without
PersistentKeepalive a peer that goes quiet has nothing to bring it back: it stays
disconnected after any interruption — a NAT mapping timing out, a device sleeping, the
panel restarting — until the user happens to generate traffic themselves. On a
UDP-obfuscated tunnel behind carrier NAT this is the common case, not the edge case.

The change

A Keepalive (seconds) field in the client form, next to the other tunnel fields.
New clients default to 25 — the conventional value, and short enough to sit inside the
~30s UDP NAT timeouts most carriers use. Existing clients keep whatever they have; the
form seeds from the stored value rather than overwriting it on save.

Translations added to all 13 locales — there is no English fallback, so a missing key
renders raw.

Making 0 actually mean 0

The hint text promises "0 disables it" and the backend refused it. ClientService.Update
marshals the client into the inbound settings JSON and UpdateInboundClient unmarshals
it again, so omitempty dropped an explicit 0 before the carry-forward — which treats a
zero KeepAlive as "field not provided" and restores the stored value — ever saw it.
The value could be raised, never lowered back to 0.

So model.Client.KeepAlive is now a *int: nil means the field was never sent, &0
means "send no keepalives". ClientRecord.KeepAlive stays a plain int — it is the
stored column, where "unset" has no meaning — and KeepAliveSeconds() / KeepAlivePtr()
bridge the two. A stored 0 converts back to nil, so the settings JSON keeps the shape it
has always had for the protocols that have no keepalive at all, rather than growing a
"keepAlive": 0 on every client.

API surface

keepAlive becomes number | null (nullable: true) on the client schema, which is the
semantics the field now has. Worth naming for REST callers: an explicit "keepAlive": 0
used to mean "leave it alone" and now means "disable keepalives" — omitting the key keeps
the old behaviour. Regenerated with make gen, and frontend/public/openapi.json copied
over docs/public/openapi.json.

Tests

  • TestUpdateCanClearKeepAliveOnAnExistingClient — 25 → an explicit 0 has to reach
    wg_keep_alive. Red without the fix: inbound keepAlive after an explicit 0 = 25.
  • TestUpdateWithoutKeepAlivePreservesTheStoredValue — an update that omits the field
    leaves a stored 25 alone, which is what the carry-forward exists for.
  • TestClientToClientOmitsUnsetKeepAlive — a record that never set a keepalive must not
    emit keepAlive into the settings JSON, which reaches xray-core verbatim through
    GenXrayInboundConfig.

The existing amneziawg-conf-parity.test.ts covers PersistentKeepalive's position in
the peer block and continues to pass. make verify is green.


🔄 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/6377 **Author:** [@YoungReckless4](https://github.com/YoungReckless4) **Created:** 8/31/2026 **Status:** ✅ Merged **Merged:** 9/10/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/amneziawg-client-keepalive` --- ### 📝 Commits (4) - [`b70e17e`](https://github.com/MHSanaei/3x-ui/commit/b70e17ed7312317d52bb2caf4c315cfd340ff055) feat(clients): let admins set PersistentKeepalive on tunnel clients - [`112b19a`](https://github.com/MHSanaei/3x-ui/commit/112b19a8ed338767d33ceacacd095f8a4e1f68ef) fix(clients): let an explicit 0 actually disable PersistentKeepalive - [`be229a3`](https://github.com/MHSanaei/3x-ui/commit/be229a31d87e797f007b3b8e87ae08aa686282ae) Merge branch 'main' into feat/amneziawg-client-keepalive - [`5675ba6`](https://github.com/MHSanaei/3x-ui/commit/5675ba6ff54ad032b2e23fee32fc04c4d4c914b0) fix(clients): repair the keepalive pointer conversion after the main merge ### 📊 Changes **33 files changed** (+220 additions, -34 deletions) <details> <summary>View changed files</summary> 📝 `docs/public/openapi.json` (+2 -0) 📝 `frontend/public/openapi.json` (+2 -0) 📝 `frontend/src/generated/examples.ts` (+1 -1) 📝 `frontend/src/generated/schemas.ts` (+2 -0) 📝 `frontend/src/generated/types.ts` (+1 -1) 📝 `frontend/src/generated/zod.ts` (+1 -1) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+12 -0) 📝 `internal/database/model/model.go` (+26 -5) 📝 `internal/database/model/model_wireguard_test.go` (+21 -2) 📝 `internal/sub/clash_service.go` (+4 -4) 📝 `internal/sub/clash_service_test.go` (+2 -2) 📝 `internal/sub/json_service.go` (+2 -2) 📝 `internal/sub/json_service_test.go` (+1 -1) 📝 `internal/sub/service.go` (+4 -4) 📝 `internal/sub/service_amneziawg_test.go` (+1 -1) 📝 `internal/web/runtime/local.go` (+2 -2) 📝 `internal/web/service/client_inbound_apply.go` (+5 -5) ➕ `internal/web/service/client_update_keepalive_test.go` (+102 -0) 📝 `internal/web/service/client_wireguard.go` (+2 -2) 📝 `internal/web/service/xray_wireguard_config_test.go` (+1 -1) _...and 13 more files_ </details> ### 📄 Description ### The gap `model.Client` already carries `KeepAlive` (column `wg_keep_alive`), the frontend schema already declares it (`schemas/protocols/inbound/amneziawg.ts`), and every client-config emitter already writes `PersistentKeepalive` when it is above zero — `amneziaWGConfigText` in Go, `buildAmneziaWGClientConfig` and `genAmneziaWGConfig` in TypeScript. Nothing in the UI could set it. The client form has inputs for the private key, public key, preshared key, allowed IPs and forwarded ports, but not this one, so the value stayed at its 0 default and the line was never emitted for any client created through the panel. ### Why it matters WireGuard only initiates a handshake when it has data to send. Without `PersistentKeepalive` a peer that goes quiet has nothing to bring it back: it stays disconnected after any interruption — a NAT mapping timing out, a device sleeping, the panel restarting — until the user happens to generate traffic themselves. On a UDP-obfuscated tunnel behind carrier NAT this is the common case, not the edge case. ### The change A `Keepalive (seconds)` field in the client form, next to the other tunnel fields. New clients default to 25 — the conventional value, and short enough to sit inside the ~30s UDP NAT timeouts most carriers use. Existing clients keep whatever they have; the form seeds from the stored value rather than overwriting it on save. Translations added to all 13 locales — there is no English fallback, so a missing key renders raw. ### Making 0 actually mean 0 The hint text promises "0 disables it" and the backend refused it. `ClientService.Update` marshals the client into the inbound settings JSON and `UpdateInboundClient` unmarshals it again, so `omitempty` dropped an explicit 0 before the carry-forward — which treats a zero `KeepAlive` as "field not provided" and restores the stored value — ever saw it. The value could be raised, never lowered back to 0. So `model.Client.KeepAlive` is now a `*int`: nil means the field was never sent, `&0` means "send no keepalives". `ClientRecord.KeepAlive` stays a plain `int` — it is the stored column, where "unset" has no meaning — and `KeepAliveSeconds()` / `KeepAlivePtr()` bridge the two. A stored 0 converts back to nil, so the settings JSON keeps the shape it has always had for the protocols that have no keepalive at all, rather than growing a `"keepAlive": 0` on every client. ### API surface `keepAlive` becomes `number | null` (`nullable: true`) on the client schema, which is the semantics the field now has. Worth naming for REST callers: an explicit `"keepAlive": 0` used to mean "leave it alone" and now means "disable keepalives" — omitting the key keeps the old behaviour. Regenerated with `make gen`, and `frontend/public/openapi.json` copied over `docs/public/openapi.json`. ### Tests - `TestUpdateCanClearKeepAliveOnAnExistingClient` — 25 → an explicit 0 has to reach `wg_keep_alive`. Red without the fix: `inbound keepAlive after an explicit 0 = 25`. - `TestUpdateWithoutKeepAlivePreservesTheStoredValue` — an update that omits the field leaves a stored 25 alone, which is what the carry-forward exists for. - `TestClientToClientOmitsUnsetKeepAlive` — a record that never set a keepalive must not emit `keepAlive` into the settings JSON, which reaches xray-core verbatim through `GenXrayInboundConfig`. The existing `amneziawg-conf-parity.test.ts` covers `PersistentKeepalive`'s position in the peer block and continues to pass. `make verify` is green. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:19 +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#6240
No description provided.