fix(runtime): drop depleted clients by email, not by stale inbound_id

Invariant: a client whose stats row is switched off is served by no local
runtime of any inbound it is attached to. client_traffics is email-keyed,
and AddClientStat re-points its inbound_id at the last inbound attached,
yet the runtime push builder and the MTProto, TUIC and AmneziaWG desired-
instance builders looked the row up by inbound_id. On every other inbound
of a multi-inbound client the depletion filter saw nothing, so a depleted
client stayed served there whenever its settings entry still read enabled
- the state the stale settings writes left in existing databases.

All four now resolve the flag through trafficDisabledEmails, keyed by the
emails the inbound actually lists. GetXrayConfig already backfills sibling
rows by email (backfillClientStats) and is unchanged.
This commit is contained in:
MHSanaei committed 2026-09-28 02:23:50 +02:00
1 parent 1110caaa65
commit 7c84ca9689
6 files changed
+245 -113

No files matched your search

+10 -11
View File
@@ -25,7 +25,6 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/util/common"
"github.com/mhsanaei/3x-ui/v3/internal/util/netsafe"
wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -2076,16 +2075,16 @@ func (s *InboundService) buildInboundForLocalRuntime(tx *gorm.DB, inbound *model
return built, nil
}
var clientStats []xray.ClientTraffic
if err := tx.Model(xray.ClientTraffic{}).
Where("inbound_id = ?", built.Id).
Select("email", "enable").
Find(&clientStats).Error; err != nil {
return nil, err
emails := make([]string, 0, len(clients))
for _, client := range clients {
if c, ok := client.(map[string]any); ok {
email, _ := c["email"].(string)
emails = append(emails, email)
}
}
enableMap := make(map[string]bool, len(clientStats))
for _, clientTraffic := range clientStats {
enableMap[clientTraffic.Email] = clientTraffic.Enable
disabled, err := trafficDisabledEmails(tx, emails)
if err != nil {
return nil, err
}
finalClients := make([]any, 0, len(clients))
@@ -2095,7 +2094,7 @@ func (s *InboundService) buildInboundForLocalRuntime(tx *gorm.DB, inbound *model
continue
}
email, _ := c["email"].(string)
if enable, exists := enableMap[email]; exists && !enable {
if _, off := disabled[email]; off {
continue
}
if manualEnable, ok := c["enable"].(bool); ok && !manualEnable {
+24 -34
View File
@@ -14,7 +14,6 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
// DesiredAmneziaWGInstances derives the AmneziaWG interfaces this panel
@@ -37,47 +36,38 @@ func (s *InboundService) DesiredAmneziaWGInstances() ([]amneziawg.Instance, erro
return nil, nil
}
ids := make([]int, 0, len(inbounds))
for _, ib := range inbounds {
ids = append(ids, ib.Id)
}
var disabledRows []xray.ClientTraffic
err = db.Model(xray.ClientTraffic{}).
Where("inbound_id IN ? AND enable = ?", ids, false).
Select("inbound_id", "email").
Find(&disabledRows).Error
if err != nil {
return nil, err
}
disabled := make(map[int]map[string]struct{}, len(disabledRows))
for _, row := range disabledRows {
if disabled[row.InboundId] == nil {
disabled[row.InboundId] = map[string]struct{}{}
}
disabled[row.InboundId][row.Email] = struct{}{}
}
instances := make([]amneziawg.Instance, 0, len(inbounds))
for _, ib := range inbounds {
inst, ok := amneziawg.InstanceFromInbound(ib)
if !ok {
continue
}
if off := disabled[ib.Id]; len(off) > 0 {
kept := make([]amneziawg.Peer, 0, len(inst.Peers))
for _, p := range inst.Peers {
if _, skip := off[p.Email]; !skip {
kept = append(kept, p)
}
}
inst.Peers = kept
}
if len(inst.Peers) == 0 {
continue
}
instances = append(instances, inst)
}
return instances, nil
emails := make([]string, 0)
for _, inst := range instances {
for _, e := range inst.Peers {
emails = append(emails, e.Email)
}
}
disabled, err := trafficDisabledEmails(db, emails)
if err != nil {
return nil, err
}
served := instances[:0]
for _, inst := range instances {
kept := make([]amneziawg.Peer, 0, len(inst.Peers))
for _, e := range inst.Peers {
if _, off := disabled[e.Email]; !off {
kept = append(kept, e)
}
}
inst.Peers = kept
if len(kept) > 0 {
served = append(served, inst)
}
}
return served, nil
}
// applyLocalAmneziaWG pushes a single local AmneziaWG inbound's current peer
+18
View File
@@ -234,3 +234,21 @@ func (s *InboundService) markClientsDisabledInSettings(tx *gorm.DB, inboundID in
}
return &snapshot, &ib, nil
}
// trafficDisabledEmails reports which emails have a switched-off stats row. The
// table is email-keyed and its inbound_id goes stale, so never filter on it.
func trafficDisabledEmails(db *gorm.DB, emails []string) (map[string]struct{}, error) {
disabled := make(map[string]struct{})
for _, batch := range chunkStrings(uniqueNonEmptyStrings(emails), sqlInChunk) {
var page []string
if err := db.Model(xray.ClientTraffic{}).
Where("email IN ? AND enable = ?", batch, false).
Pluck("email", &page).Error; err != nil {
return nil, err
}
for _, e := range page {
disabled[e] = struct{}{}
}
}
return disabled, nil
}
Loaded 3 of 6 files, more files were not shown because too many files have changed in this diff. Show more