2023-01-30 16:27:06 +03:00
|
|
|
package smtpserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mjl-/mox/dns"
|
|
|
|
"github.com/mjl-/mox/dnsbl"
|
2023-12-05 15:35:58 +03:00
|
|
|
"github.com/mjl-/mox/mlog"
|
2023-01-30 16:27:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var dnsblHealth = struct {
|
|
|
|
sync.Mutex
|
|
|
|
zones map[dns.Domain]dnsblStatus
|
|
|
|
}{
|
|
|
|
zones: map[dns.Domain]dnsblStatus{},
|
|
|
|
}
|
|
|
|
|
|
|
|
type dnsblStatus struct {
|
|
|
|
last time.Time
|
|
|
|
err error // nil, dnsbl.ErrDNS or other
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkDNSBLHealth checks healthiness of DNSBL "zone", keeping the result cached for 4 hours.
|
2023-12-05 15:35:58 +03:00
|
|
|
func checkDNSBLHealth(ctx context.Context, log mlog.Log, resolver dns.Resolver, zone dns.Domain) (rok bool) {
|
2023-01-30 16:27:06 +03:00
|
|
|
dnsblHealth.Lock()
|
|
|
|
defer dnsblHealth.Unlock()
|
|
|
|
status, ok := dnsblHealth.zones[zone]
|
|
|
|
if !ok || time.Since(status.last) > 4*time.Hour {
|
2023-12-05 15:35:58 +03:00
|
|
|
status.err = dnsbl.CheckHealth(ctx, log.Logger, resolver, zone)
|
2023-01-30 16:27:06 +03:00
|
|
|
status.last = time.Now()
|
|
|
|
dnsblHealth.zones[zone] = status
|
|
|
|
}
|
|
|
|
return status.err == nil || errors.Is(status.err, dnsbl.ErrDNS)
|
|
|
|
}
|