Files
mirror-3x-ui/internal/web/service/node_unsynced_online_test.go
T
Hamed fb14c8358a fix(inbounds): distinguish unavailable online attribution (#7289)
Distinguish unavailable, pending, explicitly idle and legacy inbound online reports across REST, WebSocket and node snapshots. Require hosting-node emails and matching inbound activity; use a labeled node-wide fallback only for confirmed unsupported endpoints (HTTP 404/405).

Track Xray freshness separately from sidecar observations and prevent accounting retries from publishing online state across core replacement. Preserve partial sidecar reports through mixed-version relays using the reserved nonempty NUL marker. Retain GUID/tag remapping and attached-client filtering.

Validated on the reviewed head: all 12 CI jobs and Docs CI passed, including 2,332 frontend tests, mandatory PostgreSQL attribution regressions, race checks, Xray/node E2E and all four master/node version combinations. Exact client attribution across simultaneously active shared-client inbounds remains an estimate; older nodes may omit idle GUIDs during rollout.

Fixes #7283.
2026-12-03 02:37:40 +03:30

153 lines
6.4 KiB
Go

package service
import (
"fmt"
"reflect"
"strconv"
"strings"
"testing"
"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"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
func useOnlineTestProcess(t *testing.T) *xray.Process {
t.Helper()
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()
})
return process
}
// Only a failed snapshot fetch used to clear a node's online set, so a node the
// sync stopped reaching (disabled, marked offline, deleted) kept its clients online.
func TestRetainSyncedNodeOnlineClientsDropsUnsyncedNodes(t *testing.T) {
setupConflictDB(t)
process := useOnlineTestProcess(t)
svc := InboundService{}
for id := 1; id <= 4; id++ {
guid := fmt.Sprintf("g%d", id)
svc.SetNodeOnlineTree(id, map[string][]string{guid: {guid + "@x"}})
process.SetNodeActiveInboundTree(id, map[string][]string{guid: {"in-" + guid}})
}
svc.RetainSyncedNodeOnlineClients([]*model.Node{
{Id: 1, Enable: true, Status: "online"},
{Id: 2, Enable: false, Status: "online"},
{Id: 3, Enable: true, Status: "offline"},
})
if got, want := svc.GetOnlineClientsByGuid(), map[string][]string{"g1": {"g1@x"}}; !reflect.DeepEqual(got, want) {
t.Errorf("online by guid = %v, want %v", got, want)
}
if got, want := svc.GetActiveInboundsByGuid(), map[string][]string{"g1": {"in-g1"}}; !reflect.DeepEqual(got, want) {
t.Errorf("active inbounds by guid = %v, want %v", got, want)
}
}
func TestSetRemoteTrafficFailureClearsNodeOnlineClients(t *testing.T) {
setupConflictDB(t)
useOnlineTestProcess(t)
svc := InboundService{}
svc.SetNodeOnlineTree(7, map[string][]string{"g7": {"a@x"}})
if err := database.GetDB().Exec("DROP TABLE inbounds").Error; err != nil {
t.Fatalf("drop inbounds: %v", err)
}
if _, err := svc.SetRemoteTraffic(7, &runtime.TrafficSnapshot{}, false, false); err == nil {
t.Fatal("SetRemoteTraffic succeeded without an inbounds table")
}
if got := svc.GetOnlineClientsByGuid(); len(got) != 0 {
t.Errorf("online by guid after a failed merge = %v, want none", got)
}
}
// A node may report only the clients linked to its own inbounds, under its own GUID: an
// unscoped report marked any master client online and grew without bound.
func TestSetRemoteTrafficKeepsOnlyTheNodesOwnClientsOnline(t *testing.T) {
db := initTrafficTestDB(t)
useOnlineTestProcess(t)
svc := &InboundService{}
seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Guid: "node-guid"})
createNodeInboundWithClient(t, db, 1, "keep", 41401, "kept@x")
settings := `{"clients":[{"email":"kept@x","enable":true}]}`
snap := snapshotWithClients(t, "keep", settings, xray.ClientTraffic{Email: "kept@x", Enable: true})
if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil {
t.Fatalf("seed sync: %v", err)
}
snap = snapshotWithClients(t, "keep", settings, xray.ClientTraffic{Email: "kept@x", Enable: true})
snap.OnlineTree = map[string][]string{"node-guid": {"kept@x", "master-only@x", "kept@x"}, "someone-else": {"kept@x"}}
if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil {
t.Fatalf("sync: %v", err)
}
if got, want := svc.GetOnlineClientsByGuid(), map[string][]string{"node-guid": {"kept@x"}}; !reflect.DeepEqual(got, want) {
t.Fatalf("online by guid = %v, want %v", got, want)
}
for _, legacy := range []bool{true, false} {
snap = snapshotWithClients(t, "keep", settings, xray.ClientTraffic{Email: "kept@x", Enable: true})
snap.OnlineTree = map[string][]string{"node-guid": {}}
if legacy {
snap.LegacyInboundActivity = true
} else {
snap.ActiveInboundTree = map[string][]string{"node-guid": {}}
}
if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil {
t.Fatal(err)
}
if idle := svc.GetOnlineClientsByGuid()["node-guid"]; idle == nil || len(idle) != 0 {
t.Fatalf("lost explicit empty node online list: %v", idle)
}
if tags, present := svc.GetActiveInboundsByGuid()["node-guid"]; !present || (tags == nil) != legacy {
t.Fatalf("legacy=%t: incorrect activity report %v, present=%t", legacy, tags, present)
}
}
}
// The client-IP reports feed the IP limit and fail2ban; a node must not plant rows for
// clients it does not serve, under other panels' GUIDs, or dated into the future.
func TestNodeClientIpReportsAreScopedToTheNode(t *testing.T) {
db := initTrafficTestDB(t)
svc := &InboundService{}
node := &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Guid: "node-guid"}
seedNodeRow(t, db, node)
createNodeInboundWithClient(t, db, 1, "keep", 41402, "kept@x")
settings := `{"clients":[{"email":"kept@x","enable":true}]}`
if _, err := svc.setRemoteTrafficLocked(1, snapshotWithClients(t, "keep", settings, xray.ClientTraffic{Email: "kept@x", Enable: true}), false, false); err != nil {
t.Fatalf("seed sync: %v", err)
}
future := time.Now().Add(240 * time.Hour).Unix()
entries := func(ip string) string { return fmt.Sprintf(`[{"ip":%q,"timestamp":%d}]`, ip, future) }
if err := svc.MergeNodeClientIps(1, []model.InboundClientIps{
{ClientEmail: "kept@x", Ips: entries("198.51.100.1")},
{ClientEmail: "master-only@x", Ips: entries("198.51.100.2")},
}); err != nil {
t.Fatalf("MergeNodeClientIps: %v", err)
}
var flat []model.InboundClientIps
db.Find(&flat)
if len(flat) != 1 || flat[0].ClientEmail != "kept@x" || strings.Contains(flat[0].Ips, strconv.FormatInt(future, 10)) {
t.Fatalf("stored client IPs = %+v, want only kept@x, dated no later than now", flat)
}
tree := map[string]map[string][]model.ClientIpEntry{
"node-guid": {"kept@x": {{IP: "198.51.100.3", Timestamp: future}}, "master-only@x": {{IP: "198.51.100.4", Timestamp: future}}},
"someone-else": {"kept@x": {{IP: "198.51.100.5", Timestamp: future}}},
}
if err := svc.MergeClientIpsByGuid(node, tree); err != nil {
t.Fatalf("MergeClientIpsByGuid: %v", err)
}
var rows []model.NodeClientIp
db.Find(&rows)
if len(rows) != 1 || rows[0].NodeGuid != "node-guid" || rows[0].Email != "kept@x" || strings.Contains(rows[0].Ips, strconv.FormatInt(future, 10)) {
t.Fatalf("stored per-node IPs = %+v, want only kept@x under node-guid, dated no later than now", rows)
}
}