xray_setting.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package service
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "slices"
  6. "github.com/mhsanaei/3x-ui/v3/util/common"
  7. "github.com/mhsanaei/3x-ui/v3/xray"
  8. )
  9. // XraySettingService provides business logic for Xray configuration management.
  10. // It handles validation and storage of Xray template configurations.
  11. type XraySettingService struct {
  12. SettingService
  13. }
  14. func (s *XraySettingService) SaveXraySetting(newXraySettings string) error {
  15. // The frontend round-trips the whole getXraySetting response back
  16. // through the textarea, so if it has ever received a wrapped
  17. // payload (see UnwrapXrayTemplateConfig) it sends that same wrapper
  18. // back here. Strip it before validation/storage, otherwise we save
  19. // garbage the next read can't recover from without this same call.
  20. newXraySettings = UnwrapXrayTemplateConfig(newXraySettings)
  21. if err := s.CheckXrayConfig(newXraySettings); err != nil {
  22. return err
  23. }
  24. if hoisted, err := EnsureStatsRouting(newXraySettings); err == nil {
  25. newXraySettings = hoisted
  26. }
  27. return s.SettingService.saveSetting("xrayTemplateConfig", newXraySettings)
  28. }
  29. func (s *XraySettingService) CheckXrayConfig(XrayTemplateConfig string) error {
  30. xrayConfig := &xray.Config{}
  31. err := json.Unmarshal([]byte(XrayTemplateConfig), xrayConfig)
  32. if err != nil {
  33. return common.NewError("xray template config invalid:", err)
  34. }
  35. return nil
  36. }
  37. // UnwrapXrayTemplateConfig returns the raw xray config JSON from `raw`,
  38. // peeling off any number of `{ "inboundTags": ..., "outboundTestUrl": ...,
  39. // "xraySetting": <real config> }` response-shaped wrappers that may have
  40. // ended up in the database.
  41. //
  42. // How it got there: getXraySetting used to embed the raw DB value as
  43. // `xraySetting` in its response without checking whether the stored
  44. // value was already that exact response shape. If the frontend then
  45. // saved it verbatim (the textarea is a round-trip of the JSON it was
  46. // handed), the wrapper got persisted — and each subsequent save nested
  47. // another layer, producing the blank Xray Settings page reported in
  48. // issue #4059.
  49. //
  50. // If `raw` does not look like a wrapper, it is returned unchanged.
  51. func UnwrapXrayTemplateConfig(raw string) string {
  52. const maxDepth = 8 // defensive cap against pathological multi-nest values
  53. for range maxDepth {
  54. var top map[string]json.RawMessage
  55. if err := json.Unmarshal([]byte(raw), &top); err != nil {
  56. return raw
  57. }
  58. inner, ok := top["xraySetting"]
  59. if !ok {
  60. return raw
  61. }
  62. // Real xray configs never contain a top-level "xraySetting" key,
  63. // but they do contain things like "inbounds"/"outbounds"/"api".
  64. // If any of those are present, we're already at the real config
  65. // and the "xraySetting" field is either user data or coincidence
  66. // — don't touch it.
  67. for _, k := range []string{"inbounds", "outbounds", "routing", "api", "dns", "log", "policy", "stats"} {
  68. if _, hit := top[k]; hit {
  69. return raw
  70. }
  71. }
  72. // Peel off one layer.
  73. unwrapped := string(inner)
  74. // `xraySetting` may be stored either as a JSON object or as a
  75. // JSON-encoded string of an object. Handle both.
  76. var asStr string
  77. if err := json.Unmarshal(inner, &asStr); err == nil {
  78. unwrapped = asStr
  79. }
  80. raw = unwrapped
  81. }
  82. return raw
  83. }
  84. // EnsureStatsRouting hoists the `api -> api` routing rule to the front
  85. // of routing.rules so the stats query path is never starved by a
  86. // catch-all rule the admin may have added or reordered above it.
  87. //
  88. // Why this matters (#4113, #2818): an admin who adds a cascade outbound
  89. // (e.g. vless to another server) and a routing rule sending all inbound
  90. // traffic to it ends up sending the internal stats inbound's traffic to
  91. // that cascade too, since rules are evaluated top-to-bottom and the
  92. // catch-all matches first. The panel's gRPC stats query then can't reach
  93. // the running xray instance, GetTraffic returns nothing, and every
  94. // client appears offline with zero traffic even though the actual proxy
  95. // path works fine.
  96. //
  97. // The api inbound is special-cased internal infrastructure for the
  98. // panel, not something the admin should ever route to a real outbound.
  99. // Keeping its rule pinned at index 0 is the only correct configuration.
  100. //
  101. // If the api rule is already at index 0 the input is returned unchanged.
  102. // If it exists somewhere else it is moved. If it is missing entirely a
  103. // default rule (`type=field, inboundTag=[api], outboundTag=api`) is
  104. // inserted at the front. Other routing entries keep their relative order.
  105. func EnsureStatsRouting(raw string) (string, error) {
  106. var cfg map[string]json.RawMessage
  107. if err := json.Unmarshal([]byte(raw), &cfg); err != nil {
  108. return raw, err
  109. }
  110. var routing map[string]json.RawMessage
  111. if r, ok := cfg["routing"]; ok && len(r) > 0 {
  112. if err := json.Unmarshal(r, &routing); err != nil {
  113. return raw, err
  114. }
  115. }
  116. if routing == nil {
  117. routing = make(map[string]json.RawMessage)
  118. }
  119. var rules []map[string]any
  120. if r, ok := routing["rules"]; ok && len(r) > 0 {
  121. if err := json.Unmarshal(r, &rules); err != nil {
  122. return raw, err
  123. }
  124. }
  125. apiIdx := findApiRule(rules)
  126. if apiIdx == 0 {
  127. return raw, nil // already correct, don't churn the JSON
  128. }
  129. var apiRule map[string]any
  130. if apiIdx > 0 {
  131. apiRule = rules[apiIdx]
  132. rules = append(rules[:apiIdx], rules[apiIdx+1:]...)
  133. } else {
  134. apiRule = map[string]any{
  135. "type": "field",
  136. "inboundTag": []string{"api"},
  137. "outboundTag": "api",
  138. }
  139. }
  140. rules = append([]map[string]any{apiRule}, rules...)
  141. rulesJSON, err := json.Marshal(rules)
  142. if err != nil {
  143. return raw, err
  144. }
  145. routing["rules"] = rulesJSON
  146. routingJSON, err := json.Marshal(routing)
  147. if err != nil {
  148. return raw, err
  149. }
  150. cfg["routing"] = routingJSON
  151. out, err := json.Marshal(cfg)
  152. if err != nil {
  153. return raw, err
  154. }
  155. return string(out), nil
  156. }
  157. // findApiRule returns the index of the routing rule that targets the
  158. // internal api inbound (inboundTag contains "api" and outboundTag is
  159. // "api"), or -1 if no such rule exists.
  160. func findApiRule(rules []map[string]any) int {
  161. for i, rule := range rules {
  162. if outTag, _ := rule["outboundTag"].(string); outTag != "api" {
  163. continue
  164. }
  165. raw, ok := rule["inboundTag"]
  166. if !ok {
  167. continue
  168. }
  169. // inboundTag is usually []string but can come as []any from a
  170. // roundtrip through map[string]any. Accept both shapes.
  171. switch tags := raw.(type) {
  172. case []any:
  173. for _, t := range tags {
  174. if s, ok := t.(string); ok && s == "api" {
  175. return i
  176. }
  177. }
  178. case []string:
  179. if slices.Contains(tags, "api") {
  180. return i
  181. }
  182. case string:
  183. if tags == "api" {
  184. return i
  185. }
  186. }
  187. }
  188. return -1
  189. }