1
0

inbound_client_traffic_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/database"
  6. "github.com/mhsanaei/3x-ui/v3/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/xray"
  8. )
  9. // TestAddClientTraffic_MatchesDespiteStaleInboundId reproduces the production bug where
  10. // client_traffics rows survive an inbound delete+recreate with a stale inbound_id (the
  11. // shared-by-email row keeps the deleted inbound's id, and AddClientStat's OnConflict-
  12. // DoNothing never refreshes it). The old `inbound_id IN (local inbounds)` filter dropped
  13. // those rows, so local traffic and online status stopped updating. The fix matches by
  14. // email and only excludes rows owned by a node inbound, so a stale local row is still
  15. // updated while a genuine node-owned row is left untouched.
  16. func TestAddClientTraffic_MatchesDespiteStaleInboundId(t *testing.T) {
  17. dbDir := t.TempDir()
  18. t.Setenv("XUI_DB_FOLDER", dbDir)
  19. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  20. t.Fatalf("InitDB: %v", err)
  21. }
  22. t.Cleanup(func() { _ = database.CloseDB() })
  23. db := database.GetDB()
  24. const localEmail = "local-user"
  25. const nodeEmail = "node-user"
  26. // A local inbound exists, but the local client's traffic row points at an inbound id
  27. // that no longer exists (a deleted earlier incarnation) — the stale-pointer scenario.
  28. localInbound := &model.Inbound{UserId: 1, Tag: "local-in", Enable: true, Port: 40001, Protocol: model.VLESS}
  29. if err := db.Create(localInbound).Error; err != nil {
  30. t.Fatalf("create local inbound: %v", err)
  31. }
  32. nodeID := 1
  33. nodeInbound := &model.Inbound{UserId: 1, Tag: "node-in", Enable: true, Port: 40002, Protocol: model.VLESS, NodeID: &nodeID}
  34. if err := db.Create(nodeInbound).Error; err != nil {
  35. t.Fatalf("create node inbound: %v", err)
  36. }
  37. if err := db.Create(&xray.ClientTraffic{InboundId: 9999, Email: localEmail, Enable: true}).Error; err != nil {
  38. t.Fatalf("create stale local client_traffics: %v", err)
  39. }
  40. if err := db.Create(&xray.ClientTraffic{InboundId: nodeInbound.Id, Email: nodeEmail, Enable: true}).Error; err != nil {
  41. t.Fatalf("create node client_traffics: %v", err)
  42. }
  43. svc := InboundService{}
  44. err := svc.addClientTraffic(db, []*xray.ClientTraffic{
  45. {Email: localEmail, Up: 10, Down: 20},
  46. {Email: nodeEmail, Up: 30, Down: 40},
  47. })
  48. if err != nil {
  49. t.Fatalf("addClientTraffic: %v", err)
  50. }
  51. var local xray.ClientTraffic
  52. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", localEmail).First(&local).Error; err != nil {
  53. t.Fatalf("reload local row: %v", err)
  54. }
  55. if local.Up != 10 || local.Down != 20 {
  56. t.Errorf("stale-pointer local row not updated: up=%d down=%d, want 10/20", local.Up, local.Down)
  57. }
  58. if local.LastOnline == 0 {
  59. t.Errorf("stale-pointer local row LastOnline not set")
  60. }
  61. var node xray.ClientTraffic
  62. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", nodeEmail).First(&node).Error; err != nil {
  63. t.Fatalf("reload node row: %v", err)
  64. }
  65. if node.Up != 0 || node.Down != 0 {
  66. t.Errorf("node-owned row should not be touched by local traffic: up=%d down=%d, want 0/0", node.Up, node.Down)
  67. }
  68. }