1
0

xray_setting.go 3.0 KB

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