xray_setting.go 18 KB

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