xray_setting.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package controller
  2. import (
  3. "x-ui/web/service"
  4. "github.com/gin-gonic/gin"
  5. )
  6. type XraySettingController struct {
  7. XraySettingService service.XraySettingService
  8. SettingService service.SettingService
  9. InboundService service.InboundService
  10. OutboundService service.OutboundService
  11. XrayService service.XrayService
  12. WarpService service.WarpService
  13. }
  14. func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
  15. a := &XraySettingController{}
  16. a.initRouter(g)
  17. return a
  18. }
  19. func (a *XraySettingController) initRouter(g *gin.RouterGroup) {
  20. g = g.Group("/xray")
  21. g.POST("/", a.getXraySetting)
  22. g.POST("/update", a.updateSetting)
  23. g.GET("/getXrayResult", a.getXrayResult)
  24. g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
  25. g.POST("/warp/:action", a.warp)
  26. g.GET("/getOutboundsTraffic", a.getOutboundsTraffic)
  27. g.POST("/resetOutboundsTraffic", a.resetOutboundsTraffic)
  28. }
  29. func (a *XraySettingController) getXraySetting(c *gin.Context) {
  30. xraySetting, err := a.SettingService.GetXrayConfigTemplate()
  31. if err != nil {
  32. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  33. return
  34. }
  35. inboundTags, err := a.InboundService.GetInboundTags()
  36. if err != nil {
  37. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  38. return
  39. }
  40. xrayResponse := "{ \"xraySetting\": " + xraySetting + ", \"inboundTags\": " + inboundTags + " }"
  41. jsonObj(c, xrayResponse, nil)
  42. }
  43. func (a *XraySettingController) updateSetting(c *gin.Context) {
  44. xraySetting := c.PostForm("xraySetting")
  45. err := a.XraySettingService.SaveXraySetting(xraySetting)
  46. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  47. }
  48. func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) {
  49. defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig()
  50. if err != nil {
  51. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  52. return
  53. }
  54. jsonObj(c, defaultJsonConfig, nil)
  55. }
  56. func (a *XraySettingController) getXrayResult(c *gin.Context) {
  57. jsonObj(c, a.XrayService.GetXrayResult(), nil)
  58. }
  59. func (a *XraySettingController) warp(c *gin.Context) {
  60. action := c.Param("action")
  61. var resp string
  62. var err error
  63. switch action {
  64. case "data":
  65. resp, err = a.WarpService.GetWarpData()
  66. case "del":
  67. err = a.WarpService.DelWarpData()
  68. case "config":
  69. resp, err = a.WarpService.GetWarpConfig()
  70. case "reg":
  71. skey := c.PostForm("privateKey")
  72. pkey := c.PostForm("publicKey")
  73. resp, err = a.WarpService.RegWarp(skey, pkey)
  74. case "license":
  75. license := c.PostForm("license")
  76. resp, err = a.WarpService.SetWarpLicense(license)
  77. }
  78. jsonObj(c, resp, err)
  79. }
  80. func (a *XraySettingController) getOutboundsTraffic(c *gin.Context) {
  81. outboundsTraffic, err := a.OutboundService.GetOutboundsTraffic()
  82. if err != nil {
  83. jsonMsg(c, "Error getting traffics", err)
  84. return
  85. }
  86. jsonObj(c, outboundsTraffic, nil)
  87. }
  88. func (a *XraySettingController) resetOutboundsTraffic(c *gin.Context) {
  89. tag := c.PostForm("tag")
  90. err := a.OutboundService.ResetOutboundTraffic(tag)
  91. if err != nil {
  92. jsonMsg(c, "Error in reset outbound traffics", err)
  93. return
  94. }
  95. jsonObj(c, "", nil)
  96. }