mirror of
https://github.com/mjl-/mox.git
synced 2024-12-25 16:03:48 +03:00
5b20cba50a
we don't want external software to include internal details like mlog. slog.Logger is/will be the standard. we still have mlog for its helper functions, and its handler that logs in concise logfmt used by mox. packages that are not meant for reuse still pass around mlog.Log for convenience. we use golang.org/x/exp/slog because we also support the previous Go toolchain version. with the next Go release, we'll switch to the builtin slog.
37 lines
899 B
Go
37 lines
899 B
Go
package smtpserver
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/mjl-/mox/dns"
|
|
"github.com/mjl-/mox/dnsbl"
|
|
"github.com/mjl-/mox/mlog"
|
|
)
|
|
|
|
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.
|
|
func checkDNSBLHealth(ctx context.Context, log mlog.Log, resolver dns.Resolver, zone dns.Domain) (rok bool) {
|
|
dnsblHealth.Lock()
|
|
defer dnsblHealth.Unlock()
|
|
status, ok := dnsblHealth.zones[zone]
|
|
if !ok || time.Since(status.last) > 4*time.Hour {
|
|
status.err = dnsbl.CheckHealth(ctx, log.Logger, resolver, zone)
|
|
status.last = time.Now()
|
|
dnsblHealth.zones[zone] = status
|
|
}
|
|
return status.err == nil || errors.Is(status.err, dnsbl.ErrDNS)
|
|
}
|