1
0

inbound.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), 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, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), 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.toasts.inboundCreateSuccess"), 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. if err != nil {
  95. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  96. return
  97. }
  98. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, err)
  99. if err == nil && needRestart {
  100. a.xrayService.SetToNeedRestart()
  101. }
  102. }
  103. func (a *InboundController) delInbound(c *gin.Context) {
  104. id, err := strconv.Atoi(c.Param("id"))
  105. if err != nil {
  106. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), err)
  107. return
  108. }
  109. needRestart := true
  110. needRestart, err = a.inboundService.DelInbound(id)
  111. if err != nil {
  112. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  113. return
  114. }
  115. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), id, err)
  116. if err == nil && needRestart {
  117. a.xrayService.SetToNeedRestart()
  118. }
  119. }
  120. func (a *InboundController) updateInbound(c *gin.Context) {
  121. id, err := strconv.Atoi(c.Param("id"))
  122. if err != nil {
  123. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  124. return
  125. }
  126. inbound := &model.Inbound{
  127. Id: id,
  128. }
  129. err = c.ShouldBind(inbound)
  130. if err != nil {
  131. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  132. return
  133. }
  134. needRestart := true
  135. inbound, needRestart, err = a.inboundService.UpdateInbound(inbound)
  136. if err != nil {
  137. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  138. return
  139. }
  140. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), inbound, err)
  141. if err == nil && needRestart {
  142. a.xrayService.SetToNeedRestart()
  143. }
  144. }
  145. func (a *InboundController) getClientIps(c *gin.Context) {
  146. email := c.Param("email")
  147. ips, err := a.inboundService.GetInboundClientIps(email)
  148. if err != nil || ips == "" {
  149. jsonObj(c, "No IP Record", nil)
  150. return
  151. }
  152. jsonObj(c, ips, nil)
  153. }
  154. func (a *InboundController) clearClientIps(c *gin.Context) {
  155. email := c.Param("email")
  156. err := a.inboundService.ClearClientIps(email)
  157. if err != nil {
  158. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  159. return
  160. }
  161. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  162. }
  163. func (a *InboundController) addInboundClient(c *gin.Context) {
  164. data := &model.Inbound{}
  165. err := c.ShouldBind(data)
  166. if err != nil {
  167. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  168. return
  169. }
  170. needRestart := true
  171. needRestart, err = a.inboundService.AddInboundClient(data)
  172. if err != nil {
  173. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  174. return
  175. }
  176. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), nil)
  177. if needRestart {
  178. a.xrayService.SetToNeedRestart()
  179. }
  180. }
  181. func (a *InboundController) delInboundClient(c *gin.Context) {
  182. id, err := strconv.Atoi(c.Param("id"))
  183. if err != nil {
  184. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  185. return
  186. }
  187. clientId := c.Param("clientId")
  188. needRestart := true
  189. needRestart, err = a.inboundService.DelInboundClient(id, clientId)
  190. if err != nil {
  191. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  192. return
  193. }
  194. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  195. if needRestart {
  196. a.xrayService.SetToNeedRestart()
  197. }
  198. }
  199. func (a *InboundController) updateInboundClient(c *gin.Context) {
  200. clientId := c.Param("clientId")
  201. inbound := &model.Inbound{}
  202. err := c.ShouldBind(inbound)
  203. if err != nil {
  204. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  205. return
  206. }
  207. needRestart := true
  208. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  209. if err != nil {
  210. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  211. return
  212. }
  213. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  214. if needRestart {
  215. a.xrayService.SetToNeedRestart()
  216. }
  217. }
  218. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  219. id, err := strconv.Atoi(c.Param("id"))
  220. if err != nil {
  221. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  222. return
  223. }
  224. email := c.Param("email")
  225. needRestart, err := a.inboundService.ResetClientTraffic(id, email)
  226. if err != nil {
  227. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  228. return
  229. }
  230. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  231. if needRestart {
  232. a.xrayService.SetToNeedRestart()
  233. }
  234. }
  235. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  236. err := a.inboundService.ResetAllTraffics()
  237. if err != nil {
  238. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  239. return
  240. } else {
  241. a.xrayService.SetToNeedRestart()
  242. }
  243. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllTrafficSuccess"), nil)
  244. }
  245. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  246. id, err := strconv.Atoi(c.Param("id"))
  247. if err != nil {
  248. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  249. return
  250. }
  251. err = a.inboundService.ResetAllClientTraffics(id)
  252. if err != nil {
  253. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  254. return
  255. } else {
  256. a.xrayService.SetToNeedRestart()
  257. }
  258. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllClientTrafficSuccess"), nil)
  259. }
  260. func (a *InboundController) importInbound(c *gin.Context) {
  261. inbound := &model.Inbound{}
  262. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  263. if err != nil {
  264. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  265. return
  266. }
  267. user := session.GetLoginUser(c)
  268. inbound.Id = 0
  269. inbound.UserId = user.Id
  270. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  271. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  272. } else {
  273. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  274. }
  275. for index := range inbound.ClientStats {
  276. inbound.ClientStats[index].Id = 0
  277. inbound.ClientStats[index].Enable = true
  278. }
  279. needRestart := false
  280. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  281. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, err)
  282. if err == nil && needRestart {
  283. a.xrayService.SetToNeedRestart()
  284. }
  285. }
  286. func (a *InboundController) delDepletedClients(c *gin.Context) {
  287. id, err := strconv.Atoi(c.Param("id"))
  288. if err != nil {
  289. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  290. return
  291. }
  292. err = a.inboundService.DelDepletedClients(id)
  293. if err != nil {
  294. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  295. return
  296. }
  297. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.delDepletedClientsSuccess"), nil)
  298. }
  299. func (a *InboundController) onlines(c *gin.Context) {
  300. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  301. }