xray_config_clients_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. }
  101. // A flow kept from a TCP+REALITY stream makes xray demand Vision on a transport that cannot run it
  102. // without VLESS encryption: "XTLS only supports TLS and REALITY directly".
  103. func TestGetXrayConfig_OmitsVisionFlowOnIneligibleStream(t *testing.T) {
  104. cases := []struct {
  105. name string
  106. stream string
  107. }{
  108. {"xdrive", `{"network":"xdrive","security":"none","xdriveSettings":{"service":"local","remoteFolder":"/tmp/shared"}}`},
  109. {"ws over tls", `{"network":"ws","security":"tls","wsSettings":{"path":"/"}}`},
  110. }
  111. for i, tc := range cases {
  112. t.Run(tc.name, func(t *testing.T) {
  113. tag := "stale-flow"
  114. seedVlessInbound(t, tag, 43110+i, []model.Client{
  115. {Email: "vision@x", ID: "44444444-4444-4444-4444-444444444444", Enable: true, Flow: "xtls-rprx-vision"},
  116. })
  117. if err := database.GetDB().Model(&model.Inbound{}).Where("tag = ?", tag).
  118. Update("stream_settings", tc.stream).Error; err != nil {
  119. t.Fatalf("set stream: %v", err)
  120. }
  121. value, _ := emittedClients(t, tag)
  122. list, ok := value.([]any)
  123. if !ok || len(list) != 1 {
  124. t.Fatalf("settings.clients = %#v, want the one enabled client", value)
  125. }
  126. entry, _ := list[0].(map[string]any)
  127. if flow, present := entry["flow"]; present {
  128. t.Fatalf("flow %q emitted for a %s stream", flow, tc.name)
  129. }
  130. })
  131. }
  132. }
  133. // xray-core runs Vision over TLS/REALITY on RAW, or over VLESS encryption on any transport.
  134. func TestGetXrayConfig_KeepsVisionFlowWhereTheCoreRunsIt(t *testing.T) {
  135. const vlessenc = `{"clients":[],"decryption":"mlkem768x25519plus.native.600s.WB3OymHLL_DJ1jdHiqcMC-u84TgvfNFlgbZIJE-NtUQ"}`
  136. cases := []struct {
  137. name string
  138. stream string
  139. settings string
  140. }{
  141. {"tcp reality", `{"network":"tcp","security":"reality","realitySettings":{}}`, `{"clients":[],"decryption":"none"}`},
  142. {"raw vlessenc", `{"network":"tcp","security":"none"}`, vlessenc},
  143. {"ws vlessenc", `{"network":"ws","security":"none","wsSettings":{"path":"/"}}`, vlessenc},
  144. {"xdrive vlessenc", `{"network":"xdrive","security":"none","xdriveSettings":{"service":"local","remoteFolder":"/tmp/shared"}}`, vlessenc},
  145. }
  146. for i, tc := range cases {
  147. t.Run(tc.name, func(t *testing.T) {
  148. tag := "vision-flow"
  149. seedVlessInbound(t, tag, 43120+i, []model.Client{
  150. {Email: "vision@x", ID: "55555555-5555-5555-5555-555555555555", Enable: true, Flow: "xtls-rprx-vision"},
  151. })
  152. if err := database.GetDB().Model(&model.Inbound{}).Where("tag = ?", tag).
  153. Updates(map[string]any{"stream_settings": tc.stream, "settings": tc.settings}).Error; err != nil {
  154. t.Fatalf("set stream: %v", err)
  155. }
  156. value, _ := emittedClients(t, tag)
  157. list, _ := value.([]any)
  158. if len(list) != 1 {
  159. t.Fatalf("settings.clients = %#v, want one client", value)
  160. }
  161. if entry, _ := list[0].(map[string]any); entry["flow"] != "xtls-rprx-vision" {
  162. t.Fatalf("flow = %#v, want xtls-rprx-vision kept on %s", entry["flow"], tc.name)
  163. }
  164. })
  165. }
  166. }