mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-29 01:04:59 +03:00
fix(traffic): make a client reset reach every counter enforcing its quota
Invariant: a client traffic reset zeroes every counter that enforces the client's quota - the master's, each hosting node's, and the local MTProto sidecar's. A node cuts a client on its own local counters, and the master adopts that verdict when both judged the same limits (#4917). Node counters: ResetClientTraffic tried the node once and dropped a failure ("nothing replays a reset"); BulkResetTraffic, ResetAllClientTraffics and ClientService.ResetAllTraffics never told the node at all. The node kept its pre-reset usage, switched the client off again on its next tick, and the master latched that - a client shown at zero usage stayed disabled. Every reset path now queues a node_pending_resets row per hosting node in its own transaction. It is delivered right after commit (per-client endpoint up to the push threshold, bulkResetTraffic above) and replayed by the node sync ahead of its snapshot, and dropped only once the node accepted it. While one is owed, the merge takes only that client's usage from the node, not its enable or limits. Deliveries to a node are serialized so a reset is not sent twice. Sidecar quota: BulkResetTraffic, ClientService.ResetAllTraffics and auto-renew zeroed the panel counters but not mtg's own quota counter, so the sidecar kept refusing a renewed or reset MTProto client. They now reset it too, scoped to the affected clients.
This commit is contained in:
@@ -547,6 +547,11 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
centralCSByEmail[centralClientStats[i].Email] = ¢ralClientStats[i]
|
||||
}
|
||||
|
||||
owedResets, err := pendingNodeResetEmails(db, nodeID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
nodeBaselines := make(map[string]nodeTrafficCounter)
|
||||
var baselineRows []model.NodeClientTraffic
|
||||
if err := db.Model(&model.NodeClientTraffic{}).
|
||||
@@ -925,6 +930,10 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
|
||||
// Node-wide total, not this inbound's possibly-stale copy (#5274).
|
||||
canon := nodeEmailTotals[cs.Email]
|
||||
// Until the node applies a reset it owes, its verdict rests on the
|
||||
// pre-reset counters: only usage may move for this client.
|
||||
_, owed := owedResets[cs.Email]
|
||||
clientFrozen := lifecycleFrozen || owed
|
||||
|
||||
base, seen := nodeBaselines[cs.Email]
|
||||
var deltaUp, deltaDown int64
|
||||
@@ -986,18 +995,18 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
|
||||
existing := centralCSByEmail[cs.Email]
|
||||
if existing != nil {
|
||||
expiryChanged := !lifecycleFrozen && existing.ExpiryTime != mergeActivationExpiry(existing.ExpiryTime, cs.ExpiryTime)
|
||||
expiryChanged := !clientFrozen && existing.ExpiryTime != mergeActivationExpiry(existing.ExpiryTime, cs.ExpiryTime)
|
||||
// Only a real latch to disabled is structural; one-way merge never
|
||||
// re-enables from the node.
|
||||
enableChanged := !lifecycleFrozen && existing.Enable && !cs.Enable &&
|
||||
enableChanged := !clientFrozen && existing.Enable && !cs.Enable &&
|
||||
!nodeDisableIsStale(existing, cs, now, deltaUp, deltaDown)
|
||||
metaChanged := !lifecycleFrozen && (existing.Total != cs.Total || existing.Reset != cs.Reset || existing.ResetWeekday != cs.ResetWeekday)
|
||||
metaChanged := !clientFrozen && (existing.Total != cs.Total || existing.Reset != cs.Reset || existing.ResetWeekday != cs.ResetWeekday)
|
||||
if enableChanged || metaChanged || expiryChanged {
|
||||
structuralChange = true
|
||||
}
|
||||
}
|
||||
|
||||
renewed := !lifecycleFrozen && seen && existing != nil && nodeClientRenewed(existing, cs, canon, base)
|
||||
renewed := !clientFrozen && seen && existing != nil && nodeClientRenewed(existing, cs, canon, base)
|
||||
if renewed {
|
||||
// Reject when the node's own settings still carry the old absolute:
|
||||
// lagging ClientStats after a master shorten mimic a renew (#6228).
|
||||
@@ -1037,7 +1046,7 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
existing.ResetWeekday = cs.ResetWeekday
|
||||
existing.ResetCount = cs.ResetCount
|
||||
structuralChange = true
|
||||
} else if lifecycleFrozen {
|
||||
} else if clientFrozen {
|
||||
// Push pending or just landed: only counters may move, the master
|
||||
// keeps expiry/enable/total/reset.
|
||||
if err := tx.Exec(
|
||||
@@ -1096,7 +1105,7 @@ func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.Traffi
|
||||
}
|
||||
// A dip plus a lagging longer expiry mimics nodeClientRenewed and would
|
||||
// undo a master shorten once the freeze lifts (#6228).
|
||||
if lifecycleFrozen && seen && (canon.Up < base.Up || canon.Down < base.Down) {
|
||||
if clientFrozen && seen && (canon.Up < base.Up || canon.Down < base.Down) {
|
||||
continue
|
||||
}
|
||||
if err := s.upsertNodeBaseline(tx, nodeID, cs.Email, canon.Up, canon.Down); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user