inbound.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package controller
  2. import (
  3. "fmt"
  4. "strconv"
  5. "x-ui/database/model"
  6. "x-ui/web/service"
  7. "x-ui/web/session"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type InboundController struct {
  11. inboundService service.InboundService
  12. xrayService service.XrayService
  13. }
  14. func NewInboundController(g *gin.RouterGroup) *InboundController {
  15. a := &InboundController{}
  16. a.initRouter(g)
  17. return a
  18. }
  19. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  20. g = g.Group("/inbound")
  21. g.POST("/list", a.getInbounds)
  22. g.POST("/add", a.addInbound)
  23. g.POST("/del/:id", a.delInbound)
  24. g.POST("/update/:id", a.updateInbound)
  25. g.POST("/clientIps/:email", a.getClientIps)
  26. g.POST("/clearClientIps/:email", a.clearClientIps)
  27. g.POST("/addClient", a.addInboundClient)
  28. g.POST("/:id/delClient/:clientId", a.delInboundClient)
  29. g.POST("/updateClient/:clientId", a.updateInboundClient)
  30. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  31. g.POST("/resetAllTraffics", a.resetAllTraffics)
  32. g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
  33. g.POST("/delDepletedClients/:id", a.delDepletedClients)
  34. g.POST("/onlines", a.onlines)
  35. }
  36. func (a *InboundController) getInbounds(c *gin.Context) {
  37. user := session.GetLoginUser(c)
  38. inbounds, err := a.inboundService.GetInbounds(user.Id)
  39. if err != nil {
  40. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  41. return
  42. }
  43. jsonObj(c, inbounds, nil)
  44. }
  45. func (a *InboundController) getInbound(c *gin.Context) {
  46. id, err := strconv.Atoi(c.Param("id"))
  47. if err != nil {
  48. jsonMsg(c, I18nWeb(c, "get"), err)
  49. return
  50. }
  51. inbound, err := a.inboundService.GetInbound(id)
  52. if err != nil {
  53. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  54. return
  55. }
  56. jsonObj(c, inbound, nil)
  57. }
  58. func (a *InboundController) getClientTraffics(c *gin.Context) {
  59. email := c.Param("email")
  60. clientTraffics, err := a.inboundService.GetClientTrafficByEmail(email)
  61. if err != nil {
  62. jsonMsg(c, "Error getting traffics", err)
  63. return
  64. }
  65. jsonObj(c, clientTraffics, nil)
  66. }
  67. func (a *InboundController) addInbound(c *gin.Context) {
  68. inbound := &model.Inbound{}
  69. err := c.ShouldBind(inbound)
  70. if err != nil {
  71. jsonMsg(c, I18nWeb(c, "pages.inbounds.create"), err)
  72. return
  73. }
  74. user := session.GetLoginUser(c)
  75. inbound.UserId = user.Id
  76. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  77. needRestart := false
  78. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  79. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  80. if err == nil && needRestart {
  81. a.xrayService.SetToNeedRestart()
  82. }
  83. }
  84. func (a *InboundController) delInbound(c *gin.Context) {
  85. id, err := strconv.Atoi(c.Param("id"))
  86. if err != nil {
  87. jsonMsg(c, I18nWeb(c, "delete"), err)
  88. return
  89. }
  90. needRestart := true
  91. needRestart, err = a.inboundService.DelInbound(id)
  92. jsonMsgObj(c, I18nWeb(c, "delete"), id, err)
  93. if err == nil && needRestart {
  94. a.xrayService.SetToNeedRestart()
  95. }
  96. }
  97. func (a *InboundController) updateInbound(c *gin.Context) {
  98. id, err := strconv.Atoi(c.Param("id"))
  99. if err != nil {
  100. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  101. return
  102. }
  103. inbound := &model.Inbound{
  104. Id: id,
  105. }
  106. err = c.ShouldBind(inbound)
  107. if err != nil {
  108. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  109. return
  110. }
  111. needRestart := true
  112. inbound, needRestart, err = a.inboundService.UpdateInbound(inbound)
  113. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.update"), inbound, err)
  114. if err == nil && needRestart {
  115. a.xrayService.SetToNeedRestart()
  116. }
  117. }
  118. func (a *InboundController) getClientIps(c *gin.Context) {
  119. email := c.Param("email")
  120. ips, err := a.inboundService.GetInboundClientIps(email)
  121. if err != nil || ips == "" {
  122. jsonObj(c, "No IP Record", nil)
  123. return
  124. }
  125. jsonObj(c, ips, nil)
  126. }
  127. func (a *InboundController) clearClientIps(c *gin.Context) {
  128. email := c.Param("email")
  129. err := a.inboundService.ClearClientIps(email)
  130. if err != nil {
  131. jsonMsg(c, "Update", err)
  132. return
  133. }
  134. jsonMsg(c, "Log Cleared", nil)
  135. }
  136. func (a *InboundController) addInboundClient(c *gin.Context) {
  137. data := &model.Inbound{}
  138. err := c.ShouldBind(data)
  139. if err != nil {
  140. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  141. return
  142. }
  143. needRestart := true
  144. needRestart, err = a.inboundService.AddInboundClient(data)
  145. if err != nil {
  146. jsonMsg(c, "Something went wrong!", err)
  147. return
  148. }
  149. jsonMsg(c, "Client(s) added", nil)
  150. if err == nil && needRestart {
  151. a.xrayService.SetToNeedRestart()
  152. }
  153. }
  154. func (a *InboundController) delInboundClient(c *gin.Context) {
  155. id, err := strconv.Atoi(c.Param("id"))
  156. if err != nil {
  157. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  158. return
  159. }
  160. clientId := c.Param("clientId")
  161. needRestart := true
  162. needRestart, err = a.inboundService.DelInboundClient(id, clientId)
  163. if err != nil {
  164. jsonMsg(c, "Something went wrong!", err)
  165. return
  166. }
  167. jsonMsg(c, "Client deleted", nil)
  168. if err == nil && needRestart {
  169. a.xrayService.SetToNeedRestart()
  170. }
  171. }
  172. func (a *InboundController) updateInboundClient(c *gin.Context) {
  173. clientId := c.Param("clientId")
  174. inbound := &model.Inbound{}
  175. err := c.ShouldBind(inbound)
  176. if err != nil {
  177. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  178. return
  179. }
  180. needRestart := true
  181. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  182. if err != nil {
  183. jsonMsg(c, "Something went wrong!", err)
  184. return
  185. }
  186. jsonMsg(c, "Client updated", nil)
  187. if err == nil && needRestart {
  188. a.xrayService.SetToNeedRestart()
  189. }
  190. }
  191. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  192. id, err := strconv.Atoi(c.Param("id"))
  193. if err != nil {
  194. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  195. return
  196. }
  197. email := c.Param("email")
  198. needRestart := true
  199. needRestart, err = a.inboundService.ResetClientTraffic(id, email)
  200. if err != nil {
  201. jsonMsg(c, "Something went wrong!", err)
  202. return
  203. }
  204. jsonMsg(c, "traffic reseted", nil)
  205. if err == nil && needRestart {
  206. a.xrayService.SetToNeedRestart()
  207. }
  208. }
  209. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  210. err := a.inboundService.ResetAllTraffics()
  211. if err != nil {
  212. jsonMsg(c, "Something went wrong!", err)
  213. return
  214. } else {
  215. a.xrayService.SetToNeedRestart()
  216. }
  217. jsonMsg(c, "All traffics reseted", nil)
  218. }
  219. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  220. id, err := strconv.Atoi(c.Param("id"))
  221. if err != nil {
  222. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  223. return
  224. }
  225. err = a.inboundService.ResetAllClientTraffics(id)
  226. if err != nil {
  227. jsonMsg(c, "Something went wrong!", err)
  228. return
  229. } else {
  230. a.xrayService.SetToNeedRestart()
  231. }
  232. jsonMsg(c, "All traffics of client reseted", nil)
  233. }
  234. func (a *InboundController) delDepletedClients(c *gin.Context) {
  235. id, err := strconv.Atoi(c.Param("id"))
  236. if err != nil {
  237. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  238. return
  239. }
  240. err = a.inboundService.DelDepletedClients(id)
  241. if err != nil {
  242. jsonMsg(c, "Something went wrong!", err)
  243. return
  244. }
  245. jsonMsg(c, "All delpeted clients are deleted", nil)
  246. }
  247. func (a *InboundController) onlines(c *gin.Context) {
  248. jsonObj(c, a.inboundService.GetOnlineClinets(), nil)
  249. }