mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-17 23:57:15 +00:00
fix(clients): sync auto-renewal across inbounds (#6339)
* fix(clients): sync auto-renewal across inbounds Propagate the renewed shared traffic state to every inbound that carries the same client email. Restore each affected runtime user while keeping renewal counters and quota resets single-counted. * fix(clients): preserve manual disable during renewal
This commit is contained in:
@@ -368,10 +368,15 @@ func (s *InboundService) autoRenewClients(tx *gorm.DB, mutationBatch *trafficMut
|
||||
var inbound_ids []int
|
||||
var inbounds []*model.Inbound
|
||||
needRestart := false
|
||||
type inboundClientKey struct {
|
||||
inboundID int
|
||||
email string
|
||||
}
|
||||
var clientsToAdd []struct {
|
||||
inbound model.Inbound
|
||||
client map[string]any
|
||||
}
|
||||
clientsToAddSet := make(map[inboundClientKey]struct{})
|
||||
|
||||
// Resolve the inbounds to renew through the client_inbounds link rather than
|
||||
// client_traffics.inbound_id, which goes stale after an inbound is deleted and
|
||||
@@ -407,8 +412,13 @@ func (s *InboundService) autoRenewClients(tx *gorm.DB, mutationBatch *trafficMut
|
||||
// instead of a linear scan of every expired row (O(clients × expired) per
|
||||
// inbound, quadratic at scale). Pointers keep the in-place mutation below.
|
||||
trafficByEmail := make(map[string]*xray.ClientTraffic, len(traffics))
|
||||
// Keep the pre-renewal quota state: the shared pointer becomes enabled while
|
||||
// processing the first inbound, while an already-enabled row paired with
|
||||
// disabled settings represents an operator-disabled client we must preserve.
|
||||
trafficWasEnabled := make(map[string]bool, len(traffics))
|
||||
for i := range traffics {
|
||||
trafficByEmail[traffics[i].Email] = traffics[i]
|
||||
trafficWasEnabled[traffics[i].Email] = traffics[i].Enable
|
||||
}
|
||||
renewedEmails := make([]string, 0, len(traffics))
|
||||
for inbound_index := range inbounds {
|
||||
@@ -453,32 +463,37 @@ func (s *InboundService) autoRenewClients(tx *gorm.DB, mutationBatch *trafficMut
|
||||
}
|
||||
renewals++
|
||||
}
|
||||
if renewals == 0 {
|
||||
continue
|
||||
if renewals > 0 {
|
||||
traffic.ExpiryTime = newExpiryTime
|
||||
traffic.ResetCount += renewals
|
||||
}
|
||||
c["expiryTime"] = newExpiryTime
|
||||
traffic.ExpiryTime = newExpiryTime
|
||||
traffic.ResetCount += renewals
|
||||
if newExpiryTime <= now {
|
||||
c["expiryTime"] = traffic.ExpiryTime
|
||||
if traffic.ExpiryTime <= now {
|
||||
// Cap ran out mid-catch-up and the client is still expired: enabling it
|
||||
// for disableInvalidClients to undo adds and removes an xray user for nothing.
|
||||
clients[client_index] = any(c)
|
||||
continue
|
||||
}
|
||||
traffic.Down = 0
|
||||
traffic.Up = 0
|
||||
renewedEmails = append(renewedEmails, email)
|
||||
if !traffic.Enable {
|
||||
if renewals > 0 {
|
||||
traffic.Down = 0
|
||||
traffic.Up = 0
|
||||
renewedEmails = append(renewedEmails, email)
|
||||
}
|
||||
if !trafficWasEnabled[email] {
|
||||
traffic.Enable = true
|
||||
c["enable"] = true
|
||||
clientsToAdd = append(clientsToAdd,
|
||||
struct {
|
||||
inbound model.Inbound
|
||||
client map[string]any
|
||||
}{
|
||||
inbound: *inbounds[inbound_index],
|
||||
client: apiUserFromClient(c, cipher),
|
||||
})
|
||||
key := inboundClientKey{inboundID: inbounds[inbound_index].Id, email: email}
|
||||
if _, planned := clientsToAddSet[key]; !planned {
|
||||
clientsToAddSet[key] = struct{}{}
|
||||
clientsToAdd = append(clientsToAdd,
|
||||
struct {
|
||||
inbound model.Inbound
|
||||
client map[string]any
|
||||
}{
|
||||
inbound: *inbounds[inbound_index],
|
||||
client: apiUserFromClient(c, cipher),
|
||||
})
|
||||
}
|
||||
}
|
||||
clients[client_index] = any(c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user