mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-06-28 16:44:21 +00:00
adc64bb804
* 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.
163 lines
5.7 KiB
Go
163 lines
5.7 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
// Cloned node servers ship an identical panelGuid in their copied settings, and
|
|
// a node cloned from the master shares the master's own GUID. effectiveNodeGuid
|
|
// must keep each physical node in its own attribution bucket by falling back to
|
|
// the node-unique id for both collision kinds, while leaving a uniquely-named
|
|
// node on its real GUID and never folding transitive (Id 0) nodes.
|
|
func TestEffectiveNodeGuid_DisambiguatesAmbiguousGuids(t *testing.T) {
|
|
nodes := []*model.Node{
|
|
{Id: 1, Guid: "dup"},
|
|
{Id: 2, Guid: "dup"},
|
|
{Id: 3, Guid: "uniq"},
|
|
{Id: 4, Guid: ""},
|
|
{Id: 5, Guid: "master"},
|
|
{Id: 0, Guid: "transitive"},
|
|
}
|
|
ambiguous := ambiguousNodeGuids(nodes, "master")
|
|
|
|
if _, ok := ambiguous["dup"]; !ok {
|
|
t.Fatalf("dup must be flagged ambiguous, got %v", ambiguous)
|
|
}
|
|
if _, ok := ambiguous["master"]; !ok {
|
|
t.Fatalf("a node sharing the master GUID must be flagged, got %v", ambiguous)
|
|
}
|
|
if _, ok := ambiguous["uniq"]; ok {
|
|
t.Fatalf("uniq must not be flagged, got %v", ambiguous)
|
|
}
|
|
if _, ok := ambiguous["transitive"]; ok {
|
|
t.Fatalf("transitive (Id 0) must not count, got %v", ambiguous)
|
|
}
|
|
|
|
cases := map[*model.Node]string{
|
|
nodes[0]: "node:1",
|
|
nodes[1]: "node:2",
|
|
nodes[2]: "uniq",
|
|
nodes[3]: "node:4",
|
|
nodes[4]: "node:5",
|
|
nodes[5]: "transitive",
|
|
}
|
|
for n, want := range cases {
|
|
if got := effectiveNodeGuid(n, ambiguous); got != want {
|
|
t.Errorf("effectiveNodeGuid(Id=%d, Guid=%q) = %q, want %q", n.Id, n.Guid, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// effectiveNodeKey (the no-preloaded-list variant used by the write paths) must
|
|
// agree with the slice helper: fall back to the node-unique id when a GUID is
|
|
// shared with another node or with the master, else keep the real GUID.
|
|
func TestEffectiveNodeKey_FallsBackOnCollision(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
selfGuid, _ := (&SettingService{}).GetPanelGuid()
|
|
if selfGuid == "" {
|
|
t.Fatal("expected a panel guid")
|
|
}
|
|
|
|
mk := func(id int, name, guid string) *model.Node {
|
|
n := &model.Node{Id: id, Name: name, Address: fmt.Sprintf("10.0.0.%d", id), Port: 2053, ApiToken: "t", Guid: guid, Status: "online"}
|
|
if err := db.Create(n).Error; err != nil {
|
|
t.Fatalf("create %s: %v", name, err)
|
|
}
|
|
return n
|
|
}
|
|
dupA := mk(1, "a", "shared")
|
|
mk(2, "b", "shared")
|
|
uniq := mk(3, "c", "solo")
|
|
masterClone := mk(4, "d", selfGuid)
|
|
|
|
if got := effectiveNodeKey(dupA); got != "node:1" {
|
|
t.Errorf("node-node collision: got %q, want node:1", got)
|
|
}
|
|
if got := effectiveNodeKey(uniq); got != "solo" {
|
|
t.Errorf("unique node: got %q, want solo", got)
|
|
}
|
|
if got := effectiveNodeKey(masterClone); got != "node:4" {
|
|
t.Errorf("master collision: got %q, want node:4", got)
|
|
}
|
|
}
|
|
|
|
// recountByGuid must split per-node counts even when two direct nodes share a
|
|
// GUID and their inbounds still carry that shared GUID as origin (pre-backfill).
|
|
func TestRecountByGuid_SplitsClonedNodesWithSharedGuid(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
svc := NodeService{}
|
|
selfGuid, _ := (&SettingService{}).GetPanelGuid()
|
|
|
|
n1 := &model.Node{Id: 1, Name: "A", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "dup", Status: "online"}
|
|
n2 := &model.Node{Id: 2, Name: "B", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "dup", Status: "online"}
|
|
n3 := &model.Node{Id: 3, Name: "C", Address: "10.0.0.3", Port: 2053, ApiToken: "t", Guid: "uniq", Status: "online"}
|
|
for _, n := range []*model.Node{n1, n2, n3} {
|
|
if err := db.Create(n).Error; err != nil {
|
|
t.Fatalf("create node %s: %v", n.Name, err)
|
|
}
|
|
}
|
|
|
|
id1, id2, id3 := 1, 2, 3
|
|
inbounds := []*model.Inbound{
|
|
{Tag: "a", Port: 1001, Protocol: model.VLESS, Settings: `{"clients":[]}`, Enable: true, NodeID: &id1, OriginNodeGuid: "dup"},
|
|
{Tag: "b", Port: 1002, Protocol: model.VLESS, Settings: `{"clients":[]}`, Enable: true, NodeID: &id2, OriginNodeGuid: "dup"},
|
|
{Tag: "c", Port: 1003, Protocol: model.VLESS, Settings: `{"clients":[]}`, Enable: true, NodeID: &id3, OriginNodeGuid: "uniq"},
|
|
}
|
|
for _, ib := range inbounds {
|
|
if err := db.Create(ib).Error; err != nil {
|
|
t.Fatalf("create inbound %s: %v", ib.Tag, err)
|
|
}
|
|
}
|
|
|
|
nodes := []*model.Node{n1, n2, n3}
|
|
svc.recountByGuid(nodes, selfGuid)
|
|
|
|
if n1.InboundCount != 1 || n2.InboundCount != 1 {
|
|
t.Errorf("cloned nodes must not share inbound counts: n1=%d n2=%d, want 1,1", n1.InboundCount, n2.InboundCount)
|
|
}
|
|
if n3.InboundCount != 1 {
|
|
t.Errorf("unique node InboundCount = %d, want 1", n3.InboundCount)
|
|
}
|
|
}
|
|
|
|
// A cloned node's IP-attribution subtree must be stored under its node-unique
|
|
// key, so a second clone sharing the GUID can't overwrite it in node_client_ips.
|
|
func TestMergeClientIpsByGuid_RemapsClonedNodeSubtree(t *testing.T) {
|
|
setupClientIpTestDB(t)
|
|
db := database.GetDB()
|
|
svc := &InboundService{}
|
|
now := time.Now().Unix()
|
|
|
|
n1 := &model.Node{Id: 1, Name: "A", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "dup", Status: "online"}
|
|
n2 := &model.Node{Id: 2, Name: "B", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "dup", Status: "online"}
|
|
for _, n := range []*model.Node{n1, n2} {
|
|
if err := db.Create(n).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
}
|
|
|
|
if err := svc.MergeClientIpsByGuid(n1, map[string]map[string][]model.ClientIpEntry{
|
|
"dup": {"u@x": {{IP: "1.1.1.1", Timestamp: now}}},
|
|
}); err != nil {
|
|
t.Fatalf("merge n1: %v", err)
|
|
}
|
|
|
|
var rows []model.NodeClientIp
|
|
if err := db.Find(&rows).Error; err != nil {
|
|
t.Fatalf("load rows: %v", err)
|
|
}
|
|
if len(rows) != 1 {
|
|
t.Fatalf("want 1 attribution row, got %d", len(rows))
|
|
}
|
|
if rows[0].NodeGuid != "node:1" {
|
|
t.Errorf("cloned node IPs must be stored under node-unique key, got %q", rows[0].NodeGuid)
|
|
}
|
|
}
|