inbound.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.GET("/list", a.getInbounds)
  22. g.GET("/get/:id", a.getInbound)
  23. g.GET("/getClientTraffics/:email", a.getClientTraffics)
  24. g.GET("/getClientTrafficsById/:id", a.getClientTrafficsById)
  25. g.POST("/add", a.addInbound)
  26. g.POST("/del/:id", a.delInbound)
  27. g.POST("/update/:id", a.updateInbound)
  28. g.POST("/clientIps/:email", a.getClientIps)
  29. g.POST("/clearClientIps/:email", a.clearClientIps)
  30. g.POST("/addClient", a.addInboundClient)
  31. g.POST("/:id/delClient/:clientId", a.delInboundClient)
  32. g.POST("/updateClient/:clientId", a.updateInboundClient)
  33. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  34. g.POST("/resetAllTraffics", a.resetAllTraffics)
  35. g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
  36. g.POST("/delDepletedClients/:id", a.delDepletedClients)
  37. g.POST("/import", a.importInbound)
  38. g.POST("/onlines", a.onlines)
  39. g.POST("/lastOnline", a.lastOnline)
  40. g.POST("/updateClientTraffic/:email", a.updateClientTraffic)
  41. }
  42. func (a *InboundController) getInbounds(c *gin.Context) {
  43. user := session.GetLoginUser(c)
  44. inbounds, err := a.inboundService.GetInbounds(user.Id)
  45. if err != nil {
  46. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  47. return
  48. }
  49. jsonObj(c, inbounds, nil)
  50. }
  51. func (a *InboundController) getInbound(c *gin.Context) {
  52. id, err := strconv.Atoi(c.Param("id"))
  53. if err != nil {
  54. jsonMsg(c, I18nWeb(c, "get"), err)
  55. return
  56. }
  57. inbound, err := a.inboundService.GetInbound(id)
  58. if err != nil {
  59. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  60. return
  61. }
  62. jsonObj(c, inbound, nil)
  63. }
  64. func (a *InboundController) getClientTraffics(c *gin.Context) {
  65. email := c.Param("email")
  66. clientTraffics, err := a.inboundService.GetClientTrafficByEmail(email)
  67. if err != nil {
  68. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  69. return
  70. }
  71. jsonObj(c, clientTraffics, nil)
  72. }
  73. func (a *InboundController) getClientTrafficsById(c *gin.Context) {
  74. id := c.Param("id")
  75. clientTraffics, err := a.inboundService.GetClientTrafficByID(id)
  76. if err != nil {
  77. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  78. return
  79. }
  80. jsonObj(c, clientTraffics, nil)
  81. }
  82. func (a *InboundController) addInbound(c *gin.Context) {
  83. inbound := &model.Inbound{}
  84. err := c.ShouldBind(inbound)
  85. if err != nil {
  86. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), err)
  87. return
  88. }
  89. user := session.GetLoginUser(c)
  90. inbound.UserId = user.Id
  91. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  92. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  93. } else {
  94. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  95. }
  96. needRestart := false
  97. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  98. if err != nil {
  99. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  100. return
  101. }
  102. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, nil)
  103. if needRestart {
  104. a.xrayService.SetToNeedRestart()
  105. }
  106. }
  107. func (a *InboundController) delInbound(c *gin.Context) {
  108. id, err := strconv.Atoi(c.Param("id"))
  109. if err != nil {
  110. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), err)
  111. return
  112. }
  113. needRestart := true
  114. needRestart, err = a.inboundService.DelInbound(id)
  115. if err != nil {
  116. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  117. return
  118. }
  119. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), id, nil)
  120. if needRestart {
  121. a.xrayService.SetToNeedRestart()
  122. }
  123. }
  124. func (a *InboundController) updateInbound(c *gin.Context) {
  125. id, err := strconv.Atoi(c.Param("id"))
  126. if err != nil {
  127. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  128. return
  129. }
  130. inbound := &model.Inbound{
  131. Id: id,
  132. }
  133. err = c.ShouldBind(inbound)
  134. if err != nil {
  135. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  136. return
  137. }
  138. needRestart := true
  139. inbound, needRestart, err = a.inboundService.UpdateInbound(inbound)
  140. if err != nil {
  141. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  142. return
  143. }
  144. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), inbound, nil)
  145. if needRestart {
  146. a.xrayService.SetToNeedRestart()
  147. }
  148. }
  149. func (a *InboundController) getClientIps(c *gin.Context) {
  150. email := c.Param("email")
  151. ips, err := a.inboundService.GetInboundClientIps(email)
  152. if err != nil || ips == "" {
  153. jsonObj(c, "No IP Record", nil)
  154. return
  155. }
  156. jsonObj(c, ips, nil)
  157. }
  158. func (a *InboundController) clearClientIps(c *gin.Context) {
  159. email := c.Param("email")
  160. err := a.inboundService.ClearClientIps(email)
  161. if err != nil {
  162. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  163. return
  164. }
  165. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  166. }
  167. func (a *InboundController) addInboundClient(c *gin.Context) {
  168. data := &model.Inbound{}
  169. err := c.ShouldBind(data)
  170. if err != nil {
  171. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  172. return
  173. }
  174. needRestart := true
  175. needRestart, err = a.inboundService.AddInboundClient(data)
  176. if err != nil {
  177. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  178. return
  179. }
  180. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), nil)
  181. if needRestart {
  182. a.xrayService.SetToNeedRestart()
  183. }
  184. }
  185. func (a *InboundController) delInboundClient(c *gin.Context) {
  186. id, err := strconv.Atoi(c.Param("id"))
  187. if err != nil {
  188. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  189. return
  190. }
  191. clientId := c.Param("clientId")
  192. needRestart := true
  193. needRestart, err = a.inboundService.DelInboundClient(id, clientId)
  194. if err != nil {
  195. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  196. return
  197. }
  198. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  199. if needRestart {
  200. a.xrayService.SetToNeedRestart()
  201. }
  202. }
  203. func (a *InboundController) updateInboundClient(c *gin.Context) {
  204. clientId := c.Param("clientId")
  205. inbound := &model.Inbound{}
  206. err := c.ShouldBind(inbound)
  207. if err != nil {
  208. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  209. return
  210. }
  211. needRestart := true
  212. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  213. if err != nil {
  214. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  215. return
  216. }
  217. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  218. if needRestart {
  219. a.xrayService.SetToNeedRestart()
  220. }
  221. }
  222. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  223. id, err := strconv.Atoi(c.Param("id"))
  224. if err != nil {
  225. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  226. return
  227. }
  228. email := c.Param("email")
  229. needRestart, err := a.inboundService.ResetClientTraffic(id, email)
  230. if err != nil {
  231. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  232. return
  233. }
  234. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  235. if needRestart {
  236. a.xrayService.SetToNeedRestart()
  237. }
  238. }
  239. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  240. err := a.inboundService.ResetAllTraffics()
  241. if err != nil {
  242. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  243. return
  244. } else {
  245. a.xrayService.SetToNeedRestart()
  246. }
  247. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllTrafficSuccess"), nil)
  248. }
  249. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  250. id, err := strconv.Atoi(c.Param("id"))
  251. if err != nil {
  252. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  253. return
  254. }
  255. err = a.inboundService.ResetAllClientTraffics(id)
  256. if err != nil {
  257. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  258. return
  259. } else {
  260. a.xrayService.SetToNeedRestart()
  261. }
  262. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllClientTrafficSuccess"), nil)
  263. }
  264. func (a *InboundController) importInbound(c *gin.Context) {
  265. inbound := &model.Inbound{}
  266. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  267. if err != nil {
  268. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  269. return
  270. }
  271. user := session.GetLoginUser(c)
  272. inbound.Id = 0
  273. inbound.UserId = user.Id
  274. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  275. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  276. } else {
  277. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  278. }
  279. for index := range inbound.ClientStats {
  280. inbound.ClientStats[index].Id = 0
  281. inbound.ClientStats[index].Enable = true
  282. }
  283. needRestart := false
  284. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  285. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, err)
  286. if err == nil && needRestart {
  287. a.xrayService.SetToNeedRestart()
  288. }
  289. }
  290. func (a *InboundController) delDepletedClients(c *gin.Context) {
  291. id, err := strconv.Atoi(c.Param("id"))
  292. if err != nil {
  293. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  294. return
  295. }
  296. err = a.inboundService.DelDepletedClients(id)
  297. if err != nil {
  298. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  299. return
  300. }
  301. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.delDepletedClientsSuccess"), nil)
  302. }
  303. func (a *InboundController) onlines(c *gin.Context) {
  304. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  305. }
  306. func (a *InboundController) lastOnline(c *gin.Context) {
  307. data, err := a.inboundService.GetClientsLastOnline()
  308. jsonObj(c, data, err)
  309. }
  310. func (a *InboundController) updateClientTraffic(c *gin.Context) {
  311. email := c.Param("email")
  312. // Define the request structure for traffic update
  313. type TrafficUpdateRequest struct {
  314. Upload int64 `json:"upload"`
  315. Download int64 `json:"download"`
  316. }
  317. var request TrafficUpdateRequest
  318. err := c.ShouldBindJSON(&request)
  319. if err != nil {
  320. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  321. return
  322. }
  323. err = a.inboundService.UpdateClientTrafficByEmail(email, request.Upload, request.Download)
  324. if err != nil {
  325. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  326. return
  327. }
  328. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  329. }