xray_setting.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. inboundTags, err := a.InboundService.GetInboundTags()
  45. if err != nil {
  46. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  47. return
  48. }
  49. outboundTestUrl, _ := a.SettingService.GetXrayOutboundTestUrl()
  50. if outboundTestUrl == "" {
  51. outboundTestUrl = "https://www.google.com/generate_204"
  52. }
  53. xrayResponse := map[string]interface{}{
  54. "xraySetting": json.RawMessage(xraySetting),
  55. "inboundTags": json.RawMessage(inboundTags),
  56. "outboundTestUrl": outboundTestUrl,
  57. }
  58. result, err := json.Marshal(xrayResponse)
  59. if err != nil {
  60. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  61. return
  62. }
  63. jsonObj(c, string(result), nil)
  64. }
  65. // updateSetting updates the Xray configuration settings.
  66. func (a *XraySettingController) updateSetting(c *gin.Context) {
  67. xraySetting := c.PostForm("xraySetting")
  68. if err := a.XraySettingService.SaveXraySetting(xraySetting); err != nil {
  69. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  70. return
  71. }
  72. outboundTestUrl := c.PostForm("outboundTestUrl")
  73. if outboundTestUrl == "" {
  74. outboundTestUrl = "https://www.google.com/generate_204"
  75. }
  76. _ = a.SettingService.SetXrayOutboundTestUrl(outboundTestUrl)
  77. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), nil)
  78. }
  79. // getDefaultXrayConfig retrieves the default Xray configuration.
  80. func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) {
  81. defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig()
  82. if err != nil {
  83. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  84. return
  85. }
  86. jsonObj(c, defaultJsonConfig, nil)
  87. }
  88. // getXrayResult retrieves the current Xray service result.
  89. func (a *XraySettingController) getXrayResult(c *gin.Context) {
  90. jsonObj(c, a.XrayService.GetXrayResult(), nil)
  91. }
  92. // warp handles Warp-related operations based on the action parameter.
  93. func (a *XraySettingController) warp(c *gin.Context) {
  94. action := c.Param("action")
  95. var resp string
  96. var err error
  97. switch action {
  98. case "data":
  99. resp, err = a.WarpService.GetWarpData()
  100. case "del":
  101. err = a.WarpService.DelWarpData()
  102. case "config":
  103. resp, err = a.WarpService.GetWarpConfig()
  104. case "reg":
  105. skey := c.PostForm("privateKey")
  106. pkey := c.PostForm("publicKey")
  107. resp, err = a.WarpService.RegWarp(skey, pkey)
  108. case "license":
  109. license := c.PostForm("license")
  110. resp, err = a.WarpService.SetWarpLicense(license)
  111. }
  112. jsonObj(c, resp, err)
  113. }
  114. // nord handles NordVPN-related operations based on the action parameter.
  115. func (a *XraySettingController) nord(c *gin.Context) {
  116. action := c.Param("action")
  117. var resp string
  118. var err error
  119. switch action {
  120. case "countries":
  121. resp, err = a.NordService.GetCountries()
  122. case "servers":
  123. countryId := c.PostForm("countryId")
  124. resp, err = a.NordService.GetServers(countryId)
  125. case "reg":
  126. token := c.PostForm("token")
  127. resp, err = a.NordService.GetCredentials(token)
  128. case "setKey":
  129. key := c.PostForm("key")
  130. resp, err = a.NordService.SetKey(key)
  131. case "data":
  132. resp, err = a.NordService.GetNordData()
  133. case "del":
  134. err = a.NordService.DelNordData()
  135. }
  136. jsonObj(c, resp, err)
  137. }
  138. // getOutboundsTraffic retrieves the traffic statistics for outbounds.
  139. func (a *XraySettingController) getOutboundsTraffic(c *gin.Context) {
  140. outboundsTraffic, err := a.OutboundService.GetOutboundsTraffic()
  141. if err != nil {
  142. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getOutboundTrafficError"), err)
  143. return
  144. }
  145. jsonObj(c, outboundsTraffic, nil)
  146. }
  147. // resetOutboundsTraffic resets the traffic statistics for the specified outbound tag.
  148. func (a *XraySettingController) resetOutboundsTraffic(c *gin.Context) {
  149. tag := c.PostForm("tag")
  150. err := a.OutboundService.ResetOutboundTraffic(tag)
  151. if err != nil {
  152. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.resetOutboundTrafficError"), err)
  153. return
  154. }
  155. jsonObj(c, "", nil)
  156. }
  157. // testOutbound tests an outbound configuration and returns the delay/response time.
  158. // Optional form "allOutbounds": JSON array of all outbounds; used to resolve sockopt.dialerProxy dependencies.
  159. func (a *XraySettingController) testOutbound(c *gin.Context) {
  160. outboundJSON := c.PostForm("outbound")
  161. allOutboundsJSON := c.PostForm("allOutbounds")
  162. if outboundJSON == "" {
  163. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("outbound parameter is required"))
  164. return
  165. }
  166. // Load the test URL from server settings to prevent SSRF via user-controlled URLs
  167. testURL, _ := a.SettingService.GetXrayOutboundTestUrl()
  168. result, err := a.OutboundService.TestOutbound(outboundJSON, testURL, allOutboundsJSON)
  169. if err != nil {
  170. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  171. return
  172. }
  173. jsonObj(c, result, nil)
  174. }