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:
Sanaei
2026-08-24 13:27:40 +02:00
parent 2d30ab3ada
commit 103b0dfe8d
4 changed files with 158 additions and 0 deletions
+33
View File
@@ -299,3 +299,36 @@ func (s *InboundService) DeleteNodeClientIpsByGuid(guid string) error {
db := database.GetDB()
return db.Where("node_guid = ?", guid).Delete(&model.NodeClientIp{}).Error
}
// pruneStaleNodeClientIps sweeps every attribution row: upsertNodeClientIps
// only revisits emails present in a scan, so unobserved rows never expire there.
func pruneStaleNodeClientIps(cutoff int64) error {
db := database.GetDB()
var rows []model.NodeClientIp
if err := db.Find(&rows).Error; err != nil {
return err
}
for _, row := range rows {
var entries []model.ClientIpEntry
if row.Ips != "" {
if err := json.Unmarshal([]byte(row.Ips), &entries); err != nil {
continue
}
}
kept := mergeModelClientIpEntries(nil, entries, cutoff)
if len(kept) == 0 {
if err := db.Delete(&model.NodeClientIp{}, row.Id).Error; err != nil {
return err
}
continue
}
if len(kept) == len(entries) {
continue
}
b, _ := json.Marshal(kept)
if err := db.Model(&model.NodeClientIp{}).Where("id = ?", row.Id).Update("ips", string(b)).Error; err != nil {
return err
}
}
return nil
}