1
0

xray_setting.go 8.7 KB

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