inbound.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. g.POST("/resetClientTraffic/:email", a.resetClientTraffic)
  29. }
  30. func (a *InboundController) startTask() {
  31. webServer := global.GetWebServer()
  32. c := webServer.GetCron()
  33. c.AddFunc("@every 10s", func() {
  34. if a.xrayService.IsNeedRestartAndSetFalse() {
  35. err := a.xrayService.RestartXray(false)
  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, I18n(c , "pages.inbounds.toasts.obtain"), err)
  47. return
  48. }
  49. jsonObj(c, inbounds, nil)
  50. }
  51. func (a *InboundController) getInbound(c *gin.Context) {
  52. id, err := strconv.Atoi(c.Param("id"))
  53. if err != nil {
  54. jsonMsg(c, I18n(c , "get"), err)
  55. return
  56. }
  57. inbound, err := a.inboundService.GetInbound(id)
  58. if err != nil {
  59. jsonMsg(c, I18n(c , "pages.inbounds.toasts.obtain"), err)
  60. return
  61. }
  62. jsonObj(c, inbound, nil)
  63. }
  64. func (a *InboundController) addInbound(c *gin.Context) {
  65. inbound := &model.Inbound{}
  66. err := c.ShouldBind(inbound)
  67. if err != nil {
  68. jsonMsg(c, I18n(c , "pages.inbounds.addTo"), err)
  69. return
  70. }
  71. user := session.GetLoginUser(c)
  72. inbound.UserId = user.Id
  73. inbound.Enable = true
  74. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  75. inbound, err = a.inboundService.AddInbound(inbound)
  76. jsonMsgObj(c, I18n(c , "pages.inbounds.addTo"), inbound, err)
  77. if err == nil {
  78. a.xrayService.SetToNeedRestart()
  79. }
  80. }
  81. func (a *InboundController) delInbound(c *gin.Context) {
  82. id, err := strconv.Atoi(c.Param("id"))
  83. if err != nil {
  84. jsonMsg(c, I18n(c , "delete"), err)
  85. return
  86. }
  87. err = a.inboundService.DelInbound(id)
  88. jsonMsgObj(c, I18n(c , "delete"), id, err)
  89. if err == nil {
  90. a.xrayService.SetToNeedRestart()
  91. }
  92. }
  93. func (a *InboundController) updateInbound(c *gin.Context) {
  94. id, err := strconv.Atoi(c.Param("id"))
  95. if err != nil {
  96. jsonMsg(c, I18n(c , "pages.inbounds.revise"), err)
  97. return
  98. }
  99. inbound := &model.Inbound{
  100. Id: id,
  101. }
  102. err = c.ShouldBind(inbound)
  103. if err != nil {
  104. jsonMsg(c, I18n(c , "pages.inbounds.revise"), err)
  105. return
  106. }
  107. inbound, err = a.inboundService.UpdateInbound(inbound)
  108. jsonMsgObj(c, I18n(c , "pages.inbounds.revise"), inbound, err)
  109. if err == nil {
  110. a.xrayService.SetToNeedRestart()
  111. }
  112. }
  113. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  114. email := c.Param("email")
  115. err := a.inboundService.ResetClientTraffic(email)
  116. if err != nil {
  117. jsonMsg(c, "something worng!", err)
  118. return
  119. }
  120. jsonMsg(c, "traffic reseted", nil)
  121. }