inbound.go 8.2 KB

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