mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-18 19:15:03 +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:
@@ -28,14 +28,16 @@ const depletedClientsClause = "reset = 0 and reset_day = 0 and reset_weekday = 0
|
||||
func (s *InboundService) AddTraffic(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (needRestart bool, clientsDisabled bool, err error) {
|
||||
var disabledNodeIDs []int
|
||||
var remotePlans []trafficInboundUpdatePlan
|
||||
var renewed []string
|
||||
err = submitTrafficWrite(func() error {
|
||||
var inner error
|
||||
needRestart, clientsDisabled, disabledNodeIDs, remotePlans, inner = s.addTrafficLocked(inboundTraffics, clientTraffics)
|
||||
needRestart, clientsDisabled, disabledNodeIDs, remotePlans, renewed, inner = s.addTrafficLocked(inboundTraffics, clientTraffics)
|
||||
return inner
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.resetMtprotoClientQuotas(renewed)
|
||||
// Off the serial writer: a hanging node must not stall traffic accounting.
|
||||
needRestart = s.applyTrafficRemotePlans(remotePlans) || needRestart
|
||||
if len(disabledNodeIDs) > 0 {
|
||||
@@ -44,7 +46,7 @@ func (s *InboundService) AddTraffic(inboundTraffics []*xray.Traffic, clientTraff
|
||||
return
|
||||
}
|
||||
|
||||
func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (bool, bool, []int, []trafficInboundUpdatePlan, error) {
|
||||
func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (bool, bool, []int, []trafficInboundUpdatePlan, []string, error) {
|
||||
db := database.GetDB()
|
||||
// Commit durable traffic before best-effort lifecycle maintenance so helper
|
||||
// failures cannot discard usage already reported by Xray.
|
||||
@@ -54,7 +56,7 @@ func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clien
|
||||
}
|
||||
return s.addClientTraffic(tx, clientTraffics)
|
||||
}); err != nil {
|
||||
return false, false, nil, nil, err
|
||||
return false, false, nil, nil, nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -99,10 +101,10 @@ func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clien
|
||||
})
|
||||
if err != nil {
|
||||
logger.Warning("traffic lifecycle maintenance failed after traffic commit:", err)
|
||||
return false, false, nil, nil, nil
|
||||
return false, false, nil, nil, nil, nil
|
||||
}
|
||||
needRestart = needRestart || s.applyTrafficMutationBatch(batch)
|
||||
return needRestart, clientsDisabled, disabledNodeIDs, batch.remotePlans, nil
|
||||
return needRestart, clientsDisabled, disabledNodeIDs, batch.remotePlans, batch.renewedEmails, nil
|
||||
}
|
||||
|
||||
func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
|
||||
@@ -515,6 +517,7 @@ func (s *InboundService) autoRenewClients(tx *gorm.DB, mutationBatch *trafficMut
|
||||
if err = clearGlobalTraffic(tx, renewedEmails...); err != nil {
|
||||
return false, 0, err
|
||||
}
|
||||
mutationBatch.renewedEmails = append(mutationBatch.renewedEmails, renewedEmails...)
|
||||
for _, clientToAdd := range clientsToAdd {
|
||||
if clientToAdd.inbound.NodeID != nil {
|
||||
mutationBatch.addNode(*clientToAdd.inbound.NodeID)
|
||||
@@ -648,33 +651,23 @@ func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
|
||||
}
|
||||
|
||||
func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (needRestart bool, err error) {
|
||||
var resetInbound *model.Inbound
|
||||
var ownNode *int
|
||||
err = submitTrafficWrite(func() error {
|
||||
var inner error
|
||||
needRestart, resetInbound, inner = s.resetClientTrafficLocked(id, clientEmail)
|
||||
needRestart, ownNode, inner = s.resetClientTrafficLocked(id, clientEmail)
|
||||
return inner
|
||||
})
|
||||
if err == nil {
|
||||
s.resetMtprotoClientQuota(clientEmail)
|
||||
if resetInbound != nil && resetInbound.NodeID != nil {
|
||||
// Attempted whatever the node's status: nothing replays a reset, so a
|
||||
// node still serving after being marked offline must get it now.
|
||||
if rt, rterr := s.runtimeFor(resetInbound); rterr != nil {
|
||||
logger.Warning("ResetClientTraffic: runtime lookup failed:", rterr)
|
||||
} else {
|
||||
ctx, cancel := nodePushContext()
|
||||
e := rt.ResetClientTraffic(ctx, resetInbound, clientEmail)
|
||||
cancel()
|
||||
if e != nil {
|
||||
logger.Warning("ResetClientTraffic: remote propagation to", rt.Name(), "failed:", e)
|
||||
}
|
||||
}
|
||||
// Siblings on other nodes are delivered by their own inbound's reset.
|
||||
if ownNode != nil {
|
||||
s.deliverNodeResetsNow([]int{*ownNode})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *InboundService) resetClientTrafficLocked(id int, clientEmail string) (bool, *model.Inbound, error) {
|
||||
func (s *InboundService) resetClientTrafficLocked(id int, clientEmail string) (bool, *int, error) {
|
||||
needRestart := false
|
||||
var reenablePlan *trafficLocalApplyPlan
|
||||
var reenableNodeID *int
|
||||
@@ -747,6 +740,9 @@ func (s *InboundService) resetClientTrafficLocked(id int, clientEmail string) (b
|
||||
if err := tx.Where("email = ?", clientEmail).Delete(&model.NodeClientTraffic{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := queueNodeResets(tx, []string{clientEmail}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Model(model.Inbound{}).
|
||||
Where("id = ?", id).
|
||||
Update("last_traffic_reset_time", now).Error; err != nil {
|
||||
@@ -775,7 +771,10 @@ func (s *InboundService) resetClientTrafficLocked(id int, clientEmail string) (b
|
||||
}
|
||||
}
|
||||
|
||||
return needRestart, inbound, nil
|
||||
if inbound != nil {
|
||||
return needRestart, inbound.NodeID, nil
|
||||
}
|
||||
return needRestart, nil, nil
|
||||
}
|
||||
|
||||
func (s *InboundService) ResetAllTraffics() error {
|
||||
|
||||
Reference in New Issue
Block a user