mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-26 09:12:10 +03:00
fix(clients): let an explicit 0 actually disable PersistentKeepalive
Addresses review feedback on the previous commit. UpdateInboundClient carries a stored keepalive forward whenever the incoming one is zero, so the settings JSON and the running peer survive a metadata-only edit that omits the field. That was a 0 -> 0 no-op while no UI could set a nonzero value. Now that the client form can, the carry-forward became reachable in the other direction: a client created at the form's default of 25 could never be returned to 0, and the hint text shipped to all 13 locales -- "0 disables it" -- described something the backend silently refused. The save even reported success, because a settings blob that came back byte-identical skips the transaction entirely. The zero value cannot carry that distinction, so model.Client.KeepAlive becomes *int: nil means the field was never sent, &0 means "send no keepalives". The pointer survives the internal marshal in ClientService.Update, which is where an explicit 0 was being erased by omitempty before UpdateInboundClient ever saw it. ClientRecord.KeepAlive stays a plain int -- it is the stored column, where "unset" has no meaning -- and the conversions bridge the two. Two tests, both red before this change in the direction they cover: an explicit 0 must reach wg_keep_alive, and an update that omits the field must still leave a stored 25 alone. Also adds the output transform every other numeric field in the client form already has, so a cleared box sends 0 rather than null.
This commit is contained in:
@@ -407,8 +407,8 @@ func (s *SubClashService) buildWireguardProxy(subReq *SubService, inbound *model
|
||||
if client.PreSharedKey != "" {
|
||||
proxy["pre-shared-key"] = client.PreSharedKey
|
||||
}
|
||||
if client.KeepAlive > 0 {
|
||||
proxy["persistent-keepalive"] = client.KeepAlive
|
||||
if ka := client.KeepAliveSeconds(); ka > 0 {
|
||||
proxy["persistent-keepalive"] = ka
|
||||
}
|
||||
for _, addr := range client.AllowedIPs {
|
||||
ip := stripCIDR(addr)
|
||||
|
||||
@@ -828,7 +828,7 @@ func TestBuildWireguardProxyForClash(t *testing.T) {
|
||||
Email: "user",
|
||||
PrivateKey: clientPriv,
|
||||
PreSharedKey: "psk-value",
|
||||
KeepAlive: 25,
|
||||
KeepAlive: model.KeepAlivePtr(25),
|
||||
AllowedIPs: []string{"10.0.0.2/32", "fd00::2/128"},
|
||||
}
|
||||
|
||||
|
||||
@@ -866,8 +866,8 @@ func (s *SubJsonService) genWireguard(inbound *model.Inbound, client model.Clien
|
||||
if client.PreSharedKey != "" {
|
||||
peer["preSharedKey"] = client.PreSharedKey
|
||||
}
|
||||
if client.KeepAlive > 0 {
|
||||
peer["keepAlive"] = client.KeepAlive
|
||||
if ka := client.KeepAliveSeconds(); ka > 0 {
|
||||
peer["keepAlive"] = ka
|
||||
}
|
||||
|
||||
settings := map[string]any{
|
||||
|
||||
@@ -380,7 +380,7 @@ func TestSubJsonServiceWireguard(t *testing.T) {
|
||||
Email: "user",
|
||||
PrivateKey: clientPriv,
|
||||
PreSharedKey: "psk-value",
|
||||
KeepAlive: 25,
|
||||
KeepAlive: model.KeepAlivePtr(25),
|
||||
AllowedIPs: []string{"10.0.0.2/32", "fd00::2/128"},
|
||||
}
|
||||
|
||||
|
||||
@@ -677,8 +677,8 @@ func (s *SubService) genWireguardLink(inbound *model.Inbound, email string) stri
|
||||
if client.PreSharedKey != "" {
|
||||
params["presharedkey"] = client.PreSharedKey
|
||||
}
|
||||
if client.KeepAlive > 0 {
|
||||
params["keepalive"] = strconv.Itoa(client.KeepAlive)
|
||||
if ka := client.KeepAliveSeconds(); ka > 0 {
|
||||
params["keepalive"] = strconv.Itoa(ka)
|
||||
}
|
||||
return buildLinkWithParams(link, params, s.genRemark(inbound, email, "", ""))
|
||||
}
|
||||
@@ -779,8 +779,8 @@ func amneziaWGConfigText(server *amneziawg.ServerSettings, client *model.Client,
|
||||
}
|
||||
b.WriteString("AllowedIPs = 0.0.0.0/0, ::/0\n")
|
||||
fmt.Fprintf(&b, "Endpoint = %s:%d", host, port)
|
||||
if client.KeepAlive > 0 {
|
||||
fmt.Fprintf(&b, "\nPersistentKeepalive = %d", client.KeepAlive)
|
||||
if ka := client.KeepAliveSeconds(); ka > 0 {
|
||||
fmt.Fprintf(&b, "\nPersistentKeepalive = %d", ka)
|
||||
}
|
||||
|
||||
return b.String()
|
||||
|
||||
@@ -214,7 +214,7 @@ func TestAmneziaWGConfigTextPeerFieldOrder(t *testing.T) {
|
||||
server := &amneziawg.ServerSettings{PublicKey: "serverPub", PrimaryDNS: "8.8.8.8", MTU: 1420}
|
||||
|
||||
t.Run("every optional field set", func(t *testing.T) {
|
||||
client := &model.Client{PrivateKey: "clientPriv", AllowedIPs: []string{"10.8.1.2/32"}, PreSharedKey: "psk", KeepAlive: 25}
|
||||
client := &model.Client{PrivateKey: "clientPriv", AllowedIPs: []string{"10.8.1.2/32"}, PreSharedKey: "psk", KeepAlive: model.KeepAlivePtr(25)}
|
||||
conf := amneziaWGConfigText(server, client, "203.0.113.7", 51820, "remark")
|
||||
if got := peerFields(t, conf); !slices.Equal(got, peerFieldOrder) {
|
||||
t.Fatalf("peer fields = %v, want %v\n%s", got, peerFieldOrder, conf)
|
||||
|
||||
Reference in New Issue
Block a user