inbound.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "strconv"
  6. "x-ui/database/model"
  7. "x-ui/logger"
  8. "x-ui/web/global"
  9. "x-ui/web/service"
  10. "x-ui/web/session"
  11. )
  12. type InboundController struct {
  13. inboundService service.InboundService
  14. xrayService service.XrayService
  15. }
  16. func NewInboundController(g *gin.RouterGroup) *InboundController {
  17. a := &InboundController{}
  18. a.initRouter(g)
  19. a.startTask()
  20. return a
  21. }
  22. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  23. g = g.Group("/inbound")
  24. g.POST("/list", a.getInbounds)
  25. g.POST("/add", a.addInbound)
  26. g.POST("/del/:id", a.delInbound)
  27. g.POST("/update/:id", a.updateInbound)
  28. }
  29. func (a *InboundController) startTask() {
  30. webServer := global.GetWebServer()
  31. c := webServer.GetCron()
  32. c.AddFunc("@every 10s", func() {
  33. if a.xrayService.IsNeedRestart() {
  34. a.xrayService.SetIsNeedRestart(false)
  35. err := a.xrayService.RestartXray()
  36. if err != nil {
  37. logger.Error("restart xray failed:", err)
  38. }
  39. }
  40. })
  41. }
  42. func (a *InboundController) getInbounds(c *gin.Context) {
  43. user := session.GetLoginUser(c)
  44. inbounds, err := a.inboundService.GetInbounds(user.Id)
  45. if err != nil {
  46. jsonMsg(c, "获取", err)
  47. return
  48. }
  49. jsonObj(c, inbounds, nil)
  50. }
  51. func (a *InboundController) addInbound(c *gin.Context) {
  52. inbound := &model.Inbound{}
  53. err := c.ShouldBind(inbound)
  54. if err != nil {
  55. jsonMsg(c, "添加", err)
  56. return
  57. }
  58. user := session.GetLoginUser(c)
  59. inbound.UserId = user.Id
  60. inbound.Enable = true
  61. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  62. err = a.inboundService.AddInbound(inbound)
  63. jsonMsg(c, "添加", err)
  64. if err == nil {
  65. a.xrayService.SetIsNeedRestart(true)
  66. }
  67. }
  68. func (a *InboundController) delInbound(c *gin.Context) {
  69. id, err := strconv.Atoi(c.Param("id"))
  70. if err != nil {
  71. jsonMsg(c, "删除", err)
  72. return
  73. }
  74. err = a.inboundService.DelInbound(id)
  75. jsonMsg(c, "删除", err)
  76. if err == nil {
  77. a.xrayService.SetIsNeedRestart(true)
  78. }
  79. }
  80. func (a *InboundController) updateInbound(c *gin.Context) {
  81. id, err := strconv.Atoi(c.Param("id"))
  82. if err != nil {
  83. jsonMsg(c, "修改", err)
  84. return
  85. }
  86. inbound := &model.Inbound{
  87. Id: id,
  88. }
  89. err = c.ShouldBind(inbound)
  90. if err != nil {
  91. jsonMsg(c, "修改", err)
  92. return
  93. }
  94. err = a.inboundService.UpdateInbound(inbound)
  95. jsonMsg(c, "修改", err)
  96. if err == nil {
  97. a.xrayService.SetIsNeedRestart(true)
  98. }
  99. }