inbound.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package controller
  2. import (
  3. "fmt"
  4. "strconv"
  5. "x-ui/database/model"
  6. "x-ui/logger"
  7. "x-ui/web/global"
  8. "x-ui/web/service"
  9. "x-ui/web/session"
  10. "github.com/gin-gonic/gin"
  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("/clientIps/:email", a.getClientIps)
  29. g.POST("/clearClientIps/:email", a.clearClientIps)
  30. g.POST("/resetClientTraffic/:email", a.resetClientTraffic)
  31. }
  32. func (a *InboundController) startTask() {
  33. webServer := global.GetWebServer()
  34. c := webServer.GetCron()
  35. c.AddFunc("@every 10s", func() {
  36. if a.xrayService.IsNeedRestartAndSetFalse() {
  37. err := a.xrayService.RestartXray(false)
  38. if err != nil {
  39. logger.Error("restart xray failed:", err)
  40. }
  41. }
  42. })
  43. }
  44. func (a *InboundController) getInbounds(c *gin.Context) {
  45. user := session.GetLoginUser(c)
  46. inbounds, err := a.inboundService.GetInbounds(user.Id)
  47. if err != nil {
  48. jsonMsg(c, I18n(c, "pages.inbounds.toasts.obtain"), err)
  49. return
  50. }
  51. jsonObj(c, inbounds, nil)
  52. }
  53. func (a *InboundController) getInbound(c *gin.Context) {
  54. id, err := strconv.Atoi(c.Param("id"))
  55. if err != nil {
  56. jsonMsg(c, I18n(c, "get"), err)
  57. return
  58. }
  59. inbound, err := a.inboundService.GetInbound(id)
  60. if err != nil {
  61. jsonMsg(c, I18n(c, "pages.inbounds.toasts.obtain"), err)
  62. return
  63. }
  64. jsonObj(c, inbound, nil)
  65. }
  66. func (a *InboundController) addInbound(c *gin.Context) {
  67. inbound := &model.Inbound{}
  68. err := c.ShouldBind(inbound)
  69. if err != nil {
  70. jsonMsg(c, I18n(c, "pages.inbounds.addTo"), err)
  71. return
  72. }
  73. user := session.GetLoginUser(c)
  74. inbound.UserId = user.Id
  75. inbound.Enable = true
  76. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  77. inbound, err = a.inboundService.AddInbound(inbound)
  78. jsonMsgObj(c, I18n(c, "pages.inbounds.addTo"), inbound, err)
  79. if err == nil {
  80. a.xrayService.SetToNeedRestart()
  81. }
  82. }
  83. func (a *InboundController) delInbound(c *gin.Context) {
  84. id, err := strconv.Atoi(c.Param("id"))
  85. if err != nil {
  86. jsonMsg(c, I18n(c, "delete"), err)
  87. return
  88. }
  89. err = a.inboundService.DelInbound(id)
  90. jsonMsgObj(c, I18n(c, "delete"), id, err)
  91. if err == nil {
  92. a.xrayService.SetToNeedRestart()
  93. }
  94. }
  95. func (a *InboundController) updateInbound(c *gin.Context) {
  96. id, err := strconv.Atoi(c.Param("id"))
  97. if err != nil {
  98. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  99. return
  100. }
  101. inbound := &model.Inbound{
  102. Id: id,
  103. }
  104. err = c.ShouldBind(inbound)
  105. if err != nil {
  106. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  107. return
  108. }
  109. inbound, err = a.inboundService.UpdateInbound(inbound)
  110. jsonMsgObj(c, I18n(c, "pages.inbounds.revise"), inbound, err)
  111. if err == nil {
  112. a.xrayService.SetToNeedRestart()
  113. }
  114. }
  115. func (a *InboundController) getClientIps(c *gin.Context) {
  116. email := c.Param("email")
  117. ips , err := a.inboundService.GetInboundClientIps(email)
  118. if err != nil {
  119. jsonObj(c, "No IP Record", nil)
  120. return
  121. }
  122. jsonObj(c, ips, nil)
  123. }
  124. func (a *InboundController) clearClientIps(c *gin.Context) {
  125. email := c.Param("email")
  126. err := a.inboundService.ClearClientIps(email)
  127. if err != nil {
  128. jsonMsg(c, "修改", err)
  129. return
  130. }
  131. jsonMsg(c, "Log Cleared", nil)
  132. }
  133. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  134. email := c.Param("email")
  135. err := a.inboundService.ResetClientTraffic(email)
  136. if err != nil {
  137. jsonMsg(c, "something worng!", err)
  138. return
  139. }
  140. jsonMsg(c, "traffic reseted", nil)
  141. }