1package smtpserver
2
3import (
4 "context"
5 "errors"
6 "sync"
7 "time"
8
9 "github.com/mjl-/mox/dns"
10 "github.com/mjl-/mox/dnsbl"
11 "github.com/mjl-/mox/mlog"
12)
13
14var dnsblHealth = struct {
15 sync.Mutex
16 zones map[dns.Domain]dnsblStatus
17}{
18 zones: map[dns.Domain]dnsblStatus{},
19}
20
21type dnsblStatus struct {
22 last time.Time
23 err error // nil, dnsbl.ErrDNS or other
24}
25
26// checkDNSBLHealth checks healthiness of DNSBL "zone", keeping the result cached for 4 hours.
27func checkDNSBLHealth(ctx context.Context, log mlog.Log, resolver dns.Resolver, zone dns.Domain) (rok bool) {
28 dnsblHealth.Lock()
29 defer dnsblHealth.Unlock()
30 status, ok := dnsblHealth.zones[zone]
31 if !ok || time.Since(status.last) > 4*time.Hour {
32 status.err = dnsbl.CheckHealth(ctx, log.Logger, resolver, zone)
33 status.last = time.Now()
34 dnsblHealth.zones[zone] = status
35 }
36 return status.err == nil || errors.Is(status.err, dnsbl.ErrDNS)
37}
38