xray_setting.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package controller
  2. import (
  3. "github.com/mhsanaei/3x-ui/v2/web/service"
  4. "github.com/gin-gonic/gin"
  5. )
  6. // XraySettingController handles Xray configuration and settings operations.
  7. type XraySettingController struct {
  8. XraySettingService service.XraySettingService
  9. SettingService service.SettingService
  10. InboundService service.InboundService
  11. OutboundService service.OutboundService
  12. XrayService service.XrayService
  13. WarpService service.WarpService
  14. }
  15. // NewXraySettingController creates a new XraySettingController and initializes its routes.
  16. func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
  17. a := &XraySettingController{}
  18. a.initRouter(g)
  19. return a
  20. }
  21. // initRouter sets up the routes for Xray settings management.
  22. func (a *XraySettingController) initRouter(g *gin.RouterGroup) {
  23. g = g.Group("/xray")
  24. g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
  25. g.GET("/getOutboundsTraffic", a.getOutboundsTraffic)
  26. g.GET("/getXrayResult", a.getXrayResult)
  27. g.POST("/", a.getXraySetting)
  28. g.POST("/warp/:action", a.warp)
  29. g.POST("/update", a.updateSetting)
  30. g.POST("/resetOutboundsTraffic", a.resetOutboundsTraffic)
  31. }
  32. // getXraySetting retrieves the Xray configuration template and inbound tags.
  33. func (a *XraySettingController) getXraySetting(c *gin.Context) {
  34. xraySetting, err := a.SettingService.GetXrayConfigTemplate()
  35. if err != nil {
  36. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  37. return
  38. }
  39. inboundTags, err := a.InboundService.GetInboundTags()
  40. if err != nil {
  41. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  42. return
  43. }
  44. xrayResponse := "{ \"xraySetting\": " + xraySetting + ", \"inboundTags\": " + inboundTags + " }"
  45. jsonObj(c, xrayResponse, nil)
  46. }
  47. // updateSetting updates the Xray configuration settings.
  48. func (a *XraySettingController) updateSetting(c *gin.Context) {
  49. xraySetting := c.PostForm("xraySetting")
  50. err := a.XraySettingService.SaveXraySetting(xraySetting)
  51. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  52. }
  53. // getDefaultXrayConfig retrieves the default Xray configuration.
  54. func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) {
  55. defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig()
  56. if err != nil {
  57. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  58. return
  59. }
  60. jsonObj(c, defaultJsonConfig, nil)
  61. }
  62. // getXrayResult retrieves the current Xray service result.
  63. func (a *XraySettingController) getXrayResult(c *gin.Context) {
  64. jsonObj(c, a.XrayService.GetXrayResult(), nil)
  65. }
  66. // warp handles Warp-related operations based on the action parameter.
  67. func (a *XraySettingController) warp(c *gin.Context) {
  68. action := c.Param("action")
  69. var resp string
  70. var err error
  71. switch action {
  72. case "data":
  73. resp, err = a.WarpService.GetWarpData()
  74. case "del":
  75. err = a.WarpService.DelWarpData()
  76. case "config":
  77. resp, err = a.WarpService.GetWarpConfig()
  78. case "reg":
  79. skey := c.PostForm("privateKey")
  80. pkey := c.PostForm("publicKey")
  81. resp, err = a.WarpService.RegWarp(skey, pkey)
  82. case "license":
  83. license := c.PostForm("license")
  84. resp, err = a.WarpService.SetWarpLicense(license)
  85. }
  86. jsonObj(c, resp, err)
  87. }
  88. // getOutboundsTraffic retrieves the traffic statistics for outbounds.
  89. func (a *XraySettingController) getOutboundsTraffic(c *gin.Context) {
  90. outboundsTraffic, err := a.OutboundService.GetOutboundsTraffic()
  91. if err != nil {
  92. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getOutboundTrafficError"), err)
  93. return
  94. }
  95. jsonObj(c, outboundsTraffic, nil)
  96. }
  97. // resetOutboundsTraffic resets the traffic statistics for the specified outbound tag.
  98. func (a *XraySettingController) resetOutboundsTraffic(c *gin.Context) {
  99. tag := c.PostForm("tag")
  100. err := a.OutboundService.ResetOutboundTraffic(tag)
  101. if err != nil {
  102. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.resetOutboundTrafficError"), err)
  103. return
  104. }
  105. jsonObj(c, "", nil)
  106. }