inbound.go 11 KB

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