inbound_client_traffic_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "time"
  6. "github.com/mhsanaei/3x-ui/v3/database"
  7. "github.com/mhsanaei/3x-ui/v3/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/xray"
  9. )
  10. // TestAddClientTraffic_MatchesDespiteStaleInboundId reproduces the production bug where
  11. // client_traffics rows survive an inbound delete+recreate with a stale inbound_id (the
  12. // shared-by-email row keeps the deleted inbound's id, and AddClientStat's OnConflict-
  13. // DoNothing never refreshes it). The old `inbound_id IN (local inbounds)` filter dropped
  14. // those rows, so local traffic and online status stopped updating. The fix matches by
  15. // email and only excludes rows owned by a node inbound, so a stale local row is still
  16. // updated while a genuine node-owned row is left untouched.
  17. func TestAddClientTraffic_MatchesDespiteStaleInboundId(t *testing.T) {
  18. dbDir := t.TempDir()
  19. t.Setenv("XUI_DB_FOLDER", dbDir)
  20. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  21. t.Fatalf("InitDB: %v", err)
  22. }
  23. t.Cleanup(func() { _ = database.CloseDB() })
  24. db := database.GetDB()
  25. const localEmail = "local-user"
  26. const nodeEmail = "node-user"
  27. // A local inbound exists, but the local client's traffic row points at an inbound id
  28. // that no longer exists (a deleted earlier incarnation) — the stale-pointer scenario.
  29. localInbound := &model.Inbound{UserId: 1, Tag: "local-in", Enable: true, Port: 40001, Protocol: model.VLESS}
  30. if err := db.Create(localInbound).Error; err != nil {
  31. t.Fatalf("create local inbound: %v", err)
  32. }
  33. nodeID := 1
  34. nodeInbound := &model.Inbound{UserId: 1, Tag: "node-in", Enable: true, Port: 40002, Protocol: model.VLESS, NodeID: &nodeID}
  35. if err := db.Create(nodeInbound).Error; err != nil {
  36. t.Fatalf("create node inbound: %v", err)
  37. }
  38. if err := db.Create(&xray.ClientTraffic{InboundId: 9999, Email: localEmail, Enable: true}).Error; err != nil {
  39. t.Fatalf("create stale local client_traffics: %v", err)
  40. }
  41. if err := db.Create(&xray.ClientTraffic{InboundId: nodeInbound.Id, Email: nodeEmail, Enable: true}).Error; err != nil {
  42. t.Fatalf("create node client_traffics: %v", err)
  43. }
  44. svc := InboundService{}
  45. err := svc.addClientTraffic(db, []*xray.ClientTraffic{
  46. {Email: localEmail, Up: 10, Down: 20},
  47. {Email: nodeEmail, Up: 30, Down: 40},
  48. })
  49. if err != nil {
  50. t.Fatalf("addClientTraffic: %v", err)
  51. }
  52. var local xray.ClientTraffic
  53. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", localEmail).First(&local).Error; err != nil {
  54. t.Fatalf("reload local row: %v", err)
  55. }
  56. if local.Up != 10 || local.Down != 20 {
  57. t.Errorf("stale-pointer local row not updated: up=%d down=%d, want 10/20", local.Up, local.Down)
  58. }
  59. if local.LastOnline == 0 {
  60. t.Errorf("stale-pointer local row LastOnline not set")
  61. }
  62. var node xray.ClientTraffic
  63. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", nodeEmail).First(&node).Error; err != nil {
  64. t.Fatalf("reload node row: %v", err)
  65. }
  66. if node.Up != 0 || node.Down != 0 {
  67. t.Errorf("node-owned row should not be touched by local traffic: up=%d down=%d, want 0/0", node.Up, node.Down)
  68. }
  69. }
  70. // TestAdjustTraffics_DelayedStartConvertsDespiteStaleInboundId covers "Start After
  71. // First Use": a delayed-start client carries a negative expiry (the duration) that
  72. // must convert to an absolute deadline on its first traffic tick. When the client's
  73. // email-keyed client_traffics row still points at a deleted inbound (stale inbound_id
  74. // after an inbound delete+recreate), the conversion used to resolve no inbound and
  75. // silently skip, leaving the client perpetually "not started". The fix resolves the
  76. // owning inbound via the client_inbounds link instead.
  77. func TestAdjustTraffics_DelayedStartConvertsDespiteStaleInboundId(t *testing.T) {
  78. dbDir := t.TempDir()
  79. t.Setenv("XUI_DB_FOLDER", dbDir)
  80. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  81. t.Fatalf("InitDB: %v", err)
  82. }
  83. t.Cleanup(func() { _ = database.CloseDB() })
  84. db := database.GetDB()
  85. const email = "delayed-user"
  86. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0d001"
  87. const sevenDays = int64(7 * 86400000)
  88. client := model.Client{Email: email, ID: uid, Auth: uid, Enable: true, ExpiryTime: -sevenDays}
  89. inbound := &model.Inbound{
  90. Tag: "vless-delayed", Enable: true, Port: 45001, Protocol: model.VLESS,
  91. StreamSettings: `{"network":"tcp","security":"reality"}`,
  92. Settings: clientsSettings(t, []model.Client{client}),
  93. }
  94. if err := db.Create(inbound).Error; err != nil {
  95. t.Fatalf("create inbound: %v", err)
  96. }
  97. svc := InboundService{}
  98. if err := svc.clientService.SyncInbound(db, inbound.Id, []model.Client{client}); err != nil {
  99. t.Fatalf("SyncInbound: %v", err)
  100. }
  101. // The email-keyed traffic row survives an inbound delete+recreate pointing at a
  102. // dead inbound id; client_inbounds still links the client to the live inbound.
  103. if err := db.Create(&xray.ClientTraffic{InboundId: 9999, Email: email, Enable: true, ExpiryTime: -sevenDays}).Error; err != nil {
  104. t.Fatalf("create stale traffic row: %v", err)
  105. }
  106. before := time.Now().UnixMilli()
  107. if err := svc.addClientTraffic(db, []*xray.ClientTraffic{{Email: email, Up: 100, Down: 200}}); err != nil {
  108. t.Fatalf("addClientTraffic: %v", err)
  109. }
  110. var row xray.ClientTraffic
  111. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).First(&row).Error; err != nil {
  112. t.Fatalf("reload traffic row: %v", err)
  113. }
  114. if row.ExpiryTime <= 0 {
  115. t.Fatalf("delayed-start expiry not converted: still %d (stale inbound_id skipped the conversion)", row.ExpiryTime)
  116. }
  117. if row.ExpiryTime < before+sevenDays-5000 || row.ExpiryTime > before+sevenDays+5000 {
  118. t.Errorf("converted expiry = %d, want ~now+7d (%d)", row.ExpiryTime, before+sevenDays)
  119. }
  120. reloaded, err := svc.GetInbound(inbound.Id)
  121. if err != nil {
  122. t.Fatalf("GetInbound: %v", err)
  123. }
  124. cs, err := svc.GetClients(reloaded)
  125. if err != nil {
  126. t.Fatalf("GetClients: %v", err)
  127. }
  128. if len(cs) != 1 || cs[0].ExpiryTime <= 0 {
  129. t.Errorf("inbound settings expiry not converted: %#v", cs)
  130. }
  131. }