xray_setting.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/mhsanaei/3x-ui/v3/util/common"
  6. "github.com/mhsanaei/3x-ui/v3/web/service"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // XraySettingController handles Xray configuration and settings operations.
  10. type XraySettingController struct {
  11. XraySettingService service.XraySettingService
  12. SettingService service.SettingService
  13. InboundService service.InboundService
  14. OutboundService service.OutboundService
  15. XrayService service.XrayService
  16. WarpService service.WarpService
  17. NordService service.NordService
  18. OutboundSubscriptionService service.OutboundSubscriptionService
  19. }
  20. // NewXraySettingController creates a new XraySettingController and initializes its routes.
  21. func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
  22. a := &XraySettingController{}
  23. a.initRouter(g)
  24. return a
  25. }
  26. // initRouter sets up the routes for Xray settings management.
  27. func (a *XraySettingController) initRouter(g *gin.RouterGroup) {
  28. g = g.Group("/xray")
  29. g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
  30. g.GET("/getOutboundsTraffic", a.getOutboundsTraffic)
  31. g.GET("/getXrayResult", a.getXrayResult)
  32. g.POST("/", a.getXraySetting)
  33. g.POST("/warp/:action", a.warp)
  34. g.POST("/nord/:action", a.nord)
  35. g.POST("/update", a.updateSetting)
  36. g.POST("/resetOutboundsTraffic", a.resetOutboundsTraffic)
  37. g.POST("/testOutbound", a.testOutbound)
  38. // Outbound subscription (remote outbound lists)
  39. g.GET("/outbound-subs", a.listOutboundSubs)
  40. g.POST("/outbound-subs", a.createOutboundSub)
  41. g.POST("/outbound-subs/:id/refresh", a.refreshOutboundSub)
  42. g.POST("/outbound-subs/:id/move", a.moveOutboundSub)
  43. g.POST("/outbound-subs/:id", a.updateOutboundSub)
  44. g.DELETE("/outbound-subs/:id", a.deleteOutboundSub)
  45. g.POST("/outbound-subs/:id/del", a.deleteOutboundSub) // axios-friendly alias
  46. g.POST("/outbound-subs/parse", a.parseOutboundSubURL) // preview without saving
  47. }
  48. // getXraySetting retrieves the Xray configuration template, inbound tags, and outbound test URL.
  49. func (a *XraySettingController) getXraySetting(c *gin.Context) {
  50. xraySetting, err := a.SettingService.GetXrayConfigTemplate()
  51. if err != nil {
  52. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  53. return
  54. }
  55. // Older versions of this handler embedded the raw DB value as
  56. // `xraySetting` in the response without checking if the value
  57. // already had that wrapper shape. When the frontend saved it
  58. // back through the textarea verbatim, the wrapper got persisted
  59. // and every subsequent save nested another layer, which is what
  60. // eventually produced the blank Xray Settings page in #4059.
  61. // Strip any such wrapper here, and heal the DB if we found one so
  62. // the next read is O(1) instead of climbing the same pile again.
  63. if unwrapped := service.UnwrapXrayTemplateConfig(xraySetting); unwrapped != xraySetting {
  64. if saveErr := a.XraySettingService.SaveXraySetting(unwrapped); saveErr == nil {
  65. xraySetting = unwrapped
  66. } else {
  67. // Don't fail the read — just serve the unwrapped value
  68. // and leave the DB healing for a later save.
  69. xraySetting = unwrapped
  70. }
  71. }
  72. inboundTags, err := a.InboundService.GetInboundTags()
  73. if err != nil {
  74. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  75. return
  76. }
  77. clientReverseTags, err := a.InboundService.GetClientReverseTags()
  78. if err != nil {
  79. clientReverseTags = "[]"
  80. }
  81. outboundTestUrl, _ := a.SettingService.GetXrayOutboundTestUrl()
  82. if outboundTestUrl == "" {
  83. outboundTestUrl = "https://www.google.com/generate_204"
  84. }
  85. xrayResponse := map[string]any{
  86. "xraySetting": json.RawMessage(xraySetting),
  87. "inboundTags": json.RawMessage(inboundTags),
  88. "clientReverseTags": json.RawMessage(clientReverseTags),
  89. "outboundTestUrl": outboundTestUrl,
  90. }
  91. // Surface subscription outbounds (and their tags) so the frontend can:
  92. // - show them as read-only items in the Outbounds tab
  93. // - let users pick them in balancers and routing rules
  94. // These are not part of the editable template; they are injected at runtime.
  95. if subObs, err := a.OutboundSubscriptionService.AllActiveOutbounds(); err == nil && len(subObs) > 0 {
  96. xrayResponse["subscriptionOutbounds"] = subObs
  97. }
  98. if subTags, err := a.OutboundSubscriptionService.AllActiveOutboundTags(); err == nil && len(subTags) > 0 {
  99. xrayResponse["subscriptionOutboundTags"] = subTags
  100. }
  101. result, err := json.Marshal(xrayResponse)
  102. if err != nil {
  103. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  104. return
  105. }
  106. jsonObj(c, string(result), nil)
  107. }
  108. // updateSetting updates the Xray configuration settings.
  109. func (a *XraySettingController) updateSetting(c *gin.Context) {
  110. xraySetting := c.PostForm("xraySetting")
  111. if err := a.XraySettingService.SaveXraySetting(xraySetting); err != nil {
  112. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  113. return
  114. }
  115. outboundTestUrl := c.PostForm("outboundTestUrl")
  116. if outboundTestUrl == "" {
  117. outboundTestUrl = "https://www.google.com/generate_204"
  118. }
  119. if err := a.SettingService.SetXrayOutboundTestUrl(outboundTestUrl); err != nil {
  120. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  121. return
  122. }
  123. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), nil)
  124. }
  125. // getDefaultXrayConfig retrieves the default Xray configuration.
  126. func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) {
  127. defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig()
  128. if err != nil {
  129. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  130. return
  131. }
  132. jsonObj(c, defaultJsonConfig, nil)
  133. }
  134. // getXrayResult retrieves the current Xray service result.
  135. func (a *XraySettingController) getXrayResult(c *gin.Context) {
  136. jsonObj(c, a.XrayService.GetXrayResult(), nil)
  137. }
  138. // warp handles Warp-related operations based on the action parameter.
  139. func (a *XraySettingController) warp(c *gin.Context) {
  140. action := c.Param("action")
  141. var resp string
  142. var err error
  143. switch action {
  144. case "data":
  145. resp, err = a.WarpService.GetWarpData()
  146. case "del":
  147. err = a.WarpService.DelWarpData()
  148. case "config":
  149. resp, err = a.WarpService.GetWarpConfig()
  150. case "reg":
  151. skey := c.PostForm("privateKey")
  152. pkey := c.PostForm("publicKey")
  153. resp, err = a.WarpService.RegWarp(skey, pkey)
  154. case "license":
  155. license := c.PostForm("license")
  156. resp, err = a.WarpService.SetWarpLicense(license)
  157. }
  158. jsonObj(c, resp, err)
  159. }
  160. // nord handles NordVPN-related operations based on the action parameter.
  161. func (a *XraySettingController) nord(c *gin.Context) {
  162. action := c.Param("action")
  163. var resp string
  164. var err error
  165. switch action {
  166. case "countries":
  167. resp, err = a.NordService.GetCountries()
  168. case "servers":
  169. countryId := c.PostForm("countryId")
  170. resp, err = a.NordService.GetServers(countryId)
  171. case "reg":
  172. token := c.PostForm("token")
  173. resp, err = a.NordService.GetCredentials(token)
  174. case "setKey":
  175. key := c.PostForm("key")
  176. resp, err = a.NordService.SetKey(key)
  177. case "data":
  178. resp, err = a.NordService.GetNordData()
  179. case "del":
  180. err = a.NordService.DelNordData()
  181. }
  182. jsonObj(c, resp, err)
  183. }
  184. // getOutboundsTraffic retrieves the traffic statistics for outbounds.
  185. func (a *XraySettingController) getOutboundsTraffic(c *gin.Context) {
  186. outboundsTraffic, err := a.OutboundService.GetOutboundsTraffic()
  187. if err != nil {
  188. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getOutboundTrafficError"), err)
  189. return
  190. }
  191. jsonObj(c, outboundsTraffic, nil)
  192. }
  193. // resetOutboundsTraffic resets the traffic statistics for the specified outbound tag.
  194. func (a *XraySettingController) resetOutboundsTraffic(c *gin.Context) {
  195. tag := c.PostForm("tag")
  196. err := a.OutboundService.ResetOutboundTraffic(tag)
  197. if err != nil {
  198. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.resetOutboundTrafficError"), err)
  199. return
  200. }
  201. jsonObj(c, "", nil)
  202. }
  203. // testOutbound tests an outbound configuration and returns the delay/response time.
  204. // Optional form "allOutbounds": JSON array of all outbounds; used to resolve sockopt.dialerProxy dependencies.
  205. // Optional form "mode": "tcp" for a fast dial-only probe (parallel-safe),
  206. // anything else (default) for a full HTTP probe through a temp xray instance.
  207. func (a *XraySettingController) testOutbound(c *gin.Context) {
  208. outboundJSON := c.PostForm("outbound")
  209. allOutboundsJSON := c.PostForm("allOutbounds")
  210. mode := c.PostForm("mode")
  211. if outboundJSON == "" {
  212. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("outbound parameter is required"))
  213. return
  214. }
  215. // Load the test URL from server settings to prevent SSRF via user-controlled URLs
  216. testURL, _ := a.SettingService.GetXrayOutboundTestUrl()
  217. testURL, err := service.SanitizePublicHTTPURL(testURL, false)
  218. if err != nil {
  219. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  220. return
  221. }
  222. result, err := a.OutboundService.TestOutbound(outboundJSON, testURL, allOutboundsJSON, mode)
  223. if err != nil {
  224. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  225. return
  226. }
  227. jsonObj(c, result, nil)
  228. }
  229. // --- Outbound Subscription handlers ---
  230. func (a *XraySettingController) listOutboundSubs(c *gin.Context) {
  231. list, err := a.OutboundSubscriptionService.List()
  232. if err != nil {
  233. jsonMsg(c, "Failed to list outbound subscriptions", err)
  234. return
  235. }
  236. jsonObj(c, list, nil)
  237. }
  238. func (a *XraySettingController) createOutboundSub(c *gin.Context) {
  239. remark := c.PostForm("remark")
  240. rawURL := c.PostForm("url")
  241. prefix := c.PostForm("tagPrefix")
  242. enabled := c.PostForm("enabled") != "false"
  243. allowPrivate := c.PostForm("allowPrivate") == "true"
  244. prepend := c.PostForm("prepend") == "true"
  245. intervalStr := c.PostForm("updateInterval")
  246. interval := 600
  247. if intervalStr != "" {
  248. if v, err := parseIntSafe(intervalStr); err == nil && v > 0 {
  249. interval = v
  250. }
  251. }
  252. sub, err := a.OutboundSubscriptionService.Create(remark, rawURL, prefix, enabled, interval, allowPrivate, prepend)
  253. if err != nil {
  254. jsonMsg(c, "Failed to create outbound subscription", err)
  255. return
  256. }
  257. jsonObj(c, sub, nil)
  258. }
  259. func (a *XraySettingController) updateOutboundSub(c *gin.Context) {
  260. id := c.Param("id")
  261. var subID int
  262. if _, err := fmt.Sscanf(id, "%d", &subID); err != nil {
  263. jsonMsg(c, "Invalid id", err)
  264. return
  265. }
  266. remark := c.PostForm("remark")
  267. rawURL := c.PostForm("url")
  268. prefix := c.PostForm("tagPrefix")
  269. enabled := c.PostForm("enabled") != "false"
  270. allowPrivate := c.PostForm("allowPrivate") == "true"
  271. prepend := c.PostForm("prepend") == "true"
  272. intervalStr := c.PostForm("updateInterval")
  273. interval := 600
  274. if intervalStr != "" {
  275. if v, err := parseIntSafe(intervalStr); err == nil && v > 0 {
  276. interval = v
  277. }
  278. }
  279. if err := a.OutboundSubscriptionService.Update(subID, remark, rawURL, prefix, enabled, interval, allowPrivate, prepend); err != nil {
  280. jsonMsg(c, "Failed to update outbound subscription", err)
  281. return
  282. }
  283. jsonObj(c, "", nil)
  284. }
  285. func (a *XraySettingController) deleteOutboundSub(c *gin.Context) {
  286. id := c.Param("id")
  287. var subID int
  288. if _, err := fmt.Sscanf(id, "%d", &subID); err != nil {
  289. jsonMsg(c, "Invalid id", err)
  290. return
  291. }
  292. if err := a.OutboundSubscriptionService.Delete(subID); err != nil {
  293. jsonMsg(c, "Failed to delete outbound subscription", err)
  294. return
  295. }
  296. // Signal that xray should drop this subscription's outbounds on next reload.
  297. a.XrayService.SetToNeedRestart()
  298. jsonObj(c, "", nil)
  299. }
  300. func (a *XraySettingController) refreshOutboundSub(c *gin.Context) {
  301. id := c.Param("id")
  302. var subID int
  303. if _, err := fmt.Sscanf(id, "%d", &subID); err != nil {
  304. jsonMsg(c, "Invalid id", err)
  305. return
  306. }
  307. obs, err := a.OutboundSubscriptionService.Refresh(subID)
  308. if err != nil {
  309. jsonMsg(c, "Refresh failed", err)
  310. return
  311. }
  312. // Signal that xray should pick up the new outbounds on next restart/reload
  313. a.XrayService.SetToNeedRestart()
  314. jsonObj(c, obs, nil)
  315. }
  316. func (a *XraySettingController) moveOutboundSub(c *gin.Context) {
  317. id := c.Param("id")
  318. var subID int
  319. if _, err := fmt.Sscanf(id, "%d", &subID); err != nil {
  320. jsonMsg(c, "Invalid id", err)
  321. return
  322. }
  323. up := c.PostForm("dir") == "up"
  324. if err := a.OutboundSubscriptionService.Move(subID, up); err != nil {
  325. jsonMsg(c, "Failed to reorder outbound subscription", err)
  326. return
  327. }
  328. // Order affects the merged outbounds, so xray needs a reload.
  329. a.XrayService.SetToNeedRestart()
  330. jsonObj(c, "", nil)
  331. }
  332. // parseOutboundSubURL is a preview endpoint: it fetches + parses the provided
  333. // URL but does not persist anything. Useful for the "add subscription" flow
  334. // so the user can see the resulting outbounds (and assigned tags) before saving.
  335. func (a *XraySettingController) parseOutboundSubURL(c *gin.Context) {
  336. rawURL := c.PostForm("url")
  337. if rawURL == "" {
  338. jsonMsg(c, "url is required", common.NewError("missing url"))
  339. return
  340. }
  341. allowPrivate := c.PostForm("allowPrivate") == "true"
  342. // Use a throw-away service instance; it only needs the settingService for proxy.
  343. svc := service.OutboundSubscriptionService{}
  344. // We don't have a direct "fetch once" that returns without storing, so we
  345. // temporarily create a disabled row, refresh it, then delete. Cleaner would
  346. // be to expose a pure ParseURL on the service, but this keeps the surface small.
  347. tmp, err := svc.Create("preview", rawURL, "", false, 600, allowPrivate, false)
  348. if err != nil {
  349. jsonMsg(c, "Failed to preview subscription", err)
  350. return
  351. }
  352. obs, err := svc.Refresh(tmp.Id)
  353. // best-effort cleanup
  354. _ = svc.Delete(tmp.Id)
  355. if err != nil {
  356. jsonMsg(c, "Failed to fetch/parse subscription", err)
  357. return
  358. }
  359. jsonObj(c, obs, nil)
  360. }
  361. func parseIntSafe(s string) (int, error) {
  362. var v int
  363. _, err := fmt.Sscanf(s, "%d", &v)
  364. return v, err
  365. }