xray_setting.go 14 KB

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