mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-16 09:36:07 +00:00
fix(nodes): cloned-node attribution, node-hosted client display (online/speed/counts), and sync robustness (#5488)
* fix(nodes): keep cloned nodes (shared panelGuid) in separate attribution buckets
#4983 keys online/inbound attribution by panelGuid, assuming it is globally unique. Cloned node servers ship an identical panelGuid in their copied settings, so the master collapsed several physical nodes into one bucket: GetMergedNodeTrees merged their online sets under one key and every inbound on those nodes (same origin_node_guid) read that merged set, so the inbound page showed online cross-attributed and counts inflated.
Fall back to the node-unique synthNodeGuid(node.Id) whenever a node's panelGuid is shared by another of the master's direct nodes. Applied consistently at originGuidFor (origin_node_guid write), the online-tree key plus a self-key remap for nodes that report a GUID-keyed tree, effectiveNodeGuid, and recountByGuid's inbound bucketing. sharedNodeGuids computes the collision set. Online now works without node changes; making panelGuids unique restores real-GUID identity and also fixes GUID-keyed IP attribution.
* fix(nodes): extend duplicate-GUID hardening to master collisions, IP attribution, and a heartbeat warning
Builds on the node-vs-node fix: a node's GUID is now also treated as ambiguous when it equals the master's own panelGuid (a node cloned from the master), so the master's local clients and that node can't merge. Centralized as ambiguousNodeGuids(nodes, selfGuid) + effectiveNodeKey(node).
Applied the same node-unique fallback to the GUID-keyed IP attribution that #4983 added but the prior commit left collapsing: MergeClientIpsByGuid remaps a cloned node's own subtree to its node-unique key, nodeGuidNameMap resolves names by that key, and node deletion purges both keys. Added a throttled heartbeat warning so the operator is told to regenerate a duplicate panelGuid. Tests cover master-collision, effectiveNodeKey, and the IP remap.
* fix(node-sync): log the client-IP-attribution 404 once per node, not every cycle
Old-build nodes lack panel/api/clients/clientIpsByGuid and answer 404 on every IP-sync cycle (~10s), which floods the debug log now that the IP phase actually runs. Note the missing endpoint once per node (re-armed if the node later recovers or is upgraded) and keep logging genuine fetch errors.
* fix(nodes): remap a cloned node's own-panelGuid origin so the inbound page shows online
These nodes report their OWN inbounds with their own panelGuid as OriginNodeGuid, so originGuidFor returned the shared GUID verbatim and never remapped it. origin_node_guid stayed the shared GUID while online was keyed under the node-unique key, so the inbound page (which reads the stored origin_node_guid) looked up an empty bucket and showed everyone offline — even though the Nodes page (which derives the key live) was correct. Treat an origin equal to the node's own panelGuid as the node's own inbound and resolve it through selfKey; keep only a genuinely different (descendant) origin across hops.
* fix(node-sync): don't delete a node's central inbounds when its snapshot is empty
The central-inbound sweep deletes any central inbound whose tag is absent from the node's snapshot, with no guard for an empty snapshot. A node mid-restart or with a transient DB error (e.g. Postgres 57P01) can return an empty inbound list with success=true, which wiped all of that node's central inbounds and their clients (and reset traffic history on re-create) — observed on the Germany node: 0 clients but still 44 online (online survives because it comes from the snapshot's online tree, not the central inbound). Skip the sweep entirely when the snapshot reports zero inbounds; a real per-inbound deletion still sweeps via a non-empty snapshot that omits one tag.
* fix(email): stay silent when SMTP notifications are disabled
The event subscriber is registered unconditionally and only checked the per-event list (smtpEnabledEvents, default login.attempt,cpu.high) — not the smtpEnable master toggle. Login events are always published, so a panel with smtpEnable=false still attempted a send on every login and logged 'email subscriber: send failed: smtp host not configured'. Gate HandleEvent on GetSmtpEnable() so a disabled-SMTP panel does nothing, matching the comment where the subscriber is registered.
* fix(nodes): count only expired/exhausted as 'ended', not disabled clients
The per-node depleted (ended) count folded disabled clients in with expired/exhausted (expired || exhausted || !Enable), so the Nodes page 'ended' chip was inflated and inconsistent with the inbound page, where disabled and depleted are separate buckets. Count only expired/exhausted in both GetAll and recountByGuid so 'ended' means the same thing on both pages.
* feat(nodes): show live speed for node-hosted inbounds
Inbound speed is computed on the dashboard from a 'traffics' delta feed, which only the local Xray poll produced — so node-hosted inbounds showed no speed. The node sync now diffs successive per-inbound cumulative totals (it polls @5s, same as the local poll) and broadcasts the byte deltas as a separate 'nodeTraffics' field, keyed by the central tag the dashboard already matches. The frontend applies 'traffics' to local inbounds and 'nodeTraffics' to node inbounds within their own scope, so the two 5s polls don't clobber each other and idle inbounds still clear. Deltas clamp to 0 on a reset; a node that fails to sync keeps a stale total so its delta is 0 (no phantom speed).
* fix(nodes): normalize node-inbound speed by elapsed time to avoid recovery spikes
Adversarial review found that a node's cumulative inbound counter keeps climbing while the master can't reach it, so the first delta after a gap (node outage, skipped poll, slow node) spans more than one 5s window but was still divided by the dashboard's fixed 5s — rendering an impossible one-tick speed spike on recovery (and a 2x over-report after a skipped poll). Now each delta is normalized to the fixed window using the real elapsed time since the inbound's counter last changed, so a backlog shows the true average rate over the gap. The change timestamp advances only on actual movement, so idle stretches average correctly when traffic resumes; resets rebaseline. Also moves the maybePushGlobals doc comment back onto its function.
* fix(inbounds): keep last speed across page navigation instead of blanking
Speed is delta-derived, so it can't be recomputed until the first poll after mount. The websocket subscription and speed state are page-scoped (useWebSocket lives in InboundsPage), so leaving to another page and returning blanked the Speed column for up to one 5s poll. Cache the last speed map across mounts (module scope, 15s recency guard) and seed the state from it, so returning shows the last throughput immediately and the next poll refreshes it. Applies to both local and node-hosted inbound speed.
* fix(inbounds): rebalance table column widths so it fills width without gaps
Inbound list columns had small fixed widths summing far below the table's
full width, so AntD spread the leftover space evenly into wide empty gaps.
Widen the content-heavy columns (protocol, clients, traffic, node) so the
slack lands there, keep the small ones (id, port, enable) tight, and make
scroll.x track the visible columns' total so the table never collapses
below content and adapts when conditional columns are hidden.
* feat(nodes): show active/disabled client counts on the nodes page like inbounds
The nodes page only showed total/online/ended, and (since ended now excludes disabled) disabled clients were invisible there. Compute per-node active and disabled counts — in both GetAll and recountByGuid, with the same depleted-wins-over-disabled precedence the inbound page uses so the buckets stay mutually exclusive — and render total/active/disabled/ended/online chips matching the inbound page (table column + mobile stats modal).
* fix(nodes): count active/disabled/ended by client email, not stale inbound_id
The per-node client breakdown filtered client_traffics by inbound_id, but that column goes stale after an inbound is delete+recreated (e.g. the Germany node), so almost every traffic row pointed at a dead inbound id and the counts collapsed — active showed ~5 instead of ~1100. Classify each node client via client_inbounds -> clients joined to client_traffics by EMAIL (the reliable key), deduped per node/guid, in both GetAll and recountByGuid. Now active/disabled/ended on the nodes page match the inbound page. Added a regression test that proves matching works with a deliberately stale inbound_id.
* style(nodes): widen Clients column so the count chips fit one tidy line
After adding the active/disabled chips, the 5 chips (total/active/disabled/ended/online) no longer fit the 160px Clients column and wrapped to two lines. Widen it to 220 and drop the Space wrap so they render on a single line like the inbound page, and zero the total tag's margin for even spacing. Same principle as 79ff283 (give the content column enough width).
* style(nodes): tighten Clients chip spacing to match the inbound page
AntD's default tag side-padding (~8px) put a wide gap between the count chips. Apply the inbound page's compact padding ('0 2px') + client-count-tag (tabular-nums) to each chip and narrow the column to 180 so the numbers sit close together like the inbound list instead of floating apart.
This commit is contained in:
+180
-52
@@ -14,6 +14,7 @@ import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
@@ -122,10 +123,8 @@ func (s *NodeService) GetAll() ([]*model.Node, error) {
|
||||
return nodes, nil
|
||||
}
|
||||
inboundsByNode := make(map[int][]int, len(nodes))
|
||||
nodeByInbound := make(map[int]int, len(inboundRows))
|
||||
for _, row := range inboundRows {
|
||||
inboundsByNode[row.NodeID] = append(inboundsByNode[row.NodeID], row.Id)
|
||||
nodeByInbound[row.Id] = row.NodeID
|
||||
}
|
||||
|
||||
type clientCountRow struct {
|
||||
@@ -150,60 +149,105 @@ func (s *NodeService) GetAll() ([]*model.Node, error) {
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
type trafficRow struct {
|
||||
InboundID int `gorm:"column:inbound_id"`
|
||||
Email string
|
||||
Enable bool
|
||||
Total int64
|
||||
Up int64
|
||||
Down int64
|
||||
ExpiryTime int64 `gorm:"column:expiry_time"`
|
||||
}
|
||||
var trafficRows []trafficRow
|
||||
inboundIDs := make([]int, 0, len(nodeByInbound))
|
||||
for id := range nodeByInbound {
|
||||
inboundIDs = append(inboundIDs, id)
|
||||
}
|
||||
// Chunk the IN clause to avoid "too many SQL variables" on SQLite
|
||||
// when there are many node-owned inbounds (common with many nodes).
|
||||
// sqliteMaxVars is defined in this package (inbound.go).
|
||||
for _, batch := range chunkInts(inboundIDs, sqliteMaxVars) {
|
||||
var page []trafficRow
|
||||
if err := db.Table("client_traffics").
|
||||
Select("inbound_id, email, enable, total, up, down, expiry_time").
|
||||
Where("inbound_id IN ?", batch).
|
||||
Scan(&page).Error; err == nil {
|
||||
trafficRows = append(trafficRows, page...)
|
||||
}
|
||||
}
|
||||
depletedByNode := make(map[int]int)
|
||||
if len(trafficRows) > 0 {
|
||||
for _, row := range trafficRows {
|
||||
nodeID, ok := nodeByInbound[row.InboundID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
expired := row.ExpiryTime > 0 && row.ExpiryTime <= now
|
||||
exhausted := row.Total > 0 && row.Up+row.Down >= row.Total
|
||||
if expired || exhausted || !row.Enable {
|
||||
depletedByNode[nodeID]++
|
||||
}
|
||||
disabledByNode := make(map[int]int)
|
||||
activeByNode := make(map[int]int)
|
||||
statuses, _ := s.nodeClientStatuses()
|
||||
seen := make(map[int]map[int]struct{}, len(nodes))
|
||||
for _, st := range statuses {
|
||||
clientsSeen := seen[st.NodeID]
|
||||
if clientsSeen == nil {
|
||||
clientsSeen = make(map[int]struct{})
|
||||
seen[st.NodeID] = clientsSeen
|
||||
}
|
||||
if _, dup := clientsSeen[st.ClientID]; dup {
|
||||
// A client attached to several inbounds of one node counts once,
|
||||
// matching the distinct ClientCount above.
|
||||
continue
|
||||
}
|
||||
clientsSeen[st.ClientID] = struct{}{}
|
||||
switch {
|
||||
case st.Depleted:
|
||||
depletedByNode[st.NodeID]++
|
||||
case st.Disabled:
|
||||
disabledByNode[st.NodeID]++
|
||||
default:
|
||||
activeByNode[st.NodeID]++
|
||||
}
|
||||
}
|
||||
onlineByGuid := s.onlineEmailsByGuid()
|
||||
selfGuid, _ := (&SettingService{}).GetPanelGuid()
|
||||
ambiguous := ambiguousNodeGuids(nodes, selfGuid)
|
||||
for _, n := range nodes {
|
||||
n.InboundCount = len(inboundsByNode[n.Id])
|
||||
n.DepletedCount = depletedByNode[n.Id]
|
||||
n.DisabledCount = disabledByNode[n.Id]
|
||||
n.ActiveCount = activeByNode[n.Id]
|
||||
// Online is attributed to the node that physically hosts the client
|
||||
// (by GUID): a client on a sub-node counts under the sub-node, not
|
||||
// the intermediate node it syncs through (#4983).
|
||||
n.OnlineCount = len(onlineByGuid[effectiveNodeGuid(n)])
|
||||
n.OnlineCount = len(onlineByGuid[effectiveNodeGuid(n, ambiguous)])
|
||||
}
|
||||
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
// nodeClientStatus is one node-hosted client's classification, carrying enough
|
||||
// identity for callers to bucket it by node id or by attribution GUID.
|
||||
type nodeClientStatus struct {
|
||||
InboundID int
|
||||
NodeID int
|
||||
ClientID int
|
||||
Depleted bool
|
||||
Disabled bool
|
||||
}
|
||||
|
||||
// nodeClientStatuses classifies every client attached to a node-hosted inbound as
|
||||
// depleted / disabled / active, matching client_traffics by EMAIL rather than by
|
||||
// inbound_id. client_traffics.inbound_id goes stale after an inbound is
|
||||
// delete+recreated, so filtering by it silently drops most rows; the
|
||||
// client_inbounds -> clients join is the reliable client set and the email join
|
||||
// pulls each client's live counters. Precedence matches the inbound page:
|
||||
// depleted (expired/exhausted) wins over disabled.
|
||||
func (s *NodeService) nodeClientStatuses() ([]nodeClientStatus, error) {
|
||||
type row struct {
|
||||
InboundID int `gorm:"column:inbound_id"`
|
||||
NodeID int `gorm:"column:node_id"`
|
||||
ClientID int `gorm:"column:client_id"`
|
||||
Enable bool `gorm:"column:enable"`
|
||||
Total int64 `gorm:"column:total"`
|
||||
Up int64 `gorm:"column:up"`
|
||||
Down int64 `gorm:"column:down"`
|
||||
ExpiryTime int64 `gorm:"column:expiry_time"`
|
||||
}
|
||||
var rows []row
|
||||
if err := database.GetDB().Table("inbounds").
|
||||
Select("inbounds.id AS inbound_id, inbounds.node_id AS node_id, clients.id AS client_id, " +
|
||||
"clients.enable AS enable, ct.total AS total, ct.up AS up, ct.down AS down, ct.expiry_time AS expiry_time").
|
||||
Joins("JOIN client_inbounds ON client_inbounds.inbound_id = inbounds.id").
|
||||
Joins("JOIN clients ON clients.id = client_inbounds.client_id").
|
||||
Joins("LEFT JOIN client_traffics ct ON ct.email = clients.email").
|
||||
Where("inbounds.node_id IS NOT NULL").
|
||||
Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
out := make([]nodeClientStatus, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
st := nodeClientStatus{InboundID: r.InboundID, NodeID: r.NodeID, ClientID: r.ClientID}
|
||||
expired := r.ExpiryTime > 0 && r.ExpiryTime <= now
|
||||
exhausted := r.Total > 0 && r.Up+r.Down >= r.Total
|
||||
switch {
|
||||
case expired || exhausted:
|
||||
st.Depleted = true
|
||||
case !r.Enable:
|
||||
st.Disabled = true
|
||||
}
|
||||
out = append(out, st)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *NodeService) onlineEmailsByGuid() map[string]map[string]struct{} {
|
||||
svc := InboundService{}
|
||||
byGuid := svc.GetOnlineClientsByGuid()
|
||||
@@ -218,14 +262,69 @@ func (s *NodeService) onlineEmailsByGuid() map[string]map[string]struct{} {
|
||||
return out
|
||||
}
|
||||
|
||||
// effectiveNodeGuid is a node's stable online-attribution key: its reported
|
||||
// panelGuid, or a master-local synthetic id when the node is an old build that
|
||||
// hasn't reported one yet (#4983).
|
||||
func effectiveNodeGuid(n *model.Node) string {
|
||||
if n.Guid != "" {
|
||||
return n.Guid
|
||||
// effectiveNodeGuid is a node's stable online/inbound attribution key: its
|
||||
// reported panelGuid, or a master-local synthetic node-id fallback when the node
|
||||
// has no GUID yet (old build) or its GUID is ambiguous. ambiguous comes from
|
||||
// ambiguousNodeGuids.
|
||||
func effectiveNodeGuid(n *model.Node, ambiguous map[string]struct{}) string {
|
||||
if n.Guid == "" {
|
||||
return synthNodeGuid(n.Id)
|
||||
}
|
||||
return synthNodeGuid(n.Id)
|
||||
if n.Id > 0 {
|
||||
if _, bad := ambiguous[n.Guid]; bad {
|
||||
return synthNodeGuid(n.Id)
|
||||
}
|
||||
}
|
||||
return n.Guid
|
||||
}
|
||||
|
||||
// ambiguousNodeGuids returns the panelGuids a node must not be attributed under
|
||||
// directly, because doing so would merge two distinct identities: a GUID
|
||||
// reported by more than one of this master's direct nodes (cloned node servers
|
||||
// ship the same panelGuid in their copied settings), or a GUID equal to the
|
||||
// master's own panelGuid (a node cloned from the master). A node holding such a
|
||||
// GUID falls back to its node-unique synthNodeGuid. Transitive sub-nodes (Id 0)
|
||||
// carry distinct descendant GUIDs by construction and are excluded.
|
||||
func ambiguousNodeGuids(nodes []*model.Node, selfGuid string) map[string]struct{} {
|
||||
counts := make(map[string]int, len(nodes))
|
||||
for _, n := range nodes {
|
||||
if n.Id > 0 && n.Guid != "" {
|
||||
counts[n.Guid]++
|
||||
}
|
||||
}
|
||||
ambiguous := make(map[string]struct{})
|
||||
for guid, c := range counts {
|
||||
if c > 1 {
|
||||
ambiguous[guid] = struct{}{}
|
||||
}
|
||||
}
|
||||
if selfGuid != "" {
|
||||
if _, ok := counts[selfGuid]; ok {
|
||||
ambiguous[selfGuid] = struct{}{}
|
||||
}
|
||||
}
|
||||
return ambiguous
|
||||
}
|
||||
|
||||
// effectiveNodeKey returns one node's attribution key without a preloaded node
|
||||
// list — its panelGuid when that GUID uniquely identifies it among the master's
|
||||
// nodes and differs from the master's own, otherwise its node-unique
|
||||
// synthNodeGuid. Same rule as effectiveNodeGuid + ambiguousNodeGuids, for the
|
||||
// write paths that handle a single node (online tree, IP attribution).
|
||||
func effectiveNodeKey(node *model.Node) string {
|
||||
if node == nil {
|
||||
return ""
|
||||
}
|
||||
if node.Guid == "" {
|
||||
return synthNodeGuid(node.Id)
|
||||
}
|
||||
var sameGuid int64
|
||||
database.GetDB().Model(&model.Node{}).Where("guid = ?", node.Guid).Count(&sameGuid)
|
||||
masterGuid, _ := (&SettingService{}).GetPanelGuid()
|
||||
if sameGuid > 1 || node.Guid == masterGuid {
|
||||
return synthNodeGuid(node.Id)
|
||||
}
|
||||
return node.Guid
|
||||
}
|
||||
|
||||
func (s *NodeService) GetById(id int) (*model.Node, error) {
|
||||
@@ -456,7 +555,9 @@ func (s *NodeService) Delete(id int) error {
|
||||
return common.NewError(fmt.Sprintf("cannot delete node: %d inbound(s) still attached to it; detach or delete them first", attached))
|
||||
}
|
||||
// Capture the node's guid before deleting the row so we can drop its per-node
|
||||
// IP attribution (NodeClientIp is keyed by guid, not node id).
|
||||
// IP attribution. NodeClientIp is keyed by the node's attribution key, which
|
||||
// is its guid normally but its node-unique key for a cloned/ambiguous-guid
|
||||
// node (see effectiveNodeKey) — so we purge both below.
|
||||
var guid string
|
||||
var n model.Node
|
||||
if err := db.Select("guid").Where("id = ?", id).First(&n).Error; err == nil {
|
||||
@@ -470,10 +571,12 @@ func (s *NodeService) Delete(id int) error {
|
||||
if err := tx.Where("node_id = ?", id).Delete(&model.NodeClientTraffic{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
guids := []string{synthNodeGuid(id)}
|
||||
if guid != "" {
|
||||
if err := tx.Where("node_guid = ?", guid).Delete(&model.NodeClientIp{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
guids = append(guids, guid)
|
||||
}
|
||||
if err := tx.Where("node_guid IN ?", guids).Delete(&model.NodeClientIp{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Where("id = ?", id).Delete(&model.Node{}).Error
|
||||
}); err != nil {
|
||||
@@ -593,6 +696,7 @@ func (s *NodeService) UpdateHeartbeat(id int, p HeartbeatPatch) error {
|
||||
// failed probe) reports none, so the stable identity survives blips.
|
||||
if p.Guid != "" {
|
||||
updates["guid"] = p.Guid
|
||||
s.warnOnDuplicateGuid(id, p.Guid)
|
||||
}
|
||||
if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
|
||||
return err
|
||||
@@ -607,6 +711,30 @@ func (s *NodeService) UpdateHeartbeat(id int, p HeartbeatPatch) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// warnedDupGuid remembers the (nodeID -> guid) pairs already warned about so a
|
||||
// cloned-server collision is logged once, not every heartbeat.
|
||||
var warnedDupGuid sync.Map
|
||||
|
||||
// warnOnDuplicateGuid logs once when a node reports a panelGuid already held by
|
||||
// another node or by the master itself (the cloned-server footgun). Attribution
|
||||
// still works — it falls back to node-unique keys — but the operator should
|
||||
// regenerate the duplicate panelGuid to restore real identity and per-node IP
|
||||
// attribution. Re-arms if the collision later clears.
|
||||
func (s *NodeService) warnOnDuplicateGuid(id int, guid string) {
|
||||
var clash int64
|
||||
database.GetDB().Model(&model.Node{}).Where("guid = ? AND id <> ?", guid, id).Count(&clash)
|
||||
masterGuid, _ := (&SettingService{}).GetPanelGuid()
|
||||
if clash == 0 && guid != masterGuid {
|
||||
warnedDupGuid.Delete(id)
|
||||
return
|
||||
}
|
||||
if prev, ok := warnedDupGuid.Load(id); ok && prev == guid {
|
||||
return
|
||||
}
|
||||
warnedDupGuid.Store(id, guid)
|
||||
logger.Warningf("node %d reports panelGuid %s already used by another node or the master (cloned server?) — regenerate it on that node so online and IP attribution stay per-node", id, guid)
|
||||
}
|
||||
|
||||
func (s *NodeService) MarkNodeDirty(id int) error {
|
||||
if id <= 0 {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user