1
0

client_apply_field_test.go 5.1 KB

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