mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-20 19:46:09 +00:00
* feat(nodes): add stable panel GUID identity (multi-hop phase 0) Per-panel autoincrement node ids are meaningless one hop away, so in a chained topology (Node1 -> Node2 -> Node3) the master cannot attribute online clients or inbounds to the physical node that hosts them (#4983). Introduce a stable self-identifier: each panel generates and persists a panelGuid (settings table, mirroring GetSecret), returns it in panel/api/server/status, and the master learns it per node via the heartbeat into a new Node.Guid column. Guarded so an old-build node or a failed probe never clears a known GUID. No behavior change yet - this is the identity foundation Phases 1-2 key on. Refs #4983 * feat(nodes): attribute inbounds to their origin node by GUID (multi-hop phase 1) Add Inbound.OriginNodeGuid: the GUID of the panel that physically hosts an inbound. Empty means this panel's own xray; set means it was synced from a node. SetRemoteTraffic now fills it per synced inbound - keeping a non-empty value the node forwarded from its own sub-node (so a transitive inbound stays attributed to the deepest node across hops), and otherwise attributing the node's own local inbounds to that node's GUID. Empty (old-build node without a GUID) leaves the existing node_id-based attribution untouched. The field rides the existing inbound JSON, so /list propagates it up the chain with no serve-side change. Phase 2 will key per-node online off this instead of the panel-local node_id. Refs #4983 * feat(nodes): key online status by node GUID end-to-end (multi-hop phase 2) Replace the panel-local node-id keying of per-node online status with the stable panelGuid, so a client several hops down a node chain is attributed to the node that physically hosts it instead of the intermediate node it syncs through (#4983). xray/process.go stores each direct node's reported GUID-keyed subtree and merges them (correct at any depth); the service assembles GetOnlineClientsByGuid (own clients under this panel's GUID + every node under its GUID). FetchTrafficSnapshot fetches the new /clients/onlinesByGuid, falling back to the flat /onlines for old-build nodes (keyed under the node's GUID or a master-local synthetic id). The node rollup, the WS onlineByGuid/activeInbounds fields, and the inbounds-page rollup all scope by GUID; local inbounds get their OriginNodeGuid filled with the panel's GUID at serve time so the frontend keys uniformly. Old-build nodes degrade to the prior flat behaviour via the synthetic node:<id> key. Refs #4983 Refs #4983 * feat(nodes): surface transitive sub-nodes on the master (multi-hop phase 3a) Each panel publishes read-only summaries of the nodes it manages via GET /panel/api/server/descendants (node API token). The heartbeat job caches each direct node's summaries; GetNodeTree merges them as transitive model.Node projections (Id 0, Transitive=true, ParentGuid = their parent node's GUID) and recomputes InboundCount/OnlineCount/DepletedCount per origin GUID so a direct node shows only its own inbounds and each sub-node shows its own (#4983). The Nodes-page list endpoint and the heartbeat broadcast now return the tree; GetAll stays direct-only for probing/syncing. One transitive level is surfaced (covers Node1->Node2->Node3); deeper recursion is a follow-up. Backend only - the Nodes-page nested UI lands next. Refs #4983 * feat(nodes): render transitive sub-nodes nested + read-only on the Nodes page (multi-hop phase 3b) The Nodes page now shows a node's downstream sub-nodes (learned via the descendants tree) as indented, read-only rows ordered right under their parent: no enable toggle, probe, edit, delete, update, selection, or history expander - just a 'Sub-node' tag whose tooltip names the parent it is reached through. Desktop table and mobile cards both handle it. Transitive rows are keyed by GUID (their Id is 0) so they don't collide with real nodes (#4983). Rows nest by parentGuid rather than AntD tree-children to avoid clashing with the existing per-row history expander. New labels added to en-US (other locales fall back until translated). Refs #4983 Refs #4983 * i18n(nodes): translate subNode/subNodeTip across all locales Phase 3b added these two Nodes-page keys (read-only sub-node tag + tooltip) only to en-US; fill in the other 12 locales so the multi-hop sub-node UI is fully localized. The {parent} placeholder is preserved in every translation. Refs #4983
This commit is contained in:
+123
-9
@@ -231,9 +231,30 @@ func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
|
||||
}
|
||||
s.enrichClientStats(db, inbounds)
|
||||
s.annotateFallbackParents(db, inbounds)
|
||||
s.annotateLocalOriginGuid(inbounds)
|
||||
return inbounds, nil
|
||||
}
|
||||
|
||||
// annotateLocalOriginGuid fills OriginNodeGuid for this panel's OWN inbounds
|
||||
// (NodeID == nil) with the panel's stable GUID; inbounds synced from a node
|
||||
// already carry the originating node's GUID. Read-time only (not persisted) so
|
||||
// the per-inbound online view can scope by GUID uniformly across a chain of
|
||||
// nodes (#4983).
|
||||
func (s *InboundService) annotateLocalOriginGuid(inbounds []*model.Inbound) {
|
||||
if len(inbounds) == 0 {
|
||||
return
|
||||
}
|
||||
guid := s.panelGuid()
|
||||
if guid == "" {
|
||||
return
|
||||
}
|
||||
for _, ib := range inbounds {
|
||||
if ib.OriginNodeGuid == "" && ib.NodeID == nil {
|
||||
ib.OriginNodeGuid = guid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetInboundsSlim returns the same list of inbounds as GetInbounds but
|
||||
// strips every per-client field other than email / enable / comment from
|
||||
// settings.clients and skips UUID/SubId enrichment on ClientStats. The
|
||||
@@ -252,6 +273,7 @@ func (s *InboundService) GetInboundsSlim(userId int) ([]*model.Inbound, error) {
|
||||
return nil, err
|
||||
}
|
||||
s.annotateFallbackParents(db, inbounds)
|
||||
s.annotateLocalOriginGuid(inbounds)
|
||||
for _, ib := range inbounds {
|
||||
ib.Settings = slimSettingsClients(ib.Settings)
|
||||
}
|
||||
@@ -1453,6 +1475,21 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
db := database.GetDB()
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
// originGuidFor attributes a synced inbound to the panel that physically
|
||||
// hosts it: inbounds the node forwards from its own sub-nodes already carry
|
||||
// a non-empty OriginNodeGuid (kept as-is across hops); the node's own local
|
||||
// inbounds report empty, so they are attributed to the node's own GUID. An
|
||||
// empty result (old-build node with no GUID yet) leaves attribution to the
|
||||
// node_id fallback downstream (#4983).
|
||||
var nodeRow model.Node
|
||||
db.Select("guid").Where("id = ?", nodeID).First(&nodeRow)
|
||||
originGuidFor := func(snapIb *model.Inbound) string {
|
||||
if snapIb.OriginNodeGuid != "" {
|
||||
return snapIb.OriginNodeGuid
|
||||
}
|
||||
return nodeRow.Guid
|
||||
}
|
||||
|
||||
var central []model.Inbound
|
||||
if err := db.Model(model.Inbound{}).
|
||||
Where("node_id = ?", nodeID).
|
||||
@@ -1598,6 +1635,7 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
newIb := model.Inbound{
|
||||
UserId: defaultUserId,
|
||||
NodeID: &nodeID,
|
||||
OriginNodeGuid: originGuidFor(snapIb),
|
||||
Tag: chosenTag,
|
||||
Listen: snapIb.Listen,
|
||||
Port: snapIb.Port,
|
||||
@@ -1645,6 +1683,12 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
updates["up"] = snapIb.Up
|
||||
updates["down"] = snapIb.Down
|
||||
}
|
||||
// Physical-home attribution is independent of config-dirty state, so
|
||||
// keep it current even while the node has pending offline edits. Writes
|
||||
// once to backfill an existing row, then stays equal (#4983).
|
||||
if og := originGuidFor(snapIb); c.OriginNodeGuid != og {
|
||||
updates["origin_node_guid"] = og
|
||||
}
|
||||
|
||||
if !dirty && (c.Settings != snapIb.Settings ||
|
||||
c.Remark != snapIb.Remark ||
|
||||
@@ -1933,7 +1977,17 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
committed = true
|
||||
|
||||
if p != nil {
|
||||
p.SetNodeOnlineClients(nodeID, snap.OnlineEmails)
|
||||
tree := snap.OnlineTree
|
||||
if len(tree) == 0 && len(snap.OnlineEmails) > 0 {
|
||||
// Old-build node (no GUID tree): key its flat online list under its
|
||||
// own effective identity so attribution still works for that branch.
|
||||
effectiveGuid := nodeRow.Guid
|
||||
if effectiveGuid == "" {
|
||||
effectiveGuid = synthNodeGuid(nodeID)
|
||||
}
|
||||
tree = map[string][]string{effectiveGuid: snap.OnlineEmails}
|
||||
}
|
||||
p.SetNodeOnlineTree(nodeID, tree)
|
||||
}
|
||||
|
||||
return structuralChange, nil
|
||||
@@ -3634,23 +3688,46 @@ func (s *InboundService) GetOnlineClients() []string {
|
||||
return p.GetOnlineClients()
|
||||
}
|
||||
|
||||
func (s *InboundService) GetOnlineClientsByNode() map[int][]string {
|
||||
// GetOnlineClientsByGuid returns online emails keyed by the panelGuid of the
|
||||
// node that physically hosts each set: this panel's own clients under its own
|
||||
// GUID, plus every node in the tree under its GUID (#4983). Replaces the old
|
||||
// node-id keying so a client three hops down is attributed to its real node,
|
||||
// not the intermediate one it was synced through.
|
||||
func (s *InboundService) GetOnlineClientsByGuid() map[string][]string {
|
||||
if p == nil {
|
||||
return map[int][]string{}
|
||||
return map[string][]string{}
|
||||
}
|
||||
return p.GetOnlineClientsByNode()
|
||||
out := p.GetMergedNodeTrees()
|
||||
if local := p.GetLocalOnlineClients(); len(local) > 0 {
|
||||
if guid := s.panelGuid(); guid != "" {
|
||||
out[guid] = mergeEmails(out[guid], local)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *InboundService) GetActiveInboundsByNode() map[int][]string {
|
||||
// GetActiveInboundsByGuid returns the inbound tags that carried traffic within
|
||||
// the grace window for THIS panel, under its own GUID. Remote nodes don't
|
||||
// report per-inbound activity, so a GUID missing from the map means "don't
|
||||
// gate" for that node's inbounds.
|
||||
func (s *InboundService) GetActiveInboundsByGuid() map[string][]string {
|
||||
if p == nil {
|
||||
return map[int][]string{}
|
||||
return map[string][]string{}
|
||||
}
|
||||
return p.GetActiveInboundsByNode()
|
||||
active := p.GetLocalActiveInbounds()
|
||||
if len(active) == 0 {
|
||||
return map[string][]string{}
|
||||
}
|
||||
guid := s.panelGuid()
|
||||
if guid == "" {
|
||||
return map[string][]string{}
|
||||
}
|
||||
return map[string][]string{guid: active}
|
||||
}
|
||||
|
||||
func (s *InboundService) SetNodeOnlineClients(nodeID int, emails []string) {
|
||||
func (s *InboundService) SetNodeOnlineTree(nodeID int, tree map[string][]string) {
|
||||
if p != nil {
|
||||
p.SetNodeOnlineClients(nodeID, emails)
|
||||
p.SetNodeOnlineTree(nodeID, tree)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3660,6 +3737,43 @@ func (s *InboundService) ClearNodeOnlineClients(nodeID int) {
|
||||
}
|
||||
}
|
||||
|
||||
// panelGuid returns this panel's stable self-identifier, used to key the local
|
||||
// panel's own clients in the per-node online maps (#4983).
|
||||
func (s *InboundService) panelGuid() string {
|
||||
guid, _ := (&SettingService{}).GetPanelGuid()
|
||||
return guid
|
||||
}
|
||||
|
||||
// synthNodeGuid is the stable per-node fallback identity for a directly-attached
|
||||
// node whose panel hasn't reported a panelGuid yet (old build). Node ids are
|
||||
// master-local, so this only composes for direct nodes — exactly the pre-#4983
|
||||
// flat-topology case where an old-build node appears.
|
||||
func synthNodeGuid(nodeID int) string {
|
||||
return fmt.Sprintf("node:%d", nodeID)
|
||||
}
|
||||
|
||||
// mergeEmails returns the deduped union of two email slices.
|
||||
func mergeEmails(a, b []string) []string {
|
||||
if len(a) == 0 {
|
||||
return b
|
||||
}
|
||||
seen := make(map[string]struct{}, len(a)+len(b))
|
||||
out := make([]string, 0, len(a)+len(b))
|
||||
for _, e := range a {
|
||||
if _, ok := seen[e]; !ok {
|
||||
seen[e] = struct{}{}
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
for _, e := range b {
|
||||
if _, ok := seen[e]; !ok {
|
||||
seen[e] = struct{}{}
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *InboundService) GetClientsLastOnline() (map[string]int64, error) {
|
||||
db := database.GetDB()
|
||||
var rows []xray.ClientTraffic
|
||||
|
||||
Reference in New Issue
Block a user