mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 16:04:50 +03:00
206 lines
6.1 KiB
Go
206 lines
6.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
)
|
|
|
|
// LocalDescendants returns this panel's read-only summaries of the nodes it
|
|
// directly manages, so a parent panel can surface them as transitive sub-nodes
|
|
// (#4983). Only nodes with a known GUID are included — a stable identity is
|
|
// required to attribute them one hop up. Not recursive: each panel reports its
|
|
// own direct nodes, and a master walks one level via each direct node's
|
|
// endpoint, which covers the Node1 -> Node2 -> Node3 case.
|
|
func (s *NodeService) LocalDescendants() ([]model.NodeSummary, error) {
|
|
selfGuid, _ := (&SettingService{}).GetPanelGuid()
|
|
db := database.GetDB()
|
|
var nodes []*model.Node
|
|
if err := db.Model(model.Node{}).Order("id asc").Find(&nodes).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]model.NodeSummary, 0, len(nodes))
|
|
for _, n := range nodes {
|
|
if n.Guid == "" {
|
|
continue
|
|
}
|
|
out = append(out, model.NodeSummary{
|
|
Guid: n.Guid,
|
|
ParentGuid: selfGuid,
|
|
Name: n.Name,
|
|
Address: n.Address,
|
|
Scheme: n.Scheme,
|
|
Port: n.Port,
|
|
Status: n.Status,
|
|
LastHeartbeat: n.LastHeartbeat,
|
|
LatencyMs: n.LatencyMs,
|
|
PanelVersion: n.PanelVersion,
|
|
XrayVersion: n.XrayVersion,
|
|
XrayState: n.XrayState,
|
|
XrayError: n.XrayError,
|
|
BasePath: n.BasePath,
|
|
Enabled: &n.Enable,
|
|
Telemetry: liveNodeTelemetry(n, time.Now()),
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
var (
|
|
nodeDescendantsMu sync.RWMutex
|
|
nodeDescendantsCache = map[int][]model.NodeSummary{}
|
|
)
|
|
|
|
// RefreshDescendants pulls a direct node's published sub-node summaries and
|
|
// caches them keyed by node id. Best-effort: a fetch error keeps the last good
|
|
// set (the node may be briefly unreachable). Called from the heartbeat job.
|
|
func (s *NodeService) RefreshDescendants(ctx context.Context, n *model.Node) {
|
|
if n == nil {
|
|
return
|
|
}
|
|
mgr := runtime.GetManager()
|
|
if mgr == nil {
|
|
return
|
|
}
|
|
rt, err := mgr.RemoteFor(n)
|
|
if err != nil {
|
|
return
|
|
}
|
|
summaries, err := rt.GetDescendants(ctx)
|
|
if err != nil {
|
|
return
|
|
}
|
|
now := time.Now().UnixMilli()
|
|
for i := range summaries {
|
|
if sample := summaries[i].Telemetry; sample != nil && sample.SampleAgeMillis != nil {
|
|
sample.SampledAt = now - min(max(0, *sample.SampleAgeMillis), nodeTelemetryFreshness.Milliseconds())
|
|
}
|
|
}
|
|
nodeDescendantsMu.Lock()
|
|
if len(summaries) == 0 {
|
|
delete(nodeDescendantsCache, n.Id)
|
|
} else {
|
|
nodeDescendantsCache[n.Id] = summaries
|
|
}
|
|
nodeDescendantsMu.Unlock()
|
|
}
|
|
|
|
// ClearDescendants drops a node's cached sub-node summaries (its probe failed).
|
|
func (s *NodeService) ClearDescendants(nodeID int) {
|
|
nodeDescendantsMu.Lock()
|
|
delete(nodeDescendantsCache, nodeID)
|
|
nodeDescendantsMu.Unlock()
|
|
}
|
|
|
|
// RetainEnabledNodeDescendants drops sub-nodes learned from nodes the heartbeat no
|
|
// longer probes: disabled ones it skips, deleted ones missing from nodes.
|
|
func (s *NodeService) RetainEnabledNodeDescendants(nodes []*model.Node) {
|
|
enabled := make(map[int]bool, len(nodes))
|
|
for _, n := range nodes {
|
|
enabled[n.Id] = n.Enable
|
|
}
|
|
nodeDescendantsMu.Lock()
|
|
for nodeID := range nodeDescendantsCache {
|
|
if !enabled[nodeID] {
|
|
delete(nodeDescendantsCache, nodeID)
|
|
}
|
|
}
|
|
nodeDescendantsMu.Unlock()
|
|
}
|
|
|
|
func cachedDescendants() []model.NodeSummary {
|
|
nodeDescendantsMu.RLock()
|
|
defer nodeDescendantsMu.RUnlock()
|
|
out := make([]model.NodeSummary, 0)
|
|
for _, list := range nodeDescendantsCache {
|
|
out = append(out, list...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// GetNodeTree returns the direct nodes plus any transitive sub-nodes learned
|
|
// from them, with per-GUID counts so each node shows only the inbounds/online
|
|
// it physically hosts (#4983). Direct nodes carry the master's own GUID as
|
|
// ParentGuid; a transitive node carries its parent node's GUID. Transitive
|
|
// nodes are read-only projections (Id == 0). Used by the Nodes page and the
|
|
// heartbeat broadcast — never for probing/syncing, which stay on ListOperational.
|
|
func (s *NodeService) GetNodeTree() ([]*model.Node, error) {
|
|
nodes, _, _, err := s.getNodeTreeWithCounts(false)
|
|
return nodes, err
|
|
}
|
|
|
|
func (s *NodeService) getNodeTreeWithCounts(overview bool) ([]*model.Node, NodeFleetCounts, NodeFleetCounts, error) {
|
|
nodes, err := s.ListOperational()
|
|
if err != nil || len(nodes) == 0 {
|
|
return nodes, NodeFleetCounts{}, NodeFleetCounts{}, err
|
|
}
|
|
selfGuid, _ := (&SettingService{}).GetPanelGuid()
|
|
directGuids := make(map[string]struct{}, len(nodes))
|
|
for _, n := range nodes {
|
|
n.ParentGuid = selfGuid
|
|
if n.Guid != "" {
|
|
directGuids[n.Guid] = struct{}{}
|
|
}
|
|
}
|
|
|
|
seen := make(map[string]struct{})
|
|
var transitive []*model.Node
|
|
for _, sum := range cachedDescendants() {
|
|
if sum.Guid == "" || sum.Guid == selfGuid {
|
|
continue
|
|
}
|
|
if _, ok := directGuids[sum.Guid]; ok {
|
|
continue // already shown as a direct node
|
|
}
|
|
if _, ok := seen[sum.Guid]; ok {
|
|
continue
|
|
}
|
|
seen[sum.Guid] = struct{}{}
|
|
transitive = append(transitive, &model.Node{
|
|
Guid: sum.Guid,
|
|
ParentGuid: sum.ParentGuid,
|
|
Name: sum.Name,
|
|
Address: sum.Address,
|
|
Scheme: sum.Scheme,
|
|
Port: sum.Port,
|
|
Status: sum.Status,
|
|
LastHeartbeat: sum.LastHeartbeat,
|
|
LatencyMs: sum.LatencyMs,
|
|
PanelVersion: sum.PanelVersion,
|
|
XrayVersion: sum.XrayVersion,
|
|
XrayState: sum.XrayState,
|
|
XrayError: sum.XrayError,
|
|
Transitive: true,
|
|
Enable: sum.Enabled == nil || *sum.Enabled,
|
|
BasePath: sum.BasePath,
|
|
Telemetry: sum.Telemetry,
|
|
})
|
|
}
|
|
all := append(nodes, transitive...)
|
|
master, fleet, err := s.countNodeFleet(all, selfGuid, overview)
|
|
return all, master, fleet, err
|
|
}
|
|
|
|
func (s *NodeService) GetNodeTreeView() ([]*NodeView, error) {
|
|
nodes, err := s.GetNodeTree()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nodeTreeViews(nodes), nil
|
|
}
|
|
|
|
func nodeTreeViews(nodes []*model.Node) []*NodeView {
|
|
views := toNodeViews(nodes)
|
|
now := time.Now()
|
|
for _, view := range views {
|
|
if view.LastHeartbeat > 0 && !view.Transitive {
|
|
view.HeartbeatAgeMillis = new(max(0, now.Sub(time.Unix(view.LastHeartbeat, 0)).Milliseconds()))
|
|
}
|
|
}
|
|
return views
|
|
}
|