inbound.go 6.8 KB

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