client_apply_field_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. )
  9. // TestResetClientExpiryTimeByEmail_MultiInbound reproduces #5039: a client
  10. // attached to several inbounds had its expiry patched only on the first
  11. // inbound's JSON, so the stale siblings reverted the change on the next sync.
  12. func TestResetClientExpiryTimeByEmail_MultiInbound(t *testing.T) {
  13. dbDir := t.TempDir()
  14. t.Setenv("XUI_DB_FOLDER", dbDir)
  15. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  16. t.Fatalf("InitDB: %v", err)
  17. }
  18. t.Cleanup(func() { _ = database.CloseDB() })
  19. db := database.GetDB()
  20. const email = "[email protected]"
  21. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c111"
  22. const oldExpiry = int64(1700000000000)
  23. const newExpiry = int64(1800000000000)
  24. clientJSON := func(expiry int64) string {
  25. b, _ := json.Marshal(map[string]any{"clients": []map[string]any{{
  26. "email": email, "id": uid, "enable": true, "expiryTime": expiry, "subId": "sub-multi-1",
  27. }}})
  28. return string(b)
  29. }
  30. first := &model.Inbound{Tag: "vless-a", Enable: true, Port: 50001, Protocol: model.VLESS,
  31. StreamSettings: `{"network":"tcp","security":"reality"}`, Settings: clientJSON(oldExpiry)}
  32. second := &model.Inbound{Tag: "vless-b", Enable: true, Port: 50002, Protocol: model.VLESS,
  33. StreamSettings: `{"network":"ws","security":"tls"}`, Settings: clientJSON(oldExpiry)}
  34. for _, ib := range []*model.Inbound{first, second} {
  35. if err := db.Create(ib).Error; err != nil {
  36. t.Fatalf("create inbound %s: %v", ib.Tag, err)
  37. }
  38. }
  39. clientSvc := ClientService{}
  40. inboundSvc := InboundService{}
  41. for _, ib := range []*model.Inbound{first, second} {
  42. clients, err := inboundSvc.GetClients(ib)
  43. if err != nil {
  44. t.Fatalf("GetClients(%s): %v", ib.Tag, err)
  45. }
  46. if err := clientSvc.SyncInbound(nil, ib.Id, clients); err != nil {
  47. t.Fatalf("SyncInbound(%s): %v", ib.Tag, err)
  48. }
  49. }
  50. if _, err := clientSvc.ResetClientExpiryTimeByEmail(&inboundSvc, email, newExpiry); err != nil {
  51. t.Fatalf("ResetClientExpiryTimeByEmail: %v", err)
  52. }
  53. for _, ib := range []*model.Inbound{first, second} {
  54. fresh, err := inboundSvc.GetInbound(ib.Id)
  55. if err != nil {
  56. t.Fatalf("GetInbound(%s): %v", ib.Tag, err)
  57. }
  58. clients, err := inboundSvc.GetClients(fresh)
  59. if err != nil {
  60. t.Fatalf("GetClients(%s): %v", ib.Tag, err)
  61. }
  62. if len(clients) != 1 || clients[0].ExpiryTime != newExpiry {
  63. t.Errorf("inbound %s settings expiry = %d, want %d (#5039)", ib.Tag, clients[0].ExpiryTime, newExpiry)
  64. }
  65. }
  66. rec, err := clientSvc.GetRecordByEmail(nil, email)
  67. if err != nil {
  68. t.Fatalf("GetRecordByEmail: %v", err)
  69. }
  70. if rec.ExpiryTime != newExpiry {
  71. t.Errorf("client record expiry = %d, want %d", rec.ExpiryTime, newExpiry)
  72. }
  73. }