From 04be3da8fc9bb6ba9f7f178400f667a499ef500c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E6=AC=A1=E5=85=83=E7=B3=95=E6=89=8B?= <188431084+Gaoshou101@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:45:27 +0800 Subject: [PATCH] fix(clients): sync client_traffics lifecycle when updating a client with no inbounds (#7173) Updating a client whose last inbound was detached rewrote the clients record but, in client_traffics, only re-asserted enable in the true direction. The auto-disable and renewal loops read expiry, quota and the reset schedule from that row, so they kept acting on the stale values and never disabled an expired client. The no-inbound branch now goes through UpdateClientStat, the helper every attached-inbound update already uses, so the whole enforcement row follows the edit and the usage counters are left alone. Fixes #7160 --- internal/web/service/client_crud.go | 12 ++-- .../service/client_update_no_inbound_test.go | 61 +++++++++++++++++++ 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/internal/web/service/client_crud.go b/internal/web/service/client_crud.go index 1f2d1a761..89f9a5614 100644 --- a/internal/web/service/client_crud.go +++ b/internal/web/service/client_crud.go @@ -942,14 +942,10 @@ func (s *ClientService) update(inboundSvc *InboundService, id int, updated model }).Error; err != nil { return needRestart, err } - // No inbound left, so UpdateClientStat never runs; re-assert the - // enforcement row's off switch here or a re-enabled client stays off. - if merged.Enable { - if err := database.GetDB().Model(xray.ClientTraffic{}). - Where("email = ?", existing.Email). - UpdateColumn("enable", true).Error; err != nil { - return needRestart, err - } + // No inbound left, so UpdateInboundClient never syncs the enforcement row the + // renewal and auto-disable loops read (#7160). + if err := inboundSvc.UpdateClientStat(database.GetDB(), existing.Email, merged.ToClient()); err != nil { + return needRestart, err } } diff --git a/internal/web/service/client_update_no_inbound_test.go b/internal/web/service/client_update_no_inbound_test.go index 993f848c4..9ce1c0bab 100644 --- a/internal/web/service/client_update_no_inbound_test.go +++ b/internal/web/service/client_update_no_inbound_test.go @@ -6,6 +6,7 @@ import ( "github.com/mhsanaei/3x-ui/v3/internal/database" "github.com/mhsanaei/3x-ui/v3/internal/database/model" + "github.com/mhsanaei/3x-ui/v3/internal/xray" ) func TestUpdate_PersistsFields_NoInbound(t *testing.T) { @@ -167,6 +168,66 @@ func TestUpdate_NoInbound_PreservesCredentialsWhenOmitted(t *testing.T) { } } +// The renewal and auto-disable loops act on client_traffics, which nothing else +// keeps in step once the client's last inbound is detached (#7160). +func TestUpdate_NoInbound_SyncsEnforcementRow(t *testing.T) { + setupBulkDB(t) + svc := &ClientService{} + inboundSvc := &InboundService{} + + email := "noib-enforcement@x" + rec := &model.ClientRecord{ + Email: email, + UUID: "11111111-1111-1111-1111-111111111111", + SubID: email, + TotalGB: 5, + ExpiryTime: 1000, + Reset: 1, + Enable: true, + } + if err := database.GetDB().Create(rec).Error; err != nil { + t.Fatalf("create record: %v", err) + } + if err := database.GetDB().Create(&xray.ClientTraffic{ + InboundId: 999, + Email: email, + Enable: true, + Up: 123, + Down: 456, + ExpiryTime: 1000, + Total: 5, + Reset: 1, + }).Error; err != nil { + t.Fatalf("create client_traffics row: %v", err) + } + + updated := rec.ToClient() + updated.Enable = false + updated.ExpiryTime = 1700000000 + updated.TotalGB = 10 + updated.Reset = 0 + updated.ResetDay = 15 + updated.ResetMax = 3 + if _, err := svc.Update(inboundSvc, rec.Id, *updated, rec.LimitHwid); err != nil { + t.Fatalf("Update: %v", err) + } + + var stat xray.ClientTraffic + if err := database.GetDB().Where("email = ?", email).First(&stat).Error; err != nil { + t.Fatalf("read client_traffics: %v", err) + } + type enforcement struct { + Enable bool + ExpiryTime, Total, Up, Down int64 + Reset, ResetDay, ResetMax int + } + got := enforcement{stat.Enable, stat.ExpiryTime, stat.Total, stat.Up, stat.Down, stat.Reset, stat.ResetDay, stat.ResetMax} + want := enforcement{false, 1700000000, 10, 123, 456, 0, 15, 3} + if got != want { + t.Fatalf("client_traffics row = %+v, want %+v", got, want) + } +} + func TestApplyClientRecordMerge_MirrorsSyncInboundRules(t *testing.T) { row := &model.ClientRecord{ UUID: "kept-uuid",