inbound.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "github.com/mhsanaei/3x-ui/v2/database/model"
  7. "github.com/mhsanaei/3x-ui/v2/web/service"
  8. "github.com/mhsanaei/3x-ui/v2/web/session"
  9. "github.com/mhsanaei/3x-ui/v2/web/websocket"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // InboundController handles HTTP requests related to Xray inbounds management.
  13. type InboundController struct {
  14. inboundService service.InboundService
  15. xrayService service.XrayService
  16. }
  17. // NewInboundController creates a new InboundController and sets up its routes.
  18. func NewInboundController(g *gin.RouterGroup) *InboundController {
  19. a := &InboundController{}
  20. a.initRouter(g)
  21. return a
  22. }
  23. // initRouter initializes the routes for inbound-related operations.
  24. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  25. g.GET("/list", a.getInbounds)
  26. g.GET("/get/:id", a.getInbound)
  27. g.GET("/getClientTraffics/:email", a.getClientTraffics)
  28. g.GET("/getClientTrafficsById/:id", a.getClientTrafficsById)
  29. g.POST("/add", a.addInbound)
  30. g.POST("/del/:id", a.delInbound)
  31. g.POST("/update/:id", a.updateInbound)
  32. g.POST("/clientIps/:email", a.getClientIps)
  33. g.POST("/clearClientIps/:email", a.clearClientIps)
  34. g.POST("/addClient", a.addInboundClient)
  35. g.POST("/:id/delClient/:clientId", a.delInboundClient)
  36. g.POST("/updateClient/:clientId", a.updateInboundClient)
  37. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  38. g.POST("/resetAllTraffics", a.resetAllTraffics)
  39. g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
  40. g.POST("/delDepletedClients/:id", a.delDepletedClients)
  41. g.POST("/import", a.importInbound)
  42. g.POST("/onlines", a.onlines)
  43. g.POST("/lastOnline", a.lastOnline)
  44. g.POST("/updateClientTraffic/:email", a.updateClientTraffic)
  45. g.POST("/:id/delClientByEmail/:email", a.delInboundClientByEmail)
  46. }
  47. // getInbounds retrieves the list of inbounds for the logged-in user.
  48. func (a *InboundController) getInbounds(c *gin.Context) {
  49. user := session.GetLoginUser(c)
  50. inbounds, err := a.inboundService.GetInbounds(user.Id)
  51. if err != nil {
  52. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  53. return
  54. }
  55. jsonObj(c, inbounds, nil)
  56. }
  57. // getInbound retrieves a specific inbound by its ID.
  58. func (a *InboundController) getInbound(c *gin.Context) {
  59. id, err := strconv.Atoi(c.Param("id"))
  60. if err != nil {
  61. jsonMsg(c, I18nWeb(c, "get"), err)
  62. return
  63. }
  64. inbound, err := a.inboundService.GetInbound(id)
  65. if err != nil {
  66. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  67. return
  68. }
  69. jsonObj(c, inbound, nil)
  70. }
  71. // getClientTraffics retrieves client traffic information by email.
  72. func (a *InboundController) getClientTraffics(c *gin.Context) {
  73. email := c.Param("email")
  74. clientTraffics, err := a.inboundService.GetClientTrafficByEmail(email)
  75. if err != nil {
  76. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  77. return
  78. }
  79. jsonObj(c, clientTraffics, nil)
  80. }
  81. // getClientTrafficsById retrieves client traffic information by inbound ID.
  82. func (a *InboundController) getClientTrafficsById(c *gin.Context) {
  83. id := c.Param("id")
  84. clientTraffics, err := a.inboundService.GetClientTrafficByID(id)
  85. if err != nil {
  86. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  87. return
  88. }
  89. jsonObj(c, clientTraffics, nil)
  90. }
  91. // addInbound creates a new inbound configuration.
  92. func (a *InboundController) addInbound(c *gin.Context) {
  93. inbound := &model.Inbound{}
  94. err := c.ShouldBind(inbound)
  95. if err != nil {
  96. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), err)
  97. return
  98. }
  99. user := session.GetLoginUser(c)
  100. inbound.UserId = user.Id
  101. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  102. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  103. } else {
  104. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  105. }
  106. inbound, needRestart, err := a.inboundService.AddInbound(inbound)
  107. if err != nil {
  108. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  109. return
  110. }
  111. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, nil)
  112. if needRestart {
  113. a.xrayService.SetToNeedRestart()
  114. }
  115. // Broadcast inbounds update via WebSocket
  116. inbounds, _ := a.inboundService.GetInbounds(user.Id)
  117. websocket.BroadcastInbounds(inbounds)
  118. }
  119. // delInbound deletes an inbound configuration by its ID.
  120. func (a *InboundController) delInbound(c *gin.Context) {
  121. id, err := strconv.Atoi(c.Param("id"))
  122. if err != nil {
  123. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), err)
  124. return
  125. }
  126. needRestart, err := a.inboundService.DelInbound(id)
  127. if err != nil {
  128. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  129. return
  130. }
  131. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), id, nil)
  132. if needRestart {
  133. a.xrayService.SetToNeedRestart()
  134. }
  135. // Broadcast inbounds update via WebSocket
  136. user := session.GetLoginUser(c)
  137. inbounds, _ := a.inboundService.GetInbounds(user.Id)
  138. websocket.BroadcastInbounds(inbounds)
  139. }
  140. // updateInbound updates an existing inbound configuration.
  141. func (a *InboundController) updateInbound(c *gin.Context) {
  142. id, err := strconv.Atoi(c.Param("id"))
  143. if err != nil {
  144. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  145. return
  146. }
  147. inbound := &model.Inbound{
  148. Id: id,
  149. }
  150. err = c.ShouldBind(inbound)
  151. if err != nil {
  152. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  153. return
  154. }
  155. inbound, needRestart, err := a.inboundService.UpdateInbound(inbound)
  156. if err != nil {
  157. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  158. return
  159. }
  160. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), inbound, nil)
  161. if needRestart {
  162. a.xrayService.SetToNeedRestart()
  163. }
  164. // Broadcast inbounds update via WebSocket
  165. user := session.GetLoginUser(c)
  166. inbounds, _ := a.inboundService.GetInbounds(user.Id)
  167. websocket.BroadcastInbounds(inbounds)
  168. }
  169. // getClientIps retrieves the IP addresses associated with a client by email.
  170. func (a *InboundController) getClientIps(c *gin.Context) {
  171. email := c.Param("email")
  172. ips, err := a.inboundService.GetInboundClientIps(email)
  173. if err != nil || ips == "" {
  174. jsonObj(c, "No IP Record", nil)
  175. return
  176. }
  177. jsonObj(c, ips, nil)
  178. }
  179. // clearClientIps clears the IP addresses for a client by email.
  180. func (a *InboundController) clearClientIps(c *gin.Context) {
  181. email := c.Param("email")
  182. err := a.inboundService.ClearClientIps(email)
  183. if err != nil {
  184. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  185. return
  186. }
  187. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  188. }
  189. // addInboundClient adds a new client to an existing inbound.
  190. func (a *InboundController) addInboundClient(c *gin.Context) {
  191. data := &model.Inbound{}
  192. err := c.ShouldBind(data)
  193. if err != nil {
  194. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  195. return
  196. }
  197. needRestart, err := a.inboundService.AddInboundClient(data)
  198. if err != nil {
  199. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  200. return
  201. }
  202. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), nil)
  203. if needRestart {
  204. a.xrayService.SetToNeedRestart()
  205. }
  206. }
  207. // delInboundClient deletes a client from an inbound by inbound ID and client ID.
  208. func (a *InboundController) delInboundClient(c *gin.Context) {
  209. id, err := strconv.Atoi(c.Param("id"))
  210. if err != nil {
  211. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  212. return
  213. }
  214. clientId := c.Param("clientId")
  215. needRestart, err := a.inboundService.DelInboundClient(id, clientId)
  216. if err != nil {
  217. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  218. return
  219. }
  220. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  221. if needRestart {
  222. a.xrayService.SetToNeedRestart()
  223. }
  224. }
  225. // updateInboundClient updates a client's configuration in an inbound.
  226. func (a *InboundController) updateInboundClient(c *gin.Context) {
  227. clientId := c.Param("clientId")
  228. inbound := &model.Inbound{}
  229. err := c.ShouldBind(inbound)
  230. if err != nil {
  231. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  232. return
  233. }
  234. needRestart, err := a.inboundService.UpdateInboundClient(inbound, clientId)
  235. if err != nil {
  236. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  237. return
  238. }
  239. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  240. if needRestart {
  241. a.xrayService.SetToNeedRestart()
  242. }
  243. }
  244. // resetClientTraffic resets the traffic counter for a specific client in an inbound.
  245. func (a *InboundController) resetClientTraffic(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. email := c.Param("email")
  252. needRestart, err := a.inboundService.ResetClientTraffic(id, email)
  253. if err != nil {
  254. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  255. return
  256. }
  257. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  258. if needRestart {
  259. a.xrayService.SetToNeedRestart()
  260. }
  261. }
  262. // resetAllTraffics resets all traffic counters across all inbounds.
  263. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  264. err := a.inboundService.ResetAllTraffics()
  265. if err != nil {
  266. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  267. return
  268. } else {
  269. a.xrayService.SetToNeedRestart()
  270. }
  271. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllTrafficSuccess"), nil)
  272. }
  273. // resetAllClientTraffics resets traffic counters for all clients in a specific inbound.
  274. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  275. id, err := strconv.Atoi(c.Param("id"))
  276. if err != nil {
  277. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  278. return
  279. }
  280. err = a.inboundService.ResetAllClientTraffics(id)
  281. if err != nil {
  282. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  283. return
  284. } else {
  285. a.xrayService.SetToNeedRestart()
  286. }
  287. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllClientTrafficSuccess"), nil)
  288. }
  289. // importInbound imports an inbound configuration from provided data.
  290. func (a *InboundController) importInbound(c *gin.Context) {
  291. inbound := &model.Inbound{}
  292. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  293. if err != nil {
  294. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  295. return
  296. }
  297. user := session.GetLoginUser(c)
  298. inbound.Id = 0
  299. inbound.UserId = user.Id
  300. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  301. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  302. } else {
  303. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  304. }
  305. for index := range inbound.ClientStats {
  306. inbound.ClientStats[index].Id = 0
  307. inbound.ClientStats[index].Enable = true
  308. }
  309. needRestart := false
  310. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  311. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, err)
  312. if err == nil && needRestart {
  313. a.xrayService.SetToNeedRestart()
  314. }
  315. }
  316. // delDepletedClients deletes clients in an inbound who have exhausted their traffic limits.
  317. func (a *InboundController) delDepletedClients(c *gin.Context) {
  318. id, err := strconv.Atoi(c.Param("id"))
  319. if err != nil {
  320. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  321. return
  322. }
  323. err = a.inboundService.DelDepletedClients(id)
  324. if err != nil {
  325. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  326. return
  327. }
  328. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.delDepletedClientsSuccess"), nil)
  329. }
  330. // onlines retrieves the list of currently online clients.
  331. func (a *InboundController) onlines(c *gin.Context) {
  332. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  333. }
  334. // lastOnline retrieves the last online timestamps for clients.
  335. func (a *InboundController) lastOnline(c *gin.Context) {
  336. data, err := a.inboundService.GetClientsLastOnline()
  337. jsonObj(c, data, err)
  338. }
  339. // updateClientTraffic updates the traffic statistics for a client by email.
  340. func (a *InboundController) updateClientTraffic(c *gin.Context) {
  341. email := c.Param("email")
  342. // Define the request structure for traffic update
  343. type TrafficUpdateRequest struct {
  344. Upload int64 `json:"upload"`
  345. Download int64 `json:"download"`
  346. }
  347. var request TrafficUpdateRequest
  348. err := c.ShouldBindJSON(&request)
  349. if err != nil {
  350. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  351. return
  352. }
  353. err = a.inboundService.UpdateClientTrafficByEmail(email, request.Upload, request.Download)
  354. if err != nil {
  355. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  356. return
  357. }
  358. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  359. }
  360. // delInboundClientByEmail deletes a client from an inbound by email address.
  361. func (a *InboundController) delInboundClientByEmail(c *gin.Context) {
  362. inboundId, err := strconv.Atoi(c.Param("id"))
  363. if err != nil {
  364. jsonMsg(c, "Invalid inbound ID", err)
  365. return
  366. }
  367. email := c.Param("email")
  368. needRestart, err := a.inboundService.DelInboundClientByEmail(inboundId, email)
  369. if err != nil {
  370. jsonMsg(c, "Failed to delete client by email", err)
  371. return
  372. }
  373. jsonMsg(c, "Client deleted successfully", nil)
  374. if needRestart {
  375. a.xrayService.SetToNeedRestart()
  376. }
  377. }