1
0

client_apply_field_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "encoding/json"
  4. "path/filepath"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  10. )
  11. // TestResetClientExpiryTimeByEmail_MultiInbound reproduces #5039: a client
  12. // attached to several inbounds had its expiry patched only on the first
  13. // inbound's JSON, so the stale siblings reverted the change on the next sync.
  14. func TestResetClientExpiryTimeByEmail_MultiInbound(t *testing.T) {
  15. dbDir := t.TempDir()
  16. t.Setenv("XUI_DB_FOLDER", dbDir)
  17. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  18. db := database.GetDB()
  19. const email = "[email protected]"
  20. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c111"
  21. const oldExpiry = int64(1700000000000)
  22. const newExpiry = int64(1800000000000)
  23. clientJSON := func(expiry int64) string {
  24. b, _ := json.Marshal(map[string]any{"clients": []map[string]any{{
  25. "email": email, "id": uid, "enable": true, "expiryTime": expiry, "subId": "sub-multi-1",
  26. }}})
  27. return string(b)
  28. }
  29. first := &model.Inbound{
  30. Tag: "vless-a", Enable: true, Port: 50001, Protocol: model.VLESS,
  31. StreamSettings: `{"network":"tcp","security":"reality"}`, Settings: clientJSON(oldExpiry),
  32. }
  33. second := &model.Inbound{
  34. Tag: "vless-b", Enable: true, Port: 50002, Protocol: model.VLESS,
  35. StreamSettings: `{"network":"ws","security":"tls"}`, Settings: clientJSON(oldExpiry),
  36. }
  37. for _, ib := range []*model.Inbound{first, second} {
  38. if err := db.Create(ib).Error; err != nil {
  39. t.Fatalf("create inbound %s: %v", ib.Tag, err)
  40. }
  41. }
  42. clientSvc := ClientService{}
  43. inboundSvc := InboundService{}
  44. for _, ib := range []*model.Inbound{first, second} {
  45. clients, err := inboundSvc.GetClients(ib)
  46. if err != nil {
  47. t.Fatalf("GetClients(%s): %v", ib.Tag, err)
  48. }
  49. if err := clientSvc.SyncInbound(nil, ib.Id, clients); err != nil {
  50. t.Fatalf("SyncInbound(%s): %v", ib.Tag, err)
  51. }
  52. }
  53. if _, err := clientSvc.ResetClientExpiryTimeByEmail(&inboundSvc, email, newExpiry); err != nil {
  54. t.Fatalf("ResetClientExpiryTimeByEmail: %v", err)
  55. }
  56. for _, ib := range []*model.Inbound{first, second} {
  57. fresh, err := inboundSvc.GetInbound(ib.Id)
  58. if err != nil {
  59. t.Fatalf("GetInbound(%s): %v", ib.Tag, err)
  60. }
  61. clients, err := inboundSvc.GetClients(fresh)
  62. if err != nil {
  63. t.Fatalf("GetClients(%s): %v", ib.Tag, err)
  64. }
  65. if len(clients) != 1 || clients[0].ExpiryTime != newExpiry {
  66. t.Errorf("inbound %s settings expiry = %d, want %d (#5039)", ib.Tag, clients[0].ExpiryTime, newExpiry)
  67. }
  68. }
  69. rec, err := clientSvc.GetRecordByEmail(nil, email)
  70. if err != nil {
  71. t.Fatalf("GetRecordByEmail: %v", err)
  72. }
  73. if rec.ExpiryTime != newExpiry {
  74. t.Errorf("client record expiry = %d, want %d", rec.ExpiryTime, newExpiry)
  75. }
  76. }
  77. func TestSetClientEnableByEmail_MultiInbound(t *testing.T) {
  78. dbDir := t.TempDir()
  79. t.Setenv("XUI_DB_FOLDER", dbDir)
  80. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  81. db := database.GetDB()
  82. const email = "[email protected]"
  83. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c222"
  84. clientJSON := `{"clients":[{"email":"` + email + `","id":"` + uid + `","enable":true,"subId":"sub-en-1"}]}`
  85. first := &model.Inbound{
  86. Tag: "vless-en-a", Enable: true, Port: 50011, Protocol: model.VLESS,
  87. StreamSettings: `{"network":"tcp","security":"reality"}`, Settings: clientJSON,
  88. }
  89. second := &model.Inbound{
  90. Tag: "vless-en-b", Enable: true, Port: 50012, Protocol: model.VLESS,
  91. StreamSettings: `{"network":"ws","security":"tls"}`, Settings: clientJSON,
  92. }
  93. for _, ib := range []*model.Inbound{first, second} {
  94. if err := db.Create(ib).Error; err != nil {
  95. t.Fatalf("create inbound %s: %v", ib.Tag, err)
  96. }
  97. }
  98. clientSvc := ClientService{}
  99. inboundSvc := InboundService{}
  100. for _, ib := range []*model.Inbound{first, second} {
  101. clients, err := inboundSvc.GetClients(ib)
  102. if err != nil {
  103. t.Fatalf("GetClients(%s): %v", ib.Tag, err)
  104. }
  105. if err := clientSvc.SyncInbound(nil, ib.Id, clients); err != nil {
  106. t.Fatalf("SyncInbound(%s): %v", ib.Tag, err)
  107. }
  108. }
  109. if err := db.Create(&xray.ClientTraffic{InboundId: first.Id, Email: email, Enable: true}).Error; err != nil {
  110. t.Fatalf("seed traffic: %v", err)
  111. }
  112. if _, _, err := clientSvc.SetClientEnableByEmail(&inboundSvc, email, false); err != nil {
  113. t.Fatalf("SetClientEnableByEmail: %v", err)
  114. }
  115. for _, ib := range []*model.Inbound{first, second} {
  116. fresh, err := inboundSvc.GetInbound(ib.Id)
  117. if err != nil {
  118. t.Fatalf("GetInbound(%s): %v", ib.Tag, err)
  119. }
  120. clients, err := inboundSvc.GetClients(fresh)
  121. if err != nil {
  122. t.Fatalf("GetClients(%s): %v", ib.Tag, err)
  123. }
  124. if len(clients) != 1 || clients[0].Enable {
  125. t.Errorf("inbound %s: client still enabled after disable-by-email; a sibling inbound kept access", ib.Tag)
  126. }
  127. }
  128. }