2026-07-11 14:17:57 -07:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-31 17:05:48 -07:00
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
2026-07-11 14:17:57 -07:00
|
|
|
"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/entity"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSetRemoteTraffic_AdoptsNodeHostRows(t *testing.T) {
|
|
|
|
|
setupConflictDB(t)
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
|
|
|
|
|
const nodeID = 6
|
|
|
|
|
if err := db.Create(&model.Node{
|
|
|
|
|
Id: nodeID,
|
|
|
|
|
Name: "host-node",
|
|
|
|
|
Address: "10.0.0.6",
|
|
|
|
|
Port: 2053,
|
|
|
|
|
ApiToken: "t",
|
|
|
|
|
Guid: "host-node-guid",
|
|
|
|
|
}).Error; err != nil {
|
|
|
|
|
t.Fatalf("create node: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snap := &runtime.TrafficSnapshot{
|
|
|
|
|
Inbounds: []*model.Inbound{{
|
|
|
|
|
Id: 77,
|
|
|
|
|
Tag: "host-adopt-443",
|
|
|
|
|
Enable: true,
|
|
|
|
|
Port: 443,
|
|
|
|
|
Protocol: model.VLESS,
|
|
|
|
|
Settings: `{"clients":[]}`,
|
|
|
|
|
}},
|
|
|
|
|
HostGroups: []*entity.HostGroup{{
|
|
|
|
|
GroupId: "g-node",
|
|
|
|
|
InboundIds: []int{77, 99},
|
|
|
|
|
Hosts: []string{"cdn.example.com:8443"},
|
|
|
|
|
Remark: "cdn",
|
|
|
|
|
Security: "tls",
|
|
|
|
|
Sni: "sni.example.com",
|
|
|
|
|
Fingerprint: "firefox",
|
|
|
|
|
}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
svc := InboundService{}
|
fix(node): stop stale expiry sync from undoing client extensions (#6228) (#6231)
* 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>
2026-08-23 14:17:56 -07:00
|
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false, false); err != nil {
|
2026-07-11 14:17:57 -07:00
|
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var central model.Inbound
|
|
|
|
|
if err := db.Where("tag = ?", "host-adopt-443").First(¢ral).Error; err != nil {
|
|
|
|
|
t.Fatalf("load adopted inbound: %v", err)
|
|
|
|
|
}
|
|
|
|
|
var hosts []model.Host
|
|
|
|
|
if err := db.Where("inbound_id = ?", central.Id).Find(&hosts).Error; err != nil {
|
|
|
|
|
t.Fatalf("load adopted hosts: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(hosts) != 1 {
|
|
|
|
|
t.Fatalf("adopted host rows = %d, want 1", len(hosts))
|
|
|
|
|
}
|
|
|
|
|
h := hosts[0]
|
|
|
|
|
if h.GroupId != "g-node" || h.Address != "cdn.example.com" || h.Port != 8443 ||
|
|
|
|
|
h.Security != "tls" || h.Sni != "sni.example.com" || h.Fingerprint != "firefox" || h.Remark != "cdn" {
|
|
|
|
|
t.Fatalf("adopted host mismatch: %+v", h)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var total int64
|
|
|
|
|
if err := db.Model(&model.Host{}).Count(&total).Error; err != nil {
|
|
|
|
|
t.Fatalf("count hosts: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if total != 1 {
|
|
|
|
|
t.Fatalf("total host rows = %d, want 1 (group member for un-adopted node inbound 99 must not materialize)", total)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-31 16:36:45 -07:00
|
|
|
|
|
|
|
|
func TestSetRemoteTraffic_SweptInboundTakesItsDependentsAlong(t *testing.T) {
|
|
|
|
|
setupConflictDB(t)
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
|
|
|
|
|
const nodeID = 6
|
|
|
|
|
if err := db.Create(&model.Node{Id: nodeID, Name: "host-node", Address: "10.0.0.6", Port: 2053, ApiToken: "t", Guid: "host-node-guid"}).Error; err != nil {
|
|
|
|
|
t.Fatalf("create node: %v", err)
|
|
|
|
|
}
|
|
|
|
|
inbound := func(id, port int, tag string) *model.Inbound {
|
|
|
|
|
return &model.Inbound{Id: id, Tag: tag, Enable: true, Port: port, Protocol: model.VLESS, Settings: `{"clients":[]}`}
|
|
|
|
|
}
|
|
|
|
|
group := func(groupId string, inboundId int) *entity.HostGroup {
|
|
|
|
|
return &entity.HostGroup{GroupId: groupId, InboundIds: []int{inboundId}, Hosts: []string{"cdn.example.com:8443"}, Remark: "cdn"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
svc := InboundService{}
|
|
|
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, &runtime.TrafficSnapshot{
|
|
|
|
|
Inbounds: []*model.Inbound{inbound(1, 443, "swept-443"), inbound(2, 444, "kept-444")},
|
|
|
|
|
HostGroups: []*entity.HostGroup{group("g-swept", 1), group("g-kept", 2)},
|
|
|
|
|
}, false, false); err != nil {
|
|
|
|
|
t.Fatalf("adopting merge: %v", err)
|
|
|
|
|
}
|
|
|
|
|
swept, kept := loadInboundByTag(t, "swept-443"), loadInboundByTag(t, "kept-444")
|
|
|
|
|
balancer := &model.SubBalancer{Remark: "both", InboundIds: []int{swept.Id, kept.Id}, Enabled: true}
|
|
|
|
|
if err := db.Create(balancer).Error; err != nil {
|
|
|
|
|
t.Fatalf("create balancer: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, &runtime.TrafficSnapshot{
|
|
|
|
|
Inbounds: []*model.Inbound{inbound(2, 444, "kept-444")},
|
|
|
|
|
}, false, false); err != nil {
|
|
|
|
|
t.Fatalf("sweeping merge: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var groups []string
|
|
|
|
|
if err := db.Model(&model.Host{}).Order("group_id").Pluck("group_id", &groups).Error; err != nil {
|
|
|
|
|
t.Fatalf("load hosts: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(groups) != 1 || groups[0] != "g-kept" {
|
|
|
|
|
t.Fatalf("host groups after the sweep = %v, want [g-kept]", groups)
|
|
|
|
|
}
|
|
|
|
|
if err := db.First(balancer, balancer.Id).Error; err != nil {
|
|
|
|
|
t.Fatalf("reload balancer: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(balancer.InboundIds) != 1 || balancer.InboundIds[0] != kept.Id {
|
|
|
|
|
t.Fatalf("balancer members after the sweep = %v, want [%d]", balancer.InboundIds, kept.Id)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-31 17:05:48 -07:00
|
|
|
|
|
|
|
|
// floodSnapshot reports inboundCount new inbounds (ids and ports from firstPort)
|
|
|
|
|
// and makes every group a member of all of them.
|
|
|
|
|
func floodSnapshot(tagPrefix string, firstPort, inboundCount int, groups ...*entity.HostGroup) *runtime.TrafficSnapshot {
|
|
|
|
|
snap := &runtime.TrafficSnapshot{HostGroups: groups}
|
|
|
|
|
ids := make([]int, inboundCount)
|
|
|
|
|
for i := range ids {
|
|
|
|
|
ids[i] = firstPort + i
|
|
|
|
|
snap.Inbounds = append(snap.Inbounds, &model.Inbound{
|
|
|
|
|
Id: firstPort + i, Tag: fmt.Sprintf("%s-%d", tagPrefix, i), Enable: true, Port: firstPort + i, Protocol: model.VLESS, Settings: `{"clients":[]}`,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
for _, g := range groups {
|
|
|
|
|
g.InboundIds = ids
|
|
|
|
|
}
|
|
|
|
|
return snap
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func countHostRows(t *testing.T, query string, args ...any) int64 {
|
|
|
|
|
t.Helper()
|
|
|
|
|
var n int64
|
|
|
|
|
if err := database.GetDB().Model(&model.Host{}).Where(query, args...).Count(&n).Error; err != nil {
|
|
|
|
|
t.Fatalf("count hosts: %v", err)
|
|
|
|
|
}
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A group is stored once per address and per inbound, so a small hosts/list answer
|
|
|
|
|
// must not be able to write an unbounded number of rows or bytes, in one merge or many.
|
|
|
|
|
func TestSetRemoteTraffic_BoundsAdoptedHostRows(t *testing.T) {
|
|
|
|
|
setupConflictDB(t)
|
|
|
|
|
|
|
|
|
|
const nodeID = 7
|
|
|
|
|
if err := database.GetDB().Create(&model.Node{
|
|
|
|
|
Id: nodeID, Name: "flood-node", Address: "10.0.0.7", Port: 2053, ApiToken: "t", Guid: "flood-node-guid",
|
|
|
|
|
}).Error; err != nil {
|
|
|
|
|
t.Fatalf("create node: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addresses := make([]string, 50)
|
|
|
|
|
for i := range addresses {
|
|
|
|
|
addresses[i] = fmt.Sprintf("h%d.example", i)
|
|
|
|
|
}
|
|
|
|
|
snap := floodSnapshot("flood", 30000, 100,
|
|
|
|
|
&entity.HostGroup{GroupId: "g-big", Hosts: []string{"cdn.example"}, Remark: "big", Sni: strings.Repeat("s", 100_000)},
|
|
|
|
|
&entity.HostGroup{GroupId: "g-many", Hosts: addresses, Remark: "many", Sni: "sni.example"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
svc := InboundService{}
|
|
|
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false, false); err != nil {
|
|
|
|
|
t.Fatalf("setRemoteTrafficLocked: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if big := countHostRows(t, "group_id = ?", "g-big"); big != 0 {
|
|
|
|
|
t.Fatalf("a 100000-byte group was stored %d times", big)
|
|
|
|
|
}
|
|
|
|
|
if total := countHostRows(t, "1 = 1"); total != maxAdoptedHostRows {
|
|
|
|
|
t.Fatalf("one merge adopted %d host rows, want %d", total, maxAdoptedHostRows)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
more := floodSnapshot("more", 31000, 10, &entity.HostGroup{GroupId: "g-more", Hosts: addresses, Remark: "more"})
|
|
|
|
|
more.Inbounds = append(more.Inbounds, snap.Inbounds...)
|
|
|
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, more, false, false); err != nil {
|
|
|
|
|
t.Fatalf("second setRemoteTrafficLocked: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if total := countHostRows(t, "1 = 1"); total != maxAdoptedHostRows {
|
|
|
|
|
t.Fatalf("a second merge raised the node's host rows to %d, want %d", total, maxAdoptedHostRows)
|
|
|
|
|
}
|
|
|
|
|
}
|