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
This commit is contained in:
二次元糕手
2026-11-24 07:45:27 +08:00
committed by GitHub
parent 5b0cbe6473
commit 04be3da8fc
2 changed files with 65 additions and 8 deletions
+4 -8
View File
@@ -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
}
}
@@ -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",