inbound_autorenew_shared_email_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package service
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  8. )
  9. func TestAutoRenewClients_UpdatesEveryInboundForSharedEmail(t *testing.T) {
  10. setupBulkDB(t)
  11. svc := &InboundService{}
  12. db := database.GetDB()
  13. past := time.Now().Add(-48 * time.Hour).UnixMilli()
  14. shared := model.Client{
  15. Email: "shared@x", ID: "11111111-1111-1111-1111-111111111111",
  16. Enable: false, Reset: 30, ExpiryTime: past,
  17. }
  18. ib1 := mkInbound(t, 30201, model.VLESS, clientsSettings(t, []model.Client{shared}))
  19. ib2 := mkInbound(t, 30202, model.VLESS, clientsSettings(t, []model.Client{shared}))
  20. for _, ib := range []*model.Inbound{ib1, ib2} {
  21. if err := svc.clientService.SyncInbound(nil, ib.Id, []model.Client{shared}); err != nil {
  22. t.Fatalf("SyncInbound %d: %v", ib.Id, err)
  23. }
  24. }
  25. if err := db.Create(&xray.ClientTraffic{
  26. InboundId: ib1.Id, Email: shared.Email, Enable: false,
  27. Up: 100, Down: 200, Reset: 30, ExpiryTime: past,
  28. }).Error; err != nil {
  29. t.Fatalf("seed client_traffics: %v", err)
  30. }
  31. batch := newTrafficMutationBatch()
  32. if _, count, err := svc.autoRenewClients(db, batch); err != nil {
  33. t.Fatalf("autoRenewClients: %v", err)
  34. } else if count != 1 {
  35. t.Fatalf("renewed count = %d, want 1 shared client", count)
  36. }
  37. var traffic xray.ClientTraffic
  38. if err := db.Where("email = ?", shared.Email).First(&traffic).Error; err != nil {
  39. t.Fatalf("read client_traffics: %v", err)
  40. }
  41. if !traffic.Enable || traffic.ExpiryTime <= time.Now().UnixMilli() {
  42. t.Fatalf("traffic state not renewed: enable=%v expiry=%d", traffic.Enable, traffic.ExpiryTime)
  43. }
  44. for _, ib := range []*model.Inbound{ib1, ib2} {
  45. reloaded, err := svc.GetInbound(ib.Id)
  46. if err != nil {
  47. t.Fatalf("GetInbound %d: %v", ib.Id, err)
  48. }
  49. clients, err := svc.GetClients(reloaded)
  50. if err != nil {
  51. t.Fatalf("GetClients %d: %v", ib.Id, err)
  52. }
  53. if len(clients) != 1 {
  54. t.Fatalf("inbound %d clients = %d, want 1", ib.Id, len(clients))
  55. }
  56. if !clients[0].Enable || clients[0].ExpiryTime != traffic.ExpiryTime {
  57. t.Errorf("inbound %d state = enable %v expiry %d, want true/%d", ib.Id, clients[0].Enable, clients[0].ExpiryTime, traffic.ExpiryTime)
  58. }
  59. }
  60. record, err := svc.clientService.GetRecordByEmail(nil, shared.Email)
  61. if err != nil {
  62. t.Fatalf("GetRecordByEmail: %v", err)
  63. }
  64. if !record.Enable || record.ExpiryTime != traffic.ExpiryTime {
  65. t.Errorf("clients row = enable %v expiry %d, want true/%d", record.Enable, record.ExpiryTime, traffic.ExpiryTime)
  66. }
  67. if len(batch.localPlans) != 2 {
  68. t.Errorf("runtime add plans = %d, want one for each inbound", len(batch.localPlans))
  69. }
  70. planCountByInbound := make(map[int]int, len(batch.localPlans))
  71. for _, plan := range batch.localPlans {
  72. planCountByInbound[plan.inbound.Id]++
  73. }
  74. for _, ib := range []*model.Inbound{ib1, ib2} {
  75. if planCountByInbound[ib.Id] != 1 {
  76. t.Errorf("inbound %d runtime add plans = %d, want 1", ib.Id, planCountByInbound[ib.Id])
  77. }
  78. }
  79. }
  80. func TestAutoRenewClients_PreservesOperatorDisabledClient(t *testing.T) {
  81. setupBulkDB(t)
  82. svc := &InboundService{}
  83. db := database.GetDB()
  84. past := time.Now().Add(-48 * time.Hour).UnixMilli()
  85. disabled := model.Client{
  86. Email: "disabled@x", ID: "22222222-2222-2222-2222-222222222222",
  87. Enable: false, Reset: 30, ExpiryTime: past,
  88. }
  89. ib := mkInbound(t, 30203, model.VLESS, clientsSettings(t, []model.Client{disabled}))
  90. if err := svc.clientService.SyncInbound(nil, ib.Id, []model.Client{disabled}); err != nil {
  91. t.Fatalf("SyncInbound: %v", err)
  92. }
  93. if err := db.Create(&xray.ClientTraffic{
  94. InboundId: ib.Id, Email: disabled.Email, Enable: true,
  95. Up: 100, Down: 200, Reset: 30, ExpiryTime: past,
  96. }).Error; err != nil {
  97. t.Fatalf("seed client_traffics: %v", err)
  98. }
  99. batch := newTrafficMutationBatch()
  100. if _, count, err := svc.autoRenewClients(db, batch); err != nil {
  101. t.Fatalf("autoRenewClients: %v", err)
  102. } else if count != 1 {
  103. t.Fatalf("renewed count = %d, want 1", count)
  104. }
  105. var traffic xray.ClientTraffic
  106. if err := db.Where("email = ?", disabled.Email).First(&traffic).Error; err != nil {
  107. t.Fatalf("read client_traffics: %v", err)
  108. }
  109. if !traffic.Enable || traffic.ExpiryTime <= time.Now().UnixMilli() {
  110. t.Fatalf("traffic state not renewed: enable=%v expiry=%d", traffic.Enable, traffic.ExpiryTime)
  111. }
  112. reloaded, err := svc.GetInbound(ib.Id)
  113. if err != nil {
  114. t.Fatalf("GetInbound: %v", err)
  115. }
  116. clients, err := svc.GetClients(reloaded)
  117. if err != nil {
  118. t.Fatalf("GetClients: %v", err)
  119. }
  120. if len(clients) != 1 {
  121. t.Fatalf("clients = %d, want 1", len(clients))
  122. }
  123. if clients[0].Enable {
  124. t.Error("operator-disabled client was enabled in inbound settings")
  125. }
  126. if clients[0].ExpiryTime != traffic.ExpiryTime {
  127. t.Errorf("settings expiry = %d, want %d", clients[0].ExpiryTime, traffic.ExpiryTime)
  128. }
  129. record, err := svc.clientService.GetRecordByEmail(nil, disabled.Email)
  130. if err != nil {
  131. t.Fatalf("GetRecordByEmail: %v", err)
  132. }
  133. if record.Enable {
  134. t.Error("operator-disabled client was enabled in clients table")
  135. }
  136. if record.ExpiryTime != traffic.ExpiryTime {
  137. t.Errorf("clients row expiry = %d, want %d", record.ExpiryTime, traffic.ExpiryTime)
  138. }
  139. if len(batch.localPlans) != 0 {
  140. t.Errorf("runtime add plans = %d, want 0", len(batch.localPlans))
  141. }
  142. }