-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreaper.go
79 lines (73 loc) · 2.73 KB
/
reaper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright (c) 2024 Johan Stenstam, johan.stenstam@internetstiftelsen.se
*/
package main
import (
"time"
"github.com/dnstapir/tapir"
)
// type WBGC map[string]*tapir.WBGlist
// 1. Iterate over all lists
// 2. Delete all items from the list that is in the ReaperData bucket for this time slot
// 3. Delete the bucket from the ReaperData map
// 4. Generate a new IXFR for the deleted items
// 5. Send the IXFR to the RPZ
func (pd *PopData) Reaper(full bool) error {
timekey := time.Now().Truncate(pd.ReaperInterval)
// tpkg := tapir.MqttPkgIn{}
tm := tapir.TapirMsg{}
pd.Logger.Printf("Reaper: working on time slot %s across all lists", timekey.Format(tapir.TimeLayout))
for _, listtype := range []string{"allowlist", "doubtlist", "denylist"} {
for listname, wbgl := range pd.Lists[listtype] {
// This loop is here to ensure that we don't have any old data in the ReaperData bucket
// that has already passed its time slot.
for t, d := range wbgl.ReaperData {
if t.Before(timekey) {
if len(d) == 0 {
continue
}
pd.Logger.Printf("Reaper: Warning: found old reaperdata for time slot %s (that has already passed). Moving %d names to current time slot (%s)", t.Format(tapir.TimeLayout), len(d), timekey.Format(tapir.TimeLayout))
pd.mu.Lock()
if _, exist := wbgl.ReaperData[timekey]; !exist {
wbgl.ReaperData[timekey] = map[string]bool{}
}
for name := range d {
wbgl.ReaperData[timekey][name] = true
}
// wbgl.ReaperData[timekey] = d
delete(wbgl.ReaperData, t)
pd.mu.Unlock()
}
}
// pd.Logger.Printf("Reaper: working on %s %s", listtype, listname)
if len(wbgl.ReaperData[timekey]) > 0 {
pd.Logger.Printf("Reaper: list [%s][%s] has %d timekeys stored", listtype, listname,
len(wbgl.ReaperData[timekey]))
pd.mu.Lock()
for name := range wbgl.ReaperData[timekey] {
pd.Logger.Printf("Reaper: removing %s from %s %s", name, listtype, listname)
delete(pd.Lists[listtype][listname].Names, name)
delete(wbgl.ReaperData[timekey], name)
tm.Removed = append(tm.Removed, tapir.Domain{Name: name})
}
// pd.Logger.Printf("Reaper: %s %s now has %d items:", listtype, listname, len(pd.Lists[listtype][listname].Names))
// for name, item := range pd.Lists[listtype][listname].Names {
// pd.Logger.Printf("Reaper: remaining: key: %s name: %s", name, item.Name)
// }
delete(wbgl.ReaperData, timekey)
pd.mu.Unlock()
}
}
}
if len(tm.Removed) > 0 {
ixfr, err := pd.GenerateRpzIxfr(&tm)
if err != nil {
pd.Logger.Printf("Reaper: Error from GenerateRpzIxfr(): %v", err)
}
err = pd.ProcessIxfrIntoAxfr(ixfr)
if err != nil {
pd.Logger.Printf("Reaper: Error from ProcessIxfrIntoAxfr(): %v", err)
}
}
return nil
}