mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-24 20:07:13 +00:00
6f7a305239
* fix(node): stop stale expiry sync from undoing client extensions (#6228) After an expired client is extended on the master, a lagging node could overwrite client_traffics with an older absolute expiry and latch enable=false. Reject older absolute expiries on merge, ignore expiry-stale disables when the master is not over quota, lift stale lifecycle fields out of adopted settings, stamp reconcile fingerprints from the pre-lift node blob, and mark the node dirty so the next tick re-pushes. * fix(node): lockstep client_traffics expiry/enable SQL with review fixes (#6228) Make expiry merge keep any master absolute (node only activates when master is unset/duration). Include this tick's up/down deltas in the enable stale-disable quota check so a crossing-tick disable is not dropped. * fix(node): add settings absolute helper for renew/lift guards (#6228) Expose settingsClientAbsoluteExpiry so traffic merge can tell a real node auto-renew (settings+stats later) from lagging ClientStats after a master shorten. Trim lift godoc to the invariant. * fix(node): authority-aware lifecycle merge for multi-node sync (#6228) While config_dirty, accumulate traffic only — do not adopt node expiry/enable/total/reset (and preserve dipped baselines so a false renew cannot fire after clear). On clean ticks, master absolute expiry wins; node auto-renew still goes through nodeClientRenewed when settings also show the later deadline. Defer settings lifecycle lift until after traffic deltas land, align SyncInbound via applyMasterClientLifecycle, and avoid re-MarkNodeDirty when already dirty. * fix(node): clear config_dirty only after the post-reconcile traffic merge (#6228) After a successful ReconcileNode, keep the node dirty through the same tick's SetRemoteTraffic so lagging ClientStats cannot clobber the just-pushed master lifecycle, then ClearNodeDirty. * test(node): cover dirty-gate, master-absolute, and renew false-positives (#6228) Add regressions for extend/shorten while dirty, clean-sibling shorten, settings vs lagging disable, renew recovery after dirty, renew with matching settings, and shorten+Reset lagging stats not treated as renew. * fix(node): address the review findings on the lifecycle merge (#6228) The automated review on #6231 flagged a blocking regression and six smaller issues. All of them are fixed here. Blocking: making the master's absolute expiry always win left nodeClientRenewed as the only channel for a node-side auto-renew, and that required a counter dip. A client that used no traffic in the period never dips, so its renewal was dropped, the master kept the expired deadline and disableInvalidClients removed it with no way back (master-side autoRenewClients skips node inbounds). The node bumps reset_count on every renewal, so that counter is now an independent renewal signal and is persisted with the renewal so it keeps converging. The deferred ClearNodeDirty made every reconcile-success tick merge in dirty mode, which suppressed inbound adoption, new client_traffics rows, the orphan sweeps and the whole SyncInbound record loop -- and left the node dirty forever whenever SetRemoteTraffic errored. The clear goes back to where it was; a separate justPushed flag now freezes only the client lifecycle merge for the tick whose push just landed. staleNodeDisable only recognised a lagging disable by an older expiry, so a quota top-up (raise totalGB, leave the expiry alone) was re-latched to disabled by the next lagging snapshot -- the #6228 symptom on a second axis. The reviewer's suggestion of dropping the expiry precondition outright fails TestNodeQuotaDisable_SameExpiryStillLatches, because the master's own counters legitimately sit below a node's after a seeded-at-zero adoption. nodeDisableIsStale instead compares the limits the node judged the client against with the master's own: matching limits mean a genuine verdict that still latches (#4917), differing limits mean the node has not seen the master's change yet. It also now measures the master deadline against wall-clock now, so an expired master row no longer looks "extended" merely because the node's copy is older still. Also: the settings lift now writes enable in both directions, so a blob fetched before a master disable cannot carry enable=true back into central settings and on to the node; the renewal guard parses the inbound settings once per inbound instead of once per renewing client; the adoption loop only writes settings when they actually changed; and two comments that described mechanisms the code does not use were corrected. The test deadlines are now relative to the run: the merge compares against now, so fixed timestamps would have rotted into the wrong side of it. --------- Co-authored-by: mrchatam <287639636+mrchatam@users.noreply.github.com> Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
451 lines
14 KiB
Go
451 lines
14 KiB
Go
package service
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
"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"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
)
|
|
|
|
func assertStringSet(t *testing.T, label string, got, want []string) {
|
|
t.Helper()
|
|
g := append([]string(nil), got...)
|
|
w := append([]string(nil), want...)
|
|
slices.Sort(g)
|
|
slices.Sort(w)
|
|
if !slices.Equal(g, w) {
|
|
t.Fatalf("%s = %v, want %v", label, got, want)
|
|
}
|
|
}
|
|
|
|
// #4983: a synced inbound's OriginNodeGuid must point at the panel that
|
|
// physically hosts it. A node's own local inbound (empty origin in its
|
|
// snapshot) is attributed to the node's own GUID; an inbound the node forwards
|
|
// from its own sub-node (non-empty origin) keeps that deeper GUID across the
|
|
// hop — so a chained Node1->Node2->Node3 attributes Node3's inbounds to Node3.
|
|
func TestSetRemoteTraffic_AttributesOriginNodeGuid(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
|
|
const nodeID = 1
|
|
if err := db.Create(&model.Node{
|
|
Id: nodeID,
|
|
Name: "node2",
|
|
Address: "10.0.0.2",
|
|
Port: 2053,
|
|
ApiToken: "t",
|
|
Guid: "node2-guid",
|
|
}).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
|
|
snap := &runtime.TrafficSnapshot{
|
|
Inbounds: []*model.Inbound{
|
|
{ // node2's own local inbound — reports no origin
|
|
Tag: "in-443-tcp",
|
|
Enable: true,
|
|
Port: 443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
},
|
|
{ // forwarded from node2's sub-node (node3) — carries node3's guid
|
|
Tag: "in-8443-tcp",
|
|
Enable: true,
|
|
Port: 8443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
OriginNodeGuid: "node3-guid",
|
|
},
|
|
},
|
|
}
|
|
|
|
svc := InboundService{}
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false, false); err != nil {
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
}
|
|
|
|
origin := func(tag string) string {
|
|
var ib model.Inbound
|
|
if err := db.Where("tag = ?", tag).First(&ib).Error; err != nil {
|
|
t.Fatalf("load inbound %q: %v", tag, err)
|
|
}
|
|
return ib.OriginNodeGuid
|
|
}
|
|
|
|
if og := origin("in-443-tcp"); og != "node2-guid" {
|
|
t.Fatalf("local inbound origin = %q, want node2-guid (the node's own GUID)", og)
|
|
}
|
|
if og := origin("in-8443-tcp"); og != "node3-guid" {
|
|
t.Fatalf("forwarded inbound origin = %q, want node3-guid (kept across the hop)", og)
|
|
}
|
|
}
|
|
|
|
// A cloned node reports its OWN inbound with its own (duplicated) panelGuid as
|
|
// the origin. That must be remapped to the node-unique key, not stored verbatim
|
|
// — otherwise origin_node_guid keeps the shared GUID while online is keyed by
|
|
// the node-unique key, and the inbound page reads an empty bucket (shows
|
|
// offline). A genuinely forwarded sub-node GUID is still kept across the hop.
|
|
func TestSetRemoteTraffic_RemapsClonedNodeOwnGuidOrigin(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
|
|
// Two nodes share one panelGuid (cloned servers).
|
|
for _, n := range []*model.Node{
|
|
{Id: 1, Name: "a", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "dup"},
|
|
{Id: 2, Name: "b", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "dup"},
|
|
} {
|
|
if err := db.Create(n).Error; err != nil {
|
|
t.Fatalf("create node %s: %v", n.Name, err)
|
|
}
|
|
}
|
|
|
|
snap := &runtime.TrafficSnapshot{
|
|
Inbounds: []*model.Inbound{
|
|
{ // node 1's OWN inbound, reporting its own (shared) panelGuid as origin
|
|
Tag: "own-443-tcp",
|
|
Enable: true,
|
|
Port: 443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
OriginNodeGuid: "dup",
|
|
},
|
|
{ // forwarded from a sub-node with a distinct guid — kept across the hop
|
|
Tag: "fwd-8443-tcp",
|
|
Enable: true,
|
|
Port: 8443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
OriginNodeGuid: "child-guid",
|
|
},
|
|
},
|
|
}
|
|
|
|
svc := InboundService{}
|
|
if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil {
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
}
|
|
|
|
origin := func(tag string) string {
|
|
var ib model.Inbound
|
|
if err := db.Where("tag = ?", tag).First(&ib).Error; err != nil {
|
|
t.Fatalf("load inbound %q: %v", tag, err)
|
|
}
|
|
return ib.OriginNodeGuid
|
|
}
|
|
|
|
if og := origin("own-443-tcp"); og != "node:1" {
|
|
t.Fatalf("cloned node's own inbound origin = %q, want node:1 (remapped from shared GUID)", og)
|
|
}
|
|
if og := origin("fwd-8443-tcp"); og != "child-guid" {
|
|
t.Fatalf("forwarded inbound origin = %q, want child-guid (kept across the hop)", og)
|
|
}
|
|
}
|
|
|
|
func TestSetRemoteTraffic_RemapsActiveInboundTreeAndCentralTags(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
|
|
previousProcess, previousResult := xrayState.snapshot()
|
|
process := xray.NewTestProcess(nil, "")
|
|
xrayState.replace(process)
|
|
t.Cleanup(func() {
|
|
xrayState.mu.Lock()
|
|
xrayState.process = previousProcess
|
|
xrayState.result = previousResult
|
|
xrayState.mu.Unlock()
|
|
})
|
|
|
|
// Force the remote inbound to be adopted with an n1- prefix on the master:
|
|
// the active-inbound tree still arrives with the node-local tag.
|
|
if err := db.Create(&model.Inbound{
|
|
Tag: "shared-tag", Enable: true, Port: 1000, Protocol: model.VLESS, Settings: `{"clients":[]}`,
|
|
}).Error; err != nil {
|
|
t.Fatalf("create local conflicting inbound: %v", err)
|
|
}
|
|
|
|
// Two cloned nodes share the same panelGuid, so the node's own active tags
|
|
// must be keyed by node:1 instead of the duplicated GUID.
|
|
for _, n := range []*model.Node{
|
|
{Id: 1, Name: "a", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "dup"},
|
|
{Id: 2, Name: "b", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "dup"},
|
|
} {
|
|
if err := db.Create(n).Error; err != nil {
|
|
t.Fatalf("create node %s: %v", n.Name, err)
|
|
}
|
|
}
|
|
|
|
snap := &runtime.TrafficSnapshot{
|
|
Inbounds: []*model.Inbound{{
|
|
Tag: "shared-tag",
|
|
Enable: true,
|
|
Port: 8443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
}},
|
|
ActiveInboundTree: map[string][]string{
|
|
"dup": {"shared-tag"},
|
|
},
|
|
}
|
|
|
|
svc := InboundService{}
|
|
if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil {
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
}
|
|
|
|
merged := process.GetMergedActiveInboundTrees()
|
|
assertStringSet(t, "active node:1", merged["node:1"], []string{"n1-shared-tag"})
|
|
if _, ok := merged["dup"]; ok {
|
|
t.Fatalf("cloned active-inbound subtree must not stay under shared GUID: %v", merged)
|
|
}
|
|
}
|
|
|
|
func TestSetRemoteTraffic_NormalizesForwardedActiveInboundSubtreeTags(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
|
|
previousProcess, previousResult := xrayState.snapshot()
|
|
process := xray.NewTestProcess(nil, "")
|
|
xrayState.replace(process)
|
|
t.Cleanup(func() {
|
|
xrayState.mu.Lock()
|
|
xrayState.process = previousProcess
|
|
xrayState.result = previousResult
|
|
xrayState.mu.Unlock()
|
|
})
|
|
|
|
for _, tag := range []string{"own-tag", "child-tag"} {
|
|
if err := db.Create(&model.Inbound{
|
|
Tag: tag, Enable: true, Port: 1000, Protocol: model.VLESS, Settings: `{"clients":[]}`,
|
|
}).Error; err != nil {
|
|
t.Fatalf("create local conflicting inbound %q: %v", tag, err)
|
|
}
|
|
}
|
|
if err := db.Create(&model.Node{
|
|
Id: 1, Name: "node2", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "node2-guid",
|
|
}).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
|
|
snap := &runtime.TrafficSnapshot{
|
|
Inbounds: []*model.Inbound{
|
|
{
|
|
Tag: "own-tag",
|
|
Enable: true,
|
|
Port: 8443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
},
|
|
{
|
|
Tag: "child-tag",
|
|
Enable: true,
|
|
Port: 9443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
OriginNodeGuid: "child-guid",
|
|
},
|
|
},
|
|
ActiveInboundTree: map[string][]string{
|
|
"node2-guid": {"own-tag"},
|
|
"child-guid": {"child-tag"},
|
|
},
|
|
}
|
|
|
|
svc := InboundService{}
|
|
if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil {
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
}
|
|
|
|
merged := process.GetMergedActiveInboundTrees()
|
|
assertStringSet(t, "direct node active tags", merged["node2-guid"], []string{"n1-own-tag"})
|
|
assertStringSet(t, "forwarded child active tags", merged["child-guid"], []string{"n1-child-tag"})
|
|
}
|
|
|
|
func TestSetRemoteTraffic_DropsForeignActiveInboundGuid(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
|
|
previousProcess, previousResult := xrayState.snapshot()
|
|
process := xray.NewTestProcess(nil, "")
|
|
xrayState.replace(process)
|
|
t.Cleanup(func() {
|
|
xrayState.mu.Lock()
|
|
xrayState.process = previousProcess
|
|
xrayState.result = previousResult
|
|
xrayState.mu.Unlock()
|
|
})
|
|
|
|
for _, n := range []*model.Node{
|
|
{Id: 1, Name: "node-a", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "node-a-guid"},
|
|
{Id: 2, Name: "node-b", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "node-b-guid"},
|
|
} {
|
|
if err := db.Create(n).Error; err != nil {
|
|
t.Fatalf("create node %s: %v", n.Name, err)
|
|
}
|
|
}
|
|
|
|
snap := &runtime.TrafficSnapshot{
|
|
Inbounds: []*model.Inbound{{
|
|
Tag: "own-tag",
|
|
Enable: true,
|
|
Port: 8443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
}},
|
|
ActiveInboundTree: map[string][]string{
|
|
"node-a-guid": {"own-tag"},
|
|
"node-b-guid": {"foreign-tag"},
|
|
},
|
|
}
|
|
|
|
svc := InboundService{}
|
|
if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil {
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
}
|
|
|
|
merged := process.GetMergedActiveInboundTrees()
|
|
assertStringSet(t, "own active tags", merged["node-a-guid"], []string{"own-tag"})
|
|
if _, ok := merged["node-b-guid"]; ok {
|
|
t.Fatalf("foreign active-inbound subtree should be ignored: %v", merged)
|
|
}
|
|
}
|
|
|
|
// A node mid-restart can return an empty inbound list with success=true. The
|
|
// sync must NOT treat that as "delete all my inbounds" — otherwise a blip wipes
|
|
// the node's central inbounds and every client on them (what happened to the
|
|
// Germany node: 0 clients but still online).
|
|
func TestSetRemoteTraffic_EmptySnapshotKeepsCentralInbounds(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
|
|
const nodeID = 1
|
|
if err := db.Create(&model.Node{
|
|
Id: nodeID, Name: "n", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "g",
|
|
}).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
nidPtr := nodeID
|
|
if err := db.Create(&model.Inbound{
|
|
UserId: 1, NodeID: &nidPtr, Tag: "remote-in", Enable: true,
|
|
Port: 443, Protocol: model.VLESS, Settings: `{"clients":[]}`,
|
|
}).Error; err != nil {
|
|
t.Fatalf("create central inbound: %v", err)
|
|
}
|
|
|
|
// Empty snapshot — the node reported no inbounds this cycle.
|
|
svc := InboundService{}
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, &runtime.TrafficSnapshot{}, false, false); err != nil {
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
}
|
|
|
|
var count int64
|
|
if err := db.Model(&model.Inbound{}).Where("tag = ?", "remote-in").Count(&count).Error; err != nil {
|
|
t.Fatalf("count inbounds: %v", err)
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("empty snapshot must not delete the central inbound; got count = %d", count)
|
|
}
|
|
}
|
|
|
|
func TestSetRemoteTraffic_PreservesLocalShareAddressStrategy(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
|
|
const nodeID = 1
|
|
if err := db.Create(&model.Node{
|
|
Id: nodeID,
|
|
Name: "node2",
|
|
Address: "10.0.0.2",
|
|
Port: 2053,
|
|
ApiToken: "t",
|
|
Guid: "node2-guid",
|
|
}).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
|
|
nodeIDPtr := nodeID
|
|
if err := db.Create(&model.Inbound{
|
|
UserId: 1,
|
|
NodeID: &nodeIDPtr,
|
|
Tag: "remote-in",
|
|
Enable: true,
|
|
Port: 443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
ShareAddrStrategy: "custom",
|
|
ShareAddr: "edge.example.com",
|
|
}).Error; err != nil {
|
|
t.Fatalf("create central inbound: %v", err)
|
|
}
|
|
|
|
snap := &runtime.TrafficSnapshot{
|
|
Inbounds: []*model.Inbound{{
|
|
Tag: "remote-in",
|
|
Enable: true,
|
|
Port: 8443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
}},
|
|
}
|
|
|
|
svc := InboundService{}
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false, false); err != nil {
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
}
|
|
|
|
var ib model.Inbound
|
|
if err := db.Where("tag = ?", "remote-in").First(&ib).Error; err != nil {
|
|
t.Fatalf("load inbound: %v", err)
|
|
}
|
|
if ib.ShareAddrStrategy != "custom" || ib.ShareAddr != "edge.example.com" {
|
|
t.Fatalf("share address fields were overwritten: strategy=%q addr=%q", ib.ShareAddrStrategy, ib.ShareAddr)
|
|
}
|
|
if ib.Port != 8443 {
|
|
t.Fatalf("sync should still update regular remote fields; port = %d, want 8443", ib.Port)
|
|
}
|
|
}
|
|
|
|
func TestSetRemoteTraffic_DefaultsShareAddressFieldsForNewCentralInbound(t *testing.T) {
|
|
setupConflictDB(t)
|
|
db := database.GetDB()
|
|
|
|
const nodeID = 1
|
|
if err := db.Create(&model.Node{
|
|
Id: nodeID,
|
|
Name: "node2",
|
|
Address: "10.0.0.2",
|
|
Port: 2053,
|
|
ApiToken: "t",
|
|
Guid: "node2-guid",
|
|
}).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
|
|
snap := &runtime.TrafficSnapshot{
|
|
Inbounds: []*model.Inbound{{
|
|
Tag: "remote-in",
|
|
Enable: true,
|
|
Port: 8443,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[]}`,
|
|
ShareAddrStrategy: "custom",
|
|
ShareAddr: "remote.example.com",
|
|
}},
|
|
}
|
|
|
|
svc := InboundService{}
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false, false); err != nil {
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
}
|
|
|
|
var ib model.Inbound
|
|
if err := db.Where("tag = ?", "remote-in").First(&ib).Error; err != nil {
|
|
t.Fatalf("load inbound: %v", err)
|
|
}
|
|
if ib.ShareAddrStrategy != "node" || ib.ShareAddr != "" {
|
|
t.Fatalf("new central inbound share fields = (%q, %q), want (node, empty)", ib.ShareAddrStrategy, ib.ShareAddr)
|
|
}
|
|
}
|