1
0

inbound.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "strings"
  8. "github.com/mhsanaei/3x-ui/v3/database/model"
  9. "github.com/mhsanaei/3x-ui/v3/web/middleware"
  10. "github.com/mhsanaei/3x-ui/v3/web/service"
  11. "github.com/mhsanaei/3x-ui/v3/web/session"
  12. "github.com/mhsanaei/3x-ui/v3/web/websocket"
  13. "github.com/gin-gonic/gin"
  14. )
  15. // InboundController handles HTTP requests related to Xray inbounds management.
  16. type InboundController struct {
  17. inboundService service.InboundService
  18. clientService service.ClientService
  19. xrayService service.XrayService
  20. fallbackService service.FallbackService
  21. }
  22. // NewInboundController creates a new InboundController and sets up its routes.
  23. func NewInboundController(g *gin.RouterGroup) *InboundController {
  24. a := &InboundController{}
  25. a.initRouter(g)
  26. return a
  27. }
  28. // broadcastInboundsUpdateClientLimit is the threshold past which we skip the
  29. // full-list push over WebSocket and signal the frontend to re-fetch via REST.
  30. // Mirrors the same heuristic used by the periodic traffic job.
  31. const broadcastInboundsUpdateClientLimit = 5000
  32. // broadcastInboundsUpdate fetches and broadcasts the inbound list for userId.
  33. // At scale (10k+ clients) the marshaled JSON exceeds the WS payload ceiling,
  34. // so we send an invalidate signal instead — frontend re-fetches via REST.
  35. // Skipped entirely when no WebSocket clients are connected.
  36. func (a *InboundController) broadcastInboundsUpdate(userId int) {
  37. if !websocket.HasClients() {
  38. return
  39. }
  40. inbounds, err := a.inboundService.GetInbounds(userId)
  41. if err != nil {
  42. return
  43. }
  44. totalClients := 0
  45. for _, ib := range inbounds {
  46. totalClients += len(ib.ClientStats)
  47. }
  48. if totalClients > broadcastInboundsUpdateClientLimit {
  49. websocket.BroadcastInvalidate(websocket.MessageTypeInbounds)
  50. return
  51. }
  52. websocket.BroadcastInbounds(inbounds)
  53. }
  54. // initRouter initializes the routes for inbound-related operations.
  55. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  56. g.GET("/list", a.getInbounds)
  57. g.GET("/list/slim", a.getInboundsSlim)
  58. g.GET("/options", a.getInboundOptions)
  59. g.GET("/get/:id", a.getInbound)
  60. g.GET("/:id/fallbacks", a.getFallbacks)
  61. g.POST("/add", a.addInbound)
  62. g.POST("/del/:id", a.delInbound)
  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.GetInbound(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. // When the central panel deploys an inbound to a remote node, it sends
  134. // the Tag pre-computed (so both DBs agree on the identifier). Local
  135. // UI submits don't include a Tag — we compute one from listen+port
  136. // using the original collision-avoiding scheme.
  137. if inbound.Tag == "" {
  138. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  139. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  140. } else {
  141. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  142. }
  143. }
  144. inbound, needRestart, err := a.inboundService.AddInbound(inbound)
  145. if err != nil {
  146. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  147. return
  148. }
  149. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, nil)
  150. if needRestart {
  151. a.xrayService.SetToNeedRestart()
  152. }
  153. a.broadcastInboundsUpdate(user.Id)
  154. notifyClientsChanged()
  155. }
  156. // delInbound deletes an inbound configuration by its ID.
  157. func (a *InboundController) delInbound(c *gin.Context) {
  158. id, err := strconv.Atoi(c.Param("id"))
  159. if err != nil {
  160. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), err)
  161. return
  162. }
  163. needRestart, err := a.inboundService.DelInbound(id)
  164. if err != nil {
  165. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  166. return
  167. }
  168. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), id, nil)
  169. if needRestart {
  170. a.xrayService.SetToNeedRestart()
  171. }
  172. user := session.GetLoginUser(c)
  173. a.broadcastInboundsUpdate(user.Id)
  174. notifyClientsChanged()
  175. }
  176. // updateInbound updates an existing inbound configuration.
  177. func (a *InboundController) updateInbound(c *gin.Context) {
  178. id, err := strconv.Atoi(c.Param("id"))
  179. if err != nil {
  180. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  181. return
  182. }
  183. inbound := &model.Inbound{
  184. Id: id,
  185. }
  186. if !middleware.BindAndValidateInto(c, inbound) {
  187. return
  188. }
  189. // Same NodeID=0 → nil normalisation as addInbound. UpdateInbound
  190. // loads the existing row's NodeID from DB anyway (Phase 1 doesn't
  191. // support migrating an inbound between nodes), but normalising here
  192. // keeps the wire shape consistent.
  193. if inbound.NodeID != nil && *inbound.NodeID == 0 {
  194. inbound.NodeID = nil
  195. }
  196. inbound, needRestart, err := a.inboundService.UpdateInbound(inbound)
  197. if err != nil {
  198. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  199. return
  200. }
  201. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), inbound, nil)
  202. if needRestart {
  203. a.xrayService.SetToNeedRestart()
  204. }
  205. user := session.GetLoginUser(c)
  206. a.broadcastInboundsUpdate(user.Id)
  207. notifyClientsChanged()
  208. }
  209. // setInboundEnable flips only the enable flag of an inbound. This is a
  210. // dedicated endpoint because the regular update path serialises the entire
  211. // settings JSON (every client) — far too heavy for an interactive switch
  212. // on inbounds with thousands of clients. Frontend optimistically updates
  213. // the UI; we just persist + sync xray + nudge other open admin sessions.
  214. func (a *InboundController) setInboundEnable(c *gin.Context) {
  215. id, err := strconv.Atoi(c.Param("id"))
  216. if err != nil {
  217. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  218. return
  219. }
  220. type form struct {
  221. Enable bool `json:"enable" form:"enable"`
  222. }
  223. var f form
  224. if err := c.ShouldBind(&f); err != nil {
  225. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  226. return
  227. }
  228. needRestart, err := a.inboundService.SetInboundEnable(id, f.Enable)
  229. if err != nil {
  230. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  231. return
  232. }
  233. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), nil)
  234. if needRestart {
  235. a.xrayService.SetToNeedRestart()
  236. }
  237. // Cross-admin sync: lightweight invalidate signal (a few hundred bytes)
  238. // instead of fetching + serialising the whole inbound list. Other open
  239. // sessions re-fetch via REST. The toggling admin's own UI already
  240. // updated optimistically.
  241. websocket.BroadcastInvalidate(websocket.MessageTypeInbounds)
  242. }
  243. // resetInboundTraffic resets traffic counters for a specific inbound.
  244. func (a *InboundController) resetInboundTraffic(c *gin.Context) {
  245. id, err := strconv.Atoi(c.Param("id"))
  246. if err != nil {
  247. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  248. return
  249. }
  250. err = a.inboundService.ResetInboundTraffic(id)
  251. if err != nil {
  252. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  253. return
  254. } else {
  255. a.xrayService.SetToNeedRestart()
  256. }
  257. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundTrafficSuccess"), nil)
  258. }
  259. // delAllInboundClients removes every client attached to a specific inbound
  260. // while keeping the inbound itself. Internally collects the current email
  261. // list from settings.clients[] and feeds it into ClientService.BulkDelete,
  262. // which handles per-inbound JSON rewriting, runtime user removal, traffic
  263. // row cleanup, and the SyncInbound mapping pass in one optimized cycle.
  264. func (a *InboundController) delAllInboundClients(c *gin.Context) {
  265. id, err := strconv.Atoi(c.Param("id"))
  266. if err != nil {
  267. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  268. return
  269. }
  270. emails, err := a.inboundService.EmailsByInbound(id)
  271. if err != nil {
  272. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  273. return
  274. }
  275. if len(emails) == 0 {
  276. jsonObj(c, service.BulkDeleteResult{}, nil)
  277. return
  278. }
  279. result, needRestart, err := a.clientService.BulkDelete(&a.inboundService, emails, false)
  280. if err != nil {
  281. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  282. return
  283. }
  284. jsonObj(c, result, nil)
  285. if needRestart {
  286. a.xrayService.SetToNeedRestart()
  287. }
  288. user := session.GetLoginUser(c)
  289. a.broadcastInboundsUpdate(user.Id)
  290. notifyClientsChanged()
  291. }
  292. // resetAllTraffics resets all traffic counters across all inbounds.
  293. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  294. err := a.inboundService.ResetAllTraffics()
  295. if err != nil {
  296. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  297. return
  298. } else {
  299. a.xrayService.SetToNeedRestart()
  300. }
  301. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllTrafficSuccess"), nil)
  302. }
  303. // importInbound imports an inbound configuration from provided data.
  304. func (a *InboundController) importInbound(c *gin.Context) {
  305. inbound := &model.Inbound{}
  306. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  307. if err != nil {
  308. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  309. return
  310. }
  311. user := session.GetLoginUser(c)
  312. inbound.Id = 0
  313. inbound.UserId = user.Id
  314. if inbound.NodeID != nil && *inbound.NodeID == 0 {
  315. inbound.NodeID = nil
  316. }
  317. if inbound.Tag == "" {
  318. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  319. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  320. } else {
  321. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  322. }
  323. }
  324. for index := range inbound.ClientStats {
  325. inbound.ClientStats[index].Id = 0
  326. inbound.ClientStats[index].Enable = true
  327. }
  328. inbound, needRestart, err := a.inboundService.AddInbound(inbound)
  329. if err != nil {
  330. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  331. return
  332. }
  333. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, nil)
  334. if needRestart {
  335. a.xrayService.SetToNeedRestart()
  336. }
  337. a.broadcastInboundsUpdate(user.Id)
  338. notifyClientsChanged()
  339. }
  340. // resolveHost mirrors what sub.SubService.ResolveRequest does for the host
  341. // field: prefers X-Forwarded-Host (first entry of any list, port stripped),
  342. // then X-Real-IP, then the host portion of c.Request.Host. Keeping it in the
  343. // controller layer means the service interface stays HTTP-agnostic — service
  344. // methods receive a plain host string instead of a *gin.Context.
  345. func resolveHost(c *gin.Context) string {
  346. if isTrustedForwardedRequest(c) {
  347. if h := strings.TrimSpace(c.GetHeader("X-Forwarded-Host")); h != "" {
  348. if i := strings.Index(h, ","); i >= 0 {
  349. h = strings.TrimSpace(h[:i])
  350. }
  351. if hp, _, err := net.SplitHostPort(h); err == nil {
  352. return hp
  353. }
  354. return h
  355. }
  356. if h := c.GetHeader("X-Real-IP"); h != "" {
  357. return h
  358. }
  359. }
  360. if h, _, err := net.SplitHostPort(c.Request.Host); err == nil {
  361. return h
  362. }
  363. return c.Request.Host
  364. }
  365. // getFallbacks returns the fallback rules attached to the master inbound.
  366. func (a *InboundController) getFallbacks(c *gin.Context) {
  367. id, err := strconv.Atoi(c.Param("id"))
  368. if err != nil {
  369. jsonMsg(c, I18nWeb(c, "get"), err)
  370. return
  371. }
  372. rows, err := a.fallbackService.GetByMaster(id)
  373. if err != nil {
  374. jsonMsg(c, I18nWeb(c, "get"), err)
  375. return
  376. }
  377. jsonObj(c, rows, nil)
  378. }
  379. // setFallbacks atomically replaces the master inbound's fallback list
  380. // and triggers an Xray restart so the new settings.fallbacks take effect.
  381. func (a *InboundController) setFallbacks(c *gin.Context) {
  382. id, err := strconv.Atoi(c.Param("id"))
  383. if err != nil {
  384. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  385. return
  386. }
  387. type body struct {
  388. Fallbacks []service.FallbackInput `json:"fallbacks"`
  389. }
  390. var b body
  391. if err := c.ShouldBindJSON(&b); err != nil {
  392. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  393. return
  394. }
  395. if err := a.fallbackService.SetByMaster(id, b.Fallbacks); err != nil {
  396. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  397. return
  398. }
  399. a.xrayService.SetToNeedRestart()
  400. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), nil)
  401. }