mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-25 12:27:13 +00:00
fix(job): expire stored client IPs of offline clients
ipStaleAfterSeconds was only applied while a row was being rewritten, and rows are only rewritten for clients present in the current online scan. A client that stopped connecting therefore kept its last addresses forever in inbound_client_ips, and node_client_ips rows (including those of deleted clients) were never revisited at all. Sweep both tables every five minutes, dropping entries past the cutoff and deleting rows that end up empty. The sweep runs ahead of the fail2ban and api-mode gates so retention holds even on panels that collect nothing. Closes #6286
This commit is contained in:
@@ -236,3 +236,39 @@ func (s *InboundService) ClearClientIps(clientEmail string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PruneStaleClientIps enforces clientIpStaleAfterSeconds for rows the online
|
||||
// scan no longer rewrites: an offline client's addresses must still expire.
|
||||
func (s *InboundService) PruneStaleClientIps() error {
|
||||
db := database.GetDB()
|
||||
cutoff := time.Now().Unix() - clientIpStaleAfterSeconds
|
||||
|
||||
var rows []model.InboundClientIps
|
||||
if err := db.Find(&rows).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, row := range rows {
|
||||
var entries []clientIpEntry
|
||||
if row.Ips != "" {
|
||||
// Legacy blobs without timestamps stay untouched; the next scan rewrites them.
|
||||
if err := json.Unmarshal([]byte(row.Ips), &entries); err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
kept := mergeClientIpEntries(nil, entries, cutoff)
|
||||
if len(kept) == 0 {
|
||||
if err := db.Delete(&model.InboundClientIps{}, row.Id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if len(kept) == len(entries) {
|
||||
continue
|
||||
}
|
||||
b, _ := json.Marshal(kept)
|
||||
if err := db.Model(&model.InboundClientIps{}).Where("id = ?", row.Id).Update("ips", string(b)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return pruneStaleNodeClientIps(cutoff)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user