1
0

xray_setting.go 17 KB

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