1
0

xray_setting_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package service
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. )
  7. func TestUnwrapXrayTemplateConfig(t *testing.T) {
  8. real := `{"log":{},"inbounds":[],"outbounds":[],"routing":{}}`
  9. t.Run("passes through a clean config", func(t *testing.T) {
  10. if got := UnwrapXrayTemplateConfig(real); got != real {
  11. t.Fatalf("clean config was modified: %s", got)
  12. }
  13. })
  14. t.Run("passes through invalid JSON unchanged", func(t *testing.T) {
  15. in := "not json at all"
  16. if got := UnwrapXrayTemplateConfig(in); got != in {
  17. t.Fatalf("invalid input was modified: %s", got)
  18. }
  19. })
  20. t.Run("unwraps one layer of response-shaped wrapper", func(t *testing.T) {
  21. wrapper := `{"inboundTags":["tag"],"outboundTestUrl":"x","xraySetting":` + real + `}`
  22. got := UnwrapXrayTemplateConfig(wrapper)
  23. if !equalJSON(t, got, real) {
  24. t.Fatalf("want %s, got %s", real, got)
  25. }
  26. })
  27. t.Run("unwraps multiple stacked layers", func(t *testing.T) {
  28. lvl1 := `{"xraySetting":` + real + `}`
  29. lvl2 := `{"xraySetting":` + lvl1 + `}`
  30. lvl3 := `{"xraySetting":` + lvl2 + `}`
  31. got := UnwrapXrayTemplateConfig(lvl3)
  32. if !equalJSON(t, got, real) {
  33. t.Fatalf("want %s, got %s", real, got)
  34. }
  35. })
  36. t.Run("handles an xraySetting stored as a JSON-encoded string", func(t *testing.T) {
  37. encoded, _ := json.Marshal(real) // becomes a quoted string
  38. wrapper := `{"xraySetting":` + string(encoded) + `}`
  39. got := UnwrapXrayTemplateConfig(wrapper)
  40. if !equalJSON(t, got, real) {
  41. t.Fatalf("want %s, got %s", real, got)
  42. }
  43. })
  44. t.Run("does not unwrap when top level already has real xray keys", func(t *testing.T) {
  45. // Pathological but defensible: if a user's actual config somehow
  46. // has both the real keys and an unrelated `xraySetting` key, we
  47. // must not strip it.
  48. in := `{"inbounds":[],"xraySetting":{"some":"thing"}}`
  49. got := UnwrapXrayTemplateConfig(in)
  50. if got != in {
  51. t.Fatalf("should have left real config alone, got %s", got)
  52. }
  53. })
  54. t.Run("stops at a reasonable depth", func(t *testing.T) {
  55. // Build a deeper-than-maxDepth chain that ends at something
  56. // non-wrapped, and confirm we end up at some valid JSON (we
  57. // don't loop forever and we don't blow the stack).
  58. s := real
  59. for i := 0; i < 16; i++ {
  60. s = `{"xraySetting":` + s + `}`
  61. }
  62. got := UnwrapXrayTemplateConfig(s)
  63. if !strings.Contains(got, `"inbounds"`) && !strings.Contains(got, `"xraySetting"`) {
  64. t.Fatalf("unexpected tail: %s", got)
  65. }
  66. })
  67. }
  68. func equalJSON(t *testing.T, a, b string) bool {
  69. t.Helper()
  70. var va, vb any
  71. if err := json.Unmarshal([]byte(a), &va); err != nil {
  72. return false
  73. }
  74. if err := json.Unmarshal([]byte(b), &vb); err != nil {
  75. return false
  76. }
  77. ja, _ := json.Marshal(va)
  78. jb, _ := json.Marshal(vb)
  79. return string(ja) == string(jb)
  80. }
  81. // firstRuleOutbound parses the (post-hoisted) config and returns
  82. // routing.rules[0].outboundTag, or "" if anything is missing.
  83. func firstRuleOutbound(t *testing.T, raw string) string {
  84. t.Helper()
  85. var cfg map[string]any
  86. if err := json.Unmarshal([]byte(raw), &cfg); err != nil {
  87. t.Fatalf("unmarshal cfg: %v", err)
  88. }
  89. routing, _ := cfg["routing"].(map[string]any)
  90. rules, _ := routing["rules"].([]any)
  91. if len(rules) == 0 {
  92. return ""
  93. }
  94. first, _ := rules[0].(map[string]any)
  95. tag, _ := first["outboundTag"].(string)
  96. return tag
  97. }
  98. func TestEnsureStatsRouting_HoistsApiRuleFromMiddle(t *testing.T) {
  99. // #4113 repro shape: admin added a cascade outbound and put a
  100. // catch-all routing rule above the api rule. stats query path
  101. // gets starved by the catch-all unless we hoist the api rule.
  102. in := `{
  103. "routing": {
  104. "rules": [
  105. {"type":"field","inboundTag":["inbound-vless"],"outboundTag":"vless-cascade"},
  106. {"type":"field","inboundTag":["api"],"outboundTag":"api"},
  107. {"type":"field","outboundTag":"blocked","ip":["geoip:private"]}
  108. ]
  109. }
  110. }`
  111. out, err := EnsureStatsRouting(in)
  112. if err != nil {
  113. t.Fatalf("unexpected err: %v", err)
  114. }
  115. if got := firstRuleOutbound(t, out); got != "api" {
  116. t.Fatalf("api rule should be at index 0 after hoist, got first outboundTag = %q\nfull: %s", got, out)
  117. }
  118. }
  119. func TestEnsureStatsRouting_NoOpWhenAlreadyFirst(t *testing.T) {
  120. // Don't churn the JSON when nothing needs fixing — same string in,
  121. // same string out. Lets the diff in the panel UI stay quiet for
  122. // well-formed configs.
  123. in := `{"routing":{"rules":[{"type":"field","inboundTag":["api"],"outboundTag":"api"},{"type":"field","outboundTag":"blocked","ip":["geoip:private"]}]}}`
  124. out, err := EnsureStatsRouting(in)
  125. if err != nil {
  126. t.Fatalf("unexpected err: %v", err)
  127. }
  128. if out != in {
  129. t.Fatalf("expected unchanged input, got: %s", out)
  130. }
  131. }
  132. func TestEnsureStatsRouting_InsertsDefaultWhenMissing(t *testing.T) {
  133. // Some admins delete the api rule by accident. Re-add a default
  134. // at the front so stats keep working after the next save.
  135. in := `{"routing":{"rules":[{"type":"field","outboundTag":"vless-cascade","inboundTag":["inbound-vless"]}]}}`
  136. out, err := EnsureStatsRouting(in)
  137. if err != nil {
  138. t.Fatalf("unexpected err: %v", err)
  139. }
  140. if got := firstRuleOutbound(t, out); got != "api" {
  141. t.Fatalf("default api rule should be inserted at index 0, got %q\nfull: %s", got, out)
  142. }
  143. // The original rule should still be there, just shifted.
  144. var cfg map[string]any
  145. json.Unmarshal([]byte(out), &cfg)
  146. rules := cfg["routing"].(map[string]any)["rules"].([]any)
  147. if len(rules) != 2 {
  148. t.Fatalf("expected 2 rules after insert, got %d: %v", len(rules), rules)
  149. }
  150. }
  151. func TestEnsureStatsRouting_NoRoutingBlock(t *testing.T) {
  152. // Pathological but possible: empty config or one without a routing
  153. // section. Don't crash, and create the section with the api rule.
  154. in := `{"log":{}}`
  155. out, err := EnsureStatsRouting(in)
  156. if err != nil {
  157. t.Fatalf("unexpected err: %v", err)
  158. }
  159. if got := firstRuleOutbound(t, out); got != "api" {
  160. t.Fatalf("api rule should be created when routing was missing, got %q\nfull: %s", got, out)
  161. }
  162. }
  163. func TestEnsureStatsRouting_InvalidJsonReturnsAsIs(t *testing.T) {
  164. // SaveXraySetting calls CheckXrayConfig before this helper, so
  165. // invalid JSON shouldn't reach us in practice — but be defensive
  166. // about garbage in (return same garbage out plus an error) so the
  167. // caller can choose to skip the hoist instead of corrupting input.
  168. in := "definitely not json"
  169. out, err := EnsureStatsRouting(in)
  170. if err == nil {
  171. t.Fatalf("expected error for invalid json, got none")
  172. }
  173. if out != in {
  174. t.Fatalf("expected raw passthrough on error, got %q", out)
  175. }
  176. }
  177. func TestEnsureStatsRouting_AcceptsInboundTagAsString(t *testing.T) {
  178. // Some manually-edited configs use a single string instead of an
  179. // array for inboundTag. Make sure we still recognize the api rule.
  180. in := `{"routing":{"rules":[{"type":"field","inboundTag":["other"],"outboundTag":"vless-cascade"},{"type":"field","inboundTag":"api","outboundTag":"api"}]}}`
  181. out, err := EnsureStatsRouting(in)
  182. if err != nil {
  183. t.Fatalf("unexpected err: %v", err)
  184. }
  185. if got := firstRuleOutbound(t, out); got != "api" {
  186. t.Fatalf("api rule with string-form inboundTag should hoist to front, got %q\nfull: %s", got, out)
  187. }
  188. }