1
0

inbound.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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("/addClient/", a.addInboundClient)
  31. g.POST("/delClient/:email", a.delInboundClient)
  32. g.POST("/updateClient/:index", a.updateInboundClient)
  33. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  34. }
  35. func (a *InboundController) startTask() {
  36. webServer := global.GetWebServer()
  37. c := webServer.GetCron()
  38. c.AddFunc("@every 10s", func() {
  39. if a.xrayService.IsNeedRestartAndSetFalse() {
  40. err := a.xrayService.RestartXray(false)
  41. if err != nil {
  42. logger.Error("restart xray failed:", err)
  43. }
  44. }
  45. })
  46. }
  47. func (a *InboundController) getInbounds(c *gin.Context) {
  48. user := session.GetLoginUser(c)
  49. inbounds, err := a.inboundService.GetInbounds(user.Id)
  50. if err != nil {
  51. jsonMsg(c, I18n(c, "pages.inbounds.toasts.obtain"), err)
  52. return
  53. }
  54. jsonObj(c, inbounds, nil)
  55. }
  56. func (a *InboundController) getInbound(c *gin.Context) {
  57. id, err := strconv.Atoi(c.Param("id"))
  58. if err != nil {
  59. jsonMsg(c, I18n(c, "get"), err)
  60. return
  61. }
  62. inbound, err := a.inboundService.GetInbound(id)
  63. if err != nil {
  64. jsonMsg(c, I18n(c, "pages.inbounds.toasts.obtain"), err)
  65. return
  66. }
  67. jsonObj(c, inbound, nil)
  68. }
  69. func (a *InboundController) addInbound(c *gin.Context) {
  70. inbound := &model.Inbound{}
  71. err := c.ShouldBind(inbound)
  72. if err != nil {
  73. jsonMsg(c, I18n(c, "pages.inbounds.addTo"), err)
  74. return
  75. }
  76. user := session.GetLoginUser(c)
  77. inbound.UserId = user.Id
  78. inbound.Enable = true
  79. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  80. inbound, err = a.inboundService.AddInbound(inbound)
  81. jsonMsgObj(c, I18n(c, "pages.inbounds.addTo"), inbound, err)
  82. if err == nil {
  83. a.xrayService.SetToNeedRestart()
  84. }
  85. }
  86. func (a *InboundController) delInbound(c *gin.Context) {
  87. id, err := strconv.Atoi(c.Param("id"))
  88. if err != nil {
  89. jsonMsg(c, I18n(c, "delete"), err)
  90. return
  91. }
  92. err = a.inboundService.DelInbound(id)
  93. jsonMsgObj(c, I18n(c, "delete"), id, err)
  94. if err == nil {
  95. a.xrayService.SetToNeedRestart()
  96. }
  97. }
  98. func (a *InboundController) updateInbound(c *gin.Context) {
  99. id, err := strconv.Atoi(c.Param("id"))
  100. if err != nil {
  101. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  102. return
  103. }
  104. inbound := &model.Inbound{
  105. Id: id,
  106. }
  107. err = c.ShouldBind(inbound)
  108. if err != nil {
  109. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  110. return
  111. }
  112. inbound, err = a.inboundService.UpdateInbound(inbound)
  113. jsonMsgObj(c, I18n(c, "pages.inbounds.revise"), inbound, err)
  114. if err == nil {
  115. a.xrayService.SetToNeedRestart()
  116. }
  117. }
  118. func (a *InboundController) getClientIps(c *gin.Context) {
  119. email := c.Param("email")
  120. ips , err := a.inboundService.GetInboundClientIps(email)
  121. if err != nil {
  122. jsonObj(c, "No IP Record", nil)
  123. return
  124. }
  125. jsonObj(c, ips, nil)
  126. }
  127. func (a *InboundController) clearClientIps(c *gin.Context) {
  128. email := c.Param("email")
  129. err := a.inboundService.ClearClientIps(email)
  130. if err != nil {
  131. jsonMsg(c, "修改", err)
  132. return
  133. }
  134. jsonMsg(c, "Log Cleared", nil)
  135. }
  136. func (a *InboundController) addInboundClient(c *gin.Context) {
  137. inbound := &model.Inbound{}
  138. err := c.ShouldBind(inbound)
  139. if err != nil {
  140. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  141. return
  142. }
  143. err = a.inboundService.AddInboundClient(inbound)
  144. if err != nil {
  145. jsonMsg(c, "something worng!", err)
  146. return
  147. }
  148. jsonMsg(c, "Client added", nil)
  149. if err == nil {
  150. a.xrayService.SetToNeedRestart()
  151. }
  152. }
  153. func (a *InboundController) delInboundClient(c *gin.Context) {
  154. email := c.Param("email")
  155. inbound := &model.Inbound{}
  156. err := c.ShouldBind(inbound)
  157. if err != nil {
  158. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  159. return
  160. }
  161. err = a.inboundService.DelInboundClient(inbound, email)
  162. if err != nil {
  163. jsonMsg(c, "something worng!", err)
  164. return
  165. }
  166. jsonMsg(c, "Client deleted", nil)
  167. if err == nil {
  168. a.xrayService.SetToNeedRestart()
  169. }
  170. }
  171. func (a *InboundController) updateInboundClient(c *gin.Context) {
  172. index, err := strconv.Atoi(c.Param("index"))
  173. if err != nil {
  174. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  175. return
  176. }
  177. inbound := &model.Inbound{}
  178. err = c.ShouldBind(inbound)
  179. if err != nil {
  180. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  181. return
  182. }
  183. err = a.inboundService.UpdateInboundClient(inbound, index)
  184. if err != nil {
  185. jsonMsg(c, "something worng!", err)
  186. return
  187. }
  188. jsonMsg(c, "Client updated", nil)
  189. if err == nil {
  190. a.xrayService.SetToNeedRestart()
  191. }
  192. }
  193. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  194. id, err := strconv.Atoi(c.Param("id"))
  195. if err != nil {
  196. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  197. return
  198. }
  199. email := c.Param("email")
  200. err = a.inboundService.ResetClientTraffic(id, email)
  201. if err != nil {
  202. jsonMsg(c, "something worng!", err)
  203. return
  204. }
  205. jsonMsg(c, "traffic reseted", nil)
  206. if err == nil {
  207. a.xrayService.SetToNeedRestart()
  208. }
  209. }