1
0

inbound.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package controller
  2. import (
  3. "encoding/json"
  4. "net"
  5. "strconv"
  6. "strings"
  7. "github.com/mhsanaei/3x-ui/v3/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/web/middleware"
  9. "github.com/mhsanaei/3x-ui/v3/web/service"
  10. "github.com/mhsanaei/3x-ui/v3/web/session"
  11. "github.com/mhsanaei/3x-ui/v3/web/websocket"
  12. "github.com/gin-gonic/gin"
  13. )
  14. // InboundController handles HTTP requests related to Xray inbounds management.
  15. type InboundController struct {
  16. inboundService service.InboundService
  17. clientService service.ClientService
  18. xrayService service.XrayService
  19. fallbackService service.FallbackService
  20. }
  21. // NewInboundController creates a new InboundController and sets up its routes.
  22. func NewInboundController(g *gin.RouterGroup) *InboundController {
  23. a := &InboundController{}
  24. a.initRouter(g)
  25. return a
  26. }
  27. // broadcastInboundsUpdateClientLimit is the threshold past which we skip the
  28. // full-list push over WebSocket and signal the frontend to re-fetch via REST.
  29. // Mirrors the same heuristic used by the periodic traffic job.
  30. const broadcastInboundsUpdateClientLimit = 5000
  31. // broadcastInboundsUpdate fetches and broadcasts the inbound list for userId.
  32. // At scale (10k+ clients) the marshaled JSON exceeds the WS payload ceiling,
  33. // so we send an invalidate signal instead — frontend re-fetches via REST.
  34. // Skipped entirely when no WebSocket clients are connected.
  35. func (a *InboundController) broadcastInboundsUpdate(userId int) {
  36. if !websocket.HasClients() {
  37. return
  38. }
  39. inbounds, err := a.inboundService.GetInbounds(userId)
  40. if err != nil {
  41. return
  42. }
  43. totalClients := 0
  44. for _, ib := range inbounds {
  45. totalClients += len(ib.ClientStats)
  46. }
  47. if totalClients > broadcastInboundsUpdateClientLimit {
  48. websocket.BroadcastInvalidate(websocket.MessageTypeInbounds)
  49. return
  50. }
  51. websocket.BroadcastInbounds(inbounds)
  52. }
  53. // initRouter initializes the routes for inbound-related operations.
  54. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  55. g.GET("/list", a.getInbounds)
  56. g.GET("/list/slim", a.getInboundsSlim)
  57. g.GET("/options", a.getInboundOptions)
  58. g.GET("/get/:id", a.getInbound)
  59. g.GET("/:id/fallbacks", a.getFallbacks)
  60. g.POST("/add", a.addInbound)
  61. g.POST("/del/:id", a.delInbound)
  62. g.POST("/bulkDel", a.bulkDelInbounds)
  63. g.POST("/update/:id", a.updateInbound)
  64. g.POST("/setEnable/:id", a.setInboundEnable)
  65. g.POST("/:id/resetTraffic", a.resetInboundTraffic)
  66. g.POST("/:id/delAllClients", a.delAllInboundClients)
  67. g.POST("/resetAllTraffics", a.resetAllTraffics)
  68. g.POST("/import", a.importInbound)
  69. g.POST("/:id/fallbacks", a.setFallbacks)
  70. }
  71. // getInbounds retrieves the list of inbounds for the logged-in user.
  72. func (a *InboundController) getInbounds(c *gin.Context) {
  73. user := session.GetLoginUser(c)
  74. inbounds, err := a.inboundService.GetInbounds(user.Id)
  75. if err != nil {
  76. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  77. return
  78. }
  79. jsonObj(c, inbounds, nil)
  80. }
  81. // getInboundsSlim is the list-page variant that strips full client
  82. // payloads from settings.clients[]. Detail-view flows still use /get/:id.
  83. func (a *InboundController) getInboundsSlim(c *gin.Context) {
  84. user := session.GetLoginUser(c)
  85. inbounds, err := a.inboundService.GetInboundsSlim(user.Id)
  86. if err != nil {
  87. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  88. return
  89. }
  90. jsonObj(c, inbounds, nil)
  91. }
  92. // getInboundOptions returns a lightweight projection of the user's inbounds
  93. // (id, remark, protocol, port, tlsFlowCapable) for pickers in the clients UI.
  94. // Avoids shipping per-client settings and traffic stats just to fill a dropdown.
  95. func (a *InboundController) getInboundOptions(c *gin.Context) {
  96. user := session.GetLoginUser(c)
  97. options, err := a.inboundService.GetInboundOptions(user.Id)
  98. if err != nil {
  99. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  100. return
  101. }
  102. jsonObj(c, options, nil)
  103. }
  104. // getInbound retrieves a specific inbound by its ID.
  105. func (a *InboundController) getInbound(c *gin.Context) {
  106. id, err := strconv.Atoi(c.Param("id"))
  107. if err != nil {
  108. jsonMsg(c, I18nWeb(c, "get"), err)
  109. return
  110. }
  111. inbound, err := a.inboundService.GetInboundDetail(id)
  112. if err != nil {
  113. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  114. return
  115. }
  116. jsonObj(c, inbound, nil)
  117. }
  118. // addInbound creates a new inbound configuration.
  119. func (a *InboundController) addInbound(c *gin.Context) {
  120. inbound, ok := middleware.BindAndValidate[model.Inbound](c)
  121. if !ok {
  122. return
  123. }
  124. user := session.GetLoginUser(c)
  125. inbound.UserId = user.Id
  126. // Treat NodeID=0 as "no node" — gin's *int form binding can land on
  127. // 0 when the field is absent or empty, and 0 is never a valid Node
  128. // row id. Without this normalization the runtime layer would try to
  129. // load Node id=0 and surface "record not found".
  130. if inbound.NodeID != nil && *inbound.NodeID == 0 {
  131. inbound.NodeID = nil
  132. }
  133. inbound, needRestart, err := a.inboundService.AddInbound(inbound)
  134. if err != nil {
  135. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  136. return
  137. }
  138. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, nil)
  139. if needRestart {
  140. a.xrayService.SetToNeedRestart()
  141. }
  142. a.broadcastInboundsUpdate(user.Id)
  143. notifyClientsChanged()
  144. }
  145. // delInbound deletes an inbound configuration by its ID.
  146. func (a *InboundController) delInbound(c *gin.Context) {
  147. id, err := strconv.Atoi(c.Param("id"))
  148. if err != nil {
  149. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), err)
  150. return
  151. }
  152. needRestart, err := a.inboundService.DelInbound(id)
  153. if err != nil {
  154. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  155. return
  156. }
  157. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), id, nil)
  158. if needRestart {
  159. a.xrayService.SetToNeedRestart()
  160. }
  161. user := session.GetLoginUser(c)
  162. a.broadcastInboundsUpdate(user.Id)
  163. notifyClientsChanged()
  164. }
  165. type bulkDelInboundsRequest struct {
  166. Ids []int `json:"ids"`
  167. }
  168. // bulkDelInbounds deletes several inbounds in one call. Failures are
  169. // reported per id and the rest still proceed; xray restarts at most once.
  170. func (a *InboundController) bulkDelInbounds(c *gin.Context) {
  171. var req bulkDelInboundsRequest
  172. if err := c.ShouldBindJSON(&req); err != nil {
  173. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  174. return
  175. }
  176. result, needRestart, err := a.inboundService.DelInbounds(req.Ids)
  177. if err != nil {
  178. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  179. return
  180. }
  181. jsonObj(c, result, nil)
  182. if needRestart {
  183. a.xrayService.SetToNeedRestart()
  184. }
  185. user := session.GetLoginUser(c)
  186. a.broadcastInboundsUpdate(user.Id)
  187. notifyClientsChanged()
  188. }
  189. // updateInbound updates an existing inbound configuration.
  190. func (a *InboundController) updateInbound(c *gin.Context) {
  191. id, err := strconv.Atoi(c.Param("id"))
  192. if err != nil {
  193. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  194. return
  195. }
  196. inbound := &model.Inbound{
  197. Id: id,
  198. }
  199. if !middleware.BindAndValidateInto(c, inbound) {
  200. return
  201. }
  202. // Same NodeID=0 → nil normalisation as addInbound. UpdateInbound
  203. // loads the existing row's NodeID from DB anyway (Phase 1 doesn't
  204. // support migrating an inbound between nodes), but normalising here
  205. // keeps the wire shape consistent.
  206. if inbound.NodeID != nil && *inbound.NodeID == 0 {
  207. inbound.NodeID = nil
  208. }
  209. inbound, needRestart, err := a.inboundService.UpdateInbound(inbound)
  210. if err != nil {
  211. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  212. return
  213. }
  214. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), inbound, nil)
  215. if needRestart {
  216. a.xrayService.SetToNeedRestart()
  217. }
  218. user := session.GetLoginUser(c)
  219. a.broadcastInboundsUpdate(user.Id)
  220. notifyClientsChanged()
  221. }
  222. // setInboundEnable flips only the enable flag of an inbound. This is a
  223. // dedicated endpoint because the regular update path serialises the entire
  224. // settings JSON (every client) — far too heavy for an interactive switch
  225. // on inbounds with thousands of clients. Frontend optimistically updates
  226. // the UI; we just persist + sync xray + nudge other open admin sessions.
  227. func (a *InboundController) setInboundEnable(c *gin.Context) {
  228. id, err := strconv.Atoi(c.Param("id"))
  229. if err != nil {
  230. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  231. return
  232. }
  233. type form struct {
  234. Enable bool `json:"enable" form:"enable"`
  235. }
  236. var f form
  237. if err := c.ShouldBind(&f); err != nil {
  238. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  239. return
  240. }
  241. needRestart, err := a.inboundService.SetInboundEnable(id, f.Enable)
  242. if err != nil {
  243. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  244. return
  245. }
  246. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), nil)
  247. if needRestart {
  248. a.xrayService.SetToNeedRestart()
  249. }
  250. // Cross-admin sync: lightweight invalidate signal (a few hundred bytes)
  251. // instead of fetching + serialising the whole inbound list. Other open
  252. // sessions re-fetch via REST. The toggling admin's own UI already
  253. // updated optimistically.
  254. websocket.BroadcastInvalidate(websocket.MessageTypeInbounds)
  255. }
  256. // resetInboundTraffic resets traffic counters for a specific inbound.
  257. func (a *InboundController) resetInboundTraffic(c *gin.Context) {
  258. id, err := strconv.Atoi(c.Param("id"))
  259. if err != nil {
  260. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  261. return
  262. }
  263. err = a.inboundService.ResetInboundTraffic(id)
  264. if err != nil {
  265. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  266. return
  267. } else {
  268. a.xrayService.SetToNeedRestart()
  269. }
  270. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundTrafficSuccess"), nil)
  271. }
  272. // delAllInboundClients removes every client attached to a specific inbound
  273. // while keeping the inbound itself. Internally collects the current email
  274. // list from settings.clients[] and feeds it into ClientService.BulkDelete,
  275. // which handles per-inbound JSON rewriting, runtime user removal, traffic
  276. // row cleanup, and the SyncInbound mapping pass in one optimized cycle.
  277. func (a *InboundController) delAllInboundClients(c *gin.Context) {
  278. id, err := strconv.Atoi(c.Param("id"))
  279. if err != nil {
  280. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  281. return
  282. }
  283. emails, err := a.inboundService.EmailsByInbound(id)
  284. if err != nil {
  285. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  286. return
  287. }
  288. if len(emails) == 0 {
  289. jsonObj(c, service.BulkDeleteResult{}, nil)
  290. return
  291. }
  292. result, needRestart, err := a.clientService.BulkDelete(&a.inboundService, emails, false)
  293. if err != nil {
  294. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  295. return
  296. }
  297. jsonObj(c, result, nil)
  298. if needRestart {
  299. a.xrayService.SetToNeedRestart()
  300. }
  301. user := session.GetLoginUser(c)
  302. a.broadcastInboundsUpdate(user.Id)
  303. notifyClientsChanged()
  304. }
  305. // resetAllTraffics resets all traffic counters across all inbounds.
  306. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  307. err := a.inboundService.ResetAllTraffics()
  308. if err != nil {
  309. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  310. return
  311. } else {
  312. a.xrayService.SetToNeedRestart()
  313. }
  314. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllTrafficSuccess"), nil)
  315. }
  316. // importInbound imports an inbound configuration from provided data.
  317. func (a *InboundController) importInbound(c *gin.Context) {
  318. inbound := &model.Inbound{}
  319. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  320. if err != nil {
  321. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  322. return
  323. }
  324. user := session.GetLoginUser(c)
  325. inbound.Id = 0
  326. inbound.UserId = user.Id
  327. if inbound.NodeID != nil && *inbound.NodeID == 0 {
  328. inbound.NodeID = nil
  329. }
  330. for index := range inbound.ClientStats {
  331. inbound.ClientStats[index].Id = 0
  332. inbound.ClientStats[index].Enable = true
  333. }
  334. inbound, needRestart, err := a.inboundService.AddInbound(inbound)
  335. if err != nil {
  336. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  337. return
  338. }
  339. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, nil)
  340. if needRestart {
  341. a.xrayService.SetToNeedRestart()
  342. }
  343. a.broadcastInboundsUpdate(user.Id)
  344. notifyClientsChanged()
  345. }
  346. // resolveHost mirrors what sub.SubService.ResolveRequest does for the host
  347. // field: prefers X-Forwarded-Host (first entry of any list, port stripped),
  348. // then X-Real-IP, then the host portion of c.Request.Host. Keeping it in the
  349. // controller layer means the service interface stays HTTP-agnostic — service
  350. // methods receive a plain host string instead of a *gin.Context.
  351. func resolveHost(c *gin.Context) string {
  352. if isTrustedForwardedRequest(c) {
  353. if h := strings.TrimSpace(c.GetHeader("X-Forwarded-Host")); h != "" {
  354. if i := strings.Index(h, ","); i >= 0 {
  355. h = strings.TrimSpace(h[:i])
  356. }
  357. if hp, _, err := net.SplitHostPort(h); err == nil {
  358. return hp
  359. }
  360. return h
  361. }
  362. if h := c.GetHeader("X-Real-IP"); h != "" {
  363. return h
  364. }
  365. }
  366. if h, _, err := net.SplitHostPort(c.Request.Host); err == nil {
  367. return h
  368. }
  369. return c.Request.Host
  370. }
  371. // getFallbacks returns the fallback rules attached to the master inbound.
  372. func (a *InboundController) getFallbacks(c *gin.Context) {
  373. id, err := strconv.Atoi(c.Param("id"))
  374. if err != nil {
  375. jsonMsg(c, I18nWeb(c, "get"), err)
  376. return
  377. }
  378. rows, err := a.fallbackService.GetByMaster(id)
  379. if err != nil {
  380. jsonMsg(c, I18nWeb(c, "get"), err)
  381. return
  382. }
  383. jsonObj(c, rows, nil)
  384. }
  385. // setFallbacks atomically replaces the master inbound's fallback list
  386. // and triggers an Xray restart so the new settings.fallbacks take effect.
  387. func (a *InboundController) setFallbacks(c *gin.Context) {
  388. id, err := strconv.Atoi(c.Param("id"))
  389. if err != nil {
  390. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  391. return
  392. }
  393. type body struct {
  394. Fallbacks []service.FallbackInput `json:"fallbacks"`
  395. }
  396. var b body
  397. if err := c.ShouldBindJSON(&b); err != nil {
  398. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  399. return
  400. }
  401. if err := a.fallbackService.SetByMaster(id, b.Fallbacks); err != nil {
  402. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  403. return
  404. }
  405. a.xrayService.SetToNeedRestart()
  406. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), nil)
  407. }