client_apply_field_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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{
  31. Tag: "vless-a", Enable: true, Port: 50001, Protocol: model.VLESS,
  32. StreamSettings: `{"network":"tcp","security":"reality"}`, Settings: clientJSON(oldExpiry),
  33. }
  34. second := &model.Inbound{
  35. Tag: "vless-b", Enable: true, Port: 50002, Protocol: model.VLESS,
  36. StreamSettings: `{"network":"ws","security":"tls"}`, Settings: clientJSON(oldExpiry),
  37. }
  38. for _, ib := range []*model.Inbound{first, second} {
  39. if err := db.Create(ib).Error; err != nil {
  40. t.Fatalf("create inbound %s: %v", ib.Tag, err)
  41. }
  42. }
  43. clientSvc := ClientService{}
  44. inboundSvc := InboundService{}
  45. for _, ib := range []*model.Inbound{first, second} {
  46. clients, err := inboundSvc.GetClients(ib)
  47. if err != nil {
  48. t.Fatalf("GetClients(%s): %v", ib.Tag, err)
  49. }
  50. if err := clientSvc.SyncInbound(nil, ib.Id, clients); err != nil {
  51. t.Fatalf("SyncInbound(%s): %v", ib.Tag, err)
  52. }
  53. }
  54. if _, err := clientSvc.ResetClientExpiryTimeByEmail(&inboundSvc, email, newExpiry); err != nil {
  55. t.Fatalf("ResetClientExpiryTimeByEmail: %v", err)
  56. }
  57. for _, ib := range []*model.Inbound{first, second} {
  58. fresh, err := inboundSvc.GetInbound(ib.Id)
  59. if err != nil {
  60. t.Fatalf("GetInbound(%s): %v", ib.Tag, err)
  61. }
  62. clients, err := inboundSvc.GetClients(fresh)
  63. if err != nil {
  64. t.Fatalf("GetClients(%s): %v", ib.Tag, err)
  65. }
  66. if len(clients) != 1 || clients[0].ExpiryTime != newExpiry {
  67. t.Errorf("inbound %s settings expiry = %d, want %d (#5039)", ib.Tag, clients[0].ExpiryTime, newExpiry)
  68. }
  69. }
  70. rec, err := clientSvc.GetRecordByEmail(nil, email)
  71. if err != nil {
  72. t.Fatalf("GetRecordByEmail: %v", err)
  73. }
  74. if rec.ExpiryTime != newExpiry {
  75. t.Errorf("client record expiry = %d, want %d", rec.ExpiryTime, newExpiry)
  76. }
  77. }