inbound.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  91. needRestart := false
  92. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  93. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  94. if err == nil && needRestart {
  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. needRestart := true
  105. needRestart, err = a.inboundService.DelInbound(id)
  106. jsonMsgObj(c, I18nWeb(c, "delete"), id, err)
  107. if err == nil && needRestart {
  108. a.xrayService.SetToNeedRestart()
  109. }
  110. }
  111. func (a *InboundController) updateInbound(c *gin.Context) {
  112. id, err := strconv.Atoi(c.Param("id"))
  113. if err != nil {
  114. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  115. return
  116. }
  117. inbound := &model.Inbound{
  118. Id: id,
  119. }
  120. err = c.ShouldBind(inbound)
  121. if err != nil {
  122. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  123. return
  124. }
  125. needRestart := true
  126. inbound, needRestart, err = a.inboundService.UpdateInbound(inbound)
  127. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.update"), inbound, err)
  128. if err == nil && needRestart {
  129. a.xrayService.SetToNeedRestart()
  130. }
  131. }
  132. func (a *InboundController) getClientIps(c *gin.Context) {
  133. email := c.Param("email")
  134. ips, err := a.inboundService.GetInboundClientIps(email)
  135. if err != nil || ips == "" {
  136. jsonObj(c, "No IP Record", nil)
  137. return
  138. }
  139. jsonObj(c, ips, nil)
  140. }
  141. func (a *InboundController) clearClientIps(c *gin.Context) {
  142. email := c.Param("email")
  143. err := a.inboundService.ClearClientIps(email)
  144. if err != nil {
  145. jsonMsg(c, "Update", err)
  146. return
  147. }
  148. jsonMsg(c, "Log Cleared", nil)
  149. }
  150. func (a *InboundController) addInboundClient(c *gin.Context) {
  151. data := &model.Inbound{}
  152. err := c.ShouldBind(data)
  153. if err != nil {
  154. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  155. return
  156. }
  157. needRestart := true
  158. needRestart, err = a.inboundService.AddInboundClient(data)
  159. if err != nil {
  160. jsonMsg(c, "Something went wrong!", err)
  161. return
  162. }
  163. jsonMsg(c, "Client(s) added", nil)
  164. if err == nil && needRestart {
  165. a.xrayService.SetToNeedRestart()
  166. }
  167. }
  168. func (a *InboundController) delInboundClient(c *gin.Context) {
  169. id, err := strconv.Atoi(c.Param("id"))
  170. if err != nil {
  171. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  172. return
  173. }
  174. clientId := c.Param("clientId")
  175. needRestart := true
  176. needRestart, 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 && needRestart {
  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. needRestart := true
  195. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  196. if err != nil {
  197. jsonMsg(c, "Something went wrong!", err)
  198. return
  199. }
  200. jsonMsg(c, "Client updated", nil)
  201. if err == nil && needRestart {
  202. a.xrayService.SetToNeedRestart()
  203. }
  204. }
  205. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  206. id, err := strconv.Atoi(c.Param("id"))
  207. if err != nil {
  208. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  209. return
  210. }
  211. email := c.Param("email")
  212. needRestart := true
  213. needRestart, err = a.inboundService.ResetClientTraffic(id, email)
  214. if err != nil {
  215. jsonMsg(c, "Something went wrong!", err)
  216. return
  217. }
  218. jsonMsg(c, "traffic reseted", nil)
  219. if err == nil && needRestart {
  220. a.xrayService.SetToNeedRestart()
  221. }
  222. }
  223. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  224. err := a.inboundService.ResetAllTraffics()
  225. if err != nil {
  226. jsonMsg(c, "Something went wrong!", err)
  227. return
  228. } else {
  229. a.xrayService.SetToNeedRestart()
  230. }
  231. jsonMsg(c, "All traffics reseted", nil)
  232. }
  233. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  234. id, err := strconv.Atoi(c.Param("id"))
  235. if err != nil {
  236. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  237. return
  238. }
  239. err = a.inboundService.ResetAllClientTraffics(id)
  240. if err != nil {
  241. jsonMsg(c, "Something went wrong!", err)
  242. return
  243. } else {
  244. a.xrayService.SetToNeedRestart()
  245. }
  246. jsonMsg(c, "All traffics of client reseted", nil)
  247. }
  248. func (a *InboundController) delDepletedClients(c *gin.Context) {
  249. id, err := strconv.Atoi(c.Param("id"))
  250. if err != nil {
  251. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  252. return
  253. }
  254. err = a.inboundService.DelDepletedClients(id)
  255. if err != nil {
  256. jsonMsg(c, "Something went wrong!", err)
  257. return
  258. }
  259. jsonMsg(c, "All delpeted clients are deleted", nil)
  260. }