inbound.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. // Package service provides business logic services for the 3x-ui web panel,
  2. // including inbound/outbound management, user administration, settings, and Xray integration.
  3. package service
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "sort"
  9. "strings"
  10. "time"
  11. "github.com/mhsanaei/3x-ui/v3/internal/database"
  12. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  13. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  14. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  15. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  16. "gorm.io/gorm"
  17. )
  18. type InboundService struct {
  19. xrayApi xray.XrayAPI
  20. clientService ClientService
  21. fallbackService FallbackService
  22. }
  23. // GetInbounds retrieves all inbounds for a specific user with client stats.
  24. func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
  25. db := database.GetDB()
  26. var inbounds []*model.Inbound
  27. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Order("id ASC").Find(&inbounds).Error
  28. if err != nil && err != gorm.ErrRecordNotFound {
  29. return nil, err
  30. }
  31. s.enrichClientStats(db, inbounds)
  32. s.annotateFallbackParents(db, inbounds)
  33. s.annotateLocalOriginGuid(inbounds)
  34. return inbounds, nil
  35. }
  36. // annotateLocalOriginGuid fills OriginNodeGuid for this panel's OWN inbounds
  37. // (NodeID == nil) with the panel's stable GUID; inbounds synced from a node
  38. // already carry the originating node's GUID. Read-time only (not persisted) so
  39. // the per-inbound online view can scope by GUID uniformly across a chain of
  40. // nodes (#4983).
  41. func (s *InboundService) annotateLocalOriginGuid(inbounds []*model.Inbound) {
  42. if len(inbounds) == 0 {
  43. return
  44. }
  45. guid := s.panelGuid()
  46. if guid == "" {
  47. return
  48. }
  49. for _, ib := range inbounds {
  50. if ib.OriginNodeGuid == "" && ib.NodeID == nil {
  51. ib.OriginNodeGuid = guid
  52. }
  53. }
  54. }
  55. // GetInboundsSlim returns the same list of inbounds as GetInbounds but
  56. // strips every per-client field other than email / enable / comment from
  57. // settings.clients and skips UUID/SubId enrichment on ClientStats. The
  58. // inbounds page only needs those three to roll up client counts and
  59. // render badges, so this trims tens of bytes per client (UUID, password,
  60. // flow, security, totalGB, expiryTime, limitIp, tgId, ...) which adds
  61. // up fast on installs with thousands of clients.
  62. //
  63. // Full client data is still available through GET /panel/api/inbounds/get/:id
  64. // for the edit/info/qr/export/clone flows that need it.
  65. func (s *InboundService) GetInboundsSlim(userId int) ([]*model.Inbound, error) {
  66. db := database.GetDB()
  67. var inbounds []*model.Inbound
  68. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Order("id ASC").Find(&inbounds).Error
  69. if err != nil && err != gorm.ErrRecordNotFound {
  70. return nil, err
  71. }
  72. s.annotateFallbackParents(db, inbounds)
  73. s.annotateLocalOriginGuid(inbounds)
  74. // Top up stats rows owned by sibling inbounds (multi-attached clients)
  75. // so the list's depleted/expiring badges see every client; the UUID/SubId
  76. // enrichment stays skipped. Must run before slimming strips the settings.
  77. s.backfillClientStats(db, inbounds)
  78. for _, ib := range inbounds {
  79. ib.Settings = slimSettingsClients(ib.Settings)
  80. }
  81. return inbounds, nil
  82. }
  83. // slimSettingsClients rewrites the inbound settings JSON so settings.clients[]
  84. // keeps only the fields the list view actually reads. Returns the input
  85. // unchanged when the JSON can't be parsed or has no clients array.
  86. func slimSettingsClients(settings string) string {
  87. if settings == "" {
  88. return settings
  89. }
  90. var raw map[string]any
  91. if err := json.Unmarshal([]byte(settings), &raw); err != nil {
  92. return settings
  93. }
  94. clients, ok := raw["clients"].([]any)
  95. if !ok || len(clients) == 0 {
  96. return settings
  97. }
  98. slim := make([]any, 0, len(clients))
  99. for _, entry := range clients {
  100. c, ok := entry.(map[string]any)
  101. if !ok {
  102. continue
  103. }
  104. row := make(map[string]any, 3)
  105. if v, ok := c["email"]; ok {
  106. row["email"] = v
  107. }
  108. if v, ok := c["enable"]; ok {
  109. row["enable"] = v
  110. }
  111. if v, ok := c["comment"]; ok && v != "" {
  112. row["comment"] = v
  113. }
  114. slim = append(slim, row)
  115. }
  116. raw["clients"] = slim
  117. out, err := json.Marshal(raw)
  118. if err != nil {
  119. return settings
  120. }
  121. return string(out)
  122. }
  123. // annotateFallbackParents fills FallbackParent on each inbound that is
  124. // the child side of a fallback rule. One DB round-trip serves the full
  125. // list — the frontend needs this to rewrite the child's client-share
  126. // link so it points at the master's reachable endpoint.
  127. func (s *InboundService) annotateFallbackParents(db *gorm.DB, inbounds []*model.Inbound) {
  128. if len(inbounds) == 0 {
  129. return
  130. }
  131. childIds := make([]int, 0, len(inbounds))
  132. for _, ib := range inbounds {
  133. childIds = append(childIds, ib.Id)
  134. }
  135. var rows []model.InboundFallback
  136. if err := db.Where("child_id IN ?", childIds).
  137. Order("sort_order ASC, id ASC").
  138. Find(&rows).Error; err != nil {
  139. return
  140. }
  141. first := make(map[int]model.InboundFallback, len(rows))
  142. for _, r := range rows {
  143. if _, ok := first[r.ChildId]; !ok {
  144. first[r.ChildId] = r
  145. }
  146. }
  147. for _, ib := range inbounds {
  148. if r, ok := first[ib.Id]; ok {
  149. ib.FallbackParent = &model.FallbackParentInfo{
  150. MasterId: r.MasterId,
  151. Path: r.Path,
  152. }
  153. }
  154. }
  155. }
  156. type InboundOption struct {
  157. Id int `json:"id" example:"1"`
  158. Remark string `json:"remark" example:"VLESS-443"`
  159. Tag string `json:"tag" example:"in-443-tcp"`
  160. Protocol string `json:"protocol" example:"vless"`
  161. Port int `json:"port" example:"443"`
  162. TlsFlowCapable bool `json:"tlsFlowCapable" example:"true"`
  163. SsMethod string `json:"ssMethod"`
  164. }
  165. func (s *InboundService) GetInboundOptions(userId int) ([]InboundOption, error) {
  166. db := database.GetDB()
  167. var rows []struct {
  168. Id int `gorm:"column:id"`
  169. Remark string `gorm:"column:remark"`
  170. Tag string `gorm:"column:tag"`
  171. Protocol string `gorm:"column:protocol"`
  172. Port int `gorm:"column:port"`
  173. StreamSettings string `gorm:"column:stream_settings"`
  174. Settings string `gorm:"column:settings"`
  175. }
  176. err := db.Table("inbounds").
  177. Select("id, remark, tag, protocol, port, stream_settings, settings").
  178. Where("user_id = ?", userId).
  179. Order("id ASC").
  180. Scan(&rows).Error
  181. if err != nil && err != gorm.ErrRecordNotFound {
  182. return nil, err
  183. }
  184. out := make([]InboundOption, 0, len(rows))
  185. for _, r := range rows {
  186. out = append(out, InboundOption{
  187. Id: r.Id,
  188. Remark: r.Remark,
  189. Tag: r.Tag,
  190. Protocol: r.Protocol,
  191. Port: r.Port,
  192. TlsFlowCapable: inboundCanEnableTlsFlow(r.Protocol, r.StreamSettings, r.Settings),
  193. SsMethod: inboundShadowsocksMethod(r.Protocol, r.Settings),
  194. })
  195. }
  196. return out, nil
  197. }
  198. // GetAllInbounds retrieves all inbounds with client stats.
  199. func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
  200. db := database.GetDB()
  201. var inbounds []*model.Inbound
  202. err := db.Model(model.Inbound{}).Preload("ClientStats").Find(&inbounds).Error
  203. if err != nil && err != gorm.ErrRecordNotFound {
  204. return nil, err
  205. }
  206. s.enrichClientStats(db, inbounds)
  207. return inbounds, nil
  208. }
  209. func (s *InboundService) GetInboundsByTrafficReset(period string) ([]*model.Inbound, error) {
  210. db := database.GetDB()
  211. var inbounds []*model.Inbound
  212. err := db.Model(model.Inbound{}).Where("traffic_reset = ?", period).Find(&inbounds).Error
  213. if err != nil && err != gorm.ErrRecordNotFound {
  214. return nil, err
  215. }
  216. return inbounds, nil
  217. }
  218. func (s *InboundService) GetClients(inbound *model.Inbound) ([]model.Client, error) {
  219. settings := map[string][]model.Client{}
  220. json.Unmarshal([]byte(inbound.Settings), &settings)
  221. if settings == nil {
  222. return nil, fmt.Errorf("setting is null")
  223. }
  224. clients := settings["clients"]
  225. if clients == nil {
  226. return nil, nil
  227. }
  228. return clients, nil
  229. }
  230. func (s *InboundService) GetAllEmails() ([]string, error) {
  231. db := database.GetDB()
  232. var emails []string
  233. query := fmt.Sprintf(
  234. "SELECT DISTINCT %s %s",
  235. database.JSONFieldText("client.value", "email"),
  236. database.JSONClientsFromInbound(),
  237. )
  238. if err := db.Raw(query).Scan(&emails).Error; err != nil {
  239. return nil, err
  240. }
  241. return emails, nil
  242. }
  243. // getAllEmailSubIDs returns email→subId. An email seen with two different
  244. // non-empty subIds is locked (mapped to "") so neither identity can claim it.
  245. func (s *InboundService) getAllEmailSubIDs() (map[string]string, error) {
  246. db := database.GetDB()
  247. var rows []struct {
  248. Email string
  249. SubID string
  250. }
  251. query := fmt.Sprintf(
  252. "SELECT %s AS email, %s AS sub_id %s",
  253. database.JSONFieldText("client.value", "email"),
  254. database.JSONFieldText("client.value", "subId"),
  255. database.JSONClientsFromInbound(),
  256. )
  257. if err := db.Raw(query).Scan(&rows).Error; err != nil {
  258. return nil, err
  259. }
  260. result := make(map[string]string, len(rows))
  261. for _, r := range rows {
  262. email := strings.ToLower(r.Email)
  263. if email == "" {
  264. continue
  265. }
  266. subID := r.SubID
  267. if existing, ok := result[email]; ok {
  268. if existing != subID {
  269. result[email] = ""
  270. }
  271. continue
  272. }
  273. result[email] = subID
  274. }
  275. return result, nil
  276. }
  277. // normalizeStreamSettings clears StreamSettings for protocols that don't use it.
  278. // Only vmess, vless, trojan, shadowsocks, hysteria, and wireguard protocols use
  279. // streamSettings (wireguard for finalmask UDP masks and sockopt on its listener).
  280. func (s *InboundService) normalizeStreamSettings(inbound *model.Inbound) {
  281. protocolsWithStream := map[model.Protocol]bool{
  282. model.VMESS: true,
  283. model.VLESS: true,
  284. model.Trojan: true,
  285. model.Shadowsocks: true,
  286. model.Hysteria: true,
  287. model.WireGuard: true,
  288. }
  289. if !protocolsWithStream[inbound.Protocol] {
  290. inbound.StreamSettings = ""
  291. }
  292. }
  293. // normalizeMtprotoSecret rebuilds an mtproto inbound's FakeTLS secret so it is
  294. // always valid and matches the configured domain before the row is persisted.
  295. func (s *InboundService) normalizeMtprotoSecret(inbound *model.Inbound) {
  296. if inbound.Protocol != model.MTProto {
  297. return
  298. }
  299. if healed, ok := model.HealMtprotoSecret(inbound.Settings); ok {
  300. inbound.Settings = healed
  301. }
  302. }
  303. // AddInbound creates a new inbound configuration.
  304. // It validates port uniqueness, client email uniqueness, and required fields,
  305. // then saves the inbound to the database and optionally adds it to the running Xray instance.
  306. // Returns the created inbound, whether Xray needs restart, and any error.
  307. func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
  308. // Normalize streamSettings based on protocol
  309. s.normalizeStreamSettings(inbound)
  310. s.normalizeMtprotoSecret(inbound)
  311. conflict, err := s.checkPortConflict(inbound, 0)
  312. if err != nil {
  313. return inbound, false, err
  314. }
  315. if conflict != nil {
  316. return inbound, false, common.NewError(conflict.String())
  317. }
  318. inbound.Tag, err = s.resolveInboundTag(inbound, 0)
  319. if err != nil {
  320. return inbound, false, err
  321. }
  322. clients, err := s.GetClients(inbound)
  323. if err != nil {
  324. return inbound, false, err
  325. }
  326. existEmail, err := s.clientService.checkEmailsExistForClients(s, clients, nil)
  327. if err != nil {
  328. return inbound, false, err
  329. }
  330. if existEmail != "" {
  331. return inbound, false, common.NewError("Duplicate email:", existEmail)
  332. }
  333. // Ensure created_at and updated_at on clients in settings
  334. if len(clients) > 0 {
  335. var settings map[string]any
  336. if err2 := json.Unmarshal([]byte(inbound.Settings), &settings); err2 == nil && settings != nil {
  337. now := time.Now().Unix() * 1000
  338. updatedClients := make([]model.Client, 0, len(clients))
  339. for _, c := range clients {
  340. if c.CreatedAt == 0 {
  341. c.CreatedAt = now
  342. }
  343. c.UpdatedAt = now
  344. updatedClients = append(updatedClients, c)
  345. }
  346. settings["clients"] = updatedClients
  347. if bs, err3 := json.MarshalIndent(settings, "", " "); err3 == nil {
  348. inbound.Settings = string(bs)
  349. } else {
  350. logger.Debug("Unable to marshal inbound settings with timestamps:", err3)
  351. }
  352. } else if err2 != nil {
  353. logger.Debug("Unable to parse inbound settings for timestamps:", err2)
  354. }
  355. }
  356. // Secure client ID
  357. for _, client := range clients {
  358. switch inbound.Protocol {
  359. case "trojan":
  360. if client.Password == "" {
  361. return inbound, false, common.NewError("empty client ID")
  362. }
  363. case "shadowsocks":
  364. if client.Email == "" {
  365. return inbound, false, common.NewError("empty client ID")
  366. }
  367. case "hysteria":
  368. if client.Auth == "" {
  369. return inbound, false, common.NewError("empty client ID")
  370. }
  371. default:
  372. if client.ID == "" {
  373. return inbound, false, common.NewError("empty client ID")
  374. }
  375. }
  376. }
  377. db := database.GetDB()
  378. tx := db.Begin()
  379. markDirty := false
  380. defer func() {
  381. if err != nil {
  382. tx.Rollback()
  383. return
  384. }
  385. tx.Commit()
  386. if markDirty && inbound.NodeID != nil {
  387. if dErr := (&NodeService{}).MarkNodeDirty(*inbound.NodeID); dErr != nil {
  388. logger.Warning("mark node dirty failed:", dErr)
  389. }
  390. }
  391. }()
  392. err = tx.Save(inbound).Error
  393. if err == nil {
  394. if len(inbound.ClientStats) == 0 {
  395. for _, client := range clients {
  396. s.AddClientStat(tx, inbound.Id, &client)
  397. }
  398. }
  399. } else {
  400. return inbound, false, err
  401. }
  402. if err = s.clientService.SyncInbound(tx, inbound.Id, clients); err != nil {
  403. return inbound, false, err
  404. }
  405. needRestart := false
  406. if inbound.Enable {
  407. rt, push, dirty, perr := s.nodePushPlan(inbound)
  408. if perr != nil {
  409. err = perr
  410. return inbound, false, err
  411. }
  412. if dirty {
  413. markDirty = true
  414. }
  415. if push {
  416. if err1 := rt.AddInbound(context.Background(), inbound); err1 == nil {
  417. logger.Debug("New inbound added on", rt.Name(), ":", inbound.Tag)
  418. } else {
  419. logger.Debug("Unable to add inbound on", rt.Name(), ":", err1)
  420. if inbound.NodeID != nil {
  421. markDirty = true
  422. } else {
  423. needRestart = true
  424. }
  425. }
  426. }
  427. }
  428. return inbound, needRestart, err
  429. }
  430. func (s *InboundService) DelInbound(id int) (bool, error) {
  431. db := database.GetDB()
  432. needRestart := false
  433. markDirty := false
  434. var ib model.Inbound
  435. loadErr := db.Model(model.Inbound{}).Where("id = ?", id).First(&ib).Error
  436. if loadErr == nil {
  437. shouldPushToRuntime := ib.NodeID != nil || ib.Enable
  438. if shouldPushToRuntime {
  439. rt, push, dirty, perr := s.nodePushPlan(&ib)
  440. if perr != nil {
  441. logger.Warning("DelInbound: node lookup failed, deleting central row anyway:", perr)
  442. markDirty = true
  443. } else if push {
  444. if err1 := rt.DelInbound(context.Background(), &ib); err1 == nil {
  445. logger.Debug("Inbound deleted on", rt.Name(), ":", ib.Tag)
  446. } else {
  447. logger.Warning("DelInbound on", rt.Name(), "failed, deleting central row anyway:", err1)
  448. if ib.NodeID == nil {
  449. needRestart = true
  450. } else {
  451. markDirty = true
  452. }
  453. }
  454. } else if ib.NodeID == nil {
  455. needRestart = true
  456. } else if dirty {
  457. markDirty = true
  458. }
  459. } else {
  460. logger.Debug("DelInbound: skipping runtime push for disabled local inbound id:", id)
  461. }
  462. } else {
  463. logger.Debug("DelInbound: inbound not found, id:", id)
  464. }
  465. if err := s.clientService.DetachInbound(db, id); err != nil {
  466. return false, err
  467. }
  468. if err := db.Delete(model.Inbound{}, id).Error; err != nil {
  469. return needRestart, err
  470. }
  471. if markDirty && ib.NodeID != nil {
  472. if dErr := (&NodeService{}).MarkNodeDirty(*ib.NodeID); dErr != nil {
  473. logger.Warning("mark node dirty failed:", dErr)
  474. }
  475. }
  476. if !database.IsPostgres() {
  477. var count int64
  478. if err := db.Model(&model.Inbound{}).Count(&count).Error; err != nil {
  479. return needRestart, err
  480. }
  481. if count == 0 {
  482. if err := db.Exec("DELETE FROM sqlite_sequence WHERE name = ?", "inbounds").Error; err != nil {
  483. return needRestart, err
  484. }
  485. }
  486. }
  487. return needRestart, nil
  488. }
  489. type BulkDelInboundResult struct {
  490. Deleted int `json:"deleted"`
  491. Skipped []BulkDelInboundReport `json:"skipped,omitempty"`
  492. }
  493. type BulkDelInboundReport struct {
  494. Id int `json:"id"`
  495. Reason string `json:"reason"`
  496. }
  497. // DelInbounds removes every inbound in the list, reusing the single-delete
  498. // path per id. Failures are recorded in Skipped and processing continues for
  499. // the rest; the aggregated needRestart is returned so the caller restarts
  500. // xray at most once.
  501. func (s *InboundService) DelInbounds(ids []int) (BulkDelInboundResult, bool, error) {
  502. result := BulkDelInboundResult{}
  503. needRestart := false
  504. for _, id := range ids {
  505. r, err := s.DelInbound(id)
  506. if err != nil {
  507. result.Skipped = append(result.Skipped, BulkDelInboundReport{Id: id, Reason: err.Error()})
  508. continue
  509. }
  510. result.Deleted++
  511. if r {
  512. needRestart = true
  513. }
  514. }
  515. return result, needRestart, nil
  516. }
  517. func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
  518. db := database.GetDB()
  519. inbound := &model.Inbound{}
  520. err := db.Model(model.Inbound{}).First(inbound, id).Error
  521. if err != nil {
  522. return nil, err
  523. }
  524. return inbound, nil
  525. }
  526. func (s *InboundService) GetInboundDetail(id int) (*model.Inbound, error) {
  527. db := database.GetDB()
  528. inbound := &model.Inbound{}
  529. err := db.Model(model.Inbound{}).Preload("ClientStats").First(inbound, id).Error
  530. if err != nil {
  531. return nil, err
  532. }
  533. s.enrichClientStats(db, []*model.Inbound{inbound})
  534. return inbound, nil
  535. }
  536. func (s *InboundService) SetInboundEnable(id int, enable bool) (bool, error) {
  537. inbound, err := s.GetInbound(id)
  538. if err != nil {
  539. return false, err
  540. }
  541. if inbound.Enable == enable {
  542. return false, nil
  543. }
  544. db := database.GetDB()
  545. if err := db.Model(model.Inbound{}).Where("id = ?", id).
  546. Update("enable", enable).Error; err != nil {
  547. return false, err
  548. }
  549. inbound.Enable = enable
  550. needRestart := false
  551. rt, push, dirty, perr := s.nodePushPlan(inbound)
  552. if perr != nil {
  553. return false, perr
  554. }
  555. // Remote nodes interpret DelInbound as a real row delete (it hits
  556. // panel/api/inbounds/del/:id on the remote), so toggling the enable
  557. // switch on a remote inbound used to wipe the row entirely (#4402).
  558. // PATCH the remote row via UpdateInbound instead — preserves the
  559. // settings/client history and just flips the enable flag.
  560. if inbound.NodeID != nil {
  561. if push {
  562. if err := rt.UpdateInbound(context.Background(), inbound, inbound); err != nil {
  563. logger.Warning("SetInboundEnable: remote UpdateInbound on", rt.Name(), "failed:", err)
  564. dirty = true
  565. }
  566. }
  567. if dirty {
  568. if dErr := (&NodeService{}).MarkNodeDirty(*inbound.NodeID); dErr != nil {
  569. logger.Warning("mark node dirty failed:", dErr)
  570. }
  571. }
  572. return false, nil
  573. }
  574. if !push {
  575. return true, nil
  576. }
  577. if err := rt.DelInbound(context.Background(), inbound); err != nil &&
  578. !strings.Contains(err.Error(), "not found") {
  579. logger.Debug("SetInboundEnable: DelInbound on", rt.Name(), "failed:", err)
  580. needRestart = true
  581. }
  582. if !enable {
  583. return needRestart, nil
  584. }
  585. runtimeInbound, err := s.buildRuntimeInboundForAPI(db, inbound)
  586. if err != nil {
  587. logger.Debug("SetInboundEnable: build runtime config failed:", err)
  588. return true, nil
  589. }
  590. if err := rt.AddInbound(context.Background(), runtimeInbound); err != nil {
  591. logger.Debug("SetInboundEnable: AddInbound on", rt.Name(), "failed:", err)
  592. needRestart = true
  593. }
  594. return needRestart, nil
  595. }
  596. func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
  597. // Normalize streamSettings based on protocol
  598. s.normalizeStreamSettings(inbound)
  599. s.normalizeMtprotoSecret(inbound)
  600. conflict, err := s.checkPortConflict(inbound, inbound.Id)
  601. if err != nil {
  602. return inbound, false, err
  603. }
  604. if conflict != nil {
  605. return inbound, false, common.NewError(conflict.String())
  606. }
  607. oldInbound, err := s.GetInbound(inbound.Id)
  608. if err != nil {
  609. return inbound, false, err
  610. }
  611. inbound.NodeID = oldInbound.NodeID
  612. tag := oldInbound.Tag
  613. oldBits := inboundTransports(oldInbound.Protocol, oldInbound.StreamSettings, oldInbound.Settings)
  614. oldTagWasAuto := isAutoGeneratedTag(tag, oldInbound.Port, oldInbound.NodeID, oldBits)
  615. db := database.GetDB()
  616. tx := db.Begin()
  617. markDirty := false
  618. defer func() {
  619. if err != nil {
  620. tx.Rollback()
  621. return
  622. }
  623. tx.Commit()
  624. if markDirty && oldInbound.NodeID != nil {
  625. if dErr := (&NodeService{}).MarkNodeDirty(*oldInbound.NodeID); dErr != nil {
  626. logger.Warning("mark node dirty failed:", dErr)
  627. }
  628. }
  629. }()
  630. err = s.updateClientTraffics(tx, oldInbound, inbound)
  631. if err != nil {
  632. return inbound, false, err
  633. }
  634. // Ensure created_at and updated_at exist in inbound.Settings clients
  635. {
  636. var oldSettings map[string]any
  637. _ = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
  638. emailToCreated := map[string]int64{}
  639. emailToUpdated := map[string]int64{}
  640. if oldSettings != nil {
  641. if oc, ok := oldSettings["clients"].([]any); ok {
  642. for _, it := range oc {
  643. if m, ok2 := it.(map[string]any); ok2 {
  644. if email, ok3 := m["email"].(string); ok3 {
  645. switch v := m["created_at"].(type) {
  646. case float64:
  647. emailToCreated[email] = int64(v)
  648. case int64:
  649. emailToCreated[email] = v
  650. }
  651. switch v := m["updated_at"].(type) {
  652. case float64:
  653. emailToUpdated[email] = int64(v)
  654. case int64:
  655. emailToUpdated[email] = v
  656. }
  657. }
  658. }
  659. }
  660. }
  661. }
  662. var newSettings map[string]any
  663. if err2 := json.Unmarshal([]byte(inbound.Settings), &newSettings); err2 == nil && newSettings != nil {
  664. now := time.Now().Unix() * 1000
  665. if nSlice, ok := newSettings["clients"].([]any); ok {
  666. for i := range nSlice {
  667. if m, ok2 := nSlice[i].(map[string]any); ok2 {
  668. email, _ := m["email"].(string)
  669. if _, ok3 := m["created_at"]; !ok3 {
  670. if v, ok4 := emailToCreated[email]; ok4 && v > 0 {
  671. m["created_at"] = v
  672. } else {
  673. m["created_at"] = now
  674. }
  675. }
  676. // Preserve client's updated_at if present; do not bump on parent inbound update
  677. if _, hasUpdated := m["updated_at"]; !hasUpdated {
  678. if v, ok4 := emailToUpdated[email]; ok4 && v > 0 {
  679. m["updated_at"] = v
  680. }
  681. }
  682. nSlice[i] = m
  683. }
  684. }
  685. newSettings["clients"] = nSlice
  686. if bs, err3 := json.MarshalIndent(newSettings, "", " "); err3 == nil {
  687. inbound.Settings = string(bs)
  688. }
  689. }
  690. }
  691. }
  692. oldInbound.Total = inbound.Total
  693. oldInbound.Remark = inbound.Remark
  694. oldInbound.Enable = inbound.Enable
  695. oldInbound.ExpiryTime = inbound.ExpiryTime
  696. oldInbound.TrafficReset = inbound.TrafficReset
  697. oldInbound.Listen = inbound.Listen
  698. oldInbound.Port = inbound.Port
  699. oldInbound.Protocol = inbound.Protocol
  700. oldInbound.Settings = inbound.Settings
  701. oldInbound.StreamSettings = inbound.StreamSettings
  702. oldInbound.Sniffing = inbound.Sniffing
  703. if oldTagWasAuto && inbound.Tag == tag {
  704. inbound.Tag = ""
  705. }
  706. oldInbound.Tag, err = s.resolveInboundTag(inbound, inbound.Id)
  707. if err != nil {
  708. return inbound, false, err
  709. }
  710. inbound.Tag = oldInbound.Tag
  711. needRestart := false
  712. rt, push, dirty, perr := s.nodePushPlan(oldInbound)
  713. if perr != nil {
  714. err = perr
  715. return inbound, false, err
  716. }
  717. if dirty {
  718. markDirty = true
  719. }
  720. if oldInbound.NodeID == nil {
  721. if !push {
  722. needRestart = true
  723. } else {
  724. oldSnapshot := *oldInbound
  725. oldSnapshot.Tag = tag
  726. if err2 := rt.DelInbound(context.Background(), &oldSnapshot); err2 == nil {
  727. logger.Debug("Old inbound deleted on", rt.Name(), ":", tag)
  728. }
  729. if inbound.Enable {
  730. runtimeInbound, err2 := s.buildRuntimeInboundForAPI(tx, oldInbound)
  731. if err2 != nil {
  732. logger.Debug("Unable to prepare runtime inbound config:", err2)
  733. needRestart = true
  734. } else if err2 := rt.AddInbound(context.Background(), runtimeInbound); err2 == nil {
  735. logger.Debug("Updated inbound added on", rt.Name(), ":", oldInbound.Tag)
  736. } else {
  737. logger.Debug("Unable to update inbound on", rt.Name(), ":", err2)
  738. needRestart = true
  739. }
  740. }
  741. }
  742. } else if push {
  743. oldSnapshot := *oldInbound
  744. oldSnapshot.Tag = tag
  745. if !inbound.Enable {
  746. if err2 := rt.DelInbound(context.Background(), &oldSnapshot); err2 != nil {
  747. logger.Warning("Unable to disable inbound on", rt.Name(), ":", err2)
  748. markDirty = true
  749. }
  750. } else if err2 := rt.UpdateInbound(context.Background(), &oldSnapshot, oldInbound); err2 != nil {
  751. logger.Warning("Unable to update inbound on", rt.Name(), ":", err2)
  752. markDirty = true
  753. }
  754. }
  755. if err = tx.Save(oldInbound).Error; err != nil {
  756. return inbound, false, err
  757. }
  758. newClients, gcErr := s.GetClients(oldInbound)
  759. if gcErr != nil {
  760. err = gcErr
  761. return inbound, false, err
  762. }
  763. if err = s.clientService.SyncInbound(tx, oldInbound.Id, newClients); err != nil {
  764. return inbound, false, err
  765. }
  766. return inbound, needRestart, nil
  767. }
  768. func (s *InboundService) buildRuntimeInboundForAPI(tx *gorm.DB, inbound *model.Inbound) (*model.Inbound, error) {
  769. if inbound == nil {
  770. return nil, fmt.Errorf("inbound is nil")
  771. }
  772. runtimeInbound := *inbound
  773. settings := map[string]any{}
  774. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  775. return nil, err
  776. }
  777. clients, ok := settings["clients"].([]any)
  778. if !ok {
  779. return &runtimeInbound, nil
  780. }
  781. var clientStats []xray.ClientTraffic
  782. err := tx.Model(xray.ClientTraffic{}).
  783. Where("inbound_id = ?", inbound.Id).
  784. Select("email", "enable").
  785. Find(&clientStats).Error
  786. if err != nil {
  787. return nil, err
  788. }
  789. enableMap := make(map[string]bool, len(clientStats))
  790. for _, clientTraffic := range clientStats {
  791. enableMap[clientTraffic.Email] = clientTraffic.Enable
  792. }
  793. finalClients := make([]any, 0, len(clients))
  794. for _, client := range clients {
  795. c, ok := client.(map[string]any)
  796. if !ok {
  797. continue
  798. }
  799. email, _ := c["email"].(string)
  800. if enable, exists := enableMap[email]; exists && !enable {
  801. continue
  802. }
  803. if manualEnable, ok := c["enable"].(bool); ok && !manualEnable {
  804. continue
  805. }
  806. finalClients = append(finalClients, c)
  807. }
  808. settings["clients"] = finalClients
  809. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  810. if err != nil {
  811. return nil, err
  812. }
  813. runtimeInbound.Settings = string(modifiedSettings)
  814. return &runtimeInbound, nil
  815. }
  816. // updateClientTraffics syncs the ClientTraffic rows with the inbound's clients
  817. // list: removes rows for emails that disappeared, inserts rows for newly-added
  818. // emails. Uses sets for O(N) lookup — the previous nested-loop implementation
  819. // was O(N²) and degraded into multi-second pauses on inbounds with thousands
  820. // of clients (toggling, saving, or deleting any such inbound felt frozen).
  821. func (s *InboundService) updateClientTraffics(tx *gorm.DB, oldInbound *model.Inbound, newInbound *model.Inbound) error {
  822. oldClients, err := s.GetClients(oldInbound)
  823. if err != nil {
  824. return err
  825. }
  826. newClients, err := s.GetClients(newInbound)
  827. if err != nil {
  828. return err
  829. }
  830. // Email is the unique key for ClientTraffic rows. Clients without an
  831. // email have no stats row to sync — skip them on both sides instead of
  832. // risking a unique-constraint hit or accidental delete of an unrelated row.
  833. oldEmails := make(map[string]struct{}, len(oldClients))
  834. for i := range oldClients {
  835. if oldClients[i].Email == "" {
  836. continue
  837. }
  838. oldEmails[oldClients[i].Email] = struct{}{}
  839. }
  840. newEmails := make(map[string]struct{}, len(newClients))
  841. for i := range newClients {
  842. if newClients[i].Email == "" {
  843. continue
  844. }
  845. newEmails[newClients[i].Email] = struct{}{}
  846. }
  847. // Drop stats rows for removed emails — but not when a sibling inbound
  848. // still references the email, since the row is the shared accumulator.
  849. for i := range oldClients {
  850. email := oldClients[i].Email
  851. if email == "" {
  852. continue
  853. }
  854. if _, kept := newEmails[email]; kept {
  855. continue
  856. }
  857. stillUsed, err := s.emailUsedByOtherInbounds(email, oldInbound.Id)
  858. if err != nil {
  859. return err
  860. }
  861. if stillUsed {
  862. continue
  863. }
  864. if err := s.DelClientStat(tx, email); err != nil {
  865. return err
  866. }
  867. // Keep inbound_client_ips in sync when the inbound edit drops an
  868. // email, so the IP-limit job doesn't keep a ghost tracking row (#4963).
  869. if err := s.DelClientIPs(tx, email); err != nil {
  870. return err
  871. }
  872. }
  873. for i := range newClients {
  874. email := newClients[i].Email
  875. if email == "" {
  876. continue
  877. }
  878. if _, existed := oldEmails[email]; existed {
  879. if err := s.UpdateClientStat(tx, email, &newClients[i]); err != nil {
  880. return err
  881. }
  882. continue
  883. }
  884. if err := s.AddClientStat(tx, oldInbound.Id, &newClients[i]); err != nil {
  885. return err
  886. }
  887. }
  888. return nil
  889. }
  890. func (s *InboundService) GetInboundTags() (string, error) {
  891. db := database.GetDB()
  892. var inboundTags []string
  893. err := db.Model(model.Inbound{}).Select("tag").Find(&inboundTags).Error
  894. if err != nil && err != gorm.ErrRecordNotFound {
  895. return "", err
  896. }
  897. tags, _ := json.Marshal(inboundTags)
  898. return string(tags), nil
  899. }
  900. func (s *InboundService) GetClientReverseTags() (string, error) {
  901. db := database.GetDB()
  902. var inbounds []model.Inbound
  903. err := db.Model(model.Inbound{}).Select("settings").Where("protocol = ?", "vless").Find(&inbounds).Error
  904. if err != nil && err != gorm.ErrRecordNotFound {
  905. return "[]", err
  906. }
  907. tagSet := make(map[string]struct{})
  908. for _, inbound := range inbounds {
  909. var settings map[string]any
  910. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  911. continue
  912. }
  913. clients, ok := settings["clients"].([]any)
  914. if !ok {
  915. continue
  916. }
  917. for _, client := range clients {
  918. clientMap, ok := client.(map[string]any)
  919. if !ok {
  920. continue
  921. }
  922. reverse, ok := clientMap["reverse"].(map[string]any)
  923. if !ok {
  924. continue
  925. }
  926. tag, _ := reverse["tag"].(string)
  927. tag = strings.TrimSpace(tag)
  928. if tag != "" {
  929. tagSet[tag] = struct{}{}
  930. }
  931. }
  932. }
  933. rawTags := make([]string, 0, len(tagSet))
  934. for tag := range tagSet {
  935. rawTags = append(rawTags, tag)
  936. }
  937. sort.Strings(rawTags)
  938. result, _ := json.Marshal(rawTags)
  939. return string(result), nil
  940. }
  941. func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error) {
  942. db := database.GetDB()
  943. var inbounds []*model.Inbound
  944. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("remark like ?", "%"+query+"%").Find(&inbounds).Error
  945. if err != nil && err != gorm.ErrRecordNotFound {
  946. return nil, err
  947. }
  948. return inbounds, nil
  949. }