inbound.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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, I18nWeb(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, I18nWeb(c, "get"), err)
  63. return
  64. }
  65. inbound, err := a.inboundService.GetInbound(id)
  66. if err != nil {
  67. jsonMsg(c, I18nWeb(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, I18nWeb(c, "pages.inbounds.create"), 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, I18nWeb(c, "pages.inbounds.create"), 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, I18nWeb(c, "delete"), err)
  102. return
  103. }
  104. err = a.inboundService.DelInbound(id)
  105. jsonMsgObj(c, I18nWeb(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, I18nWeb(c, "pages.inbounds.update"), err)
  114. return
  115. }
  116. inbound := &model.Inbound{
  117. Id: id,
  118. }
  119. err = c.ShouldBind(inbound)
  120. if err != nil {
  121. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  122. return
  123. }
  124. inbound, err = a.inboundService.UpdateInbound(inbound)
  125. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.update"), 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, "Failed to get client IPs", nil)
  135. return
  136. }
  137. if ips == "" {
  138. jsonObj(c, "No IP Record", nil)
  139. return
  140. }
  141. jsonObj(c, ips, nil)
  142. }
  143. func (a *InboundController) clearClientIps(c *gin.Context) {
  144. email := c.Param("email")
  145. err := a.inboundService.ClearClientIps(email)
  146. if err != nil {
  147. jsonMsg(c, "Update", err)
  148. return
  149. }
  150. jsonMsg(c, "Log Cleared", nil)
  151. }
  152. func (a *InboundController) addInboundClient(c *gin.Context) {
  153. data := &model.Inbound{}
  154. err := c.ShouldBind(data)
  155. if err != nil {
  156. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  157. return
  158. }
  159. err = a.inboundService.AddInboundClient(data)
  160. if err != nil {
  161. jsonMsg(c, "Something went wrong!", err)
  162. return
  163. }
  164. jsonMsg(c, "Client(s) added", nil)
  165. if err == nil {
  166. a.xrayService.SetToNeedRestart()
  167. }
  168. }
  169. func (a *InboundController) delInboundClient(c *gin.Context) {
  170. id, err := strconv.Atoi(c.Param("id"))
  171. if err != nil {
  172. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  173. return
  174. }
  175. clientId := c.Param("clientId")
  176. err = a.inboundService.DelInboundClient(id, clientId)
  177. if err != nil {
  178. jsonMsg(c, "Something went wrong!", err)
  179. return
  180. }
  181. jsonMsg(c, "Client deleted", nil)
  182. if err == nil {
  183. a.xrayService.SetToNeedRestart()
  184. }
  185. }
  186. func (a *InboundController) updateInboundClient(c *gin.Context) {
  187. clientId := c.Param("clientId")
  188. inbound := &model.Inbound{}
  189. err := c.ShouldBind(inbound)
  190. if err != nil {
  191. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  192. return
  193. }
  194. err = a.inboundService.UpdateInboundClient(inbound, clientId)
  195. if err != nil {
  196. jsonMsg(c, "Something went wrong!", err)
  197. return
  198. }
  199. jsonMsg(c, "Client updated", nil)
  200. if err == nil {
  201. a.xrayService.SetToNeedRestart()
  202. }
  203. }
  204. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  205. id, err := strconv.Atoi(c.Param("id"))
  206. if err != nil {
  207. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  208. return
  209. }
  210. email := c.Param("email")
  211. err = a.inboundService.ResetClientTraffic(id, email)
  212. if err != nil {
  213. jsonMsg(c, "Something went wrong!", err)
  214. return
  215. }
  216. jsonMsg(c, "traffic reseted", nil)
  217. if err == nil {
  218. a.xrayService.SetToNeedRestart()
  219. }
  220. }
  221. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  222. err := a.inboundService.ResetAllTraffics()
  223. if err != nil {
  224. jsonMsg(c, "Something went wrong!", err)
  225. return
  226. }
  227. jsonMsg(c, "All traffics reseted", nil)
  228. }
  229. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  230. id, err := strconv.Atoi(c.Param("id"))
  231. if err != nil {
  232. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  233. return
  234. }
  235. err = a.inboundService.ResetAllClientTraffics(id)
  236. if err != nil {
  237. jsonMsg(c, "Something went wrong!", err)
  238. return
  239. }
  240. jsonMsg(c, "All traffics of client reseted", nil)
  241. }
  242. func (a *InboundController) delDepletedClients(c *gin.Context) {
  243. id, err := strconv.Atoi(c.Param("id"))
  244. if err != nil {
  245. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  246. return
  247. }
  248. err = a.inboundService.DelDepletedClients(id)
  249. if err != nil {
  250. jsonMsg(c, "Something went wrong!", err)
  251. return
  252. }
  253. jsonMsg(c, "All delpeted clients are deleted", nil)
  254. }