inbound_node.go 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  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/util/common"
  14. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  15. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  16. "gorm.io/gorm"
  17. "gorm.io/gorm/clause"
  18. )
  19. var reportedRemoteTagConflict sync.Map
  20. var reportedForeignClientClaim sync.Map
  21. // nodeBulkPushThreshold caps how many per-client RPCs a single operation will
  22. // stream to a remote node. Above it, the panel marks the node dirty instead and
  23. // lets one ReconcileNode push converge the whole inbound — far cheaper than M
  24. // sequential round-trips. Small ops stay on the live per-client path.
  25. const nodeBulkPushThreshold = 32
  26. // nodeClientPushTimeout bounds the synchronous per-client push: the change is
  27. // committed and the node flagged dirty, so a slow node defers to the reconcile.
  28. const nodeClientPushTimeout = 4 * time.Second
  29. // nodeFanoutConcurrency bounds an operation that calls every node, as the heartbeat
  30. // does: one at a time, a few hanging nodes outlast the request's write timeout.
  31. const nodeFanoutConcurrency = 32
  32. func nodePushContext() (context.Context, context.CancelFunc) {
  33. return context.WithTimeout(context.Background(), nodeClientPushTimeout)
  34. }
  35. func (s *InboundService) runtimeFor(ib *model.Inbound) (runtime.Runtime, error) {
  36. mgr := runtime.GetManager()
  37. if mgr == nil {
  38. return nil, fmt.Errorf("runtime manager not initialised")
  39. }
  40. return mgr.RuntimeFor(ib.NodeID)
  41. }
  42. func (s *InboundService) nodePushPlan(ib *model.Inbound) (runtime.Runtime, bool, bool, error) {
  43. if ib.NodeID == nil {
  44. rt, err := s.runtimeFor(ib)
  45. if err != nil {
  46. return nil, false, false, nil
  47. }
  48. return rt, true, false, nil
  49. }
  50. nodeSvc := NodeService{}
  51. enabled, status, _, _, err := nodeSvc.NodeSyncState(*ib.NodeID)
  52. if err != nil {
  53. return nil, false, false, err
  54. }
  55. if !enabled || status == "offline" {
  56. return nil, false, true, nil
  57. }
  58. rt, err := s.runtimeFor(ib)
  59. if err != nil {
  60. return nil, false, true, nil
  61. }
  62. return rt, true, false, nil
  63. }
  64. func (s *InboundService) NodeIsPending(nodeID *int) bool {
  65. if nodeID == nil {
  66. return false
  67. }
  68. return (&NodeService{}).IsNodePending(*nodeID)
  69. }
  70. func (s *InboundService) AnyNodePending(inboundIds []int) bool {
  71. if len(inboundIds) == 0 {
  72. return false
  73. }
  74. nodeSvc := NodeService{}
  75. for _, id := range inboundIds {
  76. ib, err := s.GetInbound(id)
  77. if err != nil || ib.NodeID == nil {
  78. continue
  79. }
  80. if nodeSvc.IsNodePending(*ib.NodeID) {
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. // ReconcileNode pushes every inbound and sweeps undesired remote tags even when
  87. // individual operations fail, returning the failures joined: one inbound the
  88. // node rejects (e.g. a legacy protocol failing validation, #5685) must not
  89. // stall the rest of the node's config — or, via syncOne, its traffic sync.
  90. func (s *InboundService) ReconcileNode(ctx context.Context, rt *runtime.Remote, n *model.Node) error {
  91. if rt == nil || n == nil || n.Id <= 0 {
  92. return nil
  93. }
  94. nodeID := n.Id
  95. db := database.GetDB()
  96. var inbounds []*model.Inbound
  97. if err := db.Model(model.Inbound{}).Where("node_id = ?", nodeID).Find(&inbounds).Error; err != nil {
  98. return err
  99. }
  100. remoteInbounds, err := rt.ListInboundOptions(ctx)
  101. if err != nil {
  102. return err
  103. }
  104. remoteTags := make([]string, 0, len(remoteInbounds))
  105. remoteTagSet := make(map[string]struct{}, len(remoteTags))
  106. for _, remoteIb := range remoteInbounds {
  107. remoteTags = append(remoteTags, remoteIb.Tag)
  108. remoteTagSet[remoteIb.Tag] = struct{}{}
  109. }
  110. prefix := nodeTagPrefix(&nodeID)
  111. desiredTags := make(map[string]struct{}, len(inbounds)*2)
  112. var errs []error
  113. for _, ib := range inbounds {
  114. desiredTags[ib.Tag] = struct{}{}
  115. // existsOnNode: does the node already report this inbound under any of the
  116. // tag forms it may be stored as? If so, an unchanged push can be skipped.
  117. _, existsOnNode := remoteTagSet[ib.Tag]
  118. if prefix != "" {
  119. if stripped, found := strings.CutPrefix(ib.Tag, prefix); found {
  120. desiredTags[stripped] = struct{}{}
  121. if _, ok := remoteTagSet[stripped]; ok {
  122. existsOnNode = true
  123. }
  124. } else {
  125. desiredTags[prefix+ib.Tag] = struct{}{}
  126. if _, ok := remoteTagSet[prefix+ib.Tag]; ok {
  127. existsOnNode = true
  128. }
  129. }
  130. }
  131. runtimeIb := ib
  132. if built, bErr := s.buildInboundForNodePush(db, ib); bErr == nil {
  133. runtimeIb = built
  134. }
  135. if !existsOnNode && n.Guid != "" && ib.OriginNodeGuid == n.Guid {
  136. var compatible []runtime.RemoteInboundOption
  137. for _, remoteIb := range remoteInbounds {
  138. if remoteIb.Port == runtimeIb.Port &&
  139. remoteIb.Protocol == runtimeIb.Protocol &&
  140. strings.TrimSpace(remoteIb.Listen) == strings.TrimSpace(runtimeIb.Listen) {
  141. compatible = append(compatible, remoteIb)
  142. }
  143. }
  144. switch len(compatible) {
  145. case 1:
  146. alias := compatible[0]
  147. desiredTags[alias.Tag] = struct{}{}
  148. rt.AdoptInboundAlias(runtimeIb, alias)
  149. existsOnNode = true
  150. logger.Infof("adopted compatible inbound %q on node %s as %q", alias.Tag, n.Name, ib.Tag)
  151. case 0:
  152. // No compatible occupant: keep the normal create path, which
  153. // leaves a real port/protocol drift loud.
  154. default:
  155. for _, candidate := range compatible {
  156. desiredTags[candidate.Tag] = struct{}{}
  157. }
  158. errs = append(errs, fmt.Errorf("reconcile inbound %q: ambiguous compatible remote inbounds", ib.Tag))
  159. continue
  160. }
  161. }
  162. if _, err := rt.ReconcileInbound(ctx, runtimeIb, existsOnNode); err != nil {
  163. errs = append(errs, fmt.Errorf("reconcile inbound %q: %w", ib.Tag, err))
  164. }
  165. }
  166. // Before the next clean sync adopts the node's inbounds, "absent locally"
  167. // means "not imported yet" — sweeping now would wipe the node at onboarding.
  168. if n.InboundsAdoptedAt == 0 {
  169. return errors.Join(errs...)
  170. }
  171. // In "selected" sync mode the panel only manages the selected tags: the
  172. // rest were never imported, so their absence from the local DB must not
  173. // delete them from the node. Only a selected tag missing locally (the
  174. // panel deleted it while the node was unreachable) may be swept.
  175. selected := nodeSelectedTagSet(n)
  176. for _, tag := range remoteTags {
  177. if _, want := desiredTags[tag]; want {
  178. continue
  179. }
  180. if selected != nil {
  181. if _, managed := selected[tag]; !managed {
  182. continue
  183. }
  184. }
  185. if err := rt.DelInbound(ctx, &model.Inbound{Tag: tag}); err != nil {
  186. errs = append(errs, fmt.Errorf("reconcile delete %q: %w", tag, err))
  187. }
  188. }
  189. return errors.Join(errs...)
  190. }
  191. const resetGracePeriodMs int64 = 30000
  192. // onlineGracePeriodMs must comfortably exceed the 5s traffic-poll interval —
  193. // Xray's stats counters often report a zero delta for an active session across
  194. // a single poll, so a 5s grace would still drop the client on the next tick.
  195. // ~4 polls of slack keeps idle-but-connected clients visible without lingering
  196. // long after a real disconnect.
  197. const onlineGracePeriodMs int64 = 20000
  198. type nodeTrafficCounter struct {
  199. Up int64
  200. Down int64
  201. }
  202. func (s *InboundService) upsertNodeBaseline(tx *gorm.DB, nodeID int, email string, up, down int64) error {
  203. return tx.Clauses(clause.OnConflict{
  204. Columns: []clause.Column{{Name: "node_id"}, {Name: "email"}},
  205. DoUpdates: clause.AssignmentColumns([]string{"up", "down"}),
  206. }).Create(&model.NodeClientTraffic{NodeId: nodeID, Email: email, Up: up, Down: down}).Error
  207. }
  208. // mergeNodeClientTraffic folds one node-reported client into its central row.
  209. // While frozen (a push pending or just landed) only the counters may move.
  210. func (s *InboundService) mergeNodeClientTraffic(tx *gorm.DB, cs xray.ClientTraffic, frozen bool, deltaUp, deltaDown, now int64) error {
  211. if frozen {
  212. return tx.Exec(
  213. fmt.Sprintf(
  214. `UPDATE client_traffics
  215. SET up = %s, down = %s, last_online = %s
  216. WHERE email = ?`,
  217. database.ClampedAddExpr("up"),
  218. database.ClampedAddExpr("down"),
  219. database.GreatestExpr("last_online", "?"),
  220. ),
  221. deltaUp, deltaDown, cs.LastOnline, cs.Email,
  222. ).Error
  223. }
  224. return tx.Exec(
  225. fmt.Sprintf(
  226. `UPDATE client_traffics
  227. SET up = %s, down = %s, enable = %s, total = ?,
  228. expiry_time = %s,
  229. reset = ?, reset_day = ?, reset_weekday = ?, last_online = %s
  230. WHERE email = ?`,
  231. database.ClampedAddExpr("up"),
  232. database.ClampedAddExpr("down"),
  233. database.ClientTrafficEnableMergeExpr(),
  234. database.ClientTrafficExpiryMergeExpr(),
  235. database.GreatestExpr("last_online", "?"),
  236. ),
  237. deltaUp, deltaDown,
  238. cs.Enable, cs.ExpiryTime, cs.Total, now, deltaUp, deltaDown,
  239. cs.Total,
  240. cs.ExpiryTime, cs.Reset, cs.ResetDay, cs.ResetWeekday,
  241. cs.LastOnline, cs.Email,
  242. ).Error
  243. }
  244. // mergeActivationExpiry: master absolute wins; node may only activate when
  245. // master is unset/duration. Node auto-renew goes through nodeClientRenewed.
  246. func mergeActivationExpiry(existing, node int64) int64 {
  247. if existing > 0 {
  248. return existing
  249. }
  250. return node
  251. }
  252. // masterLimitsAllowClient reports whether the master's own deadline and quota
  253. // (including this tick's deltas) still permit the client.
  254. func masterLimitsAllowClient(master *xray.ClientTraffic, now, deltaUp, deltaDown int64) bool {
  255. if master == nil {
  256. return false
  257. }
  258. if master.ExpiryTime > 0 && master.ExpiryTime <= now {
  259. return false
  260. }
  261. if master.Total > 0 && master.Up+deltaUp+master.Down+deltaDown >= master.Total {
  262. return false
  263. }
  264. return true
  265. }
  266. // nodeDisableIsStale reports an enable=false the node decided against limits the
  267. // master has since changed, so it must not latch back (#6228 / #4917).
  268. func nodeDisableIsStale(master *xray.ClientTraffic, node xray.ClientTraffic, now, deltaUp, deltaDown int64) bool {
  269. if master == nil {
  270. return false
  271. }
  272. // Matching limits mean the node judged the client on the master's own terms:
  273. // that verdict is genuine and still latches, as #4917 requires.
  274. if node.ExpiryTime == master.ExpiryTime && node.Total == master.Total {
  275. return false
  276. }
  277. return masterLimitsAllowClient(master, now, deltaUp, deltaDown)
  278. }
  279. func clampTrafficCounter(v int64) int64 {
  280. if v > database.TrafficMax {
  281. return database.TrafficMax
  282. }
  283. if v < 0 {
  284. return 0
  285. }
  286. return v
  287. }
  288. // applyMasterClientLifecycle overlays the already-merged master row onto a
  289. // node-reported client for SyncInbound (#6228).
  290. func applyMasterClientLifecycle(c *model.Client, master *xray.ClientTraffic, cs *xray.ClientTraffic) {
  291. if master == nil {
  292. // No central row to speak for the client: the node's own latch is all
  293. // there is, and it may only disable.
  294. if cs != nil && !cs.Enable {
  295. c.Enable = false
  296. }
  297. return
  298. }
  299. c.ExpiryTime = mergeActivationExpiry(master.ExpiryTime, c.ExpiryTime)
  300. c.Enable = master.Enable
  301. }
  302. // nodeClientRenewed reports a node-side auto-renew: an absolute deadline moved
  303. // forward, evidenced by a renewal-count bump or a drop below the stored baseline.
  304. func nodeClientRenewed(existing *xray.ClientTraffic, cs xray.ClientTraffic, canon, base nodeTrafficCounter) bool {
  305. if (cs.Reset <= 0 && cs.ResetDay <= 0 && cs.ResetWeekday <= 0) || cs.ExpiryTime <= 0 || existing.ExpiryTime <= 0 {
  306. return false
  307. }
  308. if cs.ExpiryTime <= existing.ExpiryTime {
  309. return false
  310. }
  311. // A client that used no traffic in the period never dips, so the renewal
  312. // counter is the only evidence autoRenewClients leaves behind (#6228).
  313. if cs.ResetCount > existing.ResetCount {
  314. return true
  315. }
  316. return canon.Up < base.Up || canon.Down < base.Down
  317. }
  318. // liftActivatedClientRecordExpiries copies a node-activated deadline from
  319. // client_traffics onto client records still holding the negative duration (#5714).
  320. func liftActivatedClientRecordExpiries(tx *gorm.DB) error {
  321. return tx.Exec(
  322. `UPDATE clients
  323. SET expiry_time = (SELECT ct.expiry_time FROM client_traffics ct WHERE ct.email = clients.email AND ct.expiry_time > 0 LIMIT 1)
  324. WHERE clients.expiry_time < 0
  325. AND EXISTS (SELECT 1 FROM client_traffics ct WHERE ct.email = clients.email AND ct.expiry_time > 0)`,
  326. ).Error
  327. }
  328. // SnapshotHasUnadoptedInbounds reports whether the snapshot carries a tag with
  329. // no central row yet, i.e. the next merge would adopt a new inbound.
  330. func (s *InboundService) SnapshotHasUnadoptedInbounds(nodeID int, snap *runtime.TrafficSnapshot) (bool, error) {
  331. if snap == nil || len(snap.Inbounds) == 0 {
  332. return false, nil
  333. }
  334. var tags []string
  335. if err := database.GetDB().Model(model.Inbound{}).
  336. Where("node_id = ?", nodeID).
  337. Pluck("tag", &tags).Error; err != nil {
  338. return false, err
  339. }
  340. prefix := nodeTagPrefix(&nodeID)
  341. known := make(map[string]struct{}, len(tags)*2)
  342. for _, tag := range tags {
  343. known[tag] = struct{}{}
  344. if prefix != "" {
  345. if stripped, found := strings.CutPrefix(tag, prefix); found {
  346. known[stripped] = struct{}{}
  347. } else {
  348. known[prefix+tag] = struct{}{}
  349. }
  350. }
  351. }
  352. for _, ib := range snap.Inbounds {
  353. if ib == nil {
  354. continue
  355. }
  356. if _, ok := known[ib.Tag]; !ok {
  357. return true, nil
  358. }
  359. }
  360. return false, nil
  361. }
  362. // SetRemoteTraffic merges a node snapshot. justPushed marks the tick whose
  363. // config push just landed, whose snapshot may still predate it (#6228).
  364. func (s *InboundService) SetRemoteTraffic(nodeID int, snap *runtime.TrafficSnapshot, dirty, justPushed bool) (bool, error) {
  365. var structuralChange bool
  366. err := submitTrafficWrite(func() error {
  367. var inner error
  368. structuralChange, inner = s.setRemoteTrafficLocked(nodeID, snap, dirty, justPushed)
  369. return inner
  370. })
  371. if err != nil {
  372. // As on a failed fetch: a node whose snapshot did not merge keeps no online set.
  373. s.ClearNodeOnlineClients(nodeID)
  374. }
  375. return structuralChange, err
  376. }
  377. // GetNodeInboundTrafficTotals returns the current cumulative up/down for every
  378. // node-hosted inbound, keyed by tag. The node sync diffs successive snapshots of
  379. // this to derive per-inbound speed for the dashboard — node inbounds have no
  380. // local Xray poll to produce live deltas the way local inbounds do.
  381. func (s *InboundService) GetNodeInboundTrafficTotals() (map[string][2]int64, error) {
  382. var rows []struct {
  383. Tag string
  384. Up int64
  385. Down int64
  386. }
  387. if err := database.GetDB().Table("inbounds").
  388. Select("tag, up, down").
  389. Where("node_id IS NOT NULL").
  390. Scan(&rows).Error; err != nil {
  391. return nil, err
  392. }
  393. out := make(map[string][2]int64, len(rows))
  394. for _, r := range rows {
  395. out[r.Tag] = [2]int64{r.Up, r.Down}
  396. }
  397. return out, nil
  398. }
  399. func adoptedWireChanged(c, snapIb *model.Inbound, adoptedSettings string) bool {
  400. return c.Settings != adoptedSettings ||
  401. c.Enable != snapIb.Enable ||
  402. c.Remark != snapIb.Remark ||
  403. c.SubSortIndex != normalizeSubSortIndex(snapIb.SubSortIndex) ||
  404. c.Listen != snapIb.Listen ||
  405. c.Port != snapIb.Port ||
  406. c.Protocol != snapIb.Protocol ||
  407. c.Total != snapIb.Total ||
  408. c.ExpiryTime != snapIb.ExpiryTime ||
  409. c.StreamSettings != snapIb.StreamSettings ||
  410. c.Sniffing != snapIb.Sniffing ||
  411. c.TrafficReset != snapIb.TrafficReset ||
  412. c.TrafficResetDay != normalizeTrafficResetDay(snapIb.TrafficResetDay)
  413. }
  414. // adoptedWireInbound is the central inbound as it reads after adopting the
  415. // node-reported wire fields — the payload the reconcile fingerprint must track.
  416. func adoptedWireInbound(c, snapIb *model.Inbound, adoptedSettings string) *model.Inbound {
  417. a := *c
  418. a.Enable = snapIb.Enable
  419. a.Remark = snapIb.Remark
  420. a.SubSortIndex = normalizeSubSortIndex(snapIb.SubSortIndex)
  421. // ExcludeFromSub stays master-authored: older nodes omit the field and
  422. // would otherwise reset it to false on every heartbeat mirror.
  423. a.Listen = snapIb.Listen
  424. a.Port = snapIb.Port
  425. a.Protocol = snapIb.Protocol
  426. a.Total = snapIb.Total
  427. a.ExpiryTime = snapIb.ExpiryTime
  428. a.Settings = adoptedSettings
  429. a.StreamSettings = snapIb.StreamSettings
  430. a.Sniffing = snapIb.Sniffing
  431. a.TrafficReset = snapIb.TrafficReset
  432. a.TrafficResetDay = normalizeTrafficResetDay(snapIb.TrafficResetDay)
  433. return &a
  434. }
  435. // snapshotDropsEveryHubClient reports a node that lists no clients where the hub
  436. // still links some: a reset or half-started node, never an authoritative removal.
  437. func snapshotDropsEveryHubClient(tx *gorm.DB, inboundID int, wireSettings string) bool {
  438. clients, err := ParseInboundSettingsClients(wireSettings)
  439. if err != nil || len(clients) > 0 {
  440. return false
  441. }
  442. var links int64
  443. if err := tx.Table("client_inbounds").Where("inbound_id = ?", inboundID).Count(&links).Error; err != nil {
  444. return false
  445. }
  446. return links > 0
  447. }
  448. // snapshotAttachedEmails lowercases the emails a snapshot inbound's settings list;
  449. // ok is false when the settings cannot be parsed, so membership is unknown.
  450. func snapshotAttachedEmails(settings string) (map[string]struct{}, bool) {
  451. clients, err := ParseInboundSettingsClients(settings)
  452. if err != nil {
  453. return nil, false
  454. }
  455. emails := make(map[string]struct{}, len(clients))
  456. for i := range clients {
  457. emails[strings.ToLower(clients[i].Email)] = struct{}{}
  458. }
  459. return emails, true
  460. }
  461. // clientEmailsOwnedElsewhere returns the emails attached only to inbounds of
  462. // other nodes: email is unique, so adopting one would overwrite a client this
  463. // node does not serve. Attached nowhere means soft-orphaned, hence adoptable.
  464. func clientEmailsOwnedElsewhere(tx *gorm.DB, nodeID int, emails []string) (map[string]struct{}, error) {
  465. attachedEmails := func(nodeScoped bool) ([]string, error) {
  466. q := tx.Table("clients").
  467. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  468. Joins("JOIN inbounds ON inbounds.id = client_inbounds.inbound_id").
  469. Where("clients.email IN ?", emails)
  470. if nodeScoped {
  471. q = q.Where("inbounds.node_id = ?", nodeID)
  472. }
  473. var rows []string
  474. err := q.Pluck("clients.email", &rows).Error
  475. return rows, err
  476. }
  477. attached, err := attachedEmails(false)
  478. if err != nil {
  479. return nil, err
  480. }
  481. if len(attached) == 0 {
  482. return nil, nil
  483. }
  484. owned, err := attachedEmails(true)
  485. if err != nil {
  486. return nil, err
  487. }
  488. ownedSet := make(map[string]struct{}, len(owned))
  489. for _, email := range owned {
  490. ownedSet[email] = struct{}{}
  491. }
  492. foreign := make(map[string]struct{})
  493. for _, email := range attached {
  494. if _, ok := ownedSet[email]; !ok {
  495. foreign[email] = struct{}{}
  496. }
  497. }
  498. return foreign, nil
  499. }
  500. func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.TrafficSnapshot, dirty, justPushed bool) (bool, error) {
  501. if snap == nil || nodeID <= 0 {
  502. return false, nil
  503. }
  504. db := database.GetDB()
  505. now := time.Now().UnixMilli()
  506. // originGuidFor attributes a synced inbound to the panel that physically
  507. // hosts it. A node's OWN inbounds report either an empty origin or — on
  508. // builds that set it locally — the node's own panelGuid; both resolve to
  509. // selfKey, which is the node's panelGuid unless that GUID is ambiguous
  510. // (shared with another node or the master, i.e. a cloned server), in which
  511. // case it falls back to the node-unique id so #4983 attribution doesn't
  512. // collapse two physical nodes into one bucket. Only a DIFFERENT, non-empty
  513. // origin (an inbound the node forwards from its own sub-node) is kept as-is,
  514. // so a chained Node1->Node2->Node3 still attributes Node3's inbounds to Node3.
  515. var nodeRow model.Node
  516. db.Select("guid", "config_dirty", "inbound_sync_mode", "inbound_tags").Where("id = ?", nodeID).First(&nodeRow)
  517. // Re-read inside the serialized writer: a client added while this snapshot
  518. // was in flight marks the node dirty after the caller sampled the flag.
  519. dirty = dirty || nodeRow.ConfigDirty
  520. // Adoption, record sync and sweeps still run on a just-pushed tick; only the
  521. // client lifecycle merge waits for a snapshot that reflects the push.
  522. lifecycleFrozen := dirty || justPushed
  523. nodeRow.Id = nodeID
  524. unmanagedTag := unmanagedTagPredicate(&nodeRow)
  525. selfKey := effectiveNodeKey(&model.Node{Id: nodeID, Guid: nodeRow.Guid})
  526. guidShared := nodeRow.Guid != "" && selfKey != nodeRow.Guid
  527. originGuidFor := func(snapIb *model.Inbound) string {
  528. if snapIb.OriginNodeGuid != "" && snapIb.OriginNodeGuid != nodeRow.Guid {
  529. return snapIb.OriginNodeGuid
  530. }
  531. return selfKey
  532. }
  533. var central []model.Inbound
  534. if err := db.Model(model.Inbound{}).
  535. Where("node_id = ?", nodeID).
  536. Find(&central).Error; err != nil {
  537. return false, err
  538. }
  539. // Index under the stored tag and its prefix-flipped form so a snap matches
  540. // whether the n<id>- prefix lives on the node side, the central side, or
  541. // neither — a mismatch must never spawn a duplicate central inbound.
  542. tagToCentral := make(map[string]*model.Inbound, len(central)*2)
  543. prefix := nodeTagPrefix(&nodeID)
  544. for i := range central {
  545. tagToCentral[central[i].Tag] = &central[i]
  546. if prefix != "" {
  547. if stripped, found := strings.CutPrefix(central[i].Tag, prefix); found {
  548. tagToCentral[stripped] = &central[i]
  549. } else {
  550. tagToCentral[prefix+central[i].Tag] = &central[i]
  551. }
  552. }
  553. }
  554. var centralClientStats []xray.ClientTraffic
  555. if len(central) > 0 {
  556. ids := make([]int, 0, len(central))
  557. for i := range central {
  558. ids = append(ids, central[i].Id)
  559. }
  560. if err := db.Model(xray.ClientTraffic{}).
  561. Where("inbound_id IN ?", ids).
  562. Find(&centralClientStats).Error; err != nil {
  563. return false, err
  564. }
  565. }
  566. type csKey struct {
  567. inboundID int
  568. email string
  569. }
  570. centralCS := make(map[csKey]*xray.ClientTraffic, len(centralClientStats))
  571. centralCSByEmail := make(map[string]*xray.ClientTraffic, len(centralClientStats))
  572. for i := range centralClientStats {
  573. centralCS[csKey{centralClientStats[i].InboundId, centralClientStats[i].Email}] = &centralClientStats[i]
  574. centralCSByEmail[centralClientStats[i].Email] = &centralClientStats[i]
  575. }
  576. owedResets, err := pendingNodeResetEmails(db, nodeID)
  577. if err != nil {
  578. return false, err
  579. }
  580. nodeBaselines := make(map[string]nodeTrafficCounter)
  581. var baselineRows []model.NodeClientTraffic
  582. if err := db.Model(&model.NodeClientTraffic{}).
  583. Where("node_id = ?", nodeID).
  584. Find(&baselineRows).Error; err != nil {
  585. return false, err
  586. }
  587. for i := range baselineRows {
  588. nodeBaselines[baselineRows[i].Email] = nodeTrafficCounter{Up: baselineRows[i].Up, Down: baselineRows[i].Down}
  589. }
  590. var defaultUserId int
  591. if len(central) > 0 {
  592. defaultUserId = central[0].UserId
  593. } else {
  594. var u model.User
  595. if err := db.Model(model.User{}).Order("id asc").First(&u).Error; err == nil {
  596. defaultUserId = u.Id
  597. } else {
  598. defaultUserId = 1
  599. }
  600. }
  601. // Union of every email the snapshot still reports, across all inbounds.
  602. // The (node, email) baseline rows are keyed per node, not per inbound, so
  603. // the sweeps below must only drop one when the email left the node
  604. // entirely — an email whose stats moved to (or always lived under) a
  605. // sibling inbound still needs its baseline for the sibling's delta
  606. // computation (#5202).
  607. //
  608. // Xray counts traffic per email, not per inbound, so a multi-attached
  609. // client's shared counter is copied onto every inbound it's on. Fold each
  610. // email to its per-field max (nodeEmailTotals) so divergent copies can't make
  611. // the reset clamp re-add a lower sibling as fresh traffic (#5274).
  612. snapEmailsAll := make(map[string]struct{})
  613. nodeEmailTotals := make(map[string]nodeTrafficCounter)
  614. for _, snapIb := range snap.Inbounds {
  615. if snapIb == nil {
  616. continue
  617. }
  618. for i := range snapIb.ClientStats {
  619. email := snapIb.ClientStats[i].Email
  620. snapEmailsAll[email] = struct{}{}
  621. cur := nodeEmailTotals[email]
  622. if snapIb.ClientStats[i].Up > cur.Up {
  623. cur.Up = snapIb.ClientStats[i].Up
  624. }
  625. if snapIb.ClientStats[i].Down > cur.Down {
  626. cur.Down = snapIb.ClientStats[i].Down
  627. }
  628. nodeEmailTotals[email] = cur
  629. }
  630. }
  631. // The stored row of every reported email, wherever it is homed, so the merge
  632. // can skip a write that would leave it unchanged. Homed rows share centralCS's pointer.
  633. statByEmail := make(map[string]*xray.ClientTraffic, len(snapEmailsAll))
  634. if len(snapEmailsAll) > 0 {
  635. snapEmailList := make([]string, 0, len(snapEmailsAll))
  636. for email := range snapEmailsAll {
  637. snapEmailList = append(snapEmailList, email)
  638. }
  639. for _, batch := range chunkStrings(snapEmailList, sqliteMaxVars) {
  640. var found []xray.ClientTraffic
  641. if err := db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&found).Error; err != nil {
  642. return false, err
  643. }
  644. for i := range found {
  645. if homed, ok := centralCSByEmail[found[i].Email]; ok {
  646. statByEmail[found[i].Email] = homed
  647. continue
  648. }
  649. statByEmail[found[i].Email] = &found[i]
  650. }
  651. }
  652. }
  653. tx := db.Begin()
  654. committed := false
  655. defer func() {
  656. if !committed {
  657. tx.Rollback()
  658. }
  659. }()
  660. structuralChange := false
  661. lifecycleLifted := false
  662. var adoptedInbounds []*model.Inbound
  663. type pendingAdopt struct {
  664. central *model.Inbound
  665. snapIb *model.Inbound
  666. wireSettings string
  667. }
  668. var pendingAdopts []pendingAdopt
  669. degradedInbounds := map[int]string{}
  670. newInboundIDs := make(map[int]struct{})
  671. snapTags := make(map[string]struct{}, len(snap.Inbounds))
  672. for _, snapIb := range snap.Inbounds {
  673. if snapIb == nil {
  674. continue
  675. }
  676. snapTags[snapIb.Tag] = struct{}{}
  677. // Record the prefix-flipped form too so the orphan sweep below keeps a
  678. // central inbound whether its tag carries the n<id>- prefix or not.
  679. if prefix != "" {
  680. if stripped, found := strings.CutPrefix(snapIb.Tag, prefix); found {
  681. snapTags[stripped] = struct{}{}
  682. } else {
  683. snapTags[prefix+snapIb.Tag] = struct{}{}
  684. }
  685. }
  686. c, ok := tagToCentral[snapIb.Tag]
  687. if !ok {
  688. origin := originGuidFor(snapIb)
  689. var compatible []*model.Inbound
  690. for i := range central {
  691. candidate := &central[i]
  692. // An empty origin is a master-created inbound not yet tag-matched: hosted here.
  693. candidateOrigin := candidate.OriginNodeGuid
  694. if candidateOrigin == "" {
  695. candidateOrigin = selfKey
  696. }
  697. if candidateOrigin == origin &&
  698. candidate.Port == snapIb.Port &&
  699. candidate.Protocol == snapIb.Protocol &&
  700. strings.TrimSpace(candidate.Listen) == strings.TrimSpace(snapIb.Listen) {
  701. compatible = append(compatible, candidate)
  702. }
  703. }
  704. switch len(compatible) {
  705. case 1:
  706. c, ok = compatible[0], true
  707. tagToCentral[snapIb.Tag] = c
  708. snapTags[c.Tag] = struct{}{}
  709. case 0:
  710. // A genuinely new inbound follows the normal adoption path.
  711. default:
  712. return false, fmt.Errorf("setRemoteTraffic: inbound %q has ambiguous compatible central aliases", snapIb.Tag)
  713. }
  714. }
  715. if !ok {
  716. if dirty {
  717. continue
  718. }
  719. // Try snap.Tag first; on collision fall back to the n<id>-
  720. // prefixed form so local+node can both own the same port.
  721. pickFreeTag := func() (string, error) {
  722. candidates := []string{snapIb.Tag}
  723. if prefix != "" && !strings.HasPrefix(snapIb.Tag, prefix) {
  724. candidates = append(candidates, prefix+snapIb.Tag)
  725. }
  726. for _, t := range candidates {
  727. var owner model.Inbound
  728. err := tx.Where("tag = ?", t).First(&owner).Error
  729. if errors.Is(err, gorm.ErrRecordNotFound) {
  730. return t, nil
  731. }
  732. if err != nil {
  733. return "", err
  734. }
  735. }
  736. return "", nil
  737. }
  738. chosenTag, err := pickFreeTag()
  739. if err != nil {
  740. logger.Warningf("setRemoteTraffic: check tag %q failed: %v", snapIb.Tag, err)
  741. continue
  742. }
  743. if chosenTag == "" {
  744. key := fmt.Sprintf("%d:%s", nodeID, snapIb.Tag)
  745. if _, seen := reportedRemoteTagConflict.LoadOrStore(key, struct{}{}); !seen {
  746. logger.Warningf(
  747. "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)",
  748. snapIb.Tag, nodeID, nodeID,
  749. )
  750. }
  751. continue
  752. }
  753. reportedRemoteTagConflict.Delete(fmt.Sprintf("%d:%s", nodeID, snapIb.Tag))
  754. newIb := model.Inbound{
  755. UserId: defaultUserId,
  756. NodeID: &nodeID,
  757. OriginNodeGuid: originGuidFor(snapIb),
  758. Tag: chosenTag,
  759. Listen: snapIb.Listen,
  760. Port: snapIb.Port,
  761. Protocol: snapIb.Protocol,
  762. Settings: snapIb.Settings,
  763. StreamSettings: snapIb.StreamSettings,
  764. Sniffing: snapIb.Sniffing,
  765. TrafficReset: snapIb.TrafficReset,
  766. TrafficResetDay: normalizeTrafficResetDay(snapIb.TrafficResetDay),
  767. LastTrafficResetTime: snapIb.LastTrafficResetTime,
  768. Enable: snapIb.Enable,
  769. Remark: snapIb.Remark,
  770. SubSortIndex: normalizeSubSortIndex(snapIb.SubSortIndex),
  771. ExcludeFromSub: snapIb.ExcludeFromSub,
  772. Total: snapIb.Total,
  773. ExpiryTime: snapIb.ExpiryTime,
  774. Up: snapIb.Up,
  775. Down: snapIb.Down,
  776. ShareAddrStrategy: "node",
  777. DisableFlow: snapIb.DisableFlow,
  778. }
  779. if err := tx.Create(&newIb).Error; err != nil {
  780. logger.Warningf("setRemoteTraffic: create central inbound for tag %q failed: %v", snapIb.Tag, err)
  781. continue
  782. }
  783. tagToCentral[snapIb.Tag] = &newIb
  784. if newIb.Tag != snapIb.Tag {
  785. tagToCentral[newIb.Tag] = &newIb
  786. }
  787. if rows := adoptedHostRows(snap.HostGroups, snapIb.Id, newIb.Id); len(rows) > 0 {
  788. if err := tx.Create(&rows).Error; err != nil {
  789. logger.Warningf("setRemoteTraffic: adopt host rows for tag %q failed: %v", newIb.Tag, err)
  790. }
  791. }
  792. newInboundIDs[newIb.Id] = struct{}{}
  793. structuralChange = true
  794. continue
  795. }
  796. inGrace := c.LastTrafficResetTime > 0 && now-c.LastTrafficResetTime < resetGracePeriodMs
  797. // Adopting the node's settings verbatim would re-add a client the master
  798. // deleted moments ago if this snapshot was fetched before the deletion
  799. // push landed — filter just-deleted emails out while their tombstone lives.
  800. adoptedSettings := snapIb.Settings
  801. if stripped, changed := stripTombstonedClients(adoptedSettings); changed {
  802. adoptedSettings = stripped
  803. }
  804. if deduped, changed := dedupeSettingsClients(adoptedSettings); changed {
  805. adoptedSettings = deduped
  806. }
  807. updates := map[string]any{}
  808. if !dirty && snapshotDropsEveryHubClient(tx, c.Id, adoptedSettings) {
  809. degradedInbounds[c.Id] = c.Tag
  810. } else if !dirty {
  811. // Defer lifecycle lift until after client_traffics absorbs this tick's
  812. // deltas so quota stale-disable matches SQL (#6228).
  813. pendingAdopts = append(pendingAdopts, pendingAdopt{
  814. central: c, snapIb: snapIb, wireSettings: adoptedSettings,
  815. })
  816. updates["enable"] = snapIb.Enable
  817. updates["remark"] = snapIb.Remark
  818. updates["sub_sort_index"] = normalizeSubSortIndex(snapIb.SubSortIndex)
  819. updates["listen"] = snapIb.Listen
  820. updates["port"] = snapIb.Port
  821. updates["protocol"] = snapIb.Protocol
  822. updates["total"] = snapIb.Total
  823. updates["expiry_time"] = snapIb.ExpiryTime
  824. updates["stream_settings"] = snapIb.StreamSettings
  825. updates["sniffing"] = snapIb.Sniffing
  826. updates["traffic_reset"] = snapIb.TrafficReset
  827. updates["traffic_reset_day"] = normalizeTrafficResetDay(snapIb.TrafficResetDay)
  828. updates["last_traffic_reset_time"] = snapIb.LastTrafficResetTime
  829. }
  830. if !inGrace || (snapIb.Up+snapIb.Down) <= (c.Up+c.Down) {
  831. updates["up"] = snapIb.Up
  832. updates["down"] = snapIb.Down
  833. }
  834. // Physical-home attribution is independent of config-dirty state, so
  835. // keep it current even while the node has pending offline edits. Writes
  836. // once to backfill an existing row, then stays equal (#4983).
  837. if og := originGuidFor(snapIb); c.OriginNodeGuid != og {
  838. updates["origin_node_guid"] = og
  839. }
  840. if !dirty && (c.Remark != snapIb.Remark ||
  841. c.Listen != snapIb.Listen ||
  842. c.Port != snapIb.Port ||
  843. c.Total != snapIb.Total ||
  844. c.ExpiryTime != snapIb.ExpiryTime ||
  845. c.Enable != snapIb.Enable) {
  846. structuralChange = true
  847. }
  848. if len(updates) > 0 {
  849. if err := tx.Model(model.Inbound{}).
  850. Where("id = ?", c.Id).
  851. Updates(updates).Error; err != nil {
  852. return false, err
  853. }
  854. }
  855. }
  856. // The inbound each reported email now lives under, so a swept inbound's
  857. // accumulator rows follow the email instead of being dropped and re-seeded at 0.
  858. snapEmailHome := make(map[string]int, len(snapEmailsAll))
  859. for _, snapIb := range snap.Inbounds {
  860. if snapIb == nil {
  861. continue
  862. }
  863. home, ok := tagToCentral[snapIb.Tag]
  864. if !ok {
  865. continue
  866. }
  867. for i := range snapIb.ClientStats {
  868. if _, taken := snapEmailHome[snapIb.ClientStats[i].Email]; !taken {
  869. snapEmailHome[snapIb.ClientStats[i].Email] = home.Id
  870. }
  871. }
  872. }
  873. for _, c := range central {
  874. if dirty {
  875. continue
  876. }
  877. // A node inbound created disabled is never delivered, so its absence from
  878. // the snapshot is ambiguous rather than evidence of a node-side delete.
  879. if !c.Enable {
  880. continue
  881. }
  882. if len(snapTags) == 0 {
  883. // A node mid-restart or with a transient DB error can return an empty
  884. // inbound list with success=true. Treat "zero inbounds reported" as
  885. // "nothing to say", not "delete all my inbounds" — otherwise a blip
  886. // wipes the node's central inbounds and every client on them (and
  887. // resets traffic history on re-create). A real per-inbound deletion
  888. // still sweeps, because the node keeps reporting its other inbounds.
  889. continue
  890. }
  891. if _, kept := snapTags[c.Tag]; kept {
  892. continue
  893. }
  894. if unmanagedTag(c.Tag) {
  895. continue
  896. }
  897. // This drops the central inbound and its unreported clients' history, so say
  898. // so: silent removal is indistinguishable from an inbound never arriving.
  899. logger.Warningf("setRemoteTraffic: node %d no longer reports inbound %q (id %d, port %d) — removing it centrally", nodeID, c.Tag, c.Id, c.Port)
  900. var goneEmails []string
  901. if err := tx.Model(xray.ClientTraffic{}).
  902. Where("inbound_id = ?", c.Id).
  903. Pluck("email", &goneEmails).Error; err != nil {
  904. return false, err
  905. }
  906. if len(goneEmails) > 0 {
  907. // Baselines are per (node, email), not per inbound: keep them for
  908. // emails the snapshot still reports under a sibling inbound (#5202).
  909. baselineGone := make([]string, 0, len(goneEmails))
  910. for _, e := range goneEmails {
  911. if _, still := snapEmailsAll[e]; !still {
  912. baselineGone = append(baselineGone, e)
  913. }
  914. }
  915. // Chunk to avoid SQLite bind var limit when a node has many clients
  916. // removed (e.g. after API bulk delete or structural change on node inbound).
  917. for _, batch := range chunkStrings(baselineGone, sqliteMaxVars) {
  918. if err := tx.Where("node_id = ? AND email IN ?", nodeID, batch).
  919. Delete(&model.NodeClientTraffic{}).Error; err != nil {
  920. return false, err
  921. }
  922. }
  923. // The per-email row is the shared accumulator across every inbound
  924. // (and node) the email is attached to. Only drop it when this was the
  925. // email's last inbound — wiping it while a sibling still feeds it
  926. // loses the summed history, and the next node sync would re-seed the
  927. // row with that node's counter alone.
  928. sharedEmails, sErr := s.emailsUsedByOtherInbounds(tx, goneEmails, c.Id)
  929. if sErr != nil {
  930. return false, sErr
  931. }
  932. delEmails := make([]string, 0, len(goneEmails))
  933. rehome := make(map[int][]string)
  934. for _, e := range goneEmails {
  935. if home, still := snapEmailHome[e]; still {
  936. rehome[home] = append(rehome[home], e)
  937. if row, ok := centralCS[csKey{c.Id, e}]; ok {
  938. delete(centralCS, csKey{c.Id, e})
  939. row.InboundId = home
  940. centralCS[csKey{home, e}] = row
  941. }
  942. continue
  943. }
  944. if !sharedEmails[strings.ToLower(strings.TrimSpace(e))] {
  945. delEmails = append(delEmails, e)
  946. }
  947. }
  948. for home, emails := range rehome {
  949. for _, batch := range chunkStrings(emails, sqliteMaxVars) {
  950. if err := tx.Model(xray.ClientTraffic{}).
  951. Where("inbound_id = ? AND email IN ?", c.Id, batch).
  952. Update("inbound_id", home).Error; err != nil {
  953. return false, err
  954. }
  955. }
  956. }
  957. for _, batch := range chunkStrings(delEmails, sqliteMaxVars) {
  958. if err := tx.Where("inbound_id = ? AND email IN ?", c.Id, batch).
  959. Delete(&xray.ClientTraffic{}).Error; err != nil {
  960. return false, err
  961. }
  962. }
  963. }
  964. if err := s.clientService.DetachInbound(tx, c.Id); err != nil {
  965. return false, err
  966. }
  967. if err := tx.Where("id = ?", c.Id).
  968. Delete(&model.Inbound{}).Error; err != nil {
  969. return false, err
  970. }
  971. delete(tagToCentral, c.Tag)
  972. structuralChange = true
  973. }
  974. for _, snapIb := range snap.Inbounds {
  975. if snapIb == nil {
  976. continue
  977. }
  978. c, ok := tagToCentral[snapIb.Tag]
  979. if !ok {
  980. continue
  981. }
  982. snapEmails := make(map[string]struct{}, len(snapIb.ClientStats))
  983. // Parsed once per inbound on the first renewal candidate, not per client.
  984. var snapExpiries map[string]int64
  985. attachedOnNode, membershipKnown := snapshotAttachedEmails(snapIb.Settings)
  986. for _, cs := range snapIb.ClientStats {
  987. // A node detach keeps the stat row, so its quota and verdict are stale (#6724).
  988. if _, attached := attachedOnNode[strings.ToLower(cs.Email)]; membershipKnown && !attached {
  989. continue
  990. }
  991. snapEmails[cs.Email] = struct{}{}
  992. // Node-wide total, not this inbound's possibly-stale copy (#5274).
  993. canon := nodeEmailTotals[cs.Email]
  994. // Until the node applies a reset it owes, its verdict rests on the
  995. // pre-reset counters: only usage may move for this client.
  996. _, owed := owedResets[cs.Email]
  997. clientFrozen := lifecycleFrozen || owed
  998. base, seen := nodeBaselines[cs.Email]
  999. var deltaUp, deltaDown int64
  1000. if seen {
  1001. if deltaUp = canon.Up - base.Up; deltaUp < 0 {
  1002. deltaUp = 0
  1003. }
  1004. if deltaDown = canon.Down - base.Down; deltaDown < 0 {
  1005. deltaDown = 0
  1006. }
  1007. }
  1008. if _, rowExists := statByEmail[cs.Email]; !rowExists {
  1009. if dirty {
  1010. continue
  1011. }
  1012. _, isNewInbound := newInboundIDs[c.Id]
  1013. // On a known inbound a missing row plus a live tombstone means the
  1014. // master just deleted this client and the snapshot predates the
  1015. // deletion push — recreating the row (at zero) would resurrect the
  1016. // client. A freshly adopted inbound still gets its row (seeded at
  1017. // zero) so adoption semantics stay intact.
  1018. if !isNewInbound && isClientEmailTombstoned(cs.Email) {
  1019. continue
  1020. }
  1021. var seedUp, seedDown int64
  1022. if isNewInbound && !isClientEmailTombstoned(cs.Email) {
  1023. seedUp, seedDown = canon.Up, canon.Down
  1024. }
  1025. row := &xray.ClientTraffic{
  1026. InboundId: c.Id,
  1027. Email: cs.Email,
  1028. Enable: cs.Enable,
  1029. Total: cs.Total,
  1030. ExpiryTime: cs.ExpiryTime,
  1031. Reset: cs.Reset,
  1032. ResetDay: cs.ResetDay,
  1033. ResetWeekday: cs.ResetWeekday,
  1034. ResetMax: cs.ResetMax,
  1035. ResetCount: cs.ResetCount,
  1036. Up: seedUp,
  1037. Down: seedDown,
  1038. LastOnline: cs.LastOnline,
  1039. }
  1040. if err := tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
  1041. Create(row).Error; err != nil {
  1042. return false, err
  1043. }
  1044. centralCS[csKey{c.Id, cs.Email}] = row
  1045. centralCSByEmail[cs.Email] = row
  1046. statByEmail[cs.Email] = row
  1047. structuralChange = true
  1048. if err := s.upsertNodeBaseline(tx, nodeID, cs.Email, canon.Up, canon.Down); err != nil {
  1049. return false, err
  1050. }
  1051. nodeBaselines[cs.Email] = nodeTrafficCounter{Up: canon.Up, Down: canon.Down}
  1052. continue
  1053. }
  1054. existing := centralCSByEmail[cs.Email]
  1055. if existing != nil {
  1056. expiryChanged := !clientFrozen && existing.ExpiryTime != mergeActivationExpiry(existing.ExpiryTime, cs.ExpiryTime)
  1057. // Only a real latch to disabled is structural; one-way merge never
  1058. // re-enables from the node.
  1059. enableChanged := !clientFrozen && existing.Enable && !cs.Enable &&
  1060. !nodeDisableIsStale(existing, cs, now, deltaUp, deltaDown)
  1061. metaChanged := !clientFrozen && (existing.Total != cs.Total || existing.Reset != cs.Reset || existing.ResetWeekday != cs.ResetWeekday)
  1062. if enableChanged || metaChanged || expiryChanged {
  1063. structuralChange = true
  1064. }
  1065. }
  1066. renewed := !clientFrozen && seen && existing != nil && nodeClientRenewed(existing, cs, canon, base)
  1067. if renewed {
  1068. // Reject when the node's own settings still carry the old absolute:
  1069. // lagging ClientStats after a master shorten mimic a renew (#6228).
  1070. if snapExpiries == nil {
  1071. snapExpiries = settingsClientAbsoluteExpiries(snapIb.Settings)
  1072. }
  1073. if se, ok := snapExpiries[cs.Email]; ok && se <= existing.ExpiryTime {
  1074. renewed = false
  1075. }
  1076. }
  1077. if renewed {
  1078. // A renewal starts a fresh quota window: adopt the node's counters
  1079. // and enable state, drop stale pushes (mirrors autoRenewClients).
  1080. if err := tx.Exec(
  1081. fmt.Sprintf(
  1082. `UPDATE client_traffics
  1083. SET up = ?, down = ?, enable = ?, total = ?,
  1084. expiry_time = ?, reset = ?, reset_day = ?, reset_weekday = ?, reset_count = ?, last_online = %s
  1085. WHERE email = ?`,
  1086. database.GreatestExpr("last_online", "?"),
  1087. ),
  1088. canon.Up, canon.Down, cs.Enable, cs.Total,
  1089. cs.ExpiryTime, cs.Reset, cs.ResetDay, cs.ResetWeekday, cs.ResetCount,
  1090. cs.LastOnline, cs.Email,
  1091. ).Error; err != nil {
  1092. return false, err
  1093. }
  1094. if err := clearGlobalTraffic(tx, cs.Email); err != nil {
  1095. return false, err
  1096. }
  1097. existing.Up = canon.Up
  1098. existing.Down = canon.Down
  1099. existing.Enable = cs.Enable
  1100. existing.Total = cs.Total
  1101. existing.ExpiryTime = cs.ExpiryTime
  1102. existing.Reset = cs.Reset
  1103. existing.ResetDay = cs.ResetDay
  1104. existing.ResetWeekday = cs.ResetWeekday
  1105. existing.ResetCount = cs.ResetCount
  1106. existing.LastOnline = max(existing.LastOnline, cs.LastOnline)
  1107. structuralChange = true
  1108. } else {
  1109. // The row as the UPDATE below would leave it, computed from its
  1110. // pre-update values exactly as the SQL reads them.
  1111. row := statByEmail[cs.Email]
  1112. next := *row
  1113. next.Up = clampTrafficCounter(row.Up + deltaUp)
  1114. next.Down = clampTrafficCounter(row.Down + deltaDown)
  1115. next.LastOnline = max(row.LastOnline, cs.LastOnline)
  1116. if !clientFrozen {
  1117. if !cs.Enable && !nodeDisableIsStale(row, cs, now, deltaUp, deltaDown) {
  1118. next.Enable = false
  1119. }
  1120. next.ExpiryTime = mergeActivationExpiry(row.ExpiryTime, cs.ExpiryTime)
  1121. next.Total = cs.Total
  1122. next.Reset, next.ResetDay, next.ResetWeekday = cs.Reset, cs.ResetDay, cs.ResetWeekday
  1123. }
  1124. // A multi-attached client is reported once per inbound and an idle one
  1125. // every tick: rewriting an unchanged row only holds its lock longer.
  1126. if next != *row {
  1127. if err := s.mergeNodeClientTraffic(tx, cs, clientFrozen, deltaUp, deltaDown, now); err != nil {
  1128. return false, err
  1129. }
  1130. *row = next
  1131. }
  1132. }
  1133. // A dip plus a lagging longer expiry mimics nodeClientRenewed and would
  1134. // undo a master shorten once the freeze lifts (#6228).
  1135. if clientFrozen && seen && (canon.Up < base.Up || canon.Down < base.Down) {
  1136. continue
  1137. }
  1138. if seen && base == canon {
  1139. continue
  1140. }
  1141. if err := s.upsertNodeBaseline(tx, nodeID, cs.Email, canon.Up, canon.Down); err != nil {
  1142. return false, err
  1143. }
  1144. nodeBaselines[cs.Email] = nodeTrafficCounter{Up: canon.Up, Down: canon.Down}
  1145. }
  1146. for k, existing := range centralCS {
  1147. if dirty {
  1148. continue
  1149. }
  1150. if k.inboundID != c.Id {
  1151. continue
  1152. }
  1153. if _, degraded := degradedInbounds[c.Id]; degraded {
  1154. continue
  1155. }
  1156. if _, kept := snapEmails[k.email]; kept {
  1157. continue
  1158. }
  1159. // Gone from this inbound's stats but still reported by the node under
  1160. // a sibling inbound: both the shared accumulator row and the (node,
  1161. // email) baseline must survive, or the sibling's next delta would
  1162. // compute against nothing and freeze the counter (#5202).
  1163. if _, still := snapEmailsAll[k.email]; still {
  1164. continue
  1165. }
  1166. if err := tx.Where("node_id = ? AND email = ?", nodeID, existing.Email).
  1167. Delete(&model.NodeClientTraffic{}).Error; err != nil {
  1168. return false, err
  1169. }
  1170. // Same shared-accumulator rule as the inbound-removal sweep above:
  1171. // keep the row while another inbound still references the email.
  1172. stillUsed, uErr := s.emailUsedByOtherInbounds(tx, existing.Email, c.Id)
  1173. if uErr != nil {
  1174. return false, uErr
  1175. }
  1176. // Usage, quota and expiry live on this row, so a client the orphan
  1177. // sweep will mark keeps it until the reaper confirms the removal.
  1178. if !stillUsed && !clientRecordExists(tx, existing.Email) {
  1179. if err := tx.Where("inbound_id = ? AND email = ?", c.Id, existing.Email).
  1180. Delete(&xray.ClientTraffic{}).Error; err != nil {
  1181. return false, err
  1182. }
  1183. }
  1184. structuralChange = true
  1185. }
  1186. }
  1187. type oldSet struct {
  1188. inboundID int
  1189. emails map[string]struct{}
  1190. }
  1191. var perInboundOld []oldSet
  1192. syncFailedInbounds := map[int]struct{}{}
  1193. for _, p := range pendingAdopts {
  1194. lifted, liftChanged := liftClientLifecycleInSettings(p.wireSettings, centralCSByEmail)
  1195. adoptedSettings := p.wireSettings
  1196. if liftChanged {
  1197. adoptedSettings = lifted
  1198. lifecycleLifted = true
  1199. }
  1200. if p.central.Settings != adoptedSettings {
  1201. if err := tx.Model(model.Inbound{}).
  1202. Where("id = ?", p.central.Id).
  1203. Update("settings", adoptedSettings).Error; err != nil {
  1204. return false, err
  1205. }
  1206. structuralChange = true
  1207. }
  1208. // The fingerprint stamps the un-lifted wire blob on purpose: a lift must
  1209. // leave reconcile a mismatch to re-push against.
  1210. if liftChanged || adoptedWireChanged(p.central, p.snapIb, p.wireSettings) {
  1211. adoptedInbounds = append(adoptedInbounds, adoptedWireInbound(p.central, p.snapIb, p.wireSettings))
  1212. }
  1213. }
  1214. for _, snapIb := range snap.Inbounds {
  1215. if snapIb == nil {
  1216. continue
  1217. }
  1218. c, ok := tagToCentral[snapIb.Tag]
  1219. if !ok {
  1220. continue
  1221. }
  1222. if dirty {
  1223. continue
  1224. }
  1225. var oldEmailsRows []string
  1226. if err := tx.Table("clients").
  1227. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  1228. Where("client_inbounds.inbound_id = ?", c.Id).
  1229. Pluck("email", &oldEmailsRows).Error; err == nil {
  1230. oldEmails := make(map[string]struct{}, len(oldEmailsRows))
  1231. for _, e := range oldEmailsRows {
  1232. if e != "" {
  1233. oldEmails[e] = struct{}{}
  1234. }
  1235. }
  1236. perInboundOld = append(perInboundOld, oldSet{inboundID: c.Id, emails: oldEmails})
  1237. }
  1238. clients, gcErr := s.GetClients(snapIb)
  1239. if gcErr != nil {
  1240. logger.Warningf("setRemoteTraffic: parse clients for tag %q failed: %v", snapIb.Tag, gcErr)
  1241. continue
  1242. }
  1243. csByEmail := make(map[string]xray.ClientTraffic, len(snapIb.ClientStats))
  1244. for _, cs := range snapIb.ClientStats {
  1245. csByEmail[cs.Email] = cs
  1246. }
  1247. filtered := clients[:0]
  1248. for i := range clients {
  1249. if isClientEmailTombstoned(clients[i].Email) {
  1250. continue
  1251. }
  1252. existing := centralCSByEmail[clients[i].Email]
  1253. var csPtr *xray.ClientTraffic
  1254. if cs, hit := csByEmail[clients[i].Email]; hit {
  1255. csCopy := cs
  1256. csPtr = &csCopy
  1257. }
  1258. applyMasterClientLifecycle(&clients[i], existing, csPtr)
  1259. filtered = append(filtered, clients[i])
  1260. }
  1261. // A degraded node (reset/restart/removal) reports zero clients for an inbound the
  1262. // hub populates; adopting it empties links and ReapSyncOrphans deletes shared clients (#6734).
  1263. if _, degraded := degradedInbounds[c.Id]; degraded {
  1264. logger.Warningf("setRemoteTraffic: node %d reported zero clients for tag %q while the hub has %d attached — keeping them and re-pushing", nodeID, snapIb.Tag, len(oldEmailsRows))
  1265. syncFailedInbounds[c.Id] = struct{}{}
  1266. continue
  1267. }
  1268. localEmails := make([]string, 0, len(filtered))
  1269. for i := range filtered {
  1270. if filtered[i].Email != "" {
  1271. localEmails = append(localEmails, filtered[i].Email)
  1272. }
  1273. }
  1274. if len(localEmails) > 0 {
  1275. var localMeta []struct {
  1276. Email string
  1277. Comment string `gorm:"column:comment"`
  1278. }
  1279. if err := tx.Table("clients").
  1280. Select("email, comment").
  1281. Where("email IN ?", localEmails).
  1282. Find(&localMeta).Error; err == nil {
  1283. commentByEmail := make(map[string]string, len(localMeta))
  1284. for _, m := range localMeta {
  1285. commentByEmail[m.Email] = m.Comment
  1286. }
  1287. for i := range filtered {
  1288. if cmt, ok := commentByEmail[filtered[i].Email]; ok {
  1289. filtered[i].Comment = cmt
  1290. }
  1291. }
  1292. }
  1293. }
  1294. if len(localEmails) > 0 {
  1295. foreign, err := clientEmailsOwnedElsewhere(tx, nodeID, localEmails)
  1296. if err != nil {
  1297. return false, err
  1298. }
  1299. if len(foreign) > 0 {
  1300. kept := filtered[:0]
  1301. for i := range filtered {
  1302. if _, claimed := foreign[filtered[i].Email]; !claimed {
  1303. kept = append(kept, filtered[i])
  1304. continue
  1305. }
  1306. key := fmt.Sprintf("%d:%s", nodeID, filtered[i].Email)
  1307. if _, seen := reportedForeignClientClaim.LoadOrStore(key, struct{}{}); !seen {
  1308. logger.Warningf(
  1309. "setRemoteTraffic: node %d reported client %q, which is attached only to inbounds of another node — not adopting (rename one side to remove the duplicate email)",
  1310. nodeID, filtered[i].Email,
  1311. )
  1312. }
  1313. }
  1314. filtered = kept
  1315. }
  1316. }
  1317. if err := s.clientService.SyncInbound(tx, c.Id, filtered); err != nil {
  1318. logger.Warningf("setRemoteTraffic: sync clients for tag %q failed: %v", snapIb.Tag, err)
  1319. syncFailedInbounds[c.Id] = struct{}{}
  1320. }
  1321. }
  1322. for _, old := range perInboundOld {
  1323. // The sweep's premise is that links were just rebuilt from the snapshot,
  1324. // which is exactly what a failed SyncInbound violates.
  1325. if _, failed := syncFailedInbounds[old.inboundID]; failed {
  1326. continue
  1327. }
  1328. var stillAttached []string
  1329. if err := tx.Table("clients").
  1330. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  1331. Where("client_inbounds.inbound_id = ?", old.inboundID).
  1332. Pluck("email", &stillAttached).Error; err != nil {
  1333. continue
  1334. }
  1335. stillSet := make(map[string]struct{}, len(stillAttached))
  1336. for _, e := range stillAttached {
  1337. stillSet[e] = struct{}{}
  1338. }
  1339. for email := range old.emails {
  1340. if _, kept := stillSet[email]; kept {
  1341. continue
  1342. }
  1343. var attachmentCount int64
  1344. if err := tx.Table("client_inbounds").
  1345. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  1346. Where("clients.email = ?", email).
  1347. Count(&attachmentCount).Error; err != nil {
  1348. continue
  1349. }
  1350. if attachmentCount > 0 {
  1351. continue
  1352. }
  1353. // "Ended the merge unattached" is true for a real remote deletion and
  1354. // equally true for a bad merge, so record a strike instead of deleting.
  1355. if err := markSyncOrphan(tx, email, now); err != nil {
  1356. logger.Warningf("setRemoteTraffic: mark orphan %q failed: %v", email, err)
  1357. continue
  1358. }
  1359. structuralChange = true
  1360. }
  1361. }
  1362. if err := clearSyncOrphanMarks(tx); err != nil {
  1363. logger.Warning("setRemoteTraffic: clear orphan marks failed:", err)
  1364. }
  1365. if err := liftActivatedClientRecordExpiries(tx); err != nil {
  1366. logger.Warning("setRemoteTraffic: lift activated expiries failed:", err)
  1367. }
  1368. if err := tx.Commit().Error; err != nil {
  1369. return false, err
  1370. }
  1371. committed = true
  1372. if len(degradedInbounds) > 0 {
  1373. if mgr := runtime.GetManager(); mgr != nil {
  1374. if rt, rtErr := mgr.RuntimeFor(&nodeID); rtErr == nil {
  1375. if rem, ok := rt.(*runtime.Remote); ok {
  1376. for _, tag := range degradedInbounds {
  1377. rem.ForgetPushedInbound(tag)
  1378. }
  1379. }
  1380. }
  1381. }
  1382. if err := (&NodeService{}).MarkNodeDirty(nodeID); err != nil {
  1383. logger.Warningf("setRemoteTraffic: mark node %d dirty after an empty snapshot failed: %v", nodeID, err)
  1384. }
  1385. }
  1386. if lifecycleLifted && !dirty {
  1387. var already model.Node
  1388. if err := database.GetDB().Select("config_dirty").Where("id = ?", nodeID).First(&already).Error; err == nil && already.ConfigDirty {
  1389. logger.Debugf("setRemoteTraffic: node %d lifecycle lift; already dirty", nodeID)
  1390. } else {
  1391. logger.Infof("setRemoteTraffic: node %d lifecycle lift; marking dirty for re-push", nodeID)
  1392. if err := (&NodeService{}).MarkNodeDirty(nodeID); err != nil {
  1393. logger.Warningf("setRemoteTraffic: mark node %d dirty after lifecycle lift failed: %v", nodeID, err)
  1394. }
  1395. }
  1396. }
  1397. if len(adoptedInbounds) > 0 {
  1398. if mgr := runtime.GetManager(); mgr != nil {
  1399. if rt, rtErr := mgr.RuntimeFor(&nodeID); rtErr == nil {
  1400. if rem, ok := rt.(*runtime.Remote); ok {
  1401. for _, ib := range adoptedInbounds {
  1402. rem.RecordAdoptedInbound(ib)
  1403. }
  1404. }
  1405. }
  1406. }
  1407. }
  1408. if process := currentXrayProcess(); process != nil {
  1409. tree := snap.OnlineTree
  1410. switch {
  1411. case len(tree) == 0 && len(snap.OnlineEmails) > 0:
  1412. // Old-build node (no GUID tree): key its flat online list under its
  1413. // own effective identity so attribution still works for that branch.
  1414. tree = map[string][]string{selfKey: snap.OnlineEmails}
  1415. case guidShared && len(tree) > 0:
  1416. // Newer cloned node: its own clients arrive keyed under the shared
  1417. // panelGuid. Remap just that entry to the node-unique key so the
  1418. // clones don't merge; descendant subtrees keep their distinct GUIDs.
  1419. if _, ok := tree[nodeRow.Guid]; ok {
  1420. tree = remapGuidTreeKey(tree, nodeRow.Guid, selfKey)
  1421. }
  1422. }
  1423. process.SetNodeOnlineTree(nodeID, tree)
  1424. activeTree := normalizeActiveInboundTreeTags(snap.ActiveInboundTree, tagToCentral)
  1425. if guidShared && len(activeTree) > 0 {
  1426. if _, ok := activeTree[nodeRow.Guid]; ok {
  1427. activeTree = remapGuidTreeKey(activeTree, nodeRow.Guid, selfKey)
  1428. }
  1429. }
  1430. if len(activeTree) > 0 {
  1431. activeTree = filterGuidTreeKeys(activeTree, activeInboundGuidKeys(snap.Inbounds, tagToCentral, originGuidFor))
  1432. }
  1433. process.SetNodeActiveInboundTree(nodeID, activeTree)
  1434. }
  1435. return structuralChange, nil
  1436. }
  1437. func (s *InboundService) restartRemoteNodesOnDisable(nodeIDs []int) {
  1438. restartOnDisable, err := (&SettingService{}).GetRestartXrayOnClientDisable()
  1439. if err != nil {
  1440. logger.Warning("disableInvalidClients: get RestartXrayOnClientDisable failed:", err)
  1441. return
  1442. }
  1443. if !restartOnDisable {
  1444. return
  1445. }
  1446. // Best-effort and never replayed: a hanging node must not hold the traffic poll.
  1447. common.GoRecover("restart-nodes-on-client-disable", func() {
  1448. for _, nodeID := range nodeIDs {
  1449. nodeIDCopy := nodeID
  1450. rt, rtErr := runtime.GetManager().RuntimeFor(&nodeIDCopy)
  1451. if rtErr != nil {
  1452. logger.Warning("disableInvalidClients: get runtime for node", nodeID, "failed:", rtErr)
  1453. continue
  1454. }
  1455. if rtErr = rt.RestartXray(context.Background()); rtErr != nil {
  1456. logger.Warning("disableInvalidClients: restart xray on node", nodeID, "failed:", rtErr)
  1457. }
  1458. }
  1459. })
  1460. }
  1461. func (s *InboundService) GetOnlineClients() []string {
  1462. process := currentXrayProcess()
  1463. if process == nil {
  1464. return []string{}
  1465. }
  1466. return process.GetOnlineClients()
  1467. }
  1468. // GetOnlineClientsByGuid returns online emails keyed by the panelGuid of the
  1469. // node that physically hosts each set: this panel's own clients under its own
  1470. // GUID, plus every node in the tree under its GUID (#4983). Replaces the old
  1471. // node-id keying so a client three hops down is attributed to its real node,
  1472. // not the intermediate one it was synced through.
  1473. func (s *InboundService) GetOnlineClientsByGuid() map[string][]string {
  1474. process := currentXrayProcess()
  1475. if process == nil {
  1476. return map[string][]string{}
  1477. }
  1478. out := process.GetMergedNodeTrees()
  1479. if local := process.GetLocalOnlineClients(); len(local) > 0 {
  1480. if guid := s.panelGuid(); guid != "" {
  1481. out[guid] = mergeEmails(out[guid], local)
  1482. }
  1483. }
  1484. return out
  1485. }
  1486. // GetActiveInboundsByGuid returns the inbound tags that carried traffic within
  1487. // the grace window, keyed by the panelGuid of the node that physically hosts
  1488. // each inbound. A GUID missing from the map means "don't gate" for that node's
  1489. // inbounds (old-build node or no active-inbound signal).
  1490. func (s *InboundService) GetActiveInboundsByGuid() map[string][]string {
  1491. process := currentXrayProcess()
  1492. if process == nil {
  1493. return map[string][]string{}
  1494. }
  1495. out := process.GetMergedActiveInboundTrees()
  1496. active := process.GetLocalActiveInbounds()
  1497. if len(active) == 0 {
  1498. return out
  1499. }
  1500. guid := s.panelGuid()
  1501. if guid == "" {
  1502. return out
  1503. }
  1504. out[guid] = mergeEmails(out[guid], active)
  1505. return out
  1506. }
  1507. func (s *InboundService) SetNodeOnlineTree(nodeID int, tree map[string][]string) {
  1508. if process := currentXrayProcess(); process != nil {
  1509. process.SetNodeOnlineTree(nodeID, tree)
  1510. }
  1511. }
  1512. func (s *InboundService) ClearNodeOnlineClients(nodeID int) {
  1513. if process := currentXrayProcess(); process != nil {
  1514. process.ClearNodeOnlineClients(nodeID)
  1515. }
  1516. }
  1517. // RetainSyncedNodeOnlineClients keeps online clients only for nodes the traffic
  1518. // sync still fetches; a node missing from nodes was deleted.
  1519. func (s *InboundService) RetainSyncedNodeOnlineClients(nodes []*model.Node) {
  1520. process := currentXrayProcess()
  1521. if process == nil {
  1522. return
  1523. }
  1524. synced := make(map[int]bool, len(nodes))
  1525. for _, n := range nodes {
  1526. synced[n.Id] = n.Enable && n.Status == "online"
  1527. }
  1528. process.RetainNodeOnlineClients(func(nodeID int) bool { return synced[nodeID] })
  1529. }
  1530. // panelGuid returns this panel's stable self-identifier, used to key the local
  1531. // panel's own clients in the per-node online maps (#4983).
  1532. func (s *InboundService) panelGuid() string {
  1533. guid, _ := (&SettingService{}).GetPanelGuid()
  1534. return guid
  1535. }
  1536. // synthNodeGuid is the stable per-node fallback identity for a directly-attached
  1537. // node whose panel hasn't reported a panelGuid yet (old build). Node ids are
  1538. // master-local, so this only composes for direct nodes — exactly the pre-#4983
  1539. // flat-topology case where an old-build node appears.
  1540. func synthNodeGuid(nodeID int) string {
  1541. return fmt.Sprintf("node:%d", nodeID)
  1542. }
  1543. // mergeEmails returns the deduped union of two email slices.
  1544. func mergeEmails(a, b []string) []string {
  1545. if len(a) == 0 {
  1546. return b
  1547. }
  1548. seen := make(map[string]struct{}, len(a)+len(b))
  1549. out := make([]string, 0, len(a)+len(b))
  1550. for _, e := range a {
  1551. if _, ok := seen[e]; !ok {
  1552. seen[e] = struct{}{}
  1553. out = append(out, e)
  1554. }
  1555. }
  1556. for _, e := range b {
  1557. if _, ok := seen[e]; !ok {
  1558. seen[e] = struct{}{}
  1559. out = append(out, e)
  1560. }
  1561. }
  1562. return out
  1563. }
  1564. func remapGuidTreeKey(tree map[string][]string, from, to string) map[string][]string {
  1565. if from == "" || to == "" || from == to {
  1566. return tree
  1567. }
  1568. remapped := make(map[string][]string, len(tree))
  1569. for guid, values := range tree {
  1570. if guid == from {
  1571. guid = to
  1572. }
  1573. remapped[guid] = mergeEmails(remapped[guid], values)
  1574. }
  1575. return remapped
  1576. }
  1577. func normalizeActiveInboundTreeTags(tree map[string][]string, tagToCentral map[string]*model.Inbound) map[string][]string {
  1578. if len(tree) == 0 {
  1579. return nil
  1580. }
  1581. out := make(map[string][]string, len(tree))
  1582. for guid, tags := range tree {
  1583. if guid == "" || len(tags) == 0 {
  1584. continue
  1585. }
  1586. seen := make(map[string]struct{}, len(tags))
  1587. for _, tag := range tags {
  1588. if tag == "" {
  1589. continue
  1590. }
  1591. if central, ok := tagToCentral[tag]; ok && central != nil && central.Tag != "" {
  1592. tag = central.Tag
  1593. }
  1594. if _, dup := seen[tag]; dup {
  1595. continue
  1596. }
  1597. seen[tag] = struct{}{}
  1598. out[guid] = append(out[guid], tag)
  1599. }
  1600. }
  1601. if len(out) == 0 {
  1602. return nil
  1603. }
  1604. return out
  1605. }
  1606. func activeInboundGuidKeys(inbounds []*model.Inbound, tagToCentral map[string]*model.Inbound, originGuidFor func(*model.Inbound) string) map[string]struct{} {
  1607. allowed := make(map[string]struct{})
  1608. for _, ib := range inbounds {
  1609. if ib == nil {
  1610. continue
  1611. }
  1612. if _, ok := tagToCentral[ib.Tag]; !ok {
  1613. continue
  1614. }
  1615. if guid := originGuidFor(ib); guid != "" {
  1616. allowed[guid] = struct{}{}
  1617. }
  1618. }
  1619. return allowed
  1620. }
  1621. func filterGuidTreeKeys(tree map[string][]string, allowed map[string]struct{}) map[string][]string {
  1622. if len(tree) == 0 || len(allowed) == 0 {
  1623. return nil
  1624. }
  1625. out := make(map[string][]string, len(tree))
  1626. for guid, values := range tree {
  1627. if _, ok := allowed[guid]; !ok {
  1628. continue
  1629. }
  1630. if len(values) > 0 {
  1631. out[guid] = values
  1632. }
  1633. }
  1634. if len(out) == 0 {
  1635. return nil
  1636. }
  1637. return out
  1638. }
  1639. func (s *InboundService) GetClientsLastOnline() (map[string]int64, error) {
  1640. db := database.GetDB()
  1641. var rows []xray.ClientTraffic
  1642. err := db.Model(&xray.ClientTraffic{}).Select("email, last_online").Find(&rows).Error
  1643. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  1644. return nil, err
  1645. }
  1646. result := make(map[string]int64, len(rows))
  1647. for _, r := range rows {
  1648. result[r.Email] = r.LastOnline
  1649. }
  1650. return result, nil
  1651. }
  1652. // RefreshLocalOnlineClients folds the emails and inbound tags active on this
  1653. // panel's own xray this poll into the local online/active sets, applying the
  1654. // grace window and pruning stale entries. Pass nil to only prune. See
  1655. // xray.Process for why the local sets are kept separate from the shared
  1656. // last_online column.
  1657. func (s *InboundService) RefreshLocalOnlineClients(activeEmails, activeInboundTags []string) {
  1658. if process := currentXrayProcess(); process != nil {
  1659. process.RefreshLocalOnline(activeEmails, activeInboundTags, time.Now().UnixMilli(), onlineGracePeriodMs)
  1660. }
  1661. }
  1662. func (s *InboundService) FilterAndSortClientEmails(emails []string) ([]string, []string, error) {
  1663. db := database.GetDB()
  1664. // Step 1: Get ClientTraffic records for emails in the input list.
  1665. // Chunked to stay under SQLite's bind-variable limit on huge inputs.
  1666. uniqEmails := uniqueNonEmptyStrings(emails)
  1667. clients := make([]xray.ClientTraffic, 0, len(uniqEmails))
  1668. for _, batch := range chunkStrings(uniqEmails, sqliteMaxVars) {
  1669. var page []xray.ClientTraffic
  1670. if err := db.Where("email IN ?", batch).Find(&page).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  1671. return nil, nil, err
  1672. }
  1673. clients = append(clients, page...)
  1674. }
  1675. // Step 2: Sort clients by (Up + Down) descending
  1676. sort.Slice(clients, func(i, j int) bool {
  1677. return (clients[i].Up + clients[i].Down) > (clients[j].Up + clients[j].Down)
  1678. })
  1679. // Step 3: Extract sorted valid emails and track found ones
  1680. validEmails := make([]string, 0, len(clients))
  1681. found := make(map[string]bool)
  1682. for _, client := range clients {
  1683. validEmails = append(validEmails, client.Email)
  1684. found[client.Email] = true
  1685. }
  1686. // Step 4: Identify emails that were not found in the database
  1687. extraEmails := make([]string, 0)
  1688. for _, email := range emails {
  1689. if !found[email] {
  1690. extraEmails = append(extraEmails, email)
  1691. }
  1692. }
  1693. return validEmails, extraEmails, nil
  1694. }