1
0

inbound.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/mhsanaei/3x-ui/v3/database/model"
  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. xrayService service.XrayService
  19. }
  20. // NewInboundController creates a new InboundController and sets up its routes.
  21. func NewInboundController(g *gin.RouterGroup) *InboundController {
  22. a := &InboundController{}
  23. a.initRouter(g)
  24. return a
  25. }
  26. // broadcastInboundsUpdateClientLimit is the threshold past which we skip the
  27. // full-list push over WebSocket and signal the frontend to re-fetch via REST.
  28. // Mirrors the same heuristic used by the periodic traffic job.
  29. const broadcastInboundsUpdateClientLimit = 5000
  30. // broadcastInboundsUpdate fetches and broadcasts the inbound list for userId.
  31. // At scale (10k+ clients) the marshaled JSON exceeds the WS payload ceiling,
  32. // so we send an invalidate signal instead — frontend re-fetches via REST.
  33. // Skipped entirely when no WebSocket clients are connected.
  34. func (a *InboundController) broadcastInboundsUpdate(userId int) {
  35. if !websocket.HasClients() {
  36. return
  37. }
  38. inbounds, err := a.inboundService.GetInbounds(userId)
  39. if err != nil {
  40. return
  41. }
  42. totalClients := 0
  43. for _, ib := range inbounds {
  44. totalClients += len(ib.ClientStats)
  45. }
  46. if totalClients > broadcastInboundsUpdateClientLimit {
  47. websocket.BroadcastInvalidate(websocket.MessageTypeInbounds)
  48. return
  49. }
  50. websocket.BroadcastInbounds(inbounds)
  51. }
  52. // initRouter initializes the routes for inbound-related operations.
  53. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  54. g.GET("/list", a.getInbounds)
  55. g.GET("/get/:id", a.getInbound)
  56. g.GET("/getClientTraffics/:email", a.getClientTraffics)
  57. g.GET("/getClientTrafficsById/:id", a.getClientTrafficsById)
  58. g.GET("/getSubLinks/:subId", a.getSubLinks)
  59. g.GET("/getClientLinks/:id/:email", a.getClientLinks)
  60. g.POST("/add", a.addInbound)
  61. g.POST("/del/:id", a.delInbound)
  62. g.POST("/update/:id", a.updateInbound)
  63. g.POST("/setEnable/:id", a.setInboundEnable)
  64. g.POST("/clientIps/:email", a.getClientIps)
  65. g.POST("/clearClientIps/:email", a.clearClientIps)
  66. g.POST("/addClient", a.addInboundClient)
  67. g.POST("/:id/copyClients", a.copyInboundClients)
  68. g.POST("/:id/delClient/:clientId", a.delInboundClient)
  69. g.POST("/updateClient/:clientId", a.updateInboundClient)
  70. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  71. g.POST("/resetAllTraffics", a.resetAllTraffics)
  72. g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
  73. g.POST("/delDepletedClients/:id", a.delDepletedClients)
  74. g.POST("/import", a.importInbound)
  75. g.POST("/onlines", a.onlines)
  76. g.POST("/lastOnline", a.lastOnline)
  77. g.POST("/updateClientTraffic/:email", a.updateClientTraffic)
  78. g.POST("/:id/delClientByEmail/:email", a.delInboundClientByEmail)
  79. }
  80. type CopyInboundClientsRequest struct {
  81. SourceInboundID int `form:"sourceInboundId" json:"sourceInboundId"`
  82. ClientEmails []string `form:"clientEmails" json:"clientEmails"`
  83. Flow string `form:"flow" json:"flow"`
  84. }
  85. // getInbounds retrieves the list of inbounds for the logged-in user.
  86. func (a *InboundController) getInbounds(c *gin.Context) {
  87. user := session.GetLoginUser(c)
  88. inbounds, err := a.inboundService.GetInbounds(user.Id)
  89. if err != nil {
  90. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  91. return
  92. }
  93. jsonObj(c, inbounds, nil)
  94. }
  95. // getInbound retrieves a specific inbound by its ID.
  96. func (a *InboundController) getInbound(c *gin.Context) {
  97. id, err := strconv.Atoi(c.Param("id"))
  98. if err != nil {
  99. jsonMsg(c, I18nWeb(c, "get"), err)
  100. return
  101. }
  102. inbound, err := a.inboundService.GetInbound(id)
  103. if err != nil {
  104. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  105. return
  106. }
  107. jsonObj(c, inbound, nil)
  108. }
  109. // getClientTraffics retrieves client traffic information by email.
  110. func (a *InboundController) getClientTraffics(c *gin.Context) {
  111. email := c.Param("email")
  112. clientTraffics, err := a.inboundService.GetClientTrafficByEmail(email)
  113. if err != nil {
  114. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  115. return
  116. }
  117. jsonObj(c, clientTraffics, nil)
  118. }
  119. // getClientTrafficsById retrieves client traffic information by inbound ID.
  120. func (a *InboundController) getClientTrafficsById(c *gin.Context) {
  121. id := c.Param("id")
  122. clientTraffics, err := a.inboundService.GetClientTrafficByID(id)
  123. if err != nil {
  124. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  125. return
  126. }
  127. jsonObj(c, clientTraffics, nil)
  128. }
  129. // addInbound creates a new inbound configuration.
  130. func (a *InboundController) addInbound(c *gin.Context) {
  131. inbound := &model.Inbound{}
  132. err := c.ShouldBind(inbound)
  133. if err != nil {
  134. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), err)
  135. return
  136. }
  137. user := session.GetLoginUser(c)
  138. inbound.UserId = user.Id
  139. // Treat NodeID=0 as "no node" — gin's *int form binding can land on
  140. // 0 when the field is absent or empty, and 0 is never a valid Node
  141. // row id. Without this normalization the runtime layer would try to
  142. // load Node id=0 and surface "record not found".
  143. if inbound.NodeID != nil && *inbound.NodeID == 0 {
  144. inbound.NodeID = nil
  145. }
  146. // When the central panel deploys an inbound to a remote node, it sends
  147. // the Tag pre-computed (so both DBs agree on the identifier). Local
  148. // UI submits don't include a Tag — we compute one from listen+port
  149. // using the original collision-avoiding scheme.
  150. if inbound.Tag == "" {
  151. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  152. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  153. } else {
  154. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  155. }
  156. }
  157. inbound, needRestart, err := a.inboundService.AddInbound(inbound)
  158. if err != nil {
  159. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  160. return
  161. }
  162. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, nil)
  163. if needRestart {
  164. a.xrayService.SetToNeedRestart()
  165. }
  166. a.broadcastInboundsUpdate(user.Id)
  167. }
  168. // delInbound deletes an inbound configuration by its ID.
  169. func (a *InboundController) delInbound(c *gin.Context) {
  170. id, err := strconv.Atoi(c.Param("id"))
  171. if err != nil {
  172. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), err)
  173. return
  174. }
  175. needRestart, err := a.inboundService.DelInbound(id)
  176. if err != nil {
  177. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  178. return
  179. }
  180. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), id, nil)
  181. if needRestart {
  182. a.xrayService.SetToNeedRestart()
  183. }
  184. user := session.GetLoginUser(c)
  185. a.broadcastInboundsUpdate(user.Id)
  186. }
  187. // updateInbound updates an existing inbound configuration.
  188. func (a *InboundController) updateInbound(c *gin.Context) {
  189. id, err := strconv.Atoi(c.Param("id"))
  190. if err != nil {
  191. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  192. return
  193. }
  194. inbound := &model.Inbound{
  195. Id: id,
  196. }
  197. err = c.ShouldBind(inbound)
  198. if err != nil {
  199. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  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. }
  221. // setInboundEnable flips only the enable flag of an inbound. This is a
  222. // dedicated endpoint because the regular update path serialises the entire
  223. // settings JSON (every client) — far too heavy for an interactive switch
  224. // on inbounds with thousands of clients. Frontend optimistically updates
  225. // the UI; we just persist + sync xray + nudge other open admin sessions.
  226. func (a *InboundController) setInboundEnable(c *gin.Context) {
  227. id, err := strconv.Atoi(c.Param("id"))
  228. if err != nil {
  229. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  230. return
  231. }
  232. type form struct {
  233. Enable bool `json:"enable" form:"enable"`
  234. }
  235. var f form
  236. if err := c.ShouldBind(&f); err != nil {
  237. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  238. return
  239. }
  240. needRestart, err := a.inboundService.SetInboundEnable(id, f.Enable)
  241. if err != nil {
  242. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  243. return
  244. }
  245. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), nil)
  246. if needRestart {
  247. a.xrayService.SetToNeedRestart()
  248. }
  249. // Cross-admin sync: lightweight invalidate signal (a few hundred bytes)
  250. // instead of fetching + serialising the whole inbound list. Other open
  251. // sessions re-fetch via REST. The toggling admin's own UI already
  252. // updated optimistically.
  253. websocket.BroadcastInvalidate(websocket.MessageTypeInbounds)
  254. }
  255. // getClientIps retrieves the IP addresses associated with a client by email.
  256. func (a *InboundController) getClientIps(c *gin.Context) {
  257. email := c.Param("email")
  258. ips, err := a.inboundService.GetInboundClientIps(email)
  259. if err != nil || ips == "" {
  260. jsonObj(c, "No IP Record", nil)
  261. return
  262. }
  263. // Prefer returning a normalized string list for consistent UI rendering
  264. type ipWithTimestamp struct {
  265. IP string `json:"ip"`
  266. Timestamp int64 `json:"timestamp"`
  267. }
  268. var ipsWithTime []ipWithTimestamp
  269. if err := json.Unmarshal([]byte(ips), &ipsWithTime); err == nil && len(ipsWithTime) > 0 {
  270. formatted := make([]string, 0, len(ipsWithTime))
  271. for _, item := range ipsWithTime {
  272. if item.IP == "" {
  273. continue
  274. }
  275. if item.Timestamp > 0 {
  276. ts := time.Unix(item.Timestamp, 0).Local().Format("2006-01-02 15:04:05")
  277. formatted = append(formatted, fmt.Sprintf("%s (%s)", item.IP, ts))
  278. continue
  279. }
  280. formatted = append(formatted, item.IP)
  281. }
  282. jsonObj(c, formatted, nil)
  283. return
  284. }
  285. var oldIps []string
  286. if err := json.Unmarshal([]byte(ips), &oldIps); err == nil && len(oldIps) > 0 {
  287. jsonObj(c, oldIps, nil)
  288. return
  289. }
  290. // If parsing fails, return as string
  291. jsonObj(c, ips, nil)
  292. }
  293. // clearClientIps clears the IP addresses for a client by email.
  294. func (a *InboundController) clearClientIps(c *gin.Context) {
  295. email := c.Param("email")
  296. err := a.inboundService.ClearClientIps(email)
  297. if err != nil {
  298. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  299. return
  300. }
  301. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  302. }
  303. // addInboundClient adds a new client to an existing inbound.
  304. func (a *InboundController) addInboundClient(c *gin.Context) {
  305. data := &model.Inbound{}
  306. err := c.ShouldBind(data)
  307. if err != nil {
  308. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  309. return
  310. }
  311. needRestart, err := a.inboundService.AddInboundClient(data)
  312. if err != nil {
  313. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  314. return
  315. }
  316. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), nil)
  317. if needRestart {
  318. a.xrayService.SetToNeedRestart()
  319. }
  320. }
  321. // copyInboundClients copies clients from source inbound to target inbound.
  322. func (a *InboundController) copyInboundClients(c *gin.Context) {
  323. targetID, err := strconv.Atoi(c.Param("id"))
  324. if err != nil {
  325. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  326. return
  327. }
  328. req := &CopyInboundClientsRequest{}
  329. err = c.ShouldBind(req)
  330. if err != nil {
  331. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  332. return
  333. }
  334. if req.SourceInboundID <= 0 {
  335. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), fmt.Errorf("invalid source inbound id"))
  336. return
  337. }
  338. result, needRestart, err := a.inboundService.CopyInboundClients(targetID, req.SourceInboundID, req.ClientEmails, req.Flow)
  339. if err != nil {
  340. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  341. return
  342. }
  343. jsonObj(c, result, nil)
  344. if needRestart {
  345. a.xrayService.SetToNeedRestart()
  346. }
  347. }
  348. // delInboundClient deletes a client from an inbound by inbound ID and client ID.
  349. func (a *InboundController) delInboundClient(c *gin.Context) {
  350. id, err := strconv.Atoi(c.Param("id"))
  351. if err != nil {
  352. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  353. return
  354. }
  355. clientId := c.Param("clientId")
  356. needRestart, err := a.inboundService.DelInboundClient(id, clientId)
  357. if err != nil {
  358. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  359. return
  360. }
  361. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  362. if needRestart {
  363. a.xrayService.SetToNeedRestart()
  364. }
  365. }
  366. // updateInboundClient updates a client's configuration in an inbound.
  367. func (a *InboundController) updateInboundClient(c *gin.Context) {
  368. clientId := c.Param("clientId")
  369. inbound := &model.Inbound{}
  370. err := c.ShouldBind(inbound)
  371. if err != nil {
  372. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  373. return
  374. }
  375. needRestart, err := a.inboundService.UpdateInboundClient(inbound, clientId)
  376. if err != nil {
  377. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  378. return
  379. }
  380. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  381. if needRestart {
  382. a.xrayService.SetToNeedRestart()
  383. }
  384. }
  385. // resetClientTraffic resets the traffic counter for a specific client in an inbound.
  386. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  387. id, err := strconv.Atoi(c.Param("id"))
  388. if err != nil {
  389. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  390. return
  391. }
  392. email := c.Param("email")
  393. needRestart, err := a.inboundService.ResetClientTraffic(id, email)
  394. if err != nil {
  395. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  396. return
  397. }
  398. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  399. if needRestart {
  400. a.xrayService.SetToNeedRestart()
  401. }
  402. }
  403. // resetAllTraffics resets all traffic counters across all inbounds.
  404. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  405. err := a.inboundService.ResetAllTraffics()
  406. if err != nil {
  407. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  408. return
  409. } else {
  410. a.xrayService.SetToNeedRestart()
  411. }
  412. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllTrafficSuccess"), nil)
  413. }
  414. // resetAllClientTraffics resets traffic counters for all clients in a specific inbound.
  415. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  416. id, err := strconv.Atoi(c.Param("id"))
  417. if err != nil {
  418. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  419. return
  420. }
  421. err = a.inboundService.ResetAllClientTraffics(id)
  422. if err != nil {
  423. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  424. return
  425. } else {
  426. a.xrayService.SetToNeedRestart()
  427. }
  428. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllClientTrafficSuccess"), nil)
  429. }
  430. // importInbound imports an inbound configuration from provided data.
  431. func (a *InboundController) importInbound(c *gin.Context) {
  432. inbound := &model.Inbound{}
  433. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  434. if err != nil {
  435. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  436. return
  437. }
  438. user := session.GetLoginUser(c)
  439. inbound.Id = 0
  440. inbound.UserId = user.Id
  441. if inbound.Tag == "" {
  442. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  443. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  444. } else {
  445. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  446. }
  447. }
  448. for index := range inbound.ClientStats {
  449. inbound.ClientStats[index].Id = 0
  450. inbound.ClientStats[index].Enable = true
  451. }
  452. needRestart := false
  453. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  454. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, err)
  455. if err == nil && needRestart {
  456. a.xrayService.SetToNeedRestart()
  457. }
  458. }
  459. // delDepletedClients deletes clients in an inbound who have exhausted their traffic limits.
  460. func (a *InboundController) delDepletedClients(c *gin.Context) {
  461. id, err := strconv.Atoi(c.Param("id"))
  462. if err != nil {
  463. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  464. return
  465. }
  466. err = a.inboundService.DelDepletedClients(id)
  467. if err != nil {
  468. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  469. return
  470. }
  471. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.delDepletedClientsSuccess"), nil)
  472. }
  473. // onlines retrieves the list of currently online clients.
  474. func (a *InboundController) onlines(c *gin.Context) {
  475. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  476. }
  477. // lastOnline retrieves the last online timestamps for clients.
  478. func (a *InboundController) lastOnline(c *gin.Context) {
  479. data, err := a.inboundService.GetClientsLastOnline()
  480. jsonObj(c, data, err)
  481. }
  482. // updateClientTraffic updates the traffic statistics for a client by email.
  483. func (a *InboundController) updateClientTraffic(c *gin.Context) {
  484. email := c.Param("email")
  485. // Define the request structure for traffic update
  486. type TrafficUpdateRequest struct {
  487. Upload int64 `json:"upload"`
  488. Download int64 `json:"download"`
  489. }
  490. var request TrafficUpdateRequest
  491. err := c.ShouldBindJSON(&request)
  492. if err != nil {
  493. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  494. return
  495. }
  496. err = a.inboundService.UpdateClientTrafficByEmail(email, request.Upload, request.Download)
  497. if err != nil {
  498. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  499. return
  500. }
  501. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  502. }
  503. // delInboundClientByEmail deletes a client from an inbound by email address.
  504. func (a *InboundController) delInboundClientByEmail(c *gin.Context) {
  505. inboundId, err := strconv.Atoi(c.Param("id"))
  506. if err != nil {
  507. jsonMsg(c, "Invalid inbound ID", err)
  508. return
  509. }
  510. email := c.Param("email")
  511. needRestart, err := a.inboundService.DelInboundClientByEmail(inboundId, email)
  512. if err != nil {
  513. jsonMsg(c, "Failed to delete client by email", err)
  514. return
  515. }
  516. jsonMsg(c, "Client deleted successfully", nil)
  517. if needRestart {
  518. a.xrayService.SetToNeedRestart()
  519. }
  520. }
  521. // resolveHost mirrors what sub.SubService.ResolveRequest does for the host
  522. // field: prefers X-Forwarded-Host (first entry of any list, port stripped),
  523. // then X-Real-IP, then the host portion of c.Request.Host. Keeping it in the
  524. // controller layer means the service interface stays HTTP-agnostic — service
  525. // methods receive a plain host string instead of a *gin.Context.
  526. func resolveHost(c *gin.Context) string {
  527. if h := strings.TrimSpace(c.GetHeader("X-Forwarded-Host")); h != "" {
  528. if i := strings.Index(h, ","); i >= 0 {
  529. h = strings.TrimSpace(h[:i])
  530. }
  531. if hp, _, err := net.SplitHostPort(h); err == nil {
  532. return hp
  533. }
  534. return h
  535. }
  536. if h := c.GetHeader("X-Real-IP"); h != "" {
  537. return h
  538. }
  539. if h, _, err := net.SplitHostPort(c.Request.Host); err == nil {
  540. return h
  541. }
  542. return c.Request.Host
  543. }
  544. // getSubLinks returns every protocol URL produced for the given subscription
  545. // ID — the JSON-array equivalent of /sub/<subId> (no base64 wrap).
  546. func (a *InboundController) getSubLinks(c *gin.Context) {
  547. links, err := a.inboundService.GetSubLinks(resolveHost(c), c.Param("subId"))
  548. if err != nil {
  549. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  550. return
  551. }
  552. jsonObj(c, links, nil)
  553. }
  554. // getClientLinks returns the URL(s) for one client on one inbound — the same
  555. // string the Copy URL button copies in the panel UI. Empty array when the
  556. // protocol has no URL form, or when the email isn't found on the inbound.
  557. func (a *InboundController) getClientLinks(c *gin.Context) {
  558. id, err := strconv.Atoi(c.Param("id"))
  559. if err != nil {
  560. jsonMsg(c, I18nWeb(c, "get"), err)
  561. return
  562. }
  563. links, err := a.inboundService.GetClientLinks(resolveHost(c), id, c.Param("email"))
  564. if err != nil {
  565. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  566. return
  567. }
  568. jsonObj(c, links, nil)
  569. }