1
0

inbound_node.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "sync"
  9. "time"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database"
  11. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  12. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  13. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  14. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  15. "gorm.io/gorm"
  16. "gorm.io/gorm/clause"
  17. )
  18. var reportedRemoteTagConflict sync.Map
  19. func (s *InboundService) runtimeFor(ib *model.Inbound) (runtime.Runtime, error) {
  20. mgr := runtime.GetManager()
  21. if mgr == nil {
  22. return nil, fmt.Errorf("runtime manager not initialised")
  23. }
  24. return mgr.RuntimeFor(ib.NodeID)
  25. }
  26. func (s *InboundService) nodePushPlan(ib *model.Inbound) (runtime.Runtime, bool, bool, error) {
  27. if ib.NodeID == nil {
  28. rt, err := s.runtimeFor(ib)
  29. if err != nil {
  30. return nil, false, false, nil
  31. }
  32. return rt, true, false, nil
  33. }
  34. nodeSvc := NodeService{}
  35. enabled, status, _, _, err := nodeSvc.NodeSyncState(*ib.NodeID)
  36. if err != nil {
  37. return nil, false, false, err
  38. }
  39. if !enabled || status == "offline" {
  40. return nil, false, true, nil
  41. }
  42. rt, err := s.runtimeFor(ib)
  43. if err != nil {
  44. return nil, false, true, nil
  45. }
  46. return rt, true, false, nil
  47. }
  48. func (s *InboundService) NodeIsPending(nodeID *int) bool {
  49. if nodeID == nil {
  50. return false
  51. }
  52. return (&NodeService{}).IsNodePending(*nodeID)
  53. }
  54. func (s *InboundService) AnyNodePending(inboundIds []int) bool {
  55. if len(inboundIds) == 0 {
  56. return false
  57. }
  58. nodeSvc := NodeService{}
  59. for _, id := range inboundIds {
  60. ib, err := s.GetInbound(id)
  61. if err != nil || ib.NodeID == nil {
  62. continue
  63. }
  64. if nodeSvc.IsNodePending(*ib.NodeID) {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. func (s *InboundService) ReconcileNode(ctx context.Context, rt *runtime.Remote, n *model.Node) error {
  71. if rt == nil || n == nil || n.Id <= 0 {
  72. return nil
  73. }
  74. nodeID := n.Id
  75. db := database.GetDB()
  76. var inbounds []*model.Inbound
  77. if err := db.Model(model.Inbound{}).Where("node_id = ?", nodeID).Find(&inbounds).Error; err != nil {
  78. return err
  79. }
  80. remoteTags, err := rt.ListRemoteTags(ctx)
  81. if err != nil {
  82. return err
  83. }
  84. prefix := nodeTagPrefix(&nodeID)
  85. desiredTags := make(map[string]struct{}, len(inbounds)*2)
  86. for _, ib := range inbounds {
  87. desiredTags[ib.Tag] = struct{}{}
  88. if prefix != "" {
  89. if stripped, found := strings.CutPrefix(ib.Tag, prefix); found {
  90. desiredTags[stripped] = struct{}{}
  91. } else {
  92. desiredTags[prefix+ib.Tag] = struct{}{}
  93. }
  94. }
  95. if err := rt.UpdateInbound(ctx, ib, ib); err != nil {
  96. return fmt.Errorf("reconcile inbound %q: %w", ib.Tag, err)
  97. }
  98. }
  99. // In "selected" sync mode the panel only manages the selected tags: the
  100. // rest were never imported, so their absence from the local DB must not
  101. // delete them from the node. Only a selected tag missing locally (the
  102. // panel deleted it while the node was unreachable) may be swept.
  103. var selected map[string]struct{}
  104. if n.InboundSyncMode == "selected" {
  105. selected = make(map[string]struct{}, len(n.InboundTags))
  106. for _, tag := range n.InboundTags {
  107. selected[tag] = struct{}{}
  108. }
  109. }
  110. for _, tag := range remoteTags {
  111. if _, want := desiredTags[tag]; want {
  112. continue
  113. }
  114. if selected != nil {
  115. if _, managed := selected[tag]; !managed {
  116. continue
  117. }
  118. }
  119. if err := rt.DelInbound(ctx, &model.Inbound{Tag: tag}); err != nil {
  120. return fmt.Errorf("reconcile delete %q: %w", tag, err)
  121. }
  122. }
  123. return nil
  124. }
  125. const resetGracePeriodMs int64 = 30000
  126. // onlineGracePeriodMs must comfortably exceed the 5s traffic-poll interval —
  127. // Xray's stats counters often report a zero delta for an active session across
  128. // a single poll, so a 5s grace would still drop the client on the next tick.
  129. // ~4 polls of slack keeps idle-but-connected clients visible without lingering
  130. // long after a real disconnect.
  131. const onlineGracePeriodMs int64 = 20000
  132. type nodeTrafficCounter struct {
  133. Up int64
  134. Down int64
  135. }
  136. func (s *InboundService) upsertNodeBaseline(tx *gorm.DB, nodeID int, email string, up, down int64) error {
  137. return tx.Clauses(clause.OnConflict{
  138. Columns: []clause.Column{{Name: "node_id"}, {Name: "email"}},
  139. DoUpdates: clause.AssignmentColumns([]string{"up", "down"}),
  140. }).Create(&model.NodeClientTraffic{NodeId: nodeID, Email: email, Up: up, Down: down}).Error
  141. }
  142. func (s *InboundService) SetRemoteTraffic(nodeID int, snap *runtime.TrafficSnapshot, dirty bool) (bool, error) {
  143. var structuralChange bool
  144. err := submitTrafficWrite(func() error {
  145. var inner error
  146. structuralChange, inner = s.setRemoteTrafficLocked(nodeID, snap, dirty)
  147. return inner
  148. })
  149. return structuralChange, err
  150. }
  151. func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.TrafficSnapshot, dirty bool) (bool, error) {
  152. if snap == nil || nodeID <= 0 {
  153. return false, nil
  154. }
  155. db := database.GetDB()
  156. now := time.Now().UnixMilli()
  157. // originGuidFor attributes a synced inbound to the panel that physically
  158. // hosts it: inbounds the node forwards from its own sub-nodes already carry
  159. // a non-empty OriginNodeGuid (kept as-is across hops); the node's own local
  160. // inbounds report empty, so they are attributed to the node's own GUID. An
  161. // empty result (old-build node with no GUID yet) leaves attribution to the
  162. // node_id fallback downstream (#4983).
  163. var nodeRow model.Node
  164. db.Select("guid").Where("id = ?", nodeID).First(&nodeRow)
  165. originGuidFor := func(snapIb *model.Inbound) string {
  166. if snapIb.OriginNodeGuid != "" {
  167. return snapIb.OriginNodeGuid
  168. }
  169. return nodeRow.Guid
  170. }
  171. var central []model.Inbound
  172. if err := db.Model(model.Inbound{}).
  173. Where("node_id = ?", nodeID).
  174. Find(&central).Error; err != nil {
  175. return false, err
  176. }
  177. // Index under the stored tag and its prefix-flipped form so a snap matches
  178. // whether the n<id>- prefix lives on the node side, the central side, or
  179. // neither — a mismatch must never spawn a duplicate central inbound.
  180. tagToCentral := make(map[string]*model.Inbound, len(central)*2)
  181. prefix := nodeTagPrefix(&nodeID)
  182. for i := range central {
  183. tagToCentral[central[i].Tag] = &central[i]
  184. if prefix != "" {
  185. if stripped, found := strings.CutPrefix(central[i].Tag, prefix); found {
  186. tagToCentral[stripped] = &central[i]
  187. } else {
  188. tagToCentral[prefix+central[i].Tag] = &central[i]
  189. }
  190. }
  191. }
  192. var centralClientStats []xray.ClientTraffic
  193. if len(central) > 0 {
  194. ids := make([]int, 0, len(central))
  195. for i := range central {
  196. ids = append(ids, central[i].Id)
  197. }
  198. if err := db.Model(xray.ClientTraffic{}).
  199. Where("inbound_id IN ?", ids).
  200. Find(&centralClientStats).Error; err != nil {
  201. return false, err
  202. }
  203. }
  204. type csKey struct {
  205. inboundID int
  206. email string
  207. }
  208. centralCS := make(map[csKey]*xray.ClientTraffic, len(centralClientStats))
  209. centralCSByEmail := make(map[string]*xray.ClientTraffic, len(centralClientStats))
  210. for i := range centralClientStats {
  211. centralCS[csKey{centralClientStats[i].InboundId, centralClientStats[i].Email}] = &centralClientStats[i]
  212. centralCSByEmail[centralClientStats[i].Email] = &centralClientStats[i]
  213. }
  214. nodeBaselines := make(map[string]nodeTrafficCounter)
  215. var baselineRows []model.NodeClientTraffic
  216. if err := db.Model(&model.NodeClientTraffic{}).
  217. Where("node_id = ?", nodeID).
  218. Find(&baselineRows).Error; err != nil {
  219. return false, err
  220. }
  221. for i := range baselineRows {
  222. nodeBaselines[baselineRows[i].Email] = nodeTrafficCounter{Up: baselineRows[i].Up, Down: baselineRows[i].Down}
  223. }
  224. var existingEmailsList []string
  225. if err := db.Model(xray.ClientTraffic{}).Pluck("email", &existingEmailsList).Error; err != nil {
  226. return false, err
  227. }
  228. existingEmails := make(map[string]struct{}, len(existingEmailsList))
  229. for _, e := range existingEmailsList {
  230. existingEmails[e] = struct{}{}
  231. }
  232. var defaultUserId int
  233. if len(central) > 0 {
  234. defaultUserId = central[0].UserId
  235. } else {
  236. var u model.User
  237. if err := db.Model(model.User{}).Order("id asc").First(&u).Error; err == nil {
  238. defaultUserId = u.Id
  239. } else {
  240. defaultUserId = 1
  241. }
  242. }
  243. tx := db.Begin()
  244. committed := false
  245. defer func() {
  246. if !committed {
  247. tx.Rollback()
  248. }
  249. }()
  250. structuralChange := false
  251. snapTags := make(map[string]struct{}, len(snap.Inbounds))
  252. for _, snapIb := range snap.Inbounds {
  253. if snapIb == nil {
  254. continue
  255. }
  256. snapTags[snapIb.Tag] = struct{}{}
  257. // Record the prefix-flipped form too so the orphan sweep below keeps a
  258. // central inbound whether its tag carries the n<id>- prefix or not.
  259. if prefix != "" {
  260. if stripped, found := strings.CutPrefix(snapIb.Tag, prefix); found {
  261. snapTags[stripped] = struct{}{}
  262. } else {
  263. snapTags[prefix+snapIb.Tag] = struct{}{}
  264. }
  265. }
  266. c, ok := tagToCentral[snapIb.Tag]
  267. if !ok {
  268. if dirty {
  269. continue
  270. }
  271. // Try snap.Tag first; on collision fall back to the n<id>-
  272. // prefixed form so local+node can both own the same port.
  273. pickFreeTag := func() (string, error) {
  274. candidates := []string{snapIb.Tag}
  275. if prefix != "" && !strings.HasPrefix(snapIb.Tag, prefix) {
  276. candidates = append(candidates, prefix+snapIb.Tag)
  277. }
  278. for _, t := range candidates {
  279. var owner model.Inbound
  280. err := tx.Where("tag = ?", t).First(&owner).Error
  281. if errors.Is(err, gorm.ErrRecordNotFound) {
  282. return t, nil
  283. }
  284. if err != nil {
  285. return "", err
  286. }
  287. }
  288. return "", nil
  289. }
  290. chosenTag, err := pickFreeTag()
  291. if err != nil {
  292. logger.Warningf("setRemoteTraffic: check tag %q failed: %v", snapIb.Tag, err)
  293. continue
  294. }
  295. if chosenTag == "" {
  296. key := fmt.Sprintf("%d:%s", nodeID, snapIb.Tag)
  297. if _, seen := reportedRemoteTagConflict.LoadOrStore(key, struct{}{}); !seen {
  298. logger.Warningf(
  299. "setRemoteTraffic: tag %q from node %d collides with an existing inbound even after the n%d- prefix — skipping (rename one side to remove the duplicate)",
  300. snapIb.Tag, nodeID, nodeID,
  301. )
  302. }
  303. continue
  304. }
  305. newIb := model.Inbound{
  306. UserId: defaultUserId,
  307. NodeID: &nodeID,
  308. OriginNodeGuid: originGuidFor(snapIb),
  309. Tag: chosenTag,
  310. Listen: snapIb.Listen,
  311. Port: snapIb.Port,
  312. Protocol: snapIb.Protocol,
  313. Settings: snapIb.Settings,
  314. StreamSettings: snapIb.StreamSettings,
  315. Sniffing: snapIb.Sniffing,
  316. TrafficReset: snapIb.TrafficReset,
  317. LastTrafficResetTime: snapIb.LastTrafficResetTime,
  318. Enable: snapIb.Enable,
  319. Remark: snapIb.Remark,
  320. Total: snapIb.Total,
  321. ExpiryTime: snapIb.ExpiryTime,
  322. Up: snapIb.Up,
  323. Down: snapIb.Down,
  324. ShareAddrStrategy: "node",
  325. }
  326. if err := tx.Create(&newIb).Error; err != nil {
  327. logger.Warningf("setRemoteTraffic: create central inbound for tag %q failed: %v", snapIb.Tag, err)
  328. continue
  329. }
  330. tagToCentral[snapIb.Tag] = &newIb
  331. if newIb.Tag != snapIb.Tag {
  332. tagToCentral[newIb.Tag] = &newIb
  333. }
  334. structuralChange = true
  335. continue
  336. }
  337. inGrace := c.LastTrafficResetTime > 0 && now-c.LastTrafficResetTime < resetGracePeriodMs
  338. updates := map[string]any{}
  339. if !dirty {
  340. updates["enable"] = snapIb.Enable
  341. updates["remark"] = snapIb.Remark
  342. updates["listen"] = snapIb.Listen
  343. updates["port"] = snapIb.Port
  344. updates["protocol"] = snapIb.Protocol
  345. updates["total"] = snapIb.Total
  346. updates["expiry_time"] = snapIb.ExpiryTime
  347. updates["settings"] = snapIb.Settings
  348. updates["stream_settings"] = snapIb.StreamSettings
  349. updates["sniffing"] = snapIb.Sniffing
  350. updates["traffic_reset"] = snapIb.TrafficReset
  351. updates["last_traffic_reset_time"] = snapIb.LastTrafficResetTime
  352. }
  353. if !inGrace || (snapIb.Up+snapIb.Down) <= (c.Up+c.Down) {
  354. updates["up"] = snapIb.Up
  355. updates["down"] = snapIb.Down
  356. }
  357. // Physical-home attribution is independent of config-dirty state, so
  358. // keep it current even while the node has pending offline edits. Writes
  359. // once to backfill an existing row, then stays equal (#4983).
  360. if og := originGuidFor(snapIb); c.OriginNodeGuid != og {
  361. updates["origin_node_guid"] = og
  362. }
  363. if !dirty && (c.Settings != snapIb.Settings ||
  364. c.Remark != snapIb.Remark ||
  365. c.Listen != snapIb.Listen ||
  366. c.Port != snapIb.Port ||
  367. c.Total != snapIb.Total ||
  368. c.ExpiryTime != snapIb.ExpiryTime ||
  369. c.Enable != snapIb.Enable) {
  370. structuralChange = true
  371. }
  372. if len(updates) > 0 {
  373. if err := tx.Model(model.Inbound{}).
  374. Where("id = ?", c.Id).
  375. Updates(updates).Error; err != nil {
  376. return false, err
  377. }
  378. }
  379. }
  380. for _, c := range central {
  381. if dirty {
  382. continue
  383. }
  384. if _, kept := snapTags[c.Tag]; kept {
  385. continue
  386. }
  387. var goneEmails []string
  388. if err := tx.Model(xray.ClientTraffic{}).
  389. Where("inbound_id = ?", c.Id).
  390. Pluck("email", &goneEmails).Error; err != nil {
  391. return false, err
  392. }
  393. if len(goneEmails) > 0 {
  394. // Chunk to avoid SQLite bind var limit when a node has many clients
  395. // removed (e.g. after API bulk delete or structural change on node inbound).
  396. for _, batch := range chunkStrings(goneEmails, sqliteMaxVars) {
  397. if err := tx.Where("node_id = ? AND email IN ?", nodeID, batch).
  398. Delete(&model.NodeClientTraffic{}).Error; err != nil {
  399. return false, err
  400. }
  401. }
  402. // The per-email row is the shared accumulator across every inbound
  403. // (and node) the email is attached to. Only drop it when this was the
  404. // email's last inbound — wiping it while a sibling still feeds it
  405. // loses the summed history, and the next node sync would re-seed the
  406. // row with that node's counter alone.
  407. sharedEmails, sErr := s.emailsUsedByOtherInbounds(goneEmails, c.Id)
  408. if sErr != nil {
  409. return false, sErr
  410. }
  411. delEmails := make([]string, 0, len(goneEmails))
  412. for _, e := range goneEmails {
  413. if !sharedEmails[strings.ToLower(strings.TrimSpace(e))] {
  414. delEmails = append(delEmails, e)
  415. }
  416. }
  417. for _, batch := range chunkStrings(delEmails, sqliteMaxVars) {
  418. if err := tx.Where("inbound_id = ? AND email IN ?", c.Id, batch).
  419. Delete(&xray.ClientTraffic{}).Error; err != nil {
  420. return false, err
  421. }
  422. }
  423. }
  424. if err := s.clientService.DetachInbound(tx, c.Id); err != nil {
  425. return false, err
  426. }
  427. if err := tx.Where("id = ?", c.Id).
  428. Delete(&model.Inbound{}).Error; err != nil {
  429. return false, err
  430. }
  431. delete(tagToCentral, c.Tag)
  432. structuralChange = true
  433. }
  434. for _, snapIb := range snap.Inbounds {
  435. if snapIb == nil {
  436. continue
  437. }
  438. c, ok := tagToCentral[snapIb.Tag]
  439. if !ok {
  440. continue
  441. }
  442. snapEmails := make(map[string]struct{}, len(snapIb.ClientStats))
  443. for _, cs := range snapIb.ClientStats {
  444. snapEmails[cs.Email] = struct{}{}
  445. base, seen := nodeBaselines[cs.Email]
  446. var deltaUp, deltaDown int64
  447. if seen {
  448. if deltaUp = cs.Up - base.Up; deltaUp < 0 {
  449. deltaUp = cs.Up
  450. }
  451. if deltaDown = cs.Down - base.Down; deltaDown < 0 {
  452. deltaDown = cs.Down
  453. }
  454. }
  455. if _, rowExists := existingEmails[cs.Email]; !rowExists {
  456. if dirty {
  457. continue
  458. }
  459. row := &xray.ClientTraffic{
  460. InboundId: c.Id,
  461. Email: cs.Email,
  462. Enable: cs.Enable,
  463. Total: cs.Total,
  464. ExpiryTime: cs.ExpiryTime,
  465. Reset: cs.Reset,
  466. Up: cs.Up,
  467. Down: cs.Down,
  468. LastOnline: cs.LastOnline,
  469. }
  470. if err := tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
  471. Create(row).Error; err != nil {
  472. return false, err
  473. }
  474. centralCS[csKey{c.Id, cs.Email}] = row
  475. centralCSByEmail[cs.Email] = row
  476. existingEmails[cs.Email] = struct{}{}
  477. structuralChange = true
  478. if err := s.upsertNodeBaseline(tx, nodeID, cs.Email, cs.Up, cs.Down); err != nil {
  479. return false, err
  480. }
  481. nodeBaselines[cs.Email] = nodeTrafficCounter{Up: cs.Up, Down: cs.Down}
  482. continue
  483. }
  484. if existing := centralCSByEmail[cs.Email]; existing != nil &&
  485. (existing.Enable != cs.Enable ||
  486. existing.Total != cs.Total ||
  487. existing.ExpiryTime != cs.ExpiryTime ||
  488. existing.Reset != cs.Reset) {
  489. structuralChange = true
  490. }
  491. enableExpr := database.ClientTrafficEnableMergeExpr()
  492. if err := tx.Exec(
  493. fmt.Sprintf(
  494. `UPDATE client_traffics
  495. SET up = up + ?, down = down + ?, enable = %s, total = ?, expiry_time = ?, reset = ?,
  496. last_online = %s
  497. WHERE email = ?`,
  498. enableExpr,
  499. database.GreatestExpr("last_online", "?"),
  500. ),
  501. deltaUp, deltaDown, cs.Enable, cs.Total, cs.ExpiryTime, cs.Reset,
  502. cs.LastOnline, cs.Email,
  503. ).Error; err != nil {
  504. return false, err
  505. }
  506. if err := s.upsertNodeBaseline(tx, nodeID, cs.Email, cs.Up, cs.Down); err != nil {
  507. return false, err
  508. }
  509. nodeBaselines[cs.Email] = nodeTrafficCounter{Up: cs.Up, Down: cs.Down}
  510. }
  511. for k, existing := range centralCS {
  512. if dirty {
  513. continue
  514. }
  515. if k.inboundID != c.Id {
  516. continue
  517. }
  518. if _, kept := snapEmails[k.email]; kept {
  519. continue
  520. }
  521. if err := tx.Where("node_id = ? AND email = ?", nodeID, existing.Email).
  522. Delete(&model.NodeClientTraffic{}).Error; err != nil {
  523. return false, err
  524. }
  525. // Same shared-accumulator rule as the inbound-removal sweep above:
  526. // keep the row while another inbound still references the email.
  527. stillUsed, uErr := s.emailUsedByOtherInbounds(existing.Email, c.Id)
  528. if uErr != nil {
  529. return false, uErr
  530. }
  531. if !stillUsed {
  532. if err := tx.Where("inbound_id = ? AND email = ?", c.Id, existing.Email).
  533. Delete(&xray.ClientTraffic{}).Error; err != nil {
  534. return false, err
  535. }
  536. }
  537. structuralChange = true
  538. }
  539. }
  540. type oldSet struct {
  541. inboundID int
  542. emails map[string]struct{}
  543. }
  544. var perInboundOld []oldSet
  545. for _, snapIb := range snap.Inbounds {
  546. if snapIb == nil {
  547. continue
  548. }
  549. c, ok := tagToCentral[snapIb.Tag]
  550. if !ok {
  551. continue
  552. }
  553. if dirty {
  554. continue
  555. }
  556. var oldEmailsRows []string
  557. if err := tx.Table("clients").
  558. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  559. Where("client_inbounds.inbound_id = ?", c.Id).
  560. Pluck("email", &oldEmailsRows).Error; err == nil {
  561. oldEmails := make(map[string]struct{}, len(oldEmailsRows))
  562. for _, e := range oldEmailsRows {
  563. if e != "" {
  564. oldEmails[e] = struct{}{}
  565. }
  566. }
  567. perInboundOld = append(perInboundOld, oldSet{inboundID: c.Id, emails: oldEmails})
  568. }
  569. clients, gcErr := s.GetClients(snapIb)
  570. if gcErr != nil {
  571. logger.Warningf("setRemoteTraffic: parse clients for tag %q failed: %v", snapIb.Tag, gcErr)
  572. continue
  573. }
  574. csEnableByEmail := make(map[string]bool, len(snapIb.ClientStats))
  575. for _, cs := range snapIb.ClientStats {
  576. csEnableByEmail[cs.Email] = cs.Enable
  577. }
  578. filtered := clients[:0]
  579. for i := range clients {
  580. if isClientEmailTombstoned(clients[i].Email) {
  581. continue
  582. }
  583. if cse, hit := csEnableByEmail[clients[i].Email]; hit && !cse {
  584. clients[i].Enable = false
  585. }
  586. filtered = append(filtered, clients[i])
  587. }
  588. localEmails := make([]string, 0, len(filtered))
  589. for i := range filtered {
  590. if filtered[i].Email != "" {
  591. localEmails = append(localEmails, filtered[i].Email)
  592. }
  593. }
  594. if len(localEmails) > 0 {
  595. var localMeta []struct {
  596. Email string
  597. Comment string `gorm:"column:comment"`
  598. }
  599. if err := tx.Table("clients").
  600. Select("email, comment").
  601. Where("email IN ?", localEmails).
  602. Find(&localMeta).Error; err == nil {
  603. commentByEmail := make(map[string]string, len(localMeta))
  604. for _, m := range localMeta {
  605. commentByEmail[m.Email] = m.Comment
  606. }
  607. for i := range filtered {
  608. if cmt, ok := commentByEmail[filtered[i].Email]; ok {
  609. filtered[i].Comment = cmt
  610. }
  611. }
  612. }
  613. }
  614. if err := s.clientService.SyncInbound(tx, c.Id, filtered); err != nil {
  615. logger.Warningf("setRemoteTraffic: sync clients for tag %q failed: %v", snapIb.Tag, err)
  616. }
  617. }
  618. for _, old := range perInboundOld {
  619. var stillAttached []string
  620. if err := tx.Table("clients").
  621. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  622. Where("client_inbounds.inbound_id = ?", old.inboundID).
  623. Pluck("email", &stillAttached).Error; err != nil {
  624. continue
  625. }
  626. stillSet := make(map[string]struct{}, len(stillAttached))
  627. for _, e := range stillAttached {
  628. stillSet[e] = struct{}{}
  629. }
  630. for email := range old.emails {
  631. if _, kept := stillSet[email]; kept {
  632. continue
  633. }
  634. var attachmentCount int64
  635. if err := tx.Table("client_inbounds").
  636. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  637. Where("clients.email = ?", email).
  638. Count(&attachmentCount).Error; err != nil {
  639. continue
  640. }
  641. if attachmentCount > 0 {
  642. continue
  643. }
  644. if err := tx.Where("email = ?", email).Delete(&model.ClientRecord{}).Error; err != nil {
  645. logger.Warningf("setRemoteTraffic: delete ClientRecord %q failed: %v", email, err)
  646. }
  647. if err := tx.Where("email = ?", email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  648. logger.Warningf("setRemoteTraffic: delete ClientTraffic %q failed: %v", email, err)
  649. }
  650. if err := tx.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  651. logger.Warningf("setRemoteTraffic: delete NodeClientTraffic %q failed: %v", email, err)
  652. }
  653. structuralChange = true
  654. }
  655. }
  656. if err := tx.Commit().Error; err != nil {
  657. return false, err
  658. }
  659. committed = true
  660. if p != nil {
  661. tree := snap.OnlineTree
  662. if len(tree) == 0 && len(snap.OnlineEmails) > 0 {
  663. // Old-build node (no GUID tree): key its flat online list under its
  664. // own effective identity so attribution still works for that branch.
  665. effectiveGuid := nodeRow.Guid
  666. if effectiveGuid == "" {
  667. effectiveGuid = synthNodeGuid(nodeID)
  668. }
  669. tree = map[string][]string{effectiveGuid: snap.OnlineEmails}
  670. }
  671. p.SetNodeOnlineTree(nodeID, tree)
  672. }
  673. return structuralChange, nil
  674. }
  675. func (s *InboundService) restartRemoteNodesOnDisable(nodeIDs []int) {
  676. restartOnDisable, err := (&SettingService{}).GetRestartXrayOnClientDisable()
  677. if err != nil {
  678. logger.Warning("disableInvalidClients: get RestartXrayOnClientDisable failed:", err)
  679. return
  680. }
  681. if !restartOnDisable {
  682. return
  683. }
  684. for _, nodeID := range nodeIDs {
  685. nodeIDCopy := nodeID
  686. rt, rtErr := runtime.GetManager().RuntimeFor(&nodeIDCopy)
  687. if rtErr != nil {
  688. logger.Warning("disableInvalidClients: get runtime for node", nodeID, "failed:", rtErr)
  689. continue
  690. }
  691. if rtErr = rt.RestartXray(context.Background()); rtErr != nil {
  692. logger.Warning("disableInvalidClients: restart xray on node", nodeID, "failed:", rtErr)
  693. }
  694. }
  695. }
  696. func (s *InboundService) GetOnlineClients() []string {
  697. if p == nil {
  698. return []string{}
  699. }
  700. return p.GetOnlineClients()
  701. }
  702. // GetOnlineClientsByGuid returns online emails keyed by the panelGuid of the
  703. // node that physically hosts each set: this panel's own clients under its own
  704. // GUID, plus every node in the tree under its GUID (#4983). Replaces the old
  705. // node-id keying so a client three hops down is attributed to its real node,
  706. // not the intermediate one it was synced through.
  707. func (s *InboundService) GetOnlineClientsByGuid() map[string][]string {
  708. if p == nil {
  709. return map[string][]string{}
  710. }
  711. out := p.GetMergedNodeTrees()
  712. if local := p.GetLocalOnlineClients(); len(local) > 0 {
  713. if guid := s.panelGuid(); guid != "" {
  714. out[guid] = mergeEmails(out[guid], local)
  715. }
  716. }
  717. return out
  718. }
  719. // GetActiveInboundsByGuid returns the inbound tags that carried traffic within
  720. // the grace window for THIS panel, under its own GUID. Remote nodes don't
  721. // report per-inbound activity, so a GUID missing from the map means "don't
  722. // gate" for that node's inbounds.
  723. func (s *InboundService) GetActiveInboundsByGuid() map[string][]string {
  724. if p == nil {
  725. return map[string][]string{}
  726. }
  727. active := p.GetLocalActiveInbounds()
  728. if len(active) == 0 {
  729. return map[string][]string{}
  730. }
  731. guid := s.panelGuid()
  732. if guid == "" {
  733. return map[string][]string{}
  734. }
  735. return map[string][]string{guid: active}
  736. }
  737. func (s *InboundService) SetNodeOnlineTree(nodeID int, tree map[string][]string) {
  738. if p != nil {
  739. p.SetNodeOnlineTree(nodeID, tree)
  740. }
  741. }
  742. func (s *InboundService) ClearNodeOnlineClients(nodeID int) {
  743. if p != nil {
  744. p.ClearNodeOnlineClients(nodeID)
  745. }
  746. }
  747. // panelGuid returns this panel's stable self-identifier, used to key the local
  748. // panel's own clients in the per-node online maps (#4983).
  749. func (s *InboundService) panelGuid() string {
  750. guid, _ := (&SettingService{}).GetPanelGuid()
  751. return guid
  752. }
  753. // synthNodeGuid is the stable per-node fallback identity for a directly-attached
  754. // node whose panel hasn't reported a panelGuid yet (old build). Node ids are
  755. // master-local, so this only composes for direct nodes — exactly the pre-#4983
  756. // flat-topology case where an old-build node appears.
  757. func synthNodeGuid(nodeID int) string {
  758. return fmt.Sprintf("node:%d", nodeID)
  759. }
  760. // mergeEmails returns the deduped union of two email slices.
  761. func mergeEmails(a, b []string) []string {
  762. if len(a) == 0 {
  763. return b
  764. }
  765. seen := make(map[string]struct{}, len(a)+len(b))
  766. out := make([]string, 0, len(a)+len(b))
  767. for _, e := range a {
  768. if _, ok := seen[e]; !ok {
  769. seen[e] = struct{}{}
  770. out = append(out, e)
  771. }
  772. }
  773. for _, e := range b {
  774. if _, ok := seen[e]; !ok {
  775. seen[e] = struct{}{}
  776. out = append(out, e)
  777. }
  778. }
  779. return out
  780. }
  781. func (s *InboundService) GetClientsLastOnline() (map[string]int64, error) {
  782. db := database.GetDB()
  783. var rows []xray.ClientTraffic
  784. err := db.Model(&xray.ClientTraffic{}).Select("email, last_online").Find(&rows).Error
  785. if err != nil && err != gorm.ErrRecordNotFound {
  786. return nil, err
  787. }
  788. result := make(map[string]int64, len(rows))
  789. for _, r := range rows {
  790. result[r.Email] = r.LastOnline
  791. }
  792. return result, nil
  793. }
  794. // RefreshLocalOnlineClients folds the emails and inbound tags active on this
  795. // panel's own xray this poll into the local online/active sets, applying the
  796. // grace window and pruning stale entries. Pass nil to only prune. See
  797. // xray.Process for why the local sets are kept separate from the shared
  798. // last_online column.
  799. func (s *InboundService) RefreshLocalOnlineClients(activeEmails, activeInboundTags []string) {
  800. if p != nil {
  801. p.RefreshLocalOnline(activeEmails, activeInboundTags, time.Now().UnixMilli(), onlineGracePeriodMs)
  802. }
  803. }
  804. func (s *InboundService) FilterAndSortClientEmails(emails []string) ([]string, []string, error) {
  805. db := database.GetDB()
  806. // Step 1: Get ClientTraffic records for emails in the input list.
  807. // Chunked to stay under SQLite's bind-variable limit on huge inputs.
  808. uniqEmails := uniqueNonEmptyStrings(emails)
  809. clients := make([]xray.ClientTraffic, 0, len(uniqEmails))
  810. for _, batch := range chunkStrings(uniqEmails, sqliteMaxVars) {
  811. var page []xray.ClientTraffic
  812. if err := db.Where("email IN ?", batch).Find(&page).Error; err != nil && err != gorm.ErrRecordNotFound {
  813. return nil, nil, err
  814. }
  815. clients = append(clients, page...)
  816. }
  817. // Step 2: Sort clients by (Up + Down) descending
  818. sort.Slice(clients, func(i, j int) bool {
  819. return (clients[i].Up + clients[i].Down) > (clients[j].Up + clients[j].Down)
  820. })
  821. // Step 3: Extract sorted valid emails and track found ones
  822. validEmails := make([]string, 0, len(clients))
  823. found := make(map[string]bool)
  824. for _, client := range clients {
  825. validEmails = append(validEmails, client.Email)
  826. found[client.Email] = true
  827. }
  828. // Step 4: Identify emails that were not found in the database
  829. extraEmails := make([]string, 0)
  830. for _, email := range emails {
  831. if !found[email] {
  832. extraEmails = append(extraEmails, email)
  833. }
  834. }
  835. return validEmails, extraEmails, nil
  836. }