inbound.go 6.6 KB

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