inbound.go 6.2 KB

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