mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 08:25:26 +03:00
fix(nodes): prevent quota overflow in fleet counts (#7323)
Complete the remaining correctness work for #7319 on top of the SQL fleet aggregation introduced by #7342. Replace the overflowing BIGINT upload-plus-download quota comparison in countNodeFleet with a guarded subtraction-based CASE. Preserve depletion precedence, unlimited quotas, missing traffic behavior, email-based traffic attribution, host deduplication, exclusion of orphan memberships, and database error propagation. Add regression coverage for signed int64 limits, negative counters, unlimited quotas and orphan memberships. Require both fleet regression tests to execute and pass in PostgreSQL CI without weakening existing checks. Validation: reviewed head e9b344a63bdf6aa945f0e4f6b3cbe933a416fabe; all 12 CI jobs passed, including both PostgreSQL fleet tests and all seven quota subcases. Independent arithmetic comparison over 1,331 boundary combinations found zero mismatches. See https://github.com/MHSanaei/3x-ui/pull/7323#issuecomment-7388723297. The green CI is for the reviewed PR head, not a fresh post-merge run. Closes #7319.
This commit is contained in:
@@ -358,6 +358,13 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: PostgreSQL node fleet count regressions
|
||||
run: |
|
||||
set -o pipefail
|
||||
go test ./internal/web/service -run '^TestNode(FleetQuotaClassificationAtInt64Limits|OverviewCountsHostMembershipsAndUniqueFleetClients)$' -count=1 -v | tee /tmp/postgres-node-fleet.log
|
||||
grep -q '^--- PASS: TestNodeFleetQuotaClassificationAtInt64Limits ' /tmp/postgres-node-fleet.log
|
||||
grep -q '^--- PASS: TestNodeOverviewCountsHostMembershipsAndUniqueFleetClients ' /tmp/postgres-node-fleet.log
|
||||
|
||||
- name: PostgreSQL schema and migration tests
|
||||
run: |
|
||||
set -o pipefail
|
||||
|
||||
@@ -98,7 +98,12 @@ func (s *NodeService) countNodeFleet(nodes []*model.Node, selfGUID string, overv
|
||||
), classified AS (
|
||||
SELECT s.fleet, s.host, CASE
|
||||
WHEN (ct.expiry_time > 0 AND ct.expiry_time <= ?)
|
||||
OR (ct.total > 0 AND ct.up + ct.down >= ct.total) THEN 'depleted'
|
||||
OR CASE
|
||||
-- Subtract a nonnegative counter from a positive quota to stay in BIGINT range.
|
||||
WHEN ct.total > 0 AND ct.down >= 0 THEN ct.up >= ct.total - ct.down
|
||||
WHEN ct.total > 0 AND ct.up >= 0 THEN ct.down >= ct.total - ct.up
|
||||
ELSE FALSE
|
||||
END THEN 'depleted'
|
||||
WHEN COALESCE(c.enable, FALSE) = FALSE THEN 'disabled'
|
||||
ELSE 'active' END AS state
|
||||
FROM scoped s JOIN clients c ON c.id = s.client_id
|
||||
|
||||
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/testpg"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
||||
)
|
||||
|
||||
func TestNodeOverviewWithoutNodesSkipsFleetReads(t *testing.T) {
|
||||
@@ -148,6 +150,7 @@ func TestNodeOverviewCountsHostMembershipsAndUniqueFleetClients(t *testing.T) {
|
||||
{ClientId: clients[0].Id, InboundId: child.Id},
|
||||
{ClientId: clients[1].Id, InboundId: child.Id},
|
||||
{ClientId: clients[1].Id, InboundId: clone.Id},
|
||||
{ClientId: math.MaxInt32, InboundId: clone.Id},
|
||||
}
|
||||
if err := db.Create(&links).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -191,6 +194,65 @@ func TestNodeOverviewCountsHostMembershipsAndUniqueFleetClients(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeFleetQuotaClassificationAtInt64Limits(t *testing.T) {
|
||||
cleanup, err := testpg.IsolatePackage("node_fleet_quota")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(cleanup)
|
||||
dbtest.InitDB(t, filepath.Join(t.TempDir(), "fleet-quota.db"))
|
||||
db := database.GetDB()
|
||||
node := model.Node{Name: "remote", Address: "192.0.2.1", Port: 2053, Guid: "remote"}
|
||||
inbound := model.Inbound{Tag: "remote", Port: 443, NodeID: &node.Id, Settings: `{"clients":[]}`}
|
||||
client := model.ClientRecord{Email: "quota@test", Enable: true}
|
||||
for _, row := range []any{&node, &inbound, &client} {
|
||||
if err := db.Create(row).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
membership := model.ClientInbound{ClientId: client.Id, InboundId: inbound.Id}
|
||||
if err := db.Create(&membership).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
traffic := xray.ClientTraffic{Email: client.Email, InboundId: inbound.Id}
|
||||
if err := db.Create(&traffic).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
up, down, total int64
|
||||
depleted int
|
||||
}{
|
||||
{"above limit", math.MaxInt64, 1, math.MaxInt64, 1},
|
||||
{"at limit", 1, math.MaxInt64 - 1, math.MaxInt64, 1},
|
||||
{"below limit", math.MaxInt64 - 1, 0, math.MaxInt64, 0},
|
||||
{"negative upload", -1, math.MaxInt64, math.MaxInt64, 0},
|
||||
{"negative download", math.MaxInt64, -1, math.MaxInt64, 0},
|
||||
{"negative underflow", math.MinInt64, -1, 1, 0},
|
||||
{"unlimited", math.MaxInt64, 1, 0, 0},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if err := db.Model(&traffic).Updates(map[string]any{"up": tc.up, "down": tc.down, "total": tc.total}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
overview, err := (&NodeService{}).GetNodeOverview(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := NodeFleetCounts{InboundCount: 1, ClientCount: 1, ActiveCount: 1 - tc.depleted, DepletedCount: tc.depleted}
|
||||
if overview.Fleet != want || len(overview.Nodes) != 1 {
|
||||
t.Fatalf("fleet counts = %+v, nodes = %d, want %+v and one node", overview.Fleet, len(overview.Nodes), want)
|
||||
}
|
||||
got := overview.Nodes[0]
|
||||
if got.ClientCount != want.ClientCount || got.ActiveCount != want.ActiveCount ||
|
||||
got.DisabledCount != 0 || got.DepletedCount != want.DepletedCount {
|
||||
t.Fatalf("unexpected remote client counts: %+v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeOverviewUsesBestDescendantObservation(t *testing.T) {
|
||||
cleanup, err := testpg.IsolatePackage("node_fleet_descendants")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user