inbound.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. needRestart := false
  160. needRestart, err = a.inboundService.AddInboundClient(data)
  161. if err != nil {
  162. jsonMsg(c, "Something went wrong!", err)
  163. return
  164. }
  165. jsonMsg(c, "Client(s) added", nil)
  166. if err == nil && needRestart {
  167. a.xrayService.SetToNeedRestart()
  168. }
  169. }
  170. func (a *InboundController) delInboundClient(c *gin.Context) {
  171. id, err := strconv.Atoi(c.Param("id"))
  172. if err != nil {
  173. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  174. return
  175. }
  176. clientId := c.Param("clientId")
  177. needRestart := false
  178. needRestart, err = a.inboundService.DelInboundClient(id, clientId)
  179. if err != nil {
  180. jsonMsg(c, "Something went wrong!", err)
  181. return
  182. }
  183. jsonMsg(c, "Client deleted", nil)
  184. if err == nil && needRestart {
  185. a.xrayService.SetToNeedRestart()
  186. }
  187. }
  188. func (a *InboundController) updateInboundClient(c *gin.Context) {
  189. clientId := c.Param("clientId")
  190. inbound := &model.Inbound{}
  191. err := c.ShouldBind(inbound)
  192. if err != nil {
  193. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  194. return
  195. }
  196. needRestart := false
  197. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  198. if err != nil {
  199. jsonMsg(c, "Something went wrong!", err)
  200. return
  201. }
  202. jsonMsg(c, "Client updated", nil)
  203. if err == nil && needRestart {
  204. a.xrayService.SetToNeedRestart()
  205. }
  206. }
  207. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  208. id, err := strconv.Atoi(c.Param("id"))
  209. if err != nil {
  210. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  211. return
  212. }
  213. email := c.Param("email")
  214. needRestart := false
  215. needRestart, err = a.inboundService.ResetClientTraffic(id, email)
  216. if err != nil {
  217. jsonMsg(c, "Something went wrong!", err)
  218. return
  219. }
  220. jsonMsg(c, "traffic reseted", nil)
  221. if err == nil && needRestart {
  222. a.xrayService.SetToNeedRestart()
  223. }
  224. }
  225. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  226. err := a.inboundService.ResetAllTraffics()
  227. if err != nil {
  228. jsonMsg(c, "Something went wrong!", err)
  229. return
  230. } else {
  231. a.xrayService.SetToNeedRestart()
  232. }
  233. jsonMsg(c, "All traffics reseted", nil)
  234. }
  235. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  236. id, err := strconv.Atoi(c.Param("id"))
  237. if err != nil {
  238. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  239. return
  240. }
  241. err = a.inboundService.ResetAllClientTraffics(id)
  242. if err != nil {
  243. jsonMsg(c, "Something went wrong!", err)
  244. return
  245. } else {
  246. a.xrayService.SetToNeedRestart()
  247. }
  248. jsonMsg(c, "All traffics of client reseted", nil)
  249. }
  250. func (a *InboundController) delDepletedClients(c *gin.Context) {
  251. id, err := strconv.Atoi(c.Param("id"))
  252. if err != nil {
  253. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  254. return
  255. }
  256. err = a.inboundService.DelDepletedClients(id)
  257. if err != nil {
  258. jsonMsg(c, "Something went wrong!", err)
  259. return
  260. }
  261. jsonMsg(c, "All delpeted clients are deleted", nil)
  262. }