xray_setting.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. NordService service.NordService
  17. }
  18. // NewXraySettingController creates a new XraySettingController and initializes its routes.
  19. func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
  20. a := &XraySettingController{}
  21. a.initRouter(g)
  22. return a
  23. }
  24. // initRouter sets up the routes for Xray settings management.
  25. func (a *XraySettingController) initRouter(g *gin.RouterGroup) {
  26. g = g.Group("/xray")
  27. g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
  28. g.GET("/getOutboundsTraffic", a.getOutboundsTraffic)
  29. g.GET("/getXrayResult", a.getXrayResult)
  30. g.POST("/", a.getXraySetting)
  31. g.POST("/warp/:action", a.warp)
  32. g.POST("/nord/:action", a.nord)
  33. g.POST("/update", a.updateSetting)
  34. g.POST("/resetOutboundsTraffic", a.resetOutboundsTraffic)
  35. g.POST("/testOutbound", a.testOutbound)
  36. }
  37. // getXraySetting retrieves the Xray configuration template, inbound tags, and outbound test URL.
  38. func (a *XraySettingController) getXraySetting(c *gin.Context) {
  39. xraySetting, err := a.SettingService.GetXrayConfigTemplate()
  40. if err != nil {
  41. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  42. return
  43. }
  44. // Older versions of this handler embedded the raw DB value as
  45. // `xraySetting` in the response without checking if the value
  46. // already had that wrapper shape. When the frontend saved it
  47. // back through the textarea verbatim, the wrapper got persisted
  48. // and every subsequent save nested another layer, which is what
  49. // eventually produced the blank Xray Settings page in #4059.
  50. // Strip any such wrapper here, and heal the DB if we found one so
  51. // the next read is O(1) instead of climbing the same pile again.
  52. if unwrapped := service.UnwrapXrayTemplateConfig(xraySetting); unwrapped != xraySetting {
  53. if saveErr := a.XraySettingService.SaveXraySetting(unwrapped); saveErr == nil {
  54. xraySetting = unwrapped
  55. } else {
  56. // Don't fail the read — just serve the unwrapped value
  57. // and leave the DB healing for a later save.
  58. xraySetting = unwrapped
  59. }
  60. }
  61. inboundTags, err := a.InboundService.GetInboundTags()
  62. if err != nil {
  63. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  64. return
  65. }
  66. outboundTestUrl, _ := a.SettingService.GetXrayOutboundTestUrl()
  67. if outboundTestUrl == "" {
  68. outboundTestUrl = "https://www.google.com/generate_204"
  69. }
  70. xrayResponse := map[string]interface{}{
  71. "xraySetting": json.RawMessage(xraySetting),
  72. "inboundTags": json.RawMessage(inboundTags),
  73. "outboundTestUrl": outboundTestUrl,
  74. }
  75. result, err := json.Marshal(xrayResponse)
  76. if err != nil {
  77. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  78. return
  79. }
  80. jsonObj(c, string(result), nil)
  81. }
  82. // updateSetting updates the Xray configuration settings.
  83. func (a *XraySettingController) updateSetting(c *gin.Context) {
  84. xraySetting := c.PostForm("xraySetting")
  85. if err := a.XraySettingService.SaveXraySetting(xraySetting); err != nil {
  86. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  87. return
  88. }
  89. outboundTestUrl := c.PostForm("outboundTestUrl")
  90. if outboundTestUrl == "" {
  91. outboundTestUrl = "https://www.google.com/generate_204"
  92. }
  93. _ = a.SettingService.SetXrayOutboundTestUrl(outboundTestUrl)
  94. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), nil)
  95. }
  96. // getDefaultXrayConfig retrieves the default Xray configuration.
  97. func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) {
  98. defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig()
  99. if err != nil {
  100. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  101. return
  102. }
  103. jsonObj(c, defaultJsonConfig, nil)
  104. }
  105. // getXrayResult retrieves the current Xray service result.
  106. func (a *XraySettingController) getXrayResult(c *gin.Context) {
  107. jsonObj(c, a.XrayService.GetXrayResult(), nil)
  108. }
  109. // warp handles Warp-related operations based on the action parameter.
  110. func (a *XraySettingController) warp(c *gin.Context) {
  111. action := c.Param("action")
  112. var resp string
  113. var err error
  114. switch action {
  115. case "data":
  116. resp, err = a.WarpService.GetWarpData()
  117. case "del":
  118. err = a.WarpService.DelWarpData()
  119. case "config":
  120. resp, err = a.WarpService.GetWarpConfig()
  121. case "reg":
  122. skey := c.PostForm("privateKey")
  123. pkey := c.PostForm("publicKey")
  124. resp, err = a.WarpService.RegWarp(skey, pkey)
  125. case "license":
  126. license := c.PostForm("license")
  127. resp, err = a.WarpService.SetWarpLicense(license)
  128. }
  129. jsonObj(c, resp, err)
  130. }
  131. // nord handles NordVPN-related operations based on the action parameter.
  132. func (a *XraySettingController) nord(c *gin.Context) {
  133. action := c.Param("action")
  134. var resp string
  135. var err error
  136. switch action {
  137. case "countries":
  138. resp, err = a.NordService.GetCountries()
  139. case "servers":
  140. countryId := c.PostForm("countryId")
  141. resp, err = a.NordService.GetServers(countryId)
  142. case "reg":
  143. token := c.PostForm("token")
  144. resp, err = a.NordService.GetCredentials(token)
  145. case "setKey":
  146. key := c.PostForm("key")
  147. resp, err = a.NordService.SetKey(key)
  148. case "data":
  149. resp, err = a.NordService.GetNordData()
  150. case "del":
  151. err = a.NordService.DelNordData()
  152. }
  153. jsonObj(c, resp, err)
  154. }
  155. // getOutboundsTraffic retrieves the traffic statistics for outbounds.
  156. func (a *XraySettingController) getOutboundsTraffic(c *gin.Context) {
  157. outboundsTraffic, err := a.OutboundService.GetOutboundsTraffic()
  158. if err != nil {
  159. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getOutboundTrafficError"), err)
  160. return
  161. }
  162. jsonObj(c, outboundsTraffic, nil)
  163. }
  164. // resetOutboundsTraffic resets the traffic statistics for the specified outbound tag.
  165. func (a *XraySettingController) resetOutboundsTraffic(c *gin.Context) {
  166. tag := c.PostForm("tag")
  167. err := a.OutboundService.ResetOutboundTraffic(tag)
  168. if err != nil {
  169. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.resetOutboundTrafficError"), err)
  170. return
  171. }
  172. jsonObj(c, "", nil)
  173. }
  174. // testOutbound tests an outbound configuration and returns the delay/response time.
  175. // Optional form "allOutbounds": JSON array of all outbounds; used to resolve sockopt.dialerProxy dependencies.
  176. func (a *XraySettingController) testOutbound(c *gin.Context) {
  177. outboundJSON := c.PostForm("outbound")
  178. allOutboundsJSON := c.PostForm("allOutbounds")
  179. if outboundJSON == "" {
  180. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("outbound parameter is required"))
  181. return
  182. }
  183. // Load the test URL from server settings to prevent SSRF via user-controlled URLs
  184. testURL, _ := a.SettingService.GetXrayOutboundTestUrl()
  185. result, err := a.OutboundService.TestOutbound(outboundJSON, testURL, allOutboundsJSON)
  186. if err != nil {
  187. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  188. return
  189. }
  190. jsonObj(c, result, nil)
  191. }