xui.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "go.uber.org/atomic"
  5. "log"
  6. "strconv"
  7. "x-ui/database/model"
  8. "x-ui/logger"
  9. "x-ui/web/global"
  10. "x-ui/web/service"
  11. "x-ui/web/session"
  12. )
  13. type XUIController struct {
  14. BaseController
  15. inboundService service.InboundService
  16. xrayService service.XrayService
  17. isNeedXrayRestart atomic.Bool
  18. }
  19. func NewXUIController(g *gin.RouterGroup) *XUIController {
  20. a := &XUIController{}
  21. a.initRouter(g)
  22. a.startTask()
  23. return a
  24. }
  25. func (a *XUIController) initRouter(g *gin.RouterGroup) {
  26. g = g.Group("/xui")
  27. g.Use(a.checkLogin)
  28. g.GET("/", a.index)
  29. g.GET("/inbounds", a.inbounds)
  30. g.POST("/inbounds", a.getInbounds)
  31. g.POST("/inbound/add", a.addInbound)
  32. g.POST("/inbound/del/:id", a.delInbound)
  33. g.POST("/inbound/update/:id", a.updateInbound)
  34. g.GET("/setting", a.setting)
  35. }
  36. func (a *XUIController) startTask() {
  37. webServer := global.GetWebServer()
  38. c := webServer.GetCron()
  39. c.AddFunc("@every 10s", func() {
  40. if a.isNeedXrayRestart.Load() {
  41. err := a.xrayService.RestartXray()
  42. if err != nil {
  43. logger.Error("restart xray failed:", err)
  44. }
  45. a.isNeedXrayRestart.Store(false)
  46. }
  47. })
  48. }
  49. func (a *XUIController) index(c *gin.Context) {
  50. html(c, "index.html", "系统状态", nil)
  51. }
  52. func (a *XUIController) inbounds(c *gin.Context) {
  53. html(c, "inbounds.html", "入站列表", nil)
  54. }
  55. func (a *XUIController) setting(c *gin.Context) {
  56. html(c, "setting.html", "设置", nil)
  57. }
  58. func (a *XUIController) getInbounds(c *gin.Context) {
  59. user := session.GetLoginUser(c)
  60. inbounds, err := a.inboundService.GetInbounds(user.Id)
  61. if err != nil {
  62. jsonMsg(c, "获取", err)
  63. return
  64. }
  65. jsonObj(c, inbounds, nil)
  66. }
  67. func (a *XUIController) addInbound(c *gin.Context) {
  68. inbound := &model.Inbound{}
  69. err := c.ShouldBind(inbound)
  70. if err != nil {
  71. jsonMsg(c, "添加", err)
  72. return
  73. }
  74. user := session.GetLoginUser(c)
  75. inbound.UserId = user.Id
  76. inbound.Enable = true
  77. log.Println(inbound)
  78. err = a.inboundService.AddInbound(inbound)
  79. jsonMsg(c, "添加", err)
  80. if err == nil {
  81. a.isNeedXrayRestart.Store(true)
  82. }
  83. }
  84. func (a *XUIController) delInbound(c *gin.Context) {
  85. id, err := strconv.Atoi(c.Param("id"))
  86. if err != nil {
  87. jsonMsg(c, "删除", err)
  88. return
  89. }
  90. err = a.inboundService.DelInbound(id)
  91. jsonMsg(c, "删除", err)
  92. if err == nil {
  93. a.isNeedXrayRestart.Store(true)
  94. }
  95. }
  96. func (a *XUIController) updateInbound(c *gin.Context) {
  97. id, err := strconv.Atoi(c.Param("id"))
  98. if err != nil {
  99. jsonMsg(c, "修改", err)
  100. return
  101. }
  102. inbound := &model.Inbound{
  103. Id: id,
  104. }
  105. err = c.ShouldBind(inbound)
  106. if err != nil {
  107. jsonMsg(c, "修改", err)
  108. return
  109. }
  110. err = a.inboundService.UpdateInbound(inbound)
  111. jsonMsg(c, "修改", err)
  112. if err == nil {
  113. a.isNeedXrayRestart.Store(true)
  114. }
  115. }