xray_setting.go 14 KB

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