inbound.go 12 KB

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