1
0

xray_setting.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package controller
  2. import (
  3. "encoding/json"
  4. "github.com/mhsanaei/3x-ui/v2/util/common"
  5. "github.com/mhsanaei/3x-ui/v2/web/service"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // XraySettingController handles Xray configuration and settings operations.
  9. type XraySettingController struct {
  10. XraySettingService service.XraySettingService
  11. SettingService service.SettingService
  12. InboundService service.InboundService
  13. OutboundService service.OutboundService
  14. XrayService service.XrayService
  15. WarpService service.WarpService
  16. }
  17. // NewXraySettingController creates a new XraySettingController and initializes its routes.
  18. func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
  19. a := &XraySettingController{}
  20. a.initRouter(g)
  21. return a
  22. }
  23. // initRouter sets up the routes for Xray settings management.
  24. func (a *XraySettingController) initRouter(g *gin.RouterGroup) {
  25. g = g.Group("/xray")
  26. g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
  27. g.GET("/getOutboundsTraffic", a.getOutboundsTraffic)
  28. g.GET("/getXrayResult", a.getXrayResult)
  29. g.POST("/", a.getXraySetting)
  30. g.POST("/warp/:action", a.warp)
  31. g.POST("/update", a.updateSetting)
  32. g.POST("/resetOutboundsTraffic", a.resetOutboundsTraffic)
  33. g.POST("/testOutbound", a.testOutbound)
  34. }
  35. // getXraySetting retrieves the Xray configuration template, inbound tags, and outbound test URL.
  36. func (a *XraySettingController) getXraySetting(c *gin.Context) {
  37. xraySetting, err := a.SettingService.GetXrayConfigTemplate()
  38. if err != nil {
  39. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  40. return
  41. }
  42. inboundTags, err := a.InboundService.GetInboundTags()
  43. if err != nil {
  44. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  45. return
  46. }
  47. outboundTestUrl, _ := a.SettingService.GetXrayOutboundTestUrl()
  48. if outboundTestUrl == "" {
  49. outboundTestUrl = "https://www.google.com/generate_204"
  50. }
  51. xrayResponse := map[string]interface{}{
  52. "xraySetting": json.RawMessage(xraySetting),
  53. "inboundTags": json.RawMessage(inboundTags),
  54. "outboundTestUrl": outboundTestUrl,
  55. }
  56. result, err := json.Marshal(xrayResponse)
  57. if err != nil {
  58. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  59. return
  60. }
  61. jsonObj(c, string(result), nil)
  62. }
  63. // updateSetting updates the Xray configuration settings.
  64. func (a *XraySettingController) updateSetting(c *gin.Context) {
  65. xraySetting := c.PostForm("xraySetting")
  66. if err := a.XraySettingService.SaveXraySetting(xraySetting); err != nil {
  67. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  68. return
  69. }
  70. outboundTestUrl := c.PostForm("outboundTestUrl")
  71. if outboundTestUrl == "" {
  72. outboundTestUrl = "https://www.google.com/generate_204"
  73. }
  74. _ = a.SettingService.SetXrayOutboundTestUrl(outboundTestUrl)
  75. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), nil)
  76. }
  77. // getDefaultXrayConfig retrieves the default Xray configuration.
  78. func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) {
  79. defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig()
  80. if err != nil {
  81. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  82. return
  83. }
  84. jsonObj(c, defaultJsonConfig, nil)
  85. }
  86. // getXrayResult retrieves the current Xray service result.
  87. func (a *XraySettingController) getXrayResult(c *gin.Context) {
  88. jsonObj(c, a.XrayService.GetXrayResult(), nil)
  89. }
  90. // warp handles Warp-related operations based on the action parameter.
  91. func (a *XraySettingController) warp(c *gin.Context) {
  92. action := c.Param("action")
  93. var resp string
  94. var err error
  95. switch action {
  96. case "data":
  97. resp, err = a.WarpService.GetWarpData()
  98. case "del":
  99. err = a.WarpService.DelWarpData()
  100. case "config":
  101. resp, err = a.WarpService.GetWarpConfig()
  102. case "reg":
  103. skey := c.PostForm("privateKey")
  104. pkey := c.PostForm("publicKey")
  105. resp, err = a.WarpService.RegWarp(skey, pkey)
  106. case "license":
  107. license := c.PostForm("license")
  108. resp, err = a.WarpService.SetWarpLicense(license)
  109. }
  110. jsonObj(c, resp, err)
  111. }
  112. // getOutboundsTraffic retrieves the traffic statistics for outbounds.
  113. func (a *XraySettingController) getOutboundsTraffic(c *gin.Context) {
  114. outboundsTraffic, err := a.OutboundService.GetOutboundsTraffic()
  115. if err != nil {
  116. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getOutboundTrafficError"), err)
  117. return
  118. }
  119. jsonObj(c, outboundsTraffic, nil)
  120. }
  121. // resetOutboundsTraffic resets the traffic statistics for the specified outbound tag.
  122. func (a *XraySettingController) resetOutboundsTraffic(c *gin.Context) {
  123. tag := c.PostForm("tag")
  124. err := a.OutboundService.ResetOutboundTraffic(tag)
  125. if err != nil {
  126. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.resetOutboundTrafficError"), err)
  127. return
  128. }
  129. jsonObj(c, "", nil)
  130. }
  131. // testOutbound tests an outbound configuration and returns the delay/response time.
  132. // Optional form "allOutbounds": JSON array of all outbounds; used to resolve sockopt.dialerProxy dependencies.
  133. func (a *XraySettingController) testOutbound(c *gin.Context) {
  134. outboundJSON := c.PostForm("outbound")
  135. allOutboundsJSON := c.PostForm("allOutbounds")
  136. if outboundJSON == "" {
  137. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("outbound parameter is required"))
  138. return
  139. }
  140. // Load the test URL from server settings to prevent SSRF via user-controlled URLs
  141. testURL, _ := a.SettingService.GetXrayOutboundTestUrl()
  142. result, err := a.OutboundService.TestOutbound(outboundJSON, testURL, allOutboundsJSON)
  143. if err != nil {
  144. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  145. return
  146. }
  147. jsonObj(c, result, nil)
  148. }