inbound.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  79. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  80. } else {
  81. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  82. }
  83. needRestart := false
  84. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  85. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  86. if err == nil && needRestart {
  87. a.xrayService.SetToNeedRestart()
  88. }
  89. }
  90. func (a *InboundController) delInbound(c *gin.Context) {
  91. id, err := strconv.Atoi(c.Param("id"))
  92. if err != nil {
  93. jsonMsg(c, I18nWeb(c, "delete"), err)
  94. return
  95. }
  96. needRestart := true
  97. needRestart, err = a.inboundService.DelInbound(id)
  98. jsonMsgObj(c, I18nWeb(c, "delete"), id, err)
  99. if err == nil && needRestart {
  100. a.xrayService.SetToNeedRestart()
  101. }
  102. }
  103. func (a *InboundController) updateInbound(c *gin.Context) {
  104. id, err := strconv.Atoi(c.Param("id"))
  105. if err != nil {
  106. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  107. return
  108. }
  109. inbound := &model.Inbound{
  110. Id: id,
  111. }
  112. err = c.ShouldBind(inbound)
  113. if err != nil {
  114. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  115. return
  116. }
  117. needRestart := true
  118. inbound, needRestart, err = a.inboundService.UpdateInbound(inbound)
  119. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.update"), inbound, err)
  120. if err == nil && needRestart {
  121. a.xrayService.SetToNeedRestart()
  122. }
  123. }
  124. func (a *InboundController) getClientIps(c *gin.Context) {
  125. email := c.Param("email")
  126. ips, err := a.inboundService.GetInboundClientIps(email)
  127. if err != nil || ips == "" {
  128. jsonObj(c, "No IP Record", nil)
  129. return
  130. }
  131. jsonObj(c, ips, nil)
  132. }
  133. func (a *InboundController) clearClientIps(c *gin.Context) {
  134. email := c.Param("email")
  135. err := a.inboundService.ClearClientIps(email)
  136. if err != nil {
  137. jsonMsg(c, "Update", err)
  138. return
  139. }
  140. jsonMsg(c, "Log Cleared", nil)
  141. }
  142. func (a *InboundController) addInboundClient(c *gin.Context) {
  143. data := &model.Inbound{}
  144. err := c.ShouldBind(data)
  145. if err != nil {
  146. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  147. return
  148. }
  149. needRestart := true
  150. needRestart, err = a.inboundService.AddInboundClient(data)
  151. if err != nil {
  152. jsonMsg(c, "Something went wrong!", err)
  153. return
  154. }
  155. jsonMsg(c, "Client(s) added", nil)
  156. if needRestart {
  157. a.xrayService.SetToNeedRestart()
  158. }
  159. }
  160. func (a *InboundController) delInboundClient(c *gin.Context) {
  161. id, err := strconv.Atoi(c.Param("id"))
  162. if err != nil {
  163. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  164. return
  165. }
  166. clientId := c.Param("clientId")
  167. needRestart := true
  168. needRestart, err = a.inboundService.DelInboundClient(id, clientId)
  169. if err != nil {
  170. jsonMsg(c, "Something went wrong!", err)
  171. return
  172. }
  173. jsonMsg(c, "Client deleted", nil)
  174. if needRestart {
  175. a.xrayService.SetToNeedRestart()
  176. }
  177. }
  178. func (a *InboundController) updateInboundClient(c *gin.Context) {
  179. clientId := c.Param("clientId")
  180. inbound := &model.Inbound{}
  181. err := c.ShouldBind(inbound)
  182. if err != nil {
  183. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  184. return
  185. }
  186. needRestart := true
  187. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  188. if err != nil {
  189. jsonMsg(c, "Something went wrong!", err)
  190. return
  191. }
  192. jsonMsg(c, "Client updated", nil)
  193. if needRestart {
  194. a.xrayService.SetToNeedRestart()
  195. }
  196. }
  197. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  198. id, err := strconv.Atoi(c.Param("id"))
  199. if err != nil {
  200. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  201. return
  202. }
  203. email := c.Param("email")
  204. needRestart, err := a.inboundService.ResetClientTraffic(id, email)
  205. if err != nil {
  206. jsonMsg(c, "Something went wrong!", err)
  207. return
  208. }
  209. jsonMsg(c, "Traffic has been reset", nil)
  210. if needRestart {
  211. a.xrayService.SetToNeedRestart()
  212. }
  213. }
  214. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  215. err := a.inboundService.ResetAllTraffics()
  216. if err != nil {
  217. jsonMsg(c, "Something went wrong!", err)
  218. return
  219. } else {
  220. a.xrayService.SetToNeedRestart()
  221. }
  222. jsonMsg(c, "all traffic has been reset", nil)
  223. }
  224. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  225. id, err := strconv.Atoi(c.Param("id"))
  226. if err != nil {
  227. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  228. return
  229. }
  230. err = a.inboundService.ResetAllClientTraffics(id)
  231. if err != nil {
  232. jsonMsg(c, "Something went wrong!", err)
  233. return
  234. } else {
  235. a.xrayService.SetToNeedRestart()
  236. }
  237. jsonMsg(c, "All traffic from the client has been reset.", nil)
  238. }
  239. func (a *InboundController) importInbound(c *gin.Context) {
  240. inbound := &model.Inbound{}
  241. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  242. if err != nil {
  243. jsonMsg(c, "Something went wrong!", err)
  244. return
  245. }
  246. user := session.GetLoginUser(c)
  247. inbound.Id = 0
  248. inbound.UserId = user.Id
  249. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  250. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  251. } else {
  252. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  253. }
  254. for index := range inbound.ClientStats {
  255. inbound.ClientStats[index].Id = 0
  256. inbound.ClientStats[index].Enable = true
  257. }
  258. needRestart := false
  259. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  260. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  261. if err == nil && needRestart {
  262. a.xrayService.SetToNeedRestart()
  263. }
  264. }
  265. func (a *InboundController) delDepletedClients(c *gin.Context) {
  266. id, err := strconv.Atoi(c.Param("id"))
  267. if err != nil {
  268. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  269. return
  270. }
  271. err = a.inboundService.DelDepletedClients(id)
  272. if err != nil {
  273. jsonMsg(c, "Something went wrong!", err)
  274. return
  275. }
  276. jsonMsg(c, "All depleted clients are deleted", nil)
  277. }
  278. func (a *InboundController) onlines(c *gin.Context) {
  279. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  280. }