xray_config_clients_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. func seedVlessInbound(t *testing.T, tag string, port int, clients []model.Client) {
  9. t.Helper()
  10. setupSettingTestDB(t)
  11. db := database.GetDB()
  12. in := &model.Inbound{
  13. Tag: tag,
  14. Enable: true,
  15. Port: port,
  16. Protocol: model.VLESS,
  17. Settings: `{"clients":[],"decryption":"none"}`,
  18. }
  19. if err := db.Create(in).Error; err != nil {
  20. t.Fatalf("create vless inbound: %v", err)
  21. }
  22. svc := ClientService{}
  23. if err := svc.SyncInbound(nil, in.Id, clients); err != nil {
  24. t.Fatalf("SyncInbound: %v", err)
  25. }
  26. }
  27. // ClientRecord.Enable carries gorm:"default:true", so a false on insert is
  28. // replaced by the default. Production disables through an UPDATE
  29. // (disableInvalidClients); do the same here.
  30. func disableClients(t *testing.T, emails ...string) {
  31. t.Helper()
  32. if err := database.GetDB().Model(&model.ClientRecord{}).
  33. Where("email IN ?", emails).
  34. Update("enable", false).Error; err != nil {
  35. t.Fatalf("disable clients: %v", err)
  36. }
  37. }
  38. func emittedClients(t *testing.T, tag string) (any, bool) {
  39. t.Helper()
  40. svc := &XrayService{}
  41. cfg, err := svc.GetXrayConfig()
  42. if err != nil {
  43. t.Fatalf("GetXrayConfig: %v", err)
  44. }
  45. for i := range cfg.InboundConfigs {
  46. if cfg.InboundConfigs[i].Tag != tag {
  47. continue
  48. }
  49. var s map[string]any
  50. if err := json.Unmarshal([]byte(cfg.InboundConfigs[i].Settings), &s); err != nil {
  51. t.Fatalf("unmarshal emitted settings: %v", err)
  52. }
  53. v, ok := s["clients"]
  54. return v, ok
  55. }
  56. t.Fatalf("inbound %q not found in generated config", tag)
  57. return nil, false
  58. }
  59. // An inbound whose clients are all filtered out must hand xray-core an empty
  60. // array. It used to emit "clients": null, which the panel treats as invalid
  61. // data elsewhere and coerces to [] at startup (#6117).
  62. func TestGetXrayConfig_EmptyClientListIsArrayNotNull(t *testing.T) {
  63. seedVlessInbound(t, "vless-empty", 43101, []model.Client{
  64. {Email: "gone@x", ID: "11111111-1111-1111-1111-111111111111", Enable: true},
  65. })
  66. disableClients(t, "gone@x")
  67. value, present := emittedClients(t, "vless-empty")
  68. if !present {
  69. t.Fatal("the clients key must be present on a vless inbound")
  70. }
  71. if value == nil {
  72. t.Fatal(`settings.clients must be [] when every client is filtered out, got null`)
  73. }
  74. list, ok := value.([]any)
  75. if !ok {
  76. t.Fatalf("settings.clients must be an array, got %T", value)
  77. }
  78. if len(list) != 0 {
  79. t.Fatalf("a disabled client must not reach the config, got %d entries", len(list))
  80. }
  81. }
  82. // The disabled/depleted filter must keep working — an empty array is only
  83. // correct because those clients are genuinely excluded.
  84. func TestGetXrayConfig_EnabledClientsStillEmitted(t *testing.T) {
  85. seedVlessInbound(t, "vless-mixed", 43102, []model.Client{
  86. {Email: "live@x", ID: "22222222-2222-2222-2222-222222222222", Enable: true},
  87. {Email: "off@x", ID: "33333333-3333-3333-3333-333333333333", Enable: true},
  88. })
  89. disableClients(t, "off@x")
  90. value, _ := emittedClients(t, "vless-mixed")
  91. list, ok := value.([]any)
  92. if !ok {
  93. t.Fatalf("settings.clients must be an array, got %T", value)
  94. }
  95. if len(list) != 1 {
  96. t.Fatalf("expected only the enabled client, got %d entries: %#v", len(list), list)
  97. }
  98. entry, ok := list[0].(map[string]any)
  99. if !ok {
  100. t.Fatalf("client entry must be an object, got %T", list[0])
  101. }
  102. if entry["email"] != "live@x" {
  103. t.Errorf("wrong client emitted: %#v", entry)
  104. }
  105. if entry["id"] != "22222222-2222-2222-2222-222222222222" {
  106. t.Errorf("client id not carried through: %#v", entry)
  107. }
  108. }