xray_config_clients_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. func disableClients(t *testing.T, emails ...string) {
  28. t.Helper()
  29. if err := database.GetDB().Model(&model.ClientRecord{}).
  30. Where("email IN ?", emails).
  31. Update("enable", false).Error; err != nil {
  32. t.Fatalf("disable clients: %v", err)
  33. }
  34. }
  35. func emittedClients(t *testing.T, tag string) (any, bool) {
  36. t.Helper()
  37. svc := &XrayService{}
  38. cfg, err := svc.GetXrayConfig()
  39. if err != nil {
  40. t.Fatalf("GetXrayConfig: %v", err)
  41. }
  42. for i := range cfg.InboundConfigs {
  43. if cfg.InboundConfigs[i].Tag != tag {
  44. continue
  45. }
  46. var s map[string]any
  47. if err := json.Unmarshal([]byte(cfg.InboundConfigs[i].Settings), &s); err != nil {
  48. t.Fatalf("unmarshal emitted settings: %v", err)
  49. }
  50. v, ok := s["clients"]
  51. return v, ok
  52. }
  53. t.Fatalf("inbound %q not found in generated config", tag)
  54. return nil, false
  55. }
  56. func TestGetXrayConfig_EmptyClientListIsArrayNotNull(t *testing.T) {
  57. seedVlessInbound(t, "vless-empty", 43101, []model.Client{
  58. {Email: "gone@x", ID: "11111111-1111-1111-1111-111111111111", Enable: true},
  59. })
  60. disableClients(t, "gone@x")
  61. value, present := emittedClients(t, "vless-empty")
  62. if !present {
  63. t.Fatal("the clients key must be present on a vless inbound")
  64. }
  65. if value == nil {
  66. t.Fatal(`settings.clients must be [] when every client is filtered out, got null`)
  67. }
  68. list, ok := value.([]any)
  69. if !ok {
  70. t.Fatalf("settings.clients must be an array, got %T", value)
  71. }
  72. if len(list) != 0 {
  73. t.Fatalf("a disabled client must not reach the config, got %d entries", len(list))
  74. }
  75. }
  76. func TestGetXrayConfig_EnabledClientsStillEmitted(t *testing.T) {
  77. seedVlessInbound(t, "vless-mixed", 43102, []model.Client{
  78. {Email: "live@x", ID: "22222222-2222-2222-2222-222222222222", Enable: true},
  79. {Email: "off@x", ID: "33333333-3333-3333-3333-333333333333", Enable: true},
  80. })
  81. disableClients(t, "off@x")
  82. value, _ := emittedClients(t, "vless-mixed")
  83. list, ok := value.([]any)
  84. if !ok {
  85. t.Fatalf("settings.clients must be an array, got %T", value)
  86. }
  87. if len(list) != 1 {
  88. t.Fatalf("expected only the enabled client, got %d entries: %#v", len(list), list)
  89. }
  90. entry, ok := list[0].(map[string]any)
  91. if !ok {
  92. t.Fatalf("client entry must be an object, got %T", list[0])
  93. }
  94. if entry["email"] != "live@x" {
  95. t.Errorf("wrong client emitted: %#v", entry)
  96. }
  97. if entry["id"] != "22222222-2222-2222-2222-222222222222" {
  98. t.Errorf("client id not carried through: %#v", entry)
  99. }
  100. }