inbound.go 7.6 KB

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