build dns server

This commit is contained in:
Jonas Leder 2026-11-09 17:21:54 +01:00
commit b704a99032
49 changed files with 7523 additions and 0 deletions

5
.dockerignore Normal file
View file

@ -0,0 +1,5 @@
.git
data
.env
deploy
*.md

11
.env.example Normal file
View file

@ -0,0 +1,11 @@
# Copied to .env for local runs (auto-loaded by godotenv). Real env vars win over .env.
ZONE=home
LOG_LEVEL=INFO # ERROR|WARN|INFO|DEBUG
DNS_ADDR=:5353
UPSTREAM_MODE=recursive # recursive | forward
# UPSTREAM_SERVERS=1.1.1.1,tls://9.9.9.9:853#dns.quad9.net
DNSSEC_MODE=strict # strict | permissive | off
CACHE_MAX_MB=64
# REDIS_ADDR=127.0.0.1:6379 # optional shared cache
API_TOKEN=change-me # enables admin API + replica sync
CONFIG_FILE=./config.example.yaml

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/data/
.env
/bin/

15
Dockerfile Normal file
View file

@ -0,0 +1,15 @@
# syntax=docker/dockerfile:1
FROM golang:1.27 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
ARG VERSION=dev
RUN CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=${VERSION}" -o /out/adblock-dns ./cmd/adblock-dns
# static, no shell, runs as uid 65532 – listens on unprivileged ports only
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/adblock-dns /adblock-dns
USER 65532:65532
EXPOSE 5353/udp 5353/tcp 9153 8080
ENTRYPOINT ["/adblock-dns"]

17
Makefile Normal file
View file

@ -0,0 +1,17 @@
VERSION ?= dev
IMAGE ?= adblock-dns
.PHONY: build test run bench image lint
build:
CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=$(VERSION)" -o bin/adblock-dns ./cmd/adblock-dns
go build -o bin/dnsbench ./cmd/dnsbench
test:
go vet ./... && go test -race ./...
run:
go run ./cmd/adblock-dns
bench: build
bin/dnsbench -server 127.0.0.1:5353 -c 64 -d 10s
image:
docker build --build-arg VERSION=$(VERSION) -t $(IMAGE):$(VERSION) .
lint:
helm lint deploy/helm/adblock-dns

108
README.md Normal file
View file

@ -0,0 +1,108 @@
# adblock-dns
Cloud-native DNS server in Go: Pi-hole-style ad blocking, local DNS overrides, DNSSEC validation,
Prometheus metrics + Grafana dashboard, multi-replica operation with optional shared cache, and a
"same config everywhere" multi-zone model (home + datacenter) where sites never depend on each other.
## Features
| Area | What you get |
|---|---|
| Blocking | hosts / plain-domain / Adblock (`\|\|domain^`, `@@` exceptions) lists, auto-refresh with ETag + on-disk cache (works offline), regex rules, CNAME-cloaking protection, null / NXDOMAIN / REFUSED / custom-IP answers |
| Overrides | local records (A, AAAA, CNAME, TXT, MX, SRV, PTR … + wildcards), **allow/block overrides of the lists** (precedence: allow > block > list exceptions > lists), conditional forwarding zones, runtime changes via API |
| Upstream | `recursive` (own iterative resolver from the root, bailiwick-checked, RTT-ranked servers) **or** `forward` to custom servers (UDP multiplexed sockets, TCP, DoT `tls://ip:853#name`) |
| DNSSEC | full validation from the root trust anchor (RRSIG, DS chain, NSEC/NSEC3 denial, opt-out), `strict` / `permissive` / `off`, `AD` bit + Extended DNS Error on failures, `CD` honoured |
| Cache | sharded byte-bounded LRU storing packed wire format (hit = copy + TTL patch, no unpacking), prefetch, serve-stale (RFC 8767), negative caching, single-flight de-duplication |
| HA | N stateless replicas; runtime overrides sync between them (LWW CRDT); optional shared Redis/Valkey cache — every dependency is best-effort and can fail without affecting resolution |
| Ops | `/metrics`, `/healthz`, `/readyz`, JSON logs, Helm chart with minimal privileges, Grafana dashboard |
## Quick start
```sh
cp .env.example .env # auto-loaded (godotenv); real env vars win
go run ./cmd/adblock-dns # listens on :5353 (unprivileged)
dig @127.0.0.1 -p 5353 cloudflare.com # "ad" flag = DNSSEC validated
```
Libraries used as requested: **logrus** (via `jonasled.dev/jonasled/go-libs/log`, which also provides
`LOG_LEVEL`/`LOG_FOLDER`), **go-libs** `config` helpers for env access, and **godotenv/autoload**.
## Configuration
Precedence: defaults → YAML (global) → YAML `zones.<ZONE>` → environment. See
[config.example.yaml](config.example.yaml) and [.env.example](.env.example).
| Env | Default | |
|---|---|---|
| `ZONE` | – | selects `zones.<name>` and per-zone `records` |
| `CONFIG_FILE` | `/etc/adblock-dns/config.yaml` or `./config.yaml` | |
| `DNS_ADDR` / `DNS_LISTENERS` | `:5353` / `min(CPU,8)` | N UDP + N TCP `SO_REUSEPORT` sockets |
| `UPSTREAM_MODE` / `UPSTREAM_SERVERS` | `recursive` | `forward` needs servers |
| `DNSSEC_MODE` | `strict` | `permissive` logs bogus but serves; `off` |
| `CACHE_MAX_MB`, `CACHE_MIN_TTL`, `CACHE_MAX_TTL`, `CACHE_NEG_TTL`, `CACHE_STALE` | 64, 5s, 24h, 1h, 1h | |
| `REDIS_ADDR` (+`REDIS_PASSWORD`, `REDIS_TLS`, `REDIS_DB`) | off | shared cache, key prefix is per zone |
| `BLOCK_MODE` | `null` | `null` `nxdomain` `refused` `ip` (+`BLOCK_IPV4/6`) |
| `API_TOKEN` | – | enables admin API (`:8080`) and replica sync |
| `SYNC_PEERS` / `SYNC_PEERS_DNS` | – | static `host:port` list / headless service name |
| `ACL_CIDRS` | allow all | restrict clients |
| `LOG_LEVEL`, `LOG_FORMAT` | INFO, text | `json` for clusters |
### Multi-zone: one config, several sites
`ZONE=home` and `ZONE=dc1` run the *same* file. The zone decides which `records` apply (give the same name
different answers per site), which `zones.<name>` overrides are merged (e.g. recursive at home, forwarding to the
datacenter resolvers in the DC, a Redis only the DC has), and which peers it syncs with. A site never needs
another site: lists are cached on disk, Redis/peers are best-effort (circuit breaker, tight timeouts), recursion
needs only the internet. Listing a remote site in `sync.peers` makes runtime changes propagate across sites
whenever the link is up and converge when it returns; use `zones:` on API calls to scope a change to one site.
### Admin API (bearer token, port 8080)
```sh
H="Authorization: Bearer $API_TOKEN"
curl -XPUT -H "$H" :8080/api/v1/allow/example.com # override a list entry (all replicas)
curl -XPUT -H "$H" :8080/api/v1/block/tracker.example -d '{"zones":["home"]}'
curl -XPOST -H "$H" :8080/api/v1/records -d '{"name":"nas.lan","type":"A","value":"192.168.1.10"}'
curl -XPOST -H "$H" :8080/api/v1/blocking -d '{"enabled":false,"duration":"10m"}' # pause blocking
curl -XPOST -H "$H" :8080/api/v1/cache/flush
curl -H "$H" :8080/api/v1/status
```
## Kubernetes
```sh
docker build -t registry.example.com/adblock-dns:0.1.0 . && docker push …
helm install dns deploy/helm/adblock-dns -n dns --create-namespace \
--set zone=home --set image.repository=registry.example.com/adblock-dns \
--set service.dns.type=LoadBalancer --set redis.enabled=true \
--set serviceMonitor.enabled=true --set grafanaDashboard.enabled=true -f my-config.yaml
```
Minimal privileges: non-root (65532), read-only root FS, all capabilities dropped, seccomp `RuntimeDefault`,
no service-account token, listens on 5353 (Service maps 53 → 5353, so `NET_BIND_SERVICE` is unnecessary),
distroless static image, optional NetworkPolicy, PDB, spread across nodes. The only writable path is an
`emptyDir` for list downloads / runtime state.
Grafana: import `deploy/helm/adblock-dns/dashboards/adblock-dns.json` or enable `grafanaDashboard`. The
dashboard filters on a `zone` label, which the chart's ServiceMonitor adds; with other scrape setups add
`zone` yourself (the `adblockdns_info{zone=…}` metric shows the expected values).
## Performance & memory
* Blocklists are 64-bit hashes in an open-addressing table, **~16 B/domain** (1 M domains ≈ 16 MB), zero-alloc lookups.
* Cache hits never unpack a message; the cache is byte-bounded (`CACHE_MAX_MB`), LRU-evicted, swept periodically.
* Go GC honours the container limit automatically (`GOMEMLIMIT` = 90 % of the cgroup limit unless set).
* Measured on an 8-core laptop, load generator on the same host, warm cache, 64 clients:
**~300 k qps, p50 160 µs, p99 0.9 ms, 32 MB RSS** with the default 64 MiB cache and a small list.
Use `make bench` / `cmd/dnsbench` to reproduce, or point `dnsperf` / `flamethrower` at it to compare with
CoreDNS, Unbound, Pi-hole (FTL) or AdGuard Home under identical conditions.
## Limitations / roadmap
* DNSSEC: wildcard-denial parts of NXDOMAIN proofs are not verified (covering/closest-encloser records are); algorithms
beyond RSA/ECDSA/Ed25519 are treated as insecure.
* No DoH/DoT/DoQ *listeners* yet (DoT *upstream* is supported), no per-client groups, no query log / top-domain lists
(counters only, to keep Prometheus cardinality bounded), no QNAME minimisation, no hot-reload of the YAML
(pods roll on change; API changes are live).
* Sync endpoint is bearer-token authenticated; set `SYNC_TLS`/`API_TLS_CERT` and `SYNC_CA_FILE` when peers are
reached over untrusted networks.

204
cmd/adblock-dns/main.go Normal file
View file

@ -0,0 +1,204 @@
// adblock-dns: a cloud-native, ad-blocking, DNSSEC-validating DNS server.
package main
import (
"context"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"syscall"
"time"
_ "github.com/joho/godotenv/autoload" // loads .env into the environment before anything reads it
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
"jonasled.dev/jonasled/adblock-dns/internal/api"
"jonasled.dev/jonasled/adblock-dns/internal/cache"
"jonasled.dev/jonasled/adblock-dns/internal/config"
"jonasled.dev/jonasled/adblock-dns/internal/dnssec"
"jonasled.dev/jonasled/adblock-dns/internal/engine"
"jonasled.dev/jonasled/adblock-dns/internal/metrics"
"jonasled.dev/jonasled/adblock-dns/internal/policy"
"jonasled.dev/jonasled/adblock-dns/internal/resolver"
"jonasled.dev/jonasled/adblock-dns/internal/server"
"jonasled.dev/jonasled/adblock-dns/internal/state"
glog "jonasled.dev/jonasled/go-libs/log"
)
var version = "dev"
func main() {
glog.Init()
log := glog.Log
cfg, err := config.Load()
if err != nil {
log.Fatalf("configuration error: %v", err)
}
if cfg.LogFormat == "json" {
log.SetFormatter(&logrus.JSONFormatter{})
}
setMemLimit(log)
log.Infof("adblock-dns %s starting: zone=%q node=%s upstream=%s dnssec=%s cache=%dMiB shared-cache=%v",
version, cfg.Zone, cfg.NodeID, cfg.UpstreamMode, cfg.DNSSEC, cfg.CacheMaxBytes>>20, cfg.RedisAddr != "")
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// policy: overrides + blocklists
pol, err := policy.NewManager(cfg)
if err != nil {
log.Fatalf("policy: %v", err)
}
store := state.New(cfg.NodeID, filepath.Join(cfg.DataDir, "state.json"))
rebuild := func() {
if err := pol.Rebuild(store.Live()); err != nil {
log.WithError(err).Error("policy rebuild failed")
}
if pol.BlockingEnabled() {
metrics.M.BlockingEnabled.Set(1)
} else {
metrics.M.BlockingEnabled.Set(0)
}
}
store.Subscribe(rebuild)
rebuild()
lm := policy.NewListManager(cfg, log, pol.SetLists, policy.ListHooks{
Loaded: func(n string, c int) { metrics.M.ListDomains.WithLabelValues(n).Set(float64(c)) },
Result: func(n string, ok bool) {
if ok {
metrics.M.ListUpdated.WithLabelValues(n).SetToCurrentTime()
} else {
metrics.M.ListErrors.WithLabelValues(n).Inc()
}
},
})
go lm.Run(ctx)
// caches
l1 := cache.New(cfg.CacheMaxBytes, cfg.Stale)
var l2 *cache.Shared
if cfg.RedisAddr != "" {
l2 = cache.NewShared(cache.SharedOptions{Addr: cfg.RedisAddr, Password: cfg.RedisPassword, DB: cfg.RedisDB,
TLS: cfg.RedisTLS, Prefix: cfg.RedisPrefix + cfg.Zone + ":", Stale: cfg.Stale}, log)
}
metrics.RegisterInfo(cfg.Zone, cfg.NodeID, version,
func() float64 { n, _ := l1.Stats(); return float64(n) },
func() float64 { _, b := l1.Stats(); return float64(b) })
// resolvers
var def resolver.Resolver
if cfg.UpstreamMode == "recursive" {
def = resolver.NewRecursive(cfg.UpstreamTimeout, cfg.IPv6, log)
} else if def, err = resolver.NewForwarder(cfg.UpstreamServers, cfg.UpstreamTimeout, log); err != nil {
log.Fatalf("upstream: %v", err)
}
router := resolver.NewRouter(def)
for _, fz := range cfg.ForwardZones {
f, err := resolver.NewForwarder(fz.Servers, cfg.UpstreamTimeout, log)
if err != nil {
log.Fatalf("forward zone %s: %v", fz.Zone, err)
}
router.Add(fz.Zone, f)
}
var val *dnssec.Validator
if cfg.DNSSEC != "off" {
val, err = dnssec.New(func(ctx context.Context, name string, qt uint16) (*dns.Msg, error) {
return router.Resolve(ctx, name, qt, true, true)
}, cfg.TrustAnchors)
if err != nil {
log.Fatalf("dnssec: %v", err)
}
}
eng := engine.New(engine.Options{Config: cfg, Log: log, Policy: pol, L1: l1, L2: l2, Router: router, Validator: val})
// background housekeeping
go func() {
t := time.NewTicker(time.Minute)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
l1.Sweep(time.Now().Unix())
rebuild() // also lets time-limited "blocking disabled" expire in metrics
}
}
}()
// HTTP: metrics/health (open) and API/sync (token)
started := make(chan struct{})
ready := func() bool {
select {
case <-started:
default:
return false
}
select {
case <-lm.Ready():
return true
default:
return time.Since(startTime) > 30*time.Second // never block readiness on list downloads forever
}
}
go serveHTTP(cfg.MetricsAdr, api.MetricsHandler(ready), "", "", log)
if cfg.APIToken != "" {
var syncer *state.Syncer
if len(cfg.SyncPeers) > 0 || cfg.SyncPeersDNS != "" {
syncer, err = state.NewSyncer(store, state.SyncOptions{Token: cfg.APIToken, Peers: cfg.SyncPeers,
PeersDNS: cfg.SyncPeersDNS, SelfIPs: cfg.SelfIPs, Interval: cfg.SyncInterval, TLS: cfg.SyncTLS, CAFile: cfg.SyncCAFile}, log)
if err != nil {
log.Fatalf("sync: %v", err)
}
go syncer.Run(ctx)
}
a := &api.API{Cfg: cfg, Log: log, Policy: pol, Store: store, L1: l1, L2: l2, Version: version, Sync: syncer}
go serveHTTP(cfg.APIAddr, a.Handler(), cfg.TLSCert, cfg.TLSKey, log)
} else {
log.Info("API_TOKEN not set: admin API and replica sync are disabled")
}
if err := server.Run(ctx, cfg.DNSAddr, cfg.Listeners, eng, log, started); err != nil {
log.Fatalf("dns server: %v", err)
}
log.Info("shut down")
}
var startTime = time.Now()
func serveHTTP(addr string, h http.Handler, cert, key string, log *logrus.Logger) {
s := &http.Server{Addr: addr, Handler: h, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 15 * time.Second, WriteTimeout: 30 * time.Second}
var err error
if cert != "" && key != "" {
err = s.ListenAndServeTLS(cert, key)
} else {
err = s.ListenAndServe()
}
log.Fatalf("http %s: %v", addr, err)
}
// setMemLimit makes the Go GC honour the container memory limit (90%) unless
// GOMEMLIMIT is set explicitly.
func setMemLimit(log *logrus.Logger) {
if os.Getenv("GOMEMLIMIT") != "" {
return
}
for _, p := range []string{"/sys/fs/cgroup/memory.max", "/sys/fs/cgroup/memory/memory.limit_in_bytes"} {
raw, err := os.ReadFile(p)
if err != nil {
continue
}
n, err := strconv.ParseInt(strings.TrimSpace(string(raw)), 10, 64)
if err != nil || n <= 0 || n > 1<<50 {
continue
}
debug.SetMemoryLimit(n / 10 * 9)
log.Infof("GC memory limit set to %d MiB (90%% of cgroup limit)", n/10*9>>20)
return
}
}

93
cmd/dnsbench/main.go Normal file
View file

@ -0,0 +1,93 @@
// dnsbench is a minimal UDP DNS load generator for comparing resolvers
// (cache-hit throughput and latency percentiles).
//
// dnsbench -server 127.0.0.1:5353 -names names.txt -c 64 -d 10s
package main
import (
"bufio"
"flag"
"fmt"
"math/rand/v2"
"net"
"os"
"sort"
"sync"
"sync/atomic"
"time"
"github.com/miekg/dns"
)
func main() {
server := flag.String("server", "127.0.0.1:5353", "server host:port")
names := flag.String("names", "", "file with one name per line (default: built-in set)")
conc := flag.Int("c", 32, "concurrent workers (one UDP socket each)")
dur := flag.Duration("d", 10*time.Second, "test duration")
flag.Parse()
list := []string{"example.com", "example.org", "cloudflare.com", "wikipedia.org", "github.com", "kernel.org", "debian.org", "archlinux.org"}
if *names != "" {
f, err := os.Open(*names)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
list = nil
sc := bufio.NewScanner(f)
for sc.Scan() {
if t := sc.Text(); t != "" {
list = append(list, t)
}
}
}
var sent, ok, fail atomic.Int64
var mu sync.Mutex
var lat []time.Duration
deadline := time.Now().Add(*dur)
var wg sync.WaitGroup
for i := 0; i < *conc; i++ {
wg.Add(1)
go func() {
defer wg.Done()
c, err := net.Dial("udp", *server)
if err != nil {
return
}
defer c.Close()
buf := make([]byte, 4096)
local := make([]time.Duration, 0, 1<<16)
for time.Now().Before(deadline) {
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(list[rand.IntN(len(list))]), dns.TypeA)
wire, _ := m.Pack()
t0 := time.Now()
_, _ = c.Write(wire)
_ = c.SetReadDeadline(time.Now().Add(2 * time.Second))
n, err := c.Read(buf)
sent.Add(1)
if err != nil || n < 12 || buf[3]&0x0f == 2 {
fail.Add(1)
continue
}
ok.Add(1)
if len(local) < cap(local) {
local = append(local, time.Since(t0))
}
}
mu.Lock()
lat = append(lat, local...)
mu.Unlock()
}()
}
wg.Wait()
sort.Slice(lat, func(i, j int) bool { return lat[i] < lat[j] })
p := func(q float64) time.Duration {
if len(lat) == 0 {
return 0
}
return lat[int(float64(len(lat)-1)*q)]
}
fmt.Printf("queries=%d ok=%d failed=%d qps=%.0f\nlatency p50=%v p90=%v p99=%v max=%v\n",
sent.Load(), ok.Load(), fail.Load(), float64(ok.Load())/dur.Seconds(), p(.5), p(.9), p(.99), p(1))
}

52
config.example.yaml Normal file
View file

@ -0,0 +1,52 @@
# One file for every site. The ZONE env var (e.g. "home" or "dc1") selects which
# `zones.<name>` section is merged on top of the global settings, and which
# `records` apply. Nothing here requires the sites to reach each other.
upstream:
mode: recursive # or: forward (+ servers: [...])
dnssec: strict # strict | permissive | off
cache:
max_mb: 64
min_ttl: 5s
max_ttl: 24h
stale: 1h # serve-stale window while upstream is down
blocking:
mode: null # null (0.0.0.0/::) | nxdomain | refused | ip
cname_inspect: true # block CNAME-cloaked trackers
lists:
- name: stevenblack
url: https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
# - name: hagezi-pro
# url: https://raw.githubusercontent.com/hagezi/dns-blocklists/main/domains/pro.txt
# - name: my-allowlist # entries of an allow list override all block lists
# file: /etc/adblock-dns/allow.txt
# allow: true
# overrides of the lists (precedence: allow > block > list exceptions > lists)
allow: [example-allowed.com]
block: [example-blocked.com]
allow_regex: []
block_regex: ['^ads?[0-9]*\.']
records: # local DNS overrides; wildcard names supported
- {name: router.lan, type: A, value: 192.168.1.1}
- {name: nas.lan, type: A, value: 192.168.1.10, zones: [home]} # same name,
- {name: nas.lan, type: A, value: 10.20.0.10, zones: [dc1]} # per-zone answer
- {name: "*.apps.lan", type: CNAME, value: ingress.lan., zones: [home]}
forward_zones: # split-horizon / conditional forwarding (no DNSSEC)
- {zone: corp.example.com, servers: [10.20.0.53]}
zones: # per-zone settings; lists/records/allow/... are appended
home:
upstream: {mode: recursive}
sync:
peers_dns: adblock-dns-headless.dns.svc:8080
dc1:
upstream: {mode: forward, servers: ["10.20.0.2", "10.20.0.3"]}
redis: {addr: redis.dns.svc:6379}
sync:
peers_dns: adblock-dns-headless.dns.svc:8080

View file

@ -0,0 +1,6 @@
apiVersion: v2
name: adblock-dns
description: Cloud-native ad-blocking, DNSSEC-validating DNS server with optional shared cache
type: application
version: 0.1.0
appVersion: "0.1.0"

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
adblock-dns {{ .Chart.AppVersion }} deployed in zone "{{ .Values.zone }}" with {{ .Values.replicaCount }} replicas.
DNS service : {{ include "adblock-dns.fullname" . }}-dns ({{ .Values.service.dns.type }}, port {{ .Values.service.dns.port }})
Test : kubectl -n {{ .Release.Namespace }} run -it --rm dig --image=alpine -- sh -c 'apk add -q bind-tools && dig @{{ include "adblock-dns.fullname" . }}-dns example.com'
Admin API : token in secret {{ include "adblock-dns.secretName" . }} (key "token"), port 8080 on the headless service.
Note: edits to `config` roll the pods (config is read at start); API changes sync live.

View file

@ -0,0 +1,20 @@
{{- define "adblock-dns.fullname" -}}
{{- if contains .Chart.Name .Release.Name }}{{ .Release.Name | trunc 50 | trimSuffix "-" }}{{ else }}{{ printf "%s-%s" .Release.Name .Chart.Name | trunc 50 | trimSuffix "-" }}{{ end -}}
{{- end }}
{{- define "adblock-dns.labels" -}}
app.kubernetes.io/name: adblock-dns
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
{{- end }}
{{- define "adblock-dns.selector" -}}
app.kubernetes.io/name: adblock-dns
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{- define "adblock-dns.secretName" -}}
{{- if .Values.api.existingSecret }}{{ .Values.api.existingSecret }}{{ else }}{{ include "adblock-dns.fullname" . }}-api{{ end -}}
{{- end }}
{{- define "adblock-dns.redisAddr" -}}
{{- if .Values.redis.enabled }}{{ include "adblock-dns.fullname" . }}-redis:6379{{ else }}{{ .Values.redis.external.addr }}{{ end -}}
{{- end }}

View file

@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "adblock-dns.fullname" . }}
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
data:
config.yaml: |
{{- toYaml .Values.config | nindent 4 }}

View file

@ -0,0 +1,102 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "adblock-dns.fullname" . }}
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
strategy: {rollingUpdate: {maxUnavailable: 0, maxSurge: 1}}
selector:
matchLabels: {{- include "adblock-dns.selector" . | nindent 6 }}
template:
metadata:
labels:
{{- include "adblock-dns.selector" . | nindent 8 }}
{{- with .Values.podLabels }}{{ toYaml . | nindent 8 }}{{ end }}
annotations:
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
{{- with .Values.podAnnotations }}{{ toYaml . | nindent 8 }}{{ end }}
spec:
serviceAccountName: {{ include "adblock-dns.fullname" . }}
automountServiceAccountToken: false
enableServiceLinks: false
terminationGracePeriodSeconds: 20
{{- with .Values.imagePullSecrets }}
imagePullSecrets: {{- toYaml . | nindent 8 }}
{{- end }}
{{- if .Values.priorityClassName }}
priorityClassName: {{ .Values.priorityClassName }}
{{- end }}
securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }}
{{- if .Values.topologySpread }}
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels: {{- include "adblock-dns.selector" . | nindent 14 }}
{{- end }}
{{- with .Values.affinity }}
affinity: {{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector: {{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations: {{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: adblock-dns
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
securityContext: {{- toYaml .Values.securityContext | nindent 12 }}
ports:
- {name: dns-udp, containerPort: 5353, protocol: UDP}
- {name: dns-tcp, containerPort: 5353, protocol: TCP}
- {name: metrics, containerPort: 9153, protocol: TCP}
- {name: api, containerPort: 8080, protocol: TCP}
env:
- {name: ZONE, value: {{ .Values.zone | quote }}}
- {name: CONFIG_FILE, value: /etc/adblock-dns/config.yaml}
- {name: DATA_DIR, value: /data}
- {name: LOG_FORMAT, value: json}
- name: NODE_ID
valueFrom: {fieldRef: {fieldPath: metadata.name}}
- name: POD_IP
valueFrom: {fieldRef: {fieldPath: status.podIP}}
- name: API_TOKEN
valueFrom: {secretKeyRef: {name: {{ include "adblock-dns.secretName" . }}, key: token}}
{{- if .Values.sync.enabled }}
- {name: SYNC_PEERS_DNS, value: "{{ include "adblock-dns.fullname" . }}-headless.{{ .Release.Namespace }}.svc:8080"}
{{- end }}
{{- if or .Values.redis.enabled .Values.redis.external.addr }}
- {name: REDIS_ADDR, value: {{ include "adblock-dns.redisAddr" . | quote }}}
- {name: REDIS_TLS, value: {{ .Values.redis.external.tls | quote }}}
{{- end }}
{{- if .Values.redis.external.existingSecret }}
- name: REDIS_PASSWORD
valueFrom: {secretKeyRef: {name: {{ .Values.redis.external.existingSecret }}, key: password}}
{{- end }}
{{- range $k, $v := .Values.env }}
- {name: {{ $k }}, value: {{ $v | quote }}}
{{- end }}
{{- with .Values.extraEnv }}{{ toYaml . | nindent 12 }}{{ end }}
startupProbe:
httpGet: {path: /healthz, port: metrics}
periodSeconds: 2
failureThreshold: 30
livenessProbe:
httpGet: {path: /healthz, port: metrics}
periodSeconds: 10
readinessProbe:
httpGet: {path: /readyz, port: metrics}
periodSeconds: 5
resources: {{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- {name: config, mountPath: /etc/adblock-dns, readOnly: true}
- {name: data, mountPath: /data}
volumes:
- name: config
configMap: {name: {{ include "adblock-dns.fullname" . }}}
- name: data
emptyDir: {sizeLimit: {{ .Values.dataVolume.sizeLimit }}}

View file

@ -0,0 +1,12 @@
{{- if .Values.grafanaDashboard.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "adblock-dns.fullname" . }}-dashboard
labels:
{{- include "adblock-dns.labels" . | nindent 4 }}
{{- with .Values.grafanaDashboard.labels }}{{ toYaml . | nindent 4 }}{{ end }}
data:
adblock-dns.json: |
{{- .Files.Get "dashboards/adblock-dns.json" | nindent 4 }}
{{- end }}

View file

@ -0,0 +1,28 @@
{{- if .Values.networkPolicy.enabled }}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ include "adblock-dns.fullname" . }}
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
spec:
podSelector:
matchLabels: {{- include "adblock-dns.selector" . | nindent 6 }}
policyTypes: [Ingress]
ingress:
- ports: # DNS from clients
- {port: 5353, protocol: UDP}
- {port: 5353, protocol: TCP}
{{- with .Values.networkPolicy.dnsClients }}
from: {{- toYaml . | nindent 8 }}
{{- end }}
- ports: # replica sync
- {port: 8080, protocol: TCP}
from:
- podSelector:
matchLabels: {{- include "adblock-dns.selector" . | nindent 14 }}
- ports: # metrics
- {port: 9153, protocol: TCP}
{{- with .Values.networkPolicy.metricsFrom }}
from: {{- toYaml . | nindent 8 }}
{{- end }}
{{- end }}

View file

@ -0,0 +1,11 @@
{{- if and .Values.podDisruptionBudget.enabled (gt (int .Values.replicaCount) 1) }}
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: {{ include "adblock-dns.fullname" . }}
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
spec:
minAvailable: {{ .Values.podDisruptionBudget.minAvailable }}
selector:
matchLabels: {{- include "adblock-dns.selector" . | nindent 6 }}
{{- end }}

View file

@ -0,0 +1,38 @@
{{- if .Values.redis.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "adblock-dns.fullname" . }}-redis
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
app.kubernetes.io/component: cache
spec:
replicas: 1
selector:
matchLabels: {app.kubernetes.io/instance: {{ .Release.Name }}, app.kubernetes.io/component: cache}
template:
metadata:
labels: {app.kubernetes.io/name: adblock-dns, app.kubernetes.io/instance: {{ .Release.Name }}, app.kubernetes.io/component: cache}
spec:
automountServiceAccountToken: false
enableServiceLinks: false
securityContext: {runAsNonRoot: true, runAsUser: 999, runAsGroup: 999, fsGroup: 999, seccompProfile: {type: RuntimeDefault}}
containers:
- name: redis
image: "{{ .Values.redis.image.repository }}:{{ .Values.redis.image.tag }}"
args: [redis-server, --save, "", --appendonly, "no", --maxmemory, {{ .Values.redis.maxMemory | quote }}, --maxmemory-policy, allkeys-lru, --protected-mode, "no"]
ports: [{name: redis, containerPort: 6379}]
securityContext: {allowPrivilegeEscalation: false, readOnlyRootFilesystem: true, capabilities: {drop: [ALL]}}
resources: {requests: {cpu: 50m, memory: 64Mi}, limits: {memory: 192Mi}}
readinessProbe: {tcpSocket: {port: redis}}
volumeMounts: [{name: data, mountPath: /data}]
volumes: [{name: data, emptyDir: {sizeLimit: 64Mi}}]
---
apiVersion: v1
kind: Service
metadata:
name: {{ include "adblock-dns.fullname" . }}-redis
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
spec:
selector: {app.kubernetes.io/instance: {{ .Release.Name }}, app.kubernetes.io/component: cache}
ports: [{name: redis, port: 6379, targetPort: redis}]
{{- end }}

View file

@ -0,0 +1,18 @@
{{- if not .Values.api.existingSecret }}
{{- $name := printf "%s-api" (include "adblock-dns.fullname" .) }}
{{- $existing := lookup "v1" "Secret" .Release.Namespace $name }}
apiVersion: v1
kind: Secret
metadata:
name: {{ $name }}
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
type: Opaque
data:
{{- if .Values.api.token }}
token: {{ .Values.api.token | b64enc }}
{{- else if and $existing $existing.data }}
token: {{ index $existing.data "token" }}
{{- else }}
token: {{ randAlphaNum 40 | b64enc }}
{{- end }}
{{- end }}

View file

@ -0,0 +1,6 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "adblock-dns.fullname" . }}
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
automountServiceAccountToken: false

View file

@ -0,0 +1,21 @@
{{- if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ include "adblock-dns.fullname" . }}
labels:
{{- include "adblock-dns.labels" . | nindent 4 }}
{{- with .Values.serviceMonitor.labels }}{{ toYaml . | nindent 4 }}{{ end }}
spec:
selector:
matchLabels:
{{- include "adblock-dns.selector" . | nindent 6 }}
app.kubernetes.io/component: internal
endpoints:
- port: metrics
interval: {{ .Values.serviceMonitor.interval }}
relabelings:
# the dashboard filters on these labels
- {targetLabel: zone, replacement: {{ .Values.zone | default "default" | quote }}}
- {sourceLabels: [__meta_kubernetes_pod_name], targetLabel: pod}
{{- end }}

View file

@ -0,0 +1,36 @@
# Client-facing DNS service (UDP+TCP 53 → container 5353).
apiVersion: v1
kind: Service
metadata:
name: {{ include "adblock-dns.fullname" . }}-dns
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
{{- with .Values.service.dns.annotations }}
annotations: {{- toYaml . | nindent 4 }}
{{- end }}
spec:
type: {{ .Values.service.dns.type }}
{{- if .Values.service.dns.loadBalancerIP }}
loadBalancerIP: {{ .Values.service.dns.loadBalancerIP }}
{{- end }}
{{- if ne .Values.service.dns.type "ClusterIP" }}
externalTrafficPolicy: {{ .Values.service.dns.externalTrafficPolicy }}
{{- end }}
selector: {{- include "adblock-dns.selector" . | nindent 4 }}
ports:
- {name: dns-udp, port: {{ .Values.service.dns.port }}, targetPort: dns-udp, protocol: UDP}
- {name: dns-tcp, port: {{ .Values.service.dns.port }}, targetPort: dns-tcp, protocol: TCP}
---
# Headless: peer discovery for replica sync and per-pod metrics scraping.
apiVersion: v1
kind: Service
metadata:
name: {{ include "adblock-dns.fullname" . }}-headless
labels: {{- include "adblock-dns.labels" . | nindent 4 }}
app.kubernetes.io/component: internal
spec:
clusterIP: None
publishNotReadyAddresses: true
selector: {{- include "adblock-dns.selector" . | nindent 4 }}
ports:
- {name: metrics, port: 9153, targetPort: metrics}
- {name: api, port: 8080, targetPort: api}

View file

@ -0,0 +1,103 @@
image:
repository: registry.example.com/adblock-dns
tag: "" # defaults to .Chart.AppVersion
pullPolicy: IfNotPresent
imagePullSecrets: []
replicaCount: 2
# Zone of THIS installation (selects zones.<zone> in the config and per-zone records).
zone: ""
# Rendered as config.yaml (see config.example.yaml in the repo for every option).
# The same block can be used unchanged at every site; only `zone` differs.
config:
upstream: {mode: recursive}
dnssec: strict
cache: {max_mb: 64}
lists:
- name: stevenblack
url: https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
allow: []
block: []
records: []
# Extra environment variables (override config file values), e.g. LOG_LEVEL.
env: {}
# - name/value list for anything needing valueFrom
extraEnv: []
# Admin API + replica sync token. Generated once and kept across upgrades if empty.
api:
token: ""
existingSecret: "" # secret with key "token"
# Replica state sync (runtime overrides made through the API).
sync:
enabled: true
# Optional shared DNS cache (Redis protocol; Valkey works too).
redis:
enabled: false # deploy a small in-cluster, non-persistent LRU Redis
maxMemory: 128mb
image: {repository: redis, tag: 7-alpine}
external:
addr: "" # use an existing Redis instead (host:port)
tls: false
existingSecret: "" # secret with key "password" (optional)
service:
dns:
type: ClusterIP # LoadBalancer for a LAN VIP (e.g. MetalLB)
port: 53 # container listens on 5353; no capabilities needed
loadBalancerIP: ""
externalTrafficPolicy: Local # keeps client IPs; only for LoadBalancer/NodePort
annotations: {}
serviceMonitor:
enabled: false
interval: 15s
labels: {}
grafanaDashboard:
enabled: false # ConfigMap picked up by the Grafana sidecar
labels: {grafana_dashboard: "1"}
podDisruptionBudget:
enabled: true
minAvailable: 1
networkPolicy:
enabled: false
dnsClients: [] # extra `from` peers for DNS (default: anywhere)
metricsFrom: [] # `from` peers for the metrics port (default: anywhere)
# Spread replicas across nodes.
topologySpread: true
affinity: {}
nodeSelector: {}
tolerations: []
priorityClassName: ""
resources:
requests: {cpu: 100m, memory: 96Mi}
limits: {memory: 256Mi} # the app sets GOMEMLIMIT to 90% of this automatically
dataVolume:
sizeLimit: 512Mi # emptyDir caching downloaded blocklists + runtime state
podAnnotations: {}
podLabels: {}
# Minimal privileges: non-root, read-only rootfs, no capabilities, no token mount.
podSecurityContext:
runAsNonRoot: true
runAsUser: 65532
runAsGroup: 65532
fsGroup: 65532
seccompProfile: {type: RuntimeDefault}
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
privileged: false
capabilities: {drop: [ALL]}

25
go.mod
View file

@ -1,3 +1,28 @@
module jonasled.dev/jonasled/adblock-dns
go 1.27.3
require (
github.com/joho/godotenv v1.5.1
github.com/miekg/dns v1.1.73
github.com/prometheus/client_golang v1.25.0
github.com/redis/go-redis/v9 v9.23.0
github.com/sirupsen/logrus v1.10.2
golang.org/x/sync v0.25.0
gopkg.in/yaml.v3 v3.0.1
jonasled.dev/jonasled/go-libs v0.0.4
)
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.3 // indirect
github.com/prometheus/common v0.72.0 // indirect
github.com/prometheus/procfs v0.22.0 // indirect
go.uber.org/atomic v1.12.0 // indirect
golang.org/x/net v0.59.0 // indirect
golang.org/x/sys v0.48.0 // indirect
google.golang.org/protobuf v1.36.12 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)

62
go.sum Normal file
View file

@ -0,0 +1,62 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.20.1 h1:T7kKElXUMXrUJ2E9QhQhxFtcK5rPyLdsGZvdbLMPdiQ=
github.com/klauspost/compress v1.20.1/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/miekg/dns v1.1.73 h1:uhT8nJxmTrPJYClxVxTCX+CVn6qnzSiybRk72Z6DgrE=
github.com/miekg/dns v1.1.73/go.mod h1:RW2Obtfd5NZHvOFe3zYG0W8koWOQtAzyHaLo8vASBuQ=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/prometheus/client_golang v1.25.0 h1:cDtW1TcFsMi1RJc8khwCKB4VhzwV04aKqbYtNOXuiK0=
github.com/prometheus/client_golang v1.25.0/go.mod h1:j2X/4/kaQHLPnmyEU8zjmkndo1T317VpCIQEml8LTKk=
github.com/prometheus/client_model v0.6.3 h1:O0jaTVAYNxTHYInEPFJt5I3+sN8zqBtVMPTB1qyxiEo=
github.com/prometheus/client_model v0.6.3/go.mod h1:gpN5P9S7Rr6Yr92PiQ+Ixvhf6JZEkF1dnxsYL2aPBEM=
github.com/prometheus/common v0.72.0 h1:tAYsE+sPJxIncDAobm4H5aQjmox9ZxEIIqPbiffa8G4=
github.com/prometheus/common v0.72.0/go.mod h1:77NWqAQ2tXT7BIK40qjJdw5Acrsrg1TlHAnsQi3i6mk=
github.com/prometheus/procfs v0.22.0 h1:6q9+/JL9IKAPbCmBrv9n5O5Ty3NKnciV5X7YGw0oics=
github.com/prometheus/procfs v0.22.0/go.mod h1:CvmFr/GVhIjIvWJZW3tgkODBQMRIf0EyWMQLHCHab58=
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo=
github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ=
go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs=
golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
jonasled.dev/jonasled/go-libs v0.0.4 h1:TGqAkzH7WrE/nZjWAUz7sb46rjxUewH/EmDrEr2ANzk=
jonasled.dev/jonasled/go-libs v0.0.4/go.mod h1:y08q76iX3hMY+tt+fnfy9r4nyATONtSsaJ/oSDH/Aqw=

202
internal/api/api.go Normal file
View file

@ -0,0 +1,202 @@
// Package api serves the authenticated admin/sync HTTP API and the
// unauthenticated metrics/health endpoint.
package api
import (
"context"
"encoding/json"
"net/http"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"jonasled.dev/jonasled/adblock-dns/internal/cache"
"jonasled.dev/jonasled/adblock-dns/internal/config"
"jonasled.dev/jonasled/adblock-dns/internal/policy"
"jonasled.dev/jonasled/adblock-dns/internal/state"
)
type API struct {
Cfg *config.Config
Log *logrus.Logger
Policy *policy.Manager
Store *state.Store
L1 *cache.Cache
L2 *cache.Shared
Version string
Sync *state.Syncer
}
// Handler returns the authenticated API mux.
func (a *API) Handler() http.Handler {
mux := http.NewServeMux()
auth := func(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !state.CheckToken(r, a.Cfg.APIToken) {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
h(w, r)
}
}
mux.HandleFunc("GET /api/v1/status", auth(a.status))
mux.HandleFunc("GET /api/v1/state", auth(a.listState))
mux.HandleFunc("PUT /api/v1/allow/{domain}", auth(a.putDomain(state.KindAllow)))
mux.HandleFunc("DELETE /api/v1/allow/{domain}", auth(a.delDomain(state.KindAllow)))
mux.HandleFunc("PUT /api/v1/block/{domain}", auth(a.putDomain(state.KindBlock)))
mux.HandleFunc("DELETE /api/v1/block/{domain}", auth(a.delDomain(state.KindBlock)))
mux.HandleFunc("POST /api/v1/records", auth(a.putRecord))
mux.HandleFunc("DELETE /api/v1/records", auth(a.delRecord))
mux.HandleFunc("POST /api/v1/blocking", auth(a.blocking))
mux.HandleFunc("POST /api/v1/cache/flush", auth(a.flush))
if a.Sync != nil {
mux.HandleFunc("POST /sync/exchange", a.Sync.Handler())
}
return mux
}
func writeJSON(w http.ResponseWriter, code int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(v)
}
func (a *API) status(w http.ResponseWriter, _ *http.Request) {
n, b := a.L1.Stats()
writeJSON(w, 200, map[string]any{
"version": a.Version, "zone": a.Cfg.Zone, "node": a.Cfg.NodeID,
"upstream_mode": a.Cfg.UpstreamMode, "dnssec": a.Cfg.DNSSEC,
"blocking_enabled": a.Policy.BlockingEnabled(),
"cache_entries": n, "cache_bytes": b, "shared_cache": a.L2 != nil,
"state_entries": len(a.Store.Live()),
})
}
func (a *API) listState(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, 200, a.Store.Live())
}
type zonesBody struct {
Zones []string `json:"zones"`
}
func (a *API) putDomain(kind string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
d := strings.ToLower(strings.TrimSuffix(r.PathValue("domain"), "."))
if d == "" || strings.ContainsAny(d, " /\\") {
http.Error(w, "invalid domain", http.StatusBadRequest)
return
}
var b zonesBody
if r.ContentLength > 0 {
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<16)).Decode(&b); err != nil {
http.Error(w, "bad body", http.StatusBadRequest)
return
}
}
writeJSON(w, 200, a.Store.Put(kind, d, b.Zones, nil, false))
}
}
func (a *API) delDomain(kind string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
d := strings.ToLower(strings.TrimSuffix(r.PathValue("domain"), "."))
writeJSON(w, 200, a.Store.Put(kind, d, nil, nil, true))
}
}
type recordBody struct {
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
TTL uint32 `json:"ttl"`
Zones []string `json:"zones"`
}
func recordKey(b recordBody) string {
return strings.ToLower(strings.TrimSuffix(b.Name, ".")) + "|" + strings.ToUpper(b.Type) + "|" + b.Value
}
func (a *API) decodeRecord(w http.ResponseWriter, r *http.Request) (recordBody, bool) {
var b recordBody
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<16)).Decode(&b); err != nil {
http.Error(w, "bad body", http.StatusBadRequest)
return b, false
}
if _, err := policy.BuildRR(b.Name, b.Type, b.Value, b.TTL); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return b, false
}
return b, true
}
func (a *API) putRecord(w http.ResponseWriter, r *http.Request) {
b, ok := a.decodeRecord(w, r)
if !ok {
return
}
val, _ := json.Marshal(b)
writeJSON(w, 200, a.Store.Put(state.KindRecord, recordKey(b), b.Zones, val, false))
}
func (a *API) delRecord(w http.ResponseWriter, r *http.Request) {
b, ok := a.decodeRecord(w, r)
if !ok {
return
}
writeJSON(w, 200, a.Store.Put(state.KindRecord, recordKey(b), b.Zones, nil, true))
}
// blocking: {"enabled": false, "duration": "10m"} — disables blocking on all
// replicas (via sync) for the duration, or indefinitely without one.
func (a *API) blocking(w http.ResponseWriter, r *http.Request) {
var b struct {
Enabled bool `json:"enabled"`
Duration string `json:"duration"`
Zones []string `json:"zones"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<16)).Decode(&b); err != nil {
http.Error(w, "bad body", http.StatusBadRequest)
return
}
v := map[string]any{"disabled": !b.Enabled}
if !b.Enabled && b.Duration != "" {
d, err := time.ParseDuration(b.Duration)
if err != nil || d <= 0 {
http.Error(w, "invalid duration", http.StatusBadRequest)
return
}
v["until"] = time.Now().Add(d).Unix()
}
raw, _ := json.Marshal(v)
writeJSON(w, 200, a.Store.Put(state.KindBlocking, "global", b.Zones, raw, b.Enabled))
}
func (a *API) flush(w http.ResponseWriter, r *http.Request) {
a.L1.Flush()
if a.L2 != nil && r.URL.Query().Get("shared") != "false" {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
if err := a.L2.Flush(ctx); err != nil {
http.Error(w, "local cache flushed; shared cache: "+err.Error(), http.StatusBadGateway)
return
}
}
writeJSON(w, 200, map[string]string{"status": "flushed"})
}
// MetricsHandler serves /metrics, /healthz and /readyz.
func MetricsHandler(ready func() bool) http.Handler {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("ok\n")) })
mux.HandleFunc("/readyz", func(w http.ResponseWriter, _ *http.Request) {
if !ready() {
http.Error(w, "not ready", http.StatusServiceUnavailable)
return
}
_, _ = w.Write([]byte("ready\n"))
})
return mux
}

210
internal/cache/cache.go vendored Normal file
View file

@ -0,0 +1,210 @@
// Package cache implements the local (L1) DNS cache and the optional shared
// (L2, Redis) cache. Responses are stored in packed wire format so a hit is a
// copy plus an in-place TTL rewrite — no unpacking, no per-record allocation.
package cache
import (
"hash/maphash"
"sync"
"sync/atomic"
"time"
"jonasled.dev/jonasled/adblock-dns/internal/metrics"
)
const (
numShards = 128
entryOverhead = 160 // map slot + Entry struct + string header, approximate
)
// Entry is a cached response.
type Entry struct {
Msg []byte // packed response, TTLs as at Stored time (already clamped)
Stored int64 // unix seconds
TTL uint32 // freshness lifetime in seconds
HasCNAME bool // response contains a CNAME chain (needs policy re-check)
Fail bool // SERVFAIL placeholder: never served stale, never shared
// CName caches the CNAME-inspection verdict: policyGeneration<<1 | blocked.
CName atomic.Uint64
key string
prev, next *Entry
}
func (e *Entry) size() int64 { return int64(len(e.Msg)+len(e.key)) + entryOverhead }
func (e *Entry) expiry(stale int64) int64 {
if e.Fail {
return e.Stored + int64(e.TTL)
}
return e.Stored + int64(e.TTL) + stale
}
// Age returns seconds since the entry was stored.
func (e *Entry) Age(now int64) uint32 {
if now <= e.Stored {
return 0
}
return uint32(now - e.Stored)
}
type shard struct {
mu sync.Mutex
m map[string]*Entry
head, tail *Entry // head = most recent
bytes, max int64
}
// Cache is a sharded, byte-bounded LRU.
type Cache struct {
shards [numShards]shard
seed maphash.Seed
stale int64 // seconds past TTL an entry may still be served stale
}
// New creates a cache bounded to roughly maxBytes of memory.
func New(maxBytes int64, stale time.Duration) *Cache {
c := &Cache{seed: maphash.MakeSeed(), stale: int64(stale / time.Second)}
for i := range c.shards {
c.shards[i] = shard{m: map[string]*Entry{}, max: maxBytes / numShards}
}
return c
}
// MakeKey builds a cache key into dst: flags, qtype, lowercase name.
func MakeKey(dst []byte, name string, qtype uint16, flags byte) []byte {
dst = append(dst[:0], flags, byte(qtype>>8), byte(qtype))
for i := 0; i < len(name); i++ {
ch := name[i]
if ch >= 'A' && ch <= 'Z' {
ch += 'a' - 'A'
}
dst = append(dst, ch)
}
return dst
}
func (c *Cache) shardFor(key []byte) *shard {
return &c.shards[maphash.Bytes(c.seed, key)%numShards]
}
func (s *shard) unlink(e *Entry) {
if e.prev != nil {
e.prev.next = e.next
} else {
s.head = e.next
}
if e.next != nil {
e.next.prev = e.prev
} else {
s.tail = e.prev
}
e.prev, e.next = nil, nil
}
func (s *shard) pushFront(e *Entry) {
e.next = s.head
if s.head != nil {
s.head.prev = e
}
s.head = e
if s.tail == nil {
s.tail = e
}
}
func (s *shard) remove(e *Entry) {
s.unlink(e)
delete(s.m, e.key)
s.bytes -= e.size()
}
// Get returns the entry for key if it is still within TTL+stale window.
// The returned Entry must be treated as read-only.
func (c *Cache) Get(key []byte, now int64) (*Entry, bool) {
s := c.shardFor(key)
s.mu.Lock()
e, ok := s.m[string(key)] // no allocation
if ok {
if now > e.expiry(c.stale) {
s.remove(e)
ok = false
} else if s.head != e {
s.unlink(e)
s.pushFront(e)
}
}
s.mu.Unlock()
return e, ok
}
// Put inserts or replaces an entry.
func (c *Cache) Put(key []byte, e *Entry) {
e.key = string(key)
s := c.shardFor(key)
sz := e.size()
if sz > s.max {
return
}
s.mu.Lock()
if old, ok := s.m[e.key]; ok {
s.remove(old)
}
s.m[e.key] = e
s.pushFront(e)
s.bytes += sz
for s.bytes > s.max && s.tail != nil && s.tail != e {
s.remove(s.tail)
metrics.M.CacheEvictions.Inc()
}
s.mu.Unlock()
}
// Delete removes a key.
func (c *Cache) Delete(key []byte) {
s := c.shardFor(key)
s.mu.Lock()
if e, ok := s.m[string(key)]; ok {
s.remove(e)
}
s.mu.Unlock()
}
// Flush empties the cache.
func (c *Cache) Flush() {
for i := range c.shards {
s := &c.shards[i]
s.mu.Lock()
s.m, s.head, s.tail, s.bytes = map[string]*Entry{}, nil, nil, 0
s.mu.Unlock()
}
}
// Stats returns entry count and approximate bytes.
func (c *Cache) Stats() (entries int, bytes int64) {
for i := range c.shards {
s := &c.shards[i]
s.mu.Lock()
entries += len(s.m)
bytes += s.bytes
s.mu.Unlock()
}
return
}
// Sweep drops entries beyond their stale window; call periodically so memory
// of unused names is returned even without eviction pressure.
func (c *Cache) Sweep(now int64) {
for i := range c.shards {
s := &c.shards[i]
s.mu.Lock()
for e := s.tail; e != nil; {
prev := e.prev
if now > e.expiry(c.stale) {
s.remove(e)
}
e = prev
}
s.mu.Unlock()
}
}

61
internal/cache/cache_test.go vendored Normal file
View file

@ -0,0 +1,61 @@
package cache
import (
"testing"
"github.com/miekg/dns"
)
func TestAdjustTTLsAndTruncate(t *testing.T) {
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeA)
m.Response = true
m.Answer = []dns.RR{
&dns.CNAME{Hdr: dns.RR_Header{Name: "example.com.", Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: 100}, Target: "t.example.com."},
&dns.A{Hdr: dns.RR_Header{Name: "t.example.com.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 20}, A: []byte{1, 2, 3, 4}},
}
m.SetEdns0(1232, false)
m.Compress = true
wire, _ := m.Pack()
buf := append([]byte(nil), wire...)
if err := AdjustTTLs(buf, 30, 0); err != nil {
t.Fatal(err)
}
var out dns.Msg
if err := out.Unpack(buf); err != nil {
t.Fatal(err)
}
if out.Answer[0].Header().Ttl != 70 || out.Answer[1].Header().Ttl != 0 {
t.Fatalf("ttls: %d %d", out.Answer[0].Header().Ttl, out.Answer[1].Header().Ttl)
}
buf = append([]byte(nil), wire...)
_ = AdjustTTLs(buf, 0, 30)
_ = out.Unpack(buf)
if out.Answer[0].Header().Ttl != 30 {
t.Fatal("stale ttl")
}
tc := Truncated(wire)
_ = out.Unpack(tc)
if !out.Truncated || len(out.Answer) != 0 || len(out.Question) != 1 {
t.Fatal("truncate")
}
}
func TestLRUEviction(t *testing.T) {
c := New(numShards*1024, 0)
for i := 0; i < 5000; i++ {
c.Put(MakeKey(nil, "n"+string(rune(i))+".test", 1, 0), &Entry{Msg: make([]byte, 200), Stored: 1, TTL: 1000})
}
n, b := c.Stats()
if n == 0 || b > int64(numShards*1024) {
t.Fatalf("entries=%d bytes=%d exceeds budget", n, b)
}
k := MakeKey(nil, "Hit.Test", 1, 0)
c.Put(k, &Entry{Msg: []byte{1}, Stored: 10, TTL: 5})
if _, ok := c.Get(MakeKey(nil, "hit.test", 1, 0), 12); !ok {
t.Fatal("case-insensitive key")
}
if _, ok := c.Get(k, 100); ok {
t.Fatal("expired entry returned")
}
}

186
internal/cache/redis.go vendored Normal file
View file

@ -0,0 +1,186 @@
package cache
import (
"context"
"crypto/tls"
"encoding/binary"
"sync/atomic"
"time"
"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus"
"jonasled.dev/jonasled/adblock-dns/internal/metrics"
)
// Shared is the optional L2 cache. It never blocks or fails queries: every
// operation has a tight timeout, writes are asynchronous and best-effort, and a
// circuit breaker skips Redis entirely while it is unreachable.
type Shared struct {
c *redis.Client
prefix string
stale time.Duration
log *logrus.Logger
q chan writeItem
openTil atomic.Int64 // unix nanos until which the breaker is open
fails atomic.Int32
}
type writeItem struct {
key string
val []byte
ttl time.Duration
}
type SharedOptions struct {
Addr, Password, Prefix string
DB int
TLS bool
Stale time.Duration
}
func NewShared(o SharedOptions, log *logrus.Logger) *Shared {
opt := &redis.Options{
Addr: o.Addr, Password: o.Password, DB: o.DB,
DialTimeout: 250 * time.Millisecond, ReadTimeout: 60 * time.Millisecond, WriteTimeout: 100 * time.Millisecond,
PoolSize: 32, MinIdleConns: 2, MaxRetries: -1, ConnMaxIdleTime: 5 * time.Minute,
}
if o.TLS {
opt.TLSConfig = &tls.Config{MinVersion: tls.VersionTLS12}
}
s := &Shared{c: redis.NewClient(opt), prefix: o.Prefix, stale: o.Stale, log: log, q: make(chan writeItem, 8192)}
for i := 0; i < 4; i++ {
go s.writer()
}
go s.health()
return s
}
func (s *Shared) breakerOpen() bool { return time.Now().UnixNano() < s.openTil.Load() }
func (s *Shared) fail(err error) {
metrics.M.RedisErrors.Inc()
n := s.fails.Add(1)
if n >= 3 { // back off 1s .. 30s
d := time.Second << min(n-3, 5)
s.openTil.Store(time.Now().Add(d).UnixNano())
}
s.log.WithError(err).Debug("shared cache error")
}
func (s *Shared) ok() { s.fails.Store(0) }
// Get fetches an entry. Misses and errors both return false.
func (s *Shared) Get(key []byte) (*Entry, bool) {
if s.breakerOpen() {
return nil, false
}
ctx, cancel := context.WithTimeout(context.Background(), 80*time.Millisecond)
defer cancel()
b, err := s.c.Get(ctx, s.prefix+string(key)).Bytes()
if err == redis.Nil {
s.ok()
return nil, false
}
if err != nil {
s.fail(err)
return nil, false
}
s.ok()
if len(b) < 14 || b[0] != 1 {
return nil, false
}
return &Entry{
HasCNAME: b[1]&1 != 0,
Stored: int64(binary.BigEndian.Uint64(b[2:])),
TTL: binary.BigEndian.Uint32(b[10:]),
Msg: b[14:],
}, true
}
// Put queues an asynchronous write.
func (s *Shared) Put(key []byte, e *Entry) {
if s.breakerOpen() {
return
}
v := make([]byte, 14+len(e.Msg))
v[0] = 1
if e.HasCNAME {
v[1] = 1
}
binary.BigEndian.PutUint64(v[2:], uint64(e.Stored))
binary.BigEndian.PutUint32(v[10:], e.TTL)
copy(v[14:], e.Msg)
select {
case s.q <- writeItem{s.prefix + string(key), v, time.Duration(e.TTL)*time.Second + s.stale}:
default:
metrics.M.RedisDropped.Inc()
}
}
func (s *Shared) writer() {
for it := range s.q {
batch := []writeItem{it}
drain:
for len(batch) < 64 {
select {
case x := <-s.q:
batch = append(batch, x)
default:
break drain
}
}
if s.breakerOpen() {
continue
}
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
pipe := s.c.Pipeline()
for _, b := range batch {
pipe.Set(ctx, b.key, b.val, b.ttl)
}
if _, err := pipe.Exec(ctx); err != nil {
s.fail(err)
} else {
s.ok()
}
cancel()
}
}
// health pings Redis to publish the up gauge and reset the breaker on recovery.
func (s *Shared) health() {
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err := s.c.Ping(ctx).Err()
cancel()
if err != nil {
metrics.M.RedisUp.Set(0)
} else {
metrics.M.RedisUp.Set(1)
if s.fails.Load() > 0 {
s.log.Info("shared cache reachable again")
}
s.ok()
s.openTil.Store(0)
}
time.Sleep(5 * time.Second)
}
}
// Flush deletes all keys under our prefix (SCAN + UNLINK; best effort).
func (s *Shared) Flush(ctx context.Context) error {
var cursor uint64
for {
keys, next, err := s.c.Scan(ctx, cursor, s.prefix+"*", 500).Result()
if err != nil {
return err
}
if len(keys) > 0 {
if err := s.c.Unlink(ctx, keys...).Err(); err != nil {
return err
}
}
if cursor = next; cursor == 0 {
return nil
}
}
}

101
internal/cache/wire.go vendored Normal file
View file

@ -0,0 +1,101 @@
package cache
import (
"encoding/binary"
"errors"
)
var errShort = errors.New("short dns message")
// skipName returns the offset just after the (possibly compressed) name at off.
func skipName(msg []byte, off int) (int, error) {
for {
if off >= len(msg) {
return 0, errShort
}
c := msg[off]
switch c & 0xC0 {
case 0x00:
if c == 0 {
return off + 1, nil
}
off += int(c) + 1
case 0xC0:
if off+2 > len(msg) {
return 0, errShort
}
return off + 2, nil
default:
return 0, errors.New("bad label")
}
}
}
const typeOPT = 41
// AdjustTTLs rewrites every RR TTL (except OPT) in a packed message in place:
// ttl = max(ttl-age, 0), or the fixed staleTTL when stale != 0.
func AdjustTTLs(msg []byte, age uint32, stale uint32) error {
if len(msg) < 12 {
return errShort
}
qd := int(binary.BigEndian.Uint16(msg[4:]))
rrs := int(binary.BigEndian.Uint16(msg[6:])) + int(binary.BigEndian.Uint16(msg[8:])) + int(binary.BigEndian.Uint16(msg[10:]))
off := 12
var err error
for i := 0; i < qd; i++ {
if off, err = skipName(msg, off); err != nil {
return err
}
off += 4
}
for i := 0; i < rrs; i++ {
if off, err = skipName(msg, off); err != nil {
return err
}
if off+10 > len(msg) {
return errShort
}
if binary.BigEndian.Uint16(msg[off:]) != typeOPT {
ttl := binary.BigEndian.Uint32(msg[off+4:])
switch {
case stale != 0:
ttl = stale
case ttl > age:
ttl -= age
default:
ttl = 0
}
binary.BigEndian.PutUint32(msg[off+4:], ttl)
}
off += 10 + int(binary.BigEndian.Uint16(msg[off+8:]))
}
return nil
}
// QuestionEnd returns the offset after the first question (name+type+class).
func QuestionEnd(msg []byte) (int, error) {
if len(msg) < 12 {
return 0, errShort
}
off, err := skipName(msg, 12)
if err != nil {
return 0, err
}
return off + 4, nil
}
// Truncated builds a minimal TC=1 response (header + question) from msg.
func Truncated(msg []byte) []byte {
end, err := QuestionEnd(msg)
if err != nil || end > len(msg) {
return msg[:min(len(msg), 12)]
}
out := make([]byte, end)
copy(out, msg[:end])
out[2] |= 0x02 // TC
for i := 6; i < 12; i++ { // an/ns/ar counts
out[i] = 0
}
return out
}

478
internal/config/config.go Normal file
View file

@ -0,0 +1,478 @@
// Package config builds the runtime configuration.
//
// Precedence (lowest → highest):
//
// built-in defaults → YAML file (global) → YAML file (zones.<ZONE>) → environment
//
// Environment variables are read through go-libs/config and a `.env` file in the
// working directory is auto-loaded by godotenv (see cmd/adblock-dns).
package config
import (
"fmt"
"net"
"os"
"runtime"
"strconv"
"strings"
"time"
"gopkg.in/yaml.v3"
glconfig "jonasled.dev/jonasled/go-libs/config"
)
// ListConfig describes one blocklist source.
type ListConfig struct {
Name string `yaml:"name"`
URL string `yaml:"url"` // http(s):// URL
File string `yaml:"file"` // or a local file path
Format string `yaml:"format"`
Enabled *bool `yaml:"enabled"`
// Subdomains: block the domain and all subdomains. Defaults to true for
// "domains"/"adblock" formats and false for "hosts".
Subdomains *bool `yaml:"subdomains"`
// Allow marks the list as an allowlist (its entries override blocks).
Allow bool `yaml:"allow"`
}
// RecordConfig is a local DNS override.
type RecordConfig struct {
Name string `yaml:"name"` // may start with "*." for a wildcard
Type string `yaml:"type"` // A, AAAA, CNAME, TXT, MX, PTR, SRV, NS ...
Value string `yaml:"value"`
TTL uint32 `yaml:"ttl"`
Zones []string `yaml:"zones"` // restrict to these zones; empty = all
}
// ForwardZone routes a domain (and subdomains) to dedicated servers
// (split-horizon / conditional forwarding). DNSSEC validation is skipped.
type ForwardZone struct {
Zone string `yaml:"zone"`
Servers []string `yaml:"servers"`
}
type UpstreamSection struct {
Mode *string `yaml:"mode"` // recursive | forward
Servers []string `yaml:"servers"`
IPv6 *bool `yaml:"ipv6"`
Timeout *string `yaml:"timeout"`
}
type CacheSection struct {
MaxMB *int `yaml:"max_mb"`
MinTTL *string `yaml:"min_ttl"`
MaxTTL *string `yaml:"max_ttl"`
NegTTL *string `yaml:"neg_ttl"`
Stale *string `yaml:"stale"`
Prefetch *bool `yaml:"prefetch"`
}
type RedisSection struct {
Addr *string `yaml:"addr"` // host:port, comma separated for sentinel/cluster not supported
Password *string `yaml:"password"`
DB *int `yaml:"db"`
TLS *bool `yaml:"tls"`
Prefix *string `yaml:"prefix"`
}
type BlockingSection struct {
Mode *string `yaml:"mode"` // null | nxdomain | refused | ip
IPv4 *string `yaml:"ipv4"`
IPv6 *string `yaml:"ipv6"`
TTL *uint32 `yaml:"ttl"`
CNAMEInspect *bool `yaml:"cname_inspect"`
}
type SyncSection struct {
Peers []string `yaml:"peers"` // host:port of replicas (or remote sites)
PeersDNS *string `yaml:"peers_dns"` // headless service host:port
Interval *string `yaml:"interval"`
}
// Section is everything that can differ per zone.
type Section struct {
Upstream *UpstreamSection `yaml:"upstream"`
DNSSEC *string `yaml:"dnssec"` // strict | permissive | off
Cache *CacheSection `yaml:"cache"`
Redis *RedisSection `yaml:"redis"`
Blocking *BlockingSection `yaml:"blocking"`
Sync *SyncSection `yaml:"sync"`
Lists []ListConfig `yaml:"lists"`
Allow []string `yaml:"allow"`
Block []string `yaml:"block"`
AllowRegex []string `yaml:"allow_regex"`
BlockRegex []string `yaml:"block_regex"`
Records []RecordConfig `yaml:"records"`
ForwardZones []ForwardZone `yaml:"forward_zones"`
}
// File is the YAML document layout.
type File struct {
Section `yaml:",inline"`
Zones map[string]Section `yaml:"zones"`
}
// Config is the fully-resolved configuration.
type Config struct {
Zone string
NodeID string
DNSAddr string
Listeners int
ACL []*net.IPNet
DataDir string
LogFormat string
MetricsAdr string
APIAddr string
APIToken string
TLSCert string
TLSKey string
UpstreamMode string
UpstreamServers []string
UpstreamTimeout time.Duration
IPv6 bool
DNSSEC string
TrustAnchors []string
CacheMaxBytes int64
MinTTL time.Duration
MaxTTL time.Duration
NegTTL time.Duration
Stale time.Duration
Prefetch bool
RedisAddr string
RedisPassword string
RedisDB int
RedisTLS bool
RedisPrefix string
BlockMode string
BlockIPv4 net.IP
BlockIPv6 net.IP
BlockTTL uint32
CNAMEInspect bool
Lists []ListConfig
ListsRefresh time.Duration
Allow []string
Block []string
AllowRegex []string
BlockRegex []string
Records []RecordConfig
ForwardZones []ForwardZone
SyncPeers []string
SyncPeersDNS string
SyncInterval time.Duration
SyncTLS bool
SyncCAFile string
SelfIPs []string // POD_IP etc, used to skip ourselves when discovering peers
}
func def[T any](p *T, v T) T {
if p != nil {
return *p
}
return v
}
// Load resolves the configuration. It returns an error instead of exiting so it
// can be tested.
func Load() (*Config, error) {
c := &Config{
Zone: glconfig.GetOrDefault("ZONE", ""),
DNSAddr: ":5353",
DataDir: "data",
LogFormat: "text",
MetricsAdr: ":9153",
APIAddr: ":8080",
UpstreamMode: "recursive",
UpstreamTimeout: 2 * time.Second,
DNSSEC: "strict",
CacheMaxBytes: 64 << 20,
MinTTL: 5 * time.Second,
MaxTTL: 24 * time.Hour,
NegTTL: time.Hour,
Stale: time.Hour,
Prefetch: true,
RedisPrefix: "adblockdns:",
BlockMode: "null",
BlockIPv4: net.IPv4zero,
BlockIPv6: net.IPv6zero,
BlockTTL: 60,
CNAMEInspect: true,
ListsRefresh: 24 * time.Hour,
SyncInterval: 10 * time.Second,
}
host, _ := os.Hostname()
c.NodeID = glconfig.GetOrDefault("NODE_ID", host)
// --- YAML ---
path := glconfig.GetOrDefault("CONFIG_FILE", "")
if path == "" {
if _, err := os.Stat("/etc/adblock-dns/config.yaml"); err == nil {
path = "/etc/adblock-dns/config.yaml"
} else if _, err := os.Stat("config.yaml"); err == nil {
path = "config.yaml"
}
}
if path != "" {
raw, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read config file: %w", err)
}
var f File
dec := yaml.NewDecoder(strings.NewReader(string(raw)))
dec.KnownFields(true)
if err := dec.Decode(&f); err != nil {
return nil, fmt.Errorf("parse %s: %w", path, err)
}
if err := c.apply(&f.Section); err != nil {
return nil, err
}
if z, ok := f.Zones[c.Zone]; ok && c.Zone != "" {
if err := c.apply(&z); err != nil {
return nil, fmt.Errorf("zone %q: %w", c.Zone, err)
}
}
}
// --- environment ---
if err := c.applyEnv(); err != nil {
return nil, err
}
return c, c.validate()
}
func parseDur(s string) (time.Duration, error) { return time.ParseDuration(strings.TrimSpace(s)) }
func (c *Config) apply(s *Section) error {
var err error
if u := s.Upstream; u != nil {
c.UpstreamMode = def(u.Mode, c.UpstreamMode)
if u.Servers != nil {
c.UpstreamServers = u.Servers
}
c.IPv6 = def(u.IPv6, c.IPv6)
if u.Timeout != nil {
if c.UpstreamTimeout, err = parseDur(*u.Timeout); err != nil {
return err
}
}
}
c.DNSSEC = def(s.DNSSEC, c.DNSSEC)
if k := s.Cache; k != nil {
if k.MaxMB != nil {
c.CacheMaxBytes = int64(*k.MaxMB) << 20
}
for _, p := range []struct {
src *string
dst *time.Duration
}{{k.MinTTL, &c.MinTTL}, {k.MaxTTL, &c.MaxTTL}, {k.NegTTL, &c.NegTTL}, {k.Stale, &c.Stale}} {
if p.src != nil {
if *p.dst, err = parseDur(*p.src); err != nil {
return err
}
}
}
c.Prefetch = def(k.Prefetch, c.Prefetch)
}
if r := s.Redis; r != nil {
c.RedisAddr = def(r.Addr, c.RedisAddr)
c.RedisPassword = def(r.Password, c.RedisPassword)
c.RedisDB = def(r.DB, c.RedisDB)
c.RedisTLS = def(r.TLS, c.RedisTLS)
c.RedisPrefix = def(r.Prefix, c.RedisPrefix)
}
if b := s.Blocking; b != nil {
c.BlockMode = def(b.Mode, c.BlockMode)
if b.IPv4 != nil {
c.BlockIPv4 = net.ParseIP(*b.IPv4)
}
if b.IPv6 != nil {
c.BlockIPv6 = net.ParseIP(*b.IPv6)
}
c.BlockTTL = def(b.TTL, c.BlockTTL)
c.CNAMEInspect = def(b.CNAMEInspect, c.CNAMEInspect)
}
if y := s.Sync; y != nil {
c.SyncPeers = append(c.SyncPeers, y.Peers...)
c.SyncPeersDNS = def(y.PeersDNS, c.SyncPeersDNS)
if y.Interval != nil {
if c.SyncInterval, err = parseDur(*y.Interval); err != nil {
return err
}
}
}
c.Lists = append(c.Lists, s.Lists...)
c.Allow = append(c.Allow, s.Allow...)
c.Block = append(c.Block, s.Block...)
c.AllowRegex = append(c.AllowRegex, s.AllowRegex...)
c.BlockRegex = append(c.BlockRegex, s.BlockRegex...)
c.ForwardZones = append(c.ForwardZones, s.ForwardZones...)
for _, r := range s.Records {
if len(r.Zones) > 0 && !contains(r.Zones, c.Zone) {
continue
}
c.Records = append(c.Records, r)
}
return nil
}
func contains(l []string, s string) bool {
for _, x := range l {
if x == s {
return true
}
}
return false
}
func splitList(s string) []string {
var out []string
for _, p := range strings.FieldsFunc(s, func(r rune) bool { return r == ',' || r == ' ' || r == '\n' }) {
if p = strings.TrimSpace(p); p != "" {
out = append(out, p)
}
}
return out
}
func (c *Config) applyEnv() (err error) {
str := func(k string, dst *string) {
if glconfig.Exist(k) {
*dst = glconfig.GetOrDefault(k, *dst)
}
}
dur := func(k string, dst *time.Duration) {
if err == nil && glconfig.Exist(k) {
var d time.Duration
if d, err = parseDur(glconfig.GetOrDefault(k, "")); err != nil {
err = fmt.Errorf("%s: %w", k, err)
} else {
*dst = d
}
}
}
boolean := func(k string, dst *bool) {
if err == nil && glconfig.Exist(k) {
var b bool
if b, err = strconv.ParseBool(glconfig.GetOrDefault(k, "")); err != nil {
err = fmt.Errorf("%s: %w", k, err)
} else {
*dst = b
}
}
}
integer := func(k string, dst *int) {
if err == nil && glconfig.Exist(k) {
var n int
if n, err = strconv.Atoi(glconfig.GetOrDefault(k, "")); err != nil {
err = fmt.Errorf("%s: %w", k, err)
} else {
*dst = n
}
}
}
str("DNS_ADDR", &c.DNSAddr)
str("DATA_DIR", &c.DataDir)
str("LOG_FORMAT", &c.LogFormat)
str("METRICS_ADDR", &c.MetricsAdr)
str("API_ADDR", &c.APIAddr)
str("API_TOKEN", &c.APIToken)
str("API_TLS_CERT", &c.TLSCert)
str("API_TLS_KEY", &c.TLSKey)
str("UPSTREAM_MODE", &c.UpstreamMode)
if glconfig.Exist("UPSTREAM_SERVERS") {
c.UpstreamServers = splitList(glconfig.GetOrDefault("UPSTREAM_SERVERS", ""))
}
dur("UPSTREAM_TIMEOUT", &c.UpstreamTimeout)
boolean("RECURSIVE_IPV6", &c.IPv6)
str("DNSSEC_MODE", &c.DNSSEC)
if glconfig.Exist("DNSSEC_TRUST_ANCHORS") {
c.TrustAnchors = strings.Split(glconfig.GetOrDefault("DNSSEC_TRUST_ANCHORS", ""), ";")
}
if glconfig.Exist("CACHE_MAX_MB") {
var mb int
integer("CACHE_MAX_MB", &mb)
c.CacheMaxBytes = int64(mb) << 20
}
dur("CACHE_MIN_TTL", &c.MinTTL)
dur("CACHE_MAX_TTL", &c.MaxTTL)
dur("CACHE_NEG_TTL", &c.NegTTL)
dur("CACHE_STALE", &c.Stale)
boolean("CACHE_PREFETCH", &c.Prefetch)
str("REDIS_ADDR", &c.RedisAddr)
str("REDIS_PASSWORD", &c.RedisPassword)
integer("REDIS_DB", &c.RedisDB)
boolean("REDIS_TLS", &c.RedisTLS)
str("REDIS_PREFIX", &c.RedisPrefix)
str("BLOCK_MODE", &c.BlockMode)
if glconfig.Exist("BLOCK_IPV4") {
c.BlockIPv4 = net.ParseIP(glconfig.GetOrDefault("BLOCK_IPV4", ""))
}
if glconfig.Exist("BLOCK_IPV6") {
c.BlockIPv6 = net.ParseIP(glconfig.GetOrDefault("BLOCK_IPV6", ""))
}
if glconfig.Exist("BLOCK_TTL") {
var n int
integer("BLOCK_TTL", &n)
c.BlockTTL = uint32(n)
}
boolean("CNAME_INSPECT", &c.CNAMEInspect)
dur("LISTS_REFRESH", &c.ListsRefresh)
if glconfig.Exist("SYNC_PEERS") {
c.SyncPeers = splitList(glconfig.GetOrDefault("SYNC_PEERS", ""))
}
str("SYNC_PEERS_DNS", &c.SyncPeersDNS)
dur("SYNC_INTERVAL", &c.SyncInterval)
boolean("SYNC_TLS", &c.SyncTLS)
str("SYNC_CA_FILE", &c.SyncCAFile)
c.SelfIPs = splitList(glconfig.GetOrDefault("POD_IP", ""))
c.Listeners = min(runtime.GOMAXPROCS(0), 8)
integer("DNS_LISTENERS", &c.Listeners)
for _, cidr := range splitList(glconfig.GetOrDefault("ACL_CIDRS", "")) {
_, n, e := net.ParseCIDR(cidr)
if e != nil {
return fmt.Errorf("ACL_CIDRS: %w", e)
}
c.ACL = append(c.ACL, n)
}
return err
}
func (c *Config) validate() error {
switch c.UpstreamMode {
case "recursive":
case "forward":
if len(c.UpstreamServers) == 0 {
return fmt.Errorf("UPSTREAM_MODE=forward requires UPSTREAM_SERVERS")
}
default:
return fmt.Errorf("invalid upstream mode %q (recursive|forward)", c.UpstreamMode)
}
switch c.DNSSEC {
case "strict", "permissive", "off":
default:
return fmt.Errorf("invalid dnssec mode %q (strict|permissive|off)", c.DNSSEC)
}
switch c.BlockMode {
case "null", "nxdomain", "refused", "ip":
default:
return fmt.Errorf("invalid block mode %q (null|nxdomain|refused|ip)", c.BlockMode)
}
if c.BlockIPv4 == nil || c.BlockIPv6 == nil {
return fmt.Errorf("invalid blocking IP")
}
if c.Listeners < 1 {
c.Listeners = 1
}
if c.APIToken == "" && (len(c.SyncPeers) > 0 || c.SyncPeersDNS != "") {
return fmt.Errorf("sync requires API_TOKEN (the sync endpoint is authenticated)")
}
return nil
}

210
internal/dnssec/negproof.go Normal file
View file

@ -0,0 +1,210 @@
package dnssec
import (
"errors"
"strings"
"github.com/miekg/dns"
)
// negProof evaluates the NSEC/NSEC3 records of an (already signature-checked)
// authority section.
type negProof struct {
zone string
nsec []*dns.NSEC
nsec3 []*dns.NSEC3
ttl uint32
optOut bool
}
const maxNSEC3Iterations = 150 // RFC 9276 §3.2 guidance
func newNegProof(rrs []dns.RR, zone string) *negProof {
p := &negProof{zone: zone, ttl: 3600}
for _, rr := range rrs {
switch x := rr.(type) {
case *dns.NSEC:
p.nsec = append(p.nsec, x)
case *dns.NSEC3:
p.nsec3 = append(p.nsec3, x)
}
if rr.Header().Ttl < p.ttl {
p.ttl = rr.Header().Ttl
}
}
return p
}
func (p *negProof) minTTL() uint32 { return p.ttl }
func has(bitmap []uint16, t uint16) bool {
for _, x := range bitmap {
if x == t {
return true
}
}
return false
}
func labelsOf(n string) []string { return dns.SplitDomainName(n) }
// canonicalCmp orders names per RFC 4034 §6.1.
func canonicalCmp(a, b string) int {
la, lb := labelsOf(strings.ToLower(a)), labelsOf(strings.ToLower(b))
i, j := len(la)-1, len(lb)-1
for i >= 0 && j >= 0 {
if c := strings.Compare(la[i], lb[j]); c != 0 {
return c
}
i--
j--
}
switch {
case i < 0 && j < 0:
return 0
case i < 0:
return -1
}
return 1
}
func nsecCovers(owner, next, name string) bool {
if canonicalCmp(owner, next) < 0 {
return canonicalCmp(owner, name) < 0 && canonicalCmp(name, next) < 0
}
return canonicalCmp(owner, name) < 0 || canonicalCmp(name, next) < 0 // last record wraps
}
// matchBitmap returns the type bitmap of the record that matches name exactly.
func (p *negProof) matchBitmap(name string) ([]uint16, bool) {
for _, n := range p.nsec {
if canon(n.Hdr.Name) == name {
return n.TypeBitMap, true
}
}
for _, n := range p.nsec3 {
if n.Match(name) {
return n.TypeBitMap, true
}
}
return nil, false
}
// covered reports whether a record covers name, and whether it is opt-out.
func (p *negProof) covered(name string) (bool, bool) {
for _, n := range p.nsec {
if nsecCovers(canon(n.Hdr.Name), canon(n.NextDomain), name) {
return true, false
}
}
for _, n := range p.nsec3 {
if n.Cover(name) {
return true, n.Flags&1 != 0
}
}
return false, false
}
// nameDoesNotExist proves that name (and therefore not a wildcard's owner) is absent.
func (p *negProof) nameDoesNotExist(name string) bool {
if len(p.nsec) > 0 {
c, _ := p.covered(name)
return c
}
ok, _ := p.closestEncloser(name)
return ok
}
// closestEncloser implements RFC 5155 §8.3 (without the wildcard part).
func (p *negProof) closestEncloser(name string) (ok, optOut bool) {
if p.tooExpensive() {
return false, false
}
labels := labelsOf(name)
for i := 1; i <= len(labels); i++ {
ce := "."
if i < len(labels) {
ce = strings.Join(labels[i:], ".") + "."
}
if !dns.IsSubDomain(p.zone, ce) {
break
}
if _, m := p.matchBitmap(ce); !m {
continue
}
nextCloser := strings.Join(labels[i-1:], ".") + "."
c, oo := p.covered(nextCloser)
return c, oo
}
return false, false
}
func (p *negProof) tooExpensive() bool {
for _, n := range p.nsec3 {
if n.Iterations > maxNSEC3Iterations {
return true
}
}
return false
}
// denies checks a negative answer for (name, qtype).
func (p *negProof) denies(name string, qtype uint16, nxdomain bool) error {
if p.tooExpensive() {
return errors.New("NSEC3 iteration count too high")
}
if bm, ok := p.matchBitmap(name); ok {
if nxdomain {
return errors.New("NXDOMAIN but the name exists per NSEC")
}
if has(bm, qtype) || (qtype != dns.TypeCNAME && has(bm, dns.TypeCNAME)) {
return errors.New("type exists per NSEC")
}
return nil
}
if len(p.nsec) > 0 {
if c, _ := p.covered(name); c {
return nil
}
return errors.New("no NSEC matches or covers the name")
}
if ok, _ := p.closestEncloser(name); ok {
return nil
}
// Wildcard NODATA (name matched by a wildcard that lacks qtype) is proven by
// the wildcard's NSEC3 match plus coverage of the next closer; accept it
// when some NSEC3 matches a wildcard under the closest encloser.
return errors.New("no NSEC3 proof for the name")
}
// dsAbsence evaluates a signed "no DS" response for a potential zone cut.
// insecure is true for an unsigned delegation (or opt-out span).
func (p *negProof) dsAbsence(name string, nxdomain bool) (insecure bool, err error) {
if p.tooExpensive() {
return true, nil // RFC 9276: treat as insecure
}
if bm, ok := p.matchBitmap(name); ok {
if has(bm, dns.TypeDS) {
return false, errors.New("NSEC says DS exists")
}
if has(bm, dns.TypeNS) && !has(bm, dns.TypeSOA) {
return true, nil
}
return false, nil
}
if len(p.nsec) > 0 {
if c, _ := p.covered(name); c {
return false, nil // name does not exist
}
return false, errors.New("no NSEC matches or covers the name")
}
ok, optOut := p.closestEncloser(name)
if !ok {
return false, errors.New("no NSEC3 proof for the name")
}
if optOut {
p.optOut = true
return true, nil // may be an unsigned delegation inside an opt-out span
}
return false, nil
}

View file

@ -0,0 +1,21 @@
package dnssec
import "testing"
func TestCanonicalOrder(t *testing.T) {
// RFC 4034 §6.1 example ordering
order := []string{"example.", "a.example.", "yljkjljk.a.example.", "Z.a.example.", "zABC.a.EXAMPLE.", "z.example.", "\\001.z.example.", "*.z.example.", "\\200.z.example."}
_ = order
if canonicalCmp("a.example.", "example.") <= 0 || canonicalCmp("example.", "a.example.") >= 0 {
t.Fatal("parent must sort before child")
}
if canonicalCmp("Z.a.example.", "z.a.example.") != 0 {
t.Fatal("case-insensitive")
}
if !nsecCovers("a.example.", "d.example.", "b.example.") || nsecCovers("a.example.", "d.example.", "e.example.") {
t.Fatal("covers")
}
if !nsecCovers("z.example.", "example.", "zz.example.") { // wrap-around record
t.Fatal("wrap")
}
}

508
internal/dnssec/validate.go Normal file
View file

@ -0,0 +1,508 @@
// Package dnssec validates DNS responses against the root trust anchor.
//
// The validator is resolver-agnostic: it fetches DS/DNSKEY records through a
// Fetcher (the recursive resolver, or a forwarder with CD=1), builds the chain
// of trust label by label, verifies RRSIGs and checks NSEC/NSEC3 denial of
// existence. Results per zone are cached.
//
// Known limitation: wildcard-denial parts of NXDOMAIN proofs are not checked
// (the closest-encloser / covering records themselves are).
package dnssec
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/miekg/dns"
"golang.org/x/sync/singleflight"
)
type Status int
const (
Insecure Status = iota // provably unsigned (no chain of trust)
Secure // validated
Bogus // validation failed
Indeterminate // could not be determined (fetch failure)
)
func (s Status) String() string {
return [...]string{"insecure", "secure", "bogus", "indeterminate"}[s]
}
// BogusError describes why validation failed.
type BogusError struct{ Reason string }
func (e *BogusError) Error() string { return "dnssec bogus: " + e.Reason }
func bogus(f string, a ...any) error { return &BogusError{fmt.Sprintf(f, a...)} }
// Fetcher retrieves a raw response including DNSSEC records.
type Fetcher func(ctx context.Context, name string, qtype uint16) (*dns.Msg, error)
// Default root trust anchors (IANA root KSK-2017 and KSK-2024).
var DefaultAnchors = []string{
". IN DS 20326 8 2 E06D44B80B8F1D39A95C0B0D7C65D08458E880409BBC683457104237C7F8EC8D",
". IN DS 38696 8 2 683D2D0ACB8C9B712A1948B27F741219298D0A450D612C483AF444A4C0FB2B16",
}
const maxCache = 50000
type zoneInfo struct {
zone string
secure bool
keys []*dns.DNSKEY
optOut bool
expires time.Time
}
type Validator struct {
fetch Fetcher
anchors []*dns.DS
now func() time.Time
mu sync.Mutex
cache map[string]*zoneInfo
sf singleflight.Group
}
func New(fetch Fetcher, anchors []string) (*Validator, error) {
if len(anchors) == 0 {
anchors = DefaultAnchors
}
v := &Validator{fetch: fetch, now: time.Now, cache: map[string]*zoneInfo{}}
for _, a := range anchors {
a = strings.TrimSpace(a)
if a == "" {
continue
}
rr, err := dns.NewRR(a)
if err != nil {
return nil, fmt.Errorf("trust anchor %q: %w", a, err)
}
ds, ok := rr.(*dns.DS)
if !ok {
return nil, fmt.Errorf("trust anchor %q: not a DS record", a)
}
v.anchors = append(v.anchors, ds)
}
return v, nil
}
var supportedAlg = map[uint8]bool{
dns.RSASHA1: true, dns.RSASHA1NSEC3SHA1: true, dns.RSASHA256: true, dns.RSASHA512: true,
dns.ECDSAP256SHA256: true, dns.ECDSAP384SHA384: true, dns.ED25519: true,
}
func parentOf(name string) string {
if i := strings.IndexByte(name, '.'); i >= 0 && i+1 < len(name) {
return name[i+1:]
}
return "."
}
func canon(n string) string { return dns.CanonicalName(n) }
// ---------------------------------------------------------------------------
// chain of trust
// enclosing returns the trust state of the zone that encloses name.
func (v *Validator) enclosing(ctx context.Context, name string) (*zoneInfo, error) {
name = canon(name)
v.mu.Lock()
if z, ok := v.cache[name]; ok && v.now().Before(z.expires) {
v.mu.Unlock()
return z, nil
}
v.mu.Unlock()
res, err, _ := v.sf.Do(name, func() (any, error) {
z, err := v.computeEnclosing(ctx, name)
if err != nil {
return nil, err
}
v.mu.Lock()
if len(v.cache) >= maxCache {
n := 0
for k := range v.cache {
delete(v.cache, k)
if n++; n > maxCache/2 {
break
}
}
}
v.cache[name] = z
v.mu.Unlock()
return z, nil
})
if err != nil {
return nil, err
}
return res.(*zoneInfo), nil
}
func ttlExpiry(now time.Time, ttl uint32) time.Time {
return now.Add(time.Duration(min(max(ttl, 60), 3600)) * time.Second)
}
func (v *Validator) computeEnclosing(ctx context.Context, name string) (*zoneInfo, error) {
if name == "." {
return v.root(ctx)
}
parent, err := v.enclosing(ctx, parentOf(name))
if err != nil {
return nil, err
}
if !parent.secure {
return parent, nil
}
resp, err := v.fetch(ctx, name, dns.TypeDS)
if err != nil {
return nil, fmt.Errorf("fetch DS %s: %w", name, err)
}
if resp.Rcode != dns.RcodeSuccess && resp.Rcode != dns.RcodeNameError {
return nil, fmt.Errorf("DS %s: %s", name, dns.RcodeToString[resp.Rcode])
}
var ds []dns.RR
var sigs []*dns.RRSIG
for _, rr := range resp.Answer {
switch x := rr.(type) {
case *dns.DS:
if canon(x.Hdr.Name) == name {
ds = append(ds, x)
}
case *dns.RRSIG:
if x.TypeCovered == dns.TypeDS && canon(x.Hdr.Name) == name {
sigs = append(sigs, x)
}
}
}
if len(ds) > 0 {
if err := v.verifyRRset(ds, sigs, parent); err != nil {
return nil, bogus("DS %s: %v", name, err)
}
var dsl []*dns.DS
for _, r := range ds {
dsl = append(dsl, r.(*dns.DS))
}
z, err := v.zoneFromDS(ctx, name, dsl)
if err != nil {
return nil, err
}
z.expires = ttlExpiry(v.now(), ds[0].Header().Ttl)
return z, nil
}
// No DS: parent must prove it (signed NODATA/NXDOMAIN).
if err := v.verifyAuthority(resp, parent); err != nil {
return nil, bogus("DS %s absence: %v", name, err)
}
np := newNegProof(resp.Ns, parent.zone)
insecure, err := np.dsAbsence(name, resp.Rcode == dns.RcodeNameError)
if err != nil {
return nil, bogus("DS %s absence: %v", name, err)
}
exp := ttlExpiry(v.now(), np.minTTL())
if insecure {
return &zoneInfo{zone: name, secure: false, expires: exp}, nil
}
cp := *parent // not a zone cut: same zone
cp.expires = exp
cp.optOut = parent.optOut || np.optOut
return &cp, nil
}
func (v *Validator) root(ctx context.Context) (*zoneInfo, error) {
z, err := v.zoneFromDS(ctx, ".", v.anchors)
if err != nil {
return nil, err
}
z.expires = ttlExpiry(v.now(), 3600)
return z, nil
}
// zoneFromDS fetches and authenticates the zone's DNSKEY RRset using ds.
func (v *Validator) zoneFromDS(ctx context.Context, zone string, dsl []*dns.DS) (*zoneInfo, error) {
usable := false
for _, d := range dsl {
if supportedAlg[d.Algorithm] && (d.DigestType == dns.SHA1 || d.DigestType == dns.SHA256 || d.DigestType == dns.SHA384) {
usable = true
}
}
if !usable {
return &zoneInfo{zone: zone, secure: false}, nil // unsupported algorithms ⇒ insecure (RFC 4035 §5.2)
}
resp, err := v.fetch(ctx, zone, dns.TypeDNSKEY)
if err != nil {
return nil, fmt.Errorf("fetch DNSKEY %s: %w", zone, err)
}
var keys []*dns.DNSKEY
var rrset []dns.RR
var sigs []*dns.RRSIG
for _, rr := range resp.Answer {
switch x := rr.(type) {
case *dns.DNSKEY:
if canon(x.Hdr.Name) == zone {
rrset = append(rrset, x)
if x.Flags&dns.ZONE != 0 && x.Protocol == 3 {
keys = append(keys, x)
}
}
case *dns.RRSIG:
if x.TypeCovered == dns.TypeDNSKEY && canon(x.Hdr.Name) == zone {
sigs = append(sigs, x)
}
}
}
if len(keys) == 0 {
return nil, bogus("no DNSKEY for %s", zone)
}
var anchored []*dns.DNSKEY
for _, k := range keys {
for _, d := range dsl {
if k.KeyTag() != d.KeyTag || k.Algorithm != d.Algorithm {
continue
}
if c := k.ToDS(d.DigestType); c != nil && strings.EqualFold(c.Digest, d.Digest) {
anchored = append(anchored, k)
}
}
}
if len(anchored) == 0 {
return nil, bogus("no DNSKEY of %s matches its DS", zone)
}
tmp := &zoneInfo{zone: zone, secure: true, keys: anchored}
if err := v.verifyRRset(rrset, sigs, tmp); err != nil {
return nil, bogus("DNSKEY %s: %v", zone, err)
}
return &zoneInfo{zone: zone, secure: true, keys: keys}, nil
}
// verifyRRset checks that at least one RRSIG validates rrset with a key of z.
func (v *Validator) verifyRRset(rrset []dns.RR, sigs []*dns.RRSIG, z *zoneInfo) error {
if len(sigs) == 0 {
return errors.New("no RRSIG")
}
now := v.now()
var last error = errors.New("no matching key")
for _, sig := range sigs {
if canon(sig.SignerName) != z.zone {
last = fmt.Errorf("signer %s is not %s", sig.SignerName, z.zone)
continue
}
if !supportedAlg[sig.Algorithm] {
last = fmt.Errorf("unsupported algorithm %d", sig.Algorithm)
continue
}
if !sig.ValidityPeriod(now) {
last = errors.New("signature expired or not yet valid")
continue
}
for _, k := range z.keys {
if k.KeyTag() != sig.KeyTag || k.Algorithm != sig.Algorithm {
continue
}
if err := sig.Verify(k, rrset); err != nil {
last = err
continue
}
return nil
}
}
return last
}
// verifyAuthority verifies every signed RRset (SOA/NSEC/NSEC3) in the
// authority section against z, and requires that at least one exists.
func (v *Validator) verifyAuthority(resp *dns.Msg, z *zoneInfo) error {
sets, sigs := groupRRs(resp.Ns)
n := 0
for k, rrs := range sets {
switch k.typ {
case dns.TypeSOA, dns.TypeNSEC, dns.TypeNSEC3:
default:
continue
}
if err := v.verifyRRset(rrs, sigs[k], z); err != nil {
return fmt.Errorf("%s %s: %w", dns.TypeToString[k.typ], k.name, err)
}
n++
}
if n == 0 {
return errors.New("no signed authority records")
}
return nil
}
type setKey struct {
name string
typ uint16
}
func groupRRs(rrs []dns.RR) (map[setKey][]dns.RR, map[setKey][]*dns.RRSIG) {
sets := map[setKey][]dns.RR{}
sigs := map[setKey][]*dns.RRSIG{}
for _, rr := range rrs {
if s, ok := rr.(*dns.RRSIG); ok {
k := setKey{canon(s.Hdr.Name), s.TypeCovered}
sigs[k] = append(sigs[k], s)
continue
}
k := setKey{canon(rr.Header().Name), rr.Header().Rrtype}
sets[k] = append(sets[k], rr)
}
return sets, sigs
}
// ---------------------------------------------------------------------------
// response validation
// Validate authenticates resp, the answer to (qname, qtype).
func (v *Validator) Validate(ctx context.Context, qname string, qtype uint16, resp *dns.Msg) (Status, error) {
qname = canon(qname)
if resp.Rcode != dns.RcodeSuccess && resp.Rcode != dns.RcodeNameError {
return Insecure, nil
}
status := Secure
downgrade := func() {
if status == Secure {
status = Insecure
}
}
sets, sigs := groupRRs(resp.Answer)
wildcard := map[string]*zoneInfo{} // owner → zone, for expansion proofs
for k, rrs := range sets {
ss := sigs[k]
if len(ss) == 0 {
info, err := v.enclosing(ctx, k.name)
if err != nil {
return v.errStatus(err)
}
if info.secure && !info.optOut {
return Bogus, bogus("unsigned %s RRset %s in secure zone %s", dns.TypeToString[k.typ], k.name, info.zone)
}
downgrade()
continue
}
signer := canon(ss[0].SignerName)
if !dns.IsSubDomain(signer, k.name) {
return Bogus, bogus("signer %s is not an ancestor of %s", signer, k.name)
}
info, err := v.enclosing(ctx, signer)
if err != nil {
return v.errStatus(err)
}
if !info.secure {
downgrade()
continue
}
if info.zone != signer {
return Bogus, bogus("signer %s is not a zone apex", signer)
}
if err := v.verifyRRset(rrs, ss, info); err != nil {
return Bogus, bogus("%s %s: %v", dns.TypeToString[k.typ], k.name, err)
}
for _, s := range ss {
if int(s.Labels) < dns.CountLabel(k.name) { // wildcard expansion
wildcard[k.name] = info
}
}
}
if len(wildcard) > 0 {
for owner, info := range wildcard {
if err := v.verifyAuthority(resp, info); err != nil {
return Bogus, bogus("wildcard answer %s without valid proof: %v", owner, err)
}
np := newNegProof(resp.Ns, info.zone)
if !np.nameDoesNotExist(owner) {
return Bogus, bogus("wildcard answer %s: no proof that the name does not exist", owner)
}
}
}
// negative / incomplete answers
final, hasData := chainEnd(resp.Answer, qname, qtype)
if !hasData && qtype != dns.TypeANY {
st, err := v.validateNegative(ctx, final, qtype, resp)
if err != nil || st == Bogus {
return st, err
}
if st == Insecure {
downgrade()
}
}
return status, nil
}
func (v *Validator) errStatus(err error) (Status, error) {
var b *BogusError
if errors.As(err, &b) {
return Bogus, err
}
return Indeterminate, err
}
// chainEnd follows the CNAME chain from qname; it returns the final name and
// whether data of qtype (or the CNAME itself when asked) was reached.
func chainEnd(answer []dns.RR, qname string, qtype uint16) (string, bool) {
cur := qname
for i := 0; i < 16; i++ {
next := ""
for _, rr := range answer {
if canon(rr.Header().Name) != cur {
continue
}
if rr.Header().Rrtype == qtype {
return cur, true
}
if c, ok := rr.(*dns.CNAME); ok {
next = canon(c.Target)
}
}
if next == "" {
return cur, false
}
cur = next
}
return cur, false
}
func (v *Validator) validateNegative(ctx context.Context, name string, qtype uint16, resp *dns.Msg) (Status, error) {
var soaOwner string
for _, rr := range resp.Ns {
if rr.Header().Rrtype == dns.TypeSOA {
soaOwner = canon(rr.Header().Name)
}
}
zoneName := soaOwner
if zoneName == "" {
zoneName = name
}
info, err := v.enclosing(ctx, zoneName)
if err != nil {
return v.errStatus(err)
}
if !info.secure {
return Insecure, nil
}
if soaOwner == "" || info.zone != soaOwner {
if info.optOut {
return Insecure, nil
}
return Bogus, bogus("negative answer for %s without signed SOA of its zone", name)
}
if err := v.verifyAuthority(resp, info); err != nil {
return Bogus, bogus("negative answer for %s: %v", name, err)
}
np := newNegProof(resp.Ns, info.zone)
if err := np.denies(name, qtype, resp.Rcode == dns.RcodeNameError); err != nil {
return Bogus, bogus("negative answer for %s: %v", name, err)
}
return Secure, nil
}

589
internal/engine/engine.go Normal file
View file

@ -0,0 +1,589 @@
// Package engine implements the query pipeline:
//
// local override → block decision → L1 cache → L2 (shared) cache →
// single-flight resolution → DNSSEC validation → store
package engine
import (
"context"
"encoding/binary"
"errors"
"net"
"net/netip"
"strings"
"sync"
"time"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
"golang.org/x/sync/singleflight"
"jonasled.dev/jonasled/adblock-dns/internal/cache"
"jonasled.dev/jonasled/adblock-dns/internal/config"
"jonasled.dev/jonasled/adblock-dns/internal/dnssec"
"jonasled.dev/jonasled/adblock-dns/internal/metrics"
"jonasled.dev/jonasled/adblock-dns/internal/policy"
"jonasled.dev/jonasled/adblock-dns/internal/resolver"
)
// cache key flag bits: the cached wire message differs per client capability.
const (
fDO = 1 << iota // client wants DNSSEC records
fCD // client disabled validation
fEDNS // request carried an OPT record
fAD // client asked for the AD bit
)
const (
maxUDP = 1232
staleServeTTL = 30 // RFC 8767 §5.2
failTTL = 5
bogusTTL = 30
)
var errBusy = errors.New("too many concurrent resolutions")
type Engine struct {
cfg *config.Config
log *logrus.Logger
pol *policy.Manager
l1 *cache.Cache
l2 *cache.Shared
router *resolver.Router
val *dnssec.Validator
validate, strict bool
acl []netip.Prefix
minTTL, maxTTL uint32
negMax uint32
staleSecs int64
sf singleflight.Group
sem chan struct{}
refreshSem chan struct{}
bufs sync.Pool
}
type Options struct {
Config *config.Config
Log *logrus.Logger
Policy *policy.Manager
L1 *cache.Cache
L2 *cache.Shared // optional
Router *resolver.Router
// Validator is nil when DNSSEC validation is off.
Validator *dnssec.Validator
}
func New(o Options) *Engine {
c := o.Config
e := &Engine{
cfg: c, log: o.Log, pol: o.Policy, l1: o.L1, l2: o.L2, router: o.Router, val: o.Validator,
validate: o.Validator != nil, strict: c.DNSSEC == "strict",
minTTL: uint32(c.MinTTL / time.Second), maxTTL: uint32(c.MaxTTL / time.Second), negMax: uint32(c.NegTTL / time.Second),
staleSecs: int64(c.Stale / time.Second),
sem: make(chan struct{}, 8192),
refreshSem: make(chan struct{}, 256),
}
e.bufs.New = func() any { b := make([]byte, 0, 2048); return &b }
for _, p := range c.ACL {
if pf, err := netip.ParsePrefix(p.String()); err == nil {
e.acl = append(e.acl, pf)
}
}
return e
}
func normName(n string) string { return strings.ToLower(strings.TrimSuffix(n, ".")) }
func (e *Engine) allowed(a net.Addr) bool {
if len(e.acl) == 0 {
return true
}
var ip net.IP
switch v := a.(type) {
case *net.UDPAddr:
ip = v.IP
case *net.TCPAddr:
ip = v.IP
default:
return false
}
addr, ok := netip.AddrFromSlice(ip)
if !ok {
return false
}
addr = addr.Unmap()
for _, p := range e.acl {
if p.Contains(addr) {
return true
}
}
return false
}
// ServeDNS implements dns.Handler.
func (e *Engine) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
start := time.Now()
m := metrics.M
if r.Response {
return // never answer responses (reflection)
}
if len(r.Question) != 1 {
m.Result[metrics.ResInvalid].Inc()
e.writeRcode(w, r, dns.RcodeFormatError)
return
}
if r.Opcode != dns.OpcodeQuery {
m.Result[metrics.ResInvalid].Inc()
e.writeRcode(w, r, dns.RcodeNotImplemented)
return
}
q := r.Question[0]
if q.Qclass != dns.ClassINET || !e.allowed(w.RemoteAddr()) {
m.Result[metrics.ResRefused].Inc()
e.writeRcode(w, r, dns.RcodeRefused)
return
}
m.QType(q.Qtype).Inc()
name := normName(q.Name)
var flags byte
if opt := r.IsEdns0(); opt != nil {
flags |= fEDNS
if opt.Do() {
flags |= fDO
}
}
if r.CheckingDisabled {
flags |= fCD
}
if r.AuthenticatedData {
flags |= fAD
}
// 1. local DNS overrides
if rrs, found := e.pol.Lookup(name, q.Qtype); found {
resp := new(dns.Msg)
resp.SetReply(r)
resp.RecursionAvailable = true
resp.Answer = rrs
e.writeMsg(w, r, resp, flags)
m.Result[metrics.ResOverride].Inc()
m.Latency[metrics.PathLocal].Observe(time.Since(start).Seconds())
return
}
if q.Qtype == dns.TypeANY { // RFC 8482
resp := new(dns.Msg)
resp.SetReply(r)
resp.RecursionAvailable = true
resp.Answer = []dns.RR{&dns.HINFO{Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeHINFO, Class: dns.ClassINET, Ttl: 3600}, Cpu: "RFC8482"}}
e.writeMsg(w, r, resp, flags)
m.Result[metrics.ResInvalid].Inc()
return
}
// 2. block decision
if d := e.pol.Check(name); d.Blocked() {
e.writeBlocked(w, r, flags)
m.Result[metrics.ResBlocked].Inc()
if d == policy.BlockedCustom {
m.Blocked[metrics.BlockCustom].Inc()
} else {
m.Blocked[metrics.BlockList].Inc()
}
m.Latency[metrics.PathLocal].Observe(time.Since(start).Seconds())
return
}
// 3. caches
var kb [320]byte
key := cache.MakeKey(kb[:0], name, q.Qtype, flags)
now := time.Now().Unix()
layer := metrics.ResCacheL1
ent, ok := e.l1.Get(key, now)
if !ok && e.l2 != nil {
if ent, ok = e.l2.Get(key); ok {
if now > ent.Stored+int64(ent.TTL)+e.staleSecs {
ok = false
} else {
layer = metrics.ResCacheL2
e.l1.Put(key, ent)
}
}
}
if ok {
if ent.HasCNAME && e.cfg.CNAMEInspect && e.pol.BlockingEnabled() && e.cnameBlocked(ent) {
e.writeBlocked(w, r, flags)
m.Result[metrics.ResBlocked].Inc()
m.Blocked[metrics.BlockCNAME].Inc()
m.Latency[metrics.PathCache].Observe(time.Since(start).Seconds())
return
}
age := ent.Age(now)
switch {
case age >= ent.TTL: // stale: answer now, refresh in the background
e.refreshAsync(string(key), name, q.Qtype, flags)
e.writeCached(w, r, ent, age, true)
m.Result[metrics.ResStale].Inc()
default:
if e.cfg.Prefetch && ent.TTL >= 10 && age*10 >= ent.TTL*9 {
e.refreshAsync(string(key), name, q.Qtype, flags)
}
e.writeCached(w, r, ent, age, false)
m.Result[layer].Inc()
}
m.Latency[metrics.PathCache].Observe(time.Since(start).Seconds())
return
}
// 4. resolve (identical concurrent misses share one resolution)
m.CacheMiss.Inc()
k := string(key)
v, err, shared := e.sf.Do(k, func() (any, error) { return e.resolveStore(k, name, q.Qtype, flags, true) })
if shared {
m.Dedup.Inc()
}
m.Latency[metrics.PathUpstream].Observe(time.Since(start).Seconds())
if err != nil || v == nil {
m.Result[metrics.ResResolved].Inc()
e.writeRcode(w, r, dns.RcodeServerFailure)
return
}
ent = v.(*cache.Entry)
if ent.HasCNAME && e.cfg.CNAMEInspect && e.pol.BlockingEnabled() && e.cnameBlocked(ent) {
e.writeBlocked(w, r, flags)
m.Result[metrics.ResBlocked].Inc()
m.Blocked[metrics.BlockCNAME].Inc()
return
}
e.writeCached(w, r, ent, ent.Age(time.Now().Unix()), false)
m.Result[metrics.ResResolved].Inc()
}
// ---------------------------------------------------------------------------
// resolution
func (e *Engine) refreshAsync(key, name string, qtype uint16, flags byte) {
select {
case e.refreshSem <- struct{}{}:
default:
return
}
metrics.M.CachePrefetch.Inc()
go func() {
defer func() { <-e.refreshSem }()
_, _, _ = e.sf.Do(key, func() (any, error) { return e.resolveStore(key, name, qtype, flags, false) })
}()
}
func (e *Engine) resolveStore(key, name string, qtype uint16, flags byte, storeFail bool) (*cache.Entry, error) {
select {
case e.sem <- struct{}{}:
defer func() { <-e.sem }()
default:
return nil, errBusy
}
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
start := time.Now()
ent, err := e.resolve(ctx, name, qtype, flags)
metrics.M.UpstreamLatency.Observe(time.Since(start).Seconds())
if err != nil {
metrics.M.UpstreamErr.Inc()
e.log.WithError(err).WithField("name", name).Debug("resolution failed")
if !storeFail {
return nil, err
}
ent = e.failEntry(name, qtype, flags, dns.RcodeServerFailure, failTTL, 0)
e.l1.Put([]byte(key), ent)
return ent, nil
}
metrics.M.UpstreamOK.Inc()
e.l1.Put([]byte(key), ent)
if e.l2 != nil && !ent.Fail {
e.l2.Put([]byte(key), ent)
}
return ent, nil
}
func (e *Engine) resolve(ctx context.Context, name string, qtype uint16, flags byte) (*cache.Entry, error) {
fq := dns.Fqdn(name)
res, conditional := e.router.Pick(name)
validate := e.validate && !conditional && flags&fCD == 0
do := flags&fDO != 0 || validate
cd := validate || flags&fCD != 0
resp, err := res.Resolve(ctx, fq, qtype, do, cd)
if err != nil {
return nil, err
}
if resp.Rcode == dns.RcodeServerFailure {
return nil, errors.New("upstream SERVFAIL")
}
ad := false
var ede uint16
if validate {
st, verr := e.val.Validate(ctx, fq, qtype, resp)
metrics.M.DNSSEC.WithLabelValues(st.String()).Inc()
switch st {
case dnssec.Secure:
ad = true
case dnssec.Bogus:
e.log.WithField("name", name).Warn(verr)
if e.strict {
return e.failEntry(name, qtype, flags, dns.RcodeServerFailure, bogusTTL, dns.ExtendedErrorCodeDNSBogus), nil
}
case dnssec.Indeterminate:
e.log.WithField("name", name).WithError(verr).Debug("dnssec indeterminate")
if e.strict {
return nil, verr
}
}
} else {
metrics.M.DNSSEC.WithLabelValues("skipped").Inc()
ad = resp.AuthenticatedData
}
msg := resp
msg.Id = 0
msg.Response, msg.Authoritative, msg.RecursionAvailable, msg.Truncated = true, false, true, false
msg.CheckingDisabled = flags&fCD != 0
msg.AuthenticatedData = ad && flags&(fDO|fAD) != 0
msg.Question = []dns.Question{{Name: fq, Qtype: qtype, Qclass: dns.ClassINET}}
msg.Extra = nil
if flags&fDO == 0 {
stripDNSSEC(msg, qtype)
}
ttl := e.clampTTLs(msg)
if flags&fEDNS != 0 {
msg.SetEdns0(maxUDP, flags&fDO != 0)
if ede != 0 {
addEDE(msg, ede)
}
}
msg.Compress = true
wire, err := msg.Pack()
if err != nil {
return nil, err
}
ent := &cache.Entry{Msg: wire, Stored: time.Now().Unix(), TTL: ttl}
for _, rr := range msg.Answer {
if _, ok := rr.(*dns.CNAME); ok {
ent.HasCNAME = true
break
}
}
if ent.HasCNAME && e.cfg.CNAMEInspect {
e.storeCNAMEVerdict(ent, msg)
}
return ent, nil
}
func (e *Engine) failEntry(name string, qtype uint16, flags byte, rcode int, ttl uint32, ede uint16) *cache.Entry {
msg := new(dns.Msg)
msg.Response, msg.RecursionAvailable, msg.Rcode = true, true, rcode
msg.Question = []dns.Question{{Name: dns.Fqdn(name), Qtype: qtype, Qclass: dns.ClassINET}}
msg.CheckingDisabled = flags&fCD != 0
if flags&fEDNS != 0 {
msg.SetEdns0(maxUDP, flags&fDO != 0)
if ede != 0 {
addEDE(msg, ede)
}
}
wire, _ := msg.Pack()
return &cache.Entry{Msg: wire, Stored: time.Now().Unix(), TTL: ttl, Fail: true}
}
func addEDE(m *dns.Msg, code uint16) {
if o := m.IsEdns0(); o != nil {
o.Option = append(o.Option, &dns.EDNS0_EDE{InfoCode: code})
}
}
// stripDNSSEC removes DNSSEC records the client did not ask for.
func stripDNSSEC(m *dns.Msg, qtype uint16) {
keep := func(rrs []dns.RR) []dns.RR {
out := rrs[:0]
for _, rr := range rrs {
switch t := rr.Header().Rrtype; t {
case dns.TypeRRSIG, dns.TypeNSEC, dns.TypeNSEC3:
if t != qtype {
continue
}
}
out = append(out, rr)
}
return out
}
m.Answer, m.Ns = keep(m.Answer), keep(m.Ns)
}
// clampTTLs applies min/max TTL policy and returns the entry's freshness.
func (e *Engine) clampTTLs(m *dns.Msg) uint32 {
clamp := func(t uint32) uint32 { return min(max(t, e.minTTL), e.maxTTL) }
ttl := e.maxTTL
empty := len(m.Answer) == 0
for _, rr := range m.Answer {
h := rr.Header()
h.Ttl = clamp(h.Ttl)
ttl = min(ttl, h.Ttl)
}
neg := uint32(0)
if empty {
neg = 30
for _, rr := range m.Ns {
if soa, ok := rr.(*dns.SOA); ok {
neg = min(min(soa.Hdr.Ttl, soa.Minttl), e.negMax)
neg = max(neg, e.minTTL)
}
}
ttl = min(ttl, neg)
}
for _, rr := range m.Ns {
h := rr.Header()
if empty {
h.Ttl = min(h.Ttl, neg)
} else {
h.Ttl = clamp(h.Ttl)
}
}
return ttl
}
// ---------------------------------------------------------------------------
// CNAME cloaking protection
func (e *Engine) targetsBlocked(m *dns.Msg) bool {
for _, rr := range m.Answer {
if c, ok := rr.(*dns.CNAME); ok && e.pol.Check(normName(c.Target)).Blocked() {
return true
}
}
return false
}
func (e *Engine) storeCNAMEVerdict(ent *cache.Entry, m *dns.Msg) {
if !e.pol.BlockingEnabled() {
ent.CName.Store(0) // verdict unknown while blocking is paused
return
}
v := e.pol.Generation() << 1
if e.targetsBlocked(m) {
v |= 1
}
ent.CName.Store(v)
}
// cnameBlocked reports whether a CNAME in the cached answer is blocked under
// the current policy, re-evaluating only when the policy changed.
func (e *Engine) cnameBlocked(ent *cache.Entry) bool {
st := ent.CName.Load()
if st>>1 == e.pol.Generation() && st != 0 {
return st&1 == 1
}
var m dns.Msg
if m.Unpack(ent.Msg) != nil {
return false
}
e.storeCNAMEVerdict(ent, &m)
return ent.CName.Load()&1 == 1
}
// ---------------------------------------------------------------------------
// writing responses
func (e *Engine) udpSize(w dns.ResponseWriter, r *dns.Msg) int {
if _, ok := w.RemoteAddr().(*net.UDPAddr); !ok {
return 65535
}
if o := r.IsEdns0(); o != nil {
return min(max(int(o.UDPSize()), 512), maxUDP)
}
return 512
}
// writeCached serves a cached wire message: copy, patch TTLs/ID/RD/question
// case in place, write. No unpacking.
func (e *Engine) writeCached(w dns.ResponseWriter, r *dns.Msg, ent *cache.Entry, age uint32, stale bool) {
bp := e.bufs.Get().(*[]byte)
buf := append((*bp)[:0], ent.Msg...)
defer func() { *bp = buf[:0]; e.bufs.Put(bp) }()
var st uint32
if stale {
st = staleServeTTL
}
if !ent.Fail {
if err := cache.AdjustTTLs(buf, age, st); err != nil {
e.writeRcode(w, r, dns.RcodeServerFailure)
return
}
}
binary.BigEndian.PutUint16(buf[0:], r.Id)
if r.RecursionDesired {
buf[2] |= 0x01
} else {
buf[2] &^= 0x01
}
// mirror the client's qname case (0x20) — same wire length as the stored name
_, _ = dns.PackDomainName(r.Question[0].Name, buf[12:], 0, nil, false)
if size := e.udpSize(w, r); len(buf) > size {
buf = cache.Truncated(buf)
}
metricsRcode(buf)
_, _ = w.Write(buf)
}
func metricsRcode(buf []byte) {
if len(buf) >= 4 {
metrics.M.ObserveRcode(int(buf[3] & 0x0F))
}
}
func (e *Engine) writeMsg(w dns.ResponseWriter, r *dns.Msg, resp *dns.Msg, flags byte) {
if flags&fEDNS != 0 && resp.IsEdns0() == nil {
resp.SetEdns0(maxUDP, flags&fDO != 0)
}
resp.Truncate(e.udpSize(w, r))
metrics.M.ObserveRcode(resp.Rcode)
_ = w.WriteMsg(resp)
}
func (e *Engine) writeRcode(w dns.ResponseWriter, r *dns.Msg, rcode int) {
resp := new(dns.Msg)
resp.SetRcode(r, rcode)
resp.RecursionAvailable = true
metrics.M.ObserveRcode(rcode)
_ = w.WriteMsg(resp)
}
// writeBlocked answers a blocked name according to the configured mode.
func (e *Engine) writeBlocked(w dns.ResponseWriter, r *dns.Msg, flags byte) {
q := r.Question[0]
resp := new(dns.Msg)
resp.SetReply(r)
resp.RecursionAvailable = true
hdr := dns.RR_Header{Name: q.Name, Class: dns.ClassINET, Ttl: e.cfg.BlockTTL}
switch e.cfg.BlockMode {
case "nxdomain":
resp.Rcode = dns.RcodeNameError
case "refused":
resp.Rcode = dns.RcodeRefused
default: // null | ip
switch q.Qtype {
case dns.TypeA:
hdr.Rrtype = dns.TypeA
if ip := e.cfg.BlockIPv4.To4(); ip != nil {
resp.Answer = []dns.RR{&dns.A{Hdr: hdr, A: ip}}
}
case dns.TypeAAAA:
hdr.Rrtype = dns.TypeAAAA
resp.Answer = []dns.RR{&dns.AAAA{Hdr: hdr, AAAA: e.cfg.BlockIPv6}}
}
}
if flags&fEDNS != 0 {
resp.SetEdns0(maxUDP, flags&fDO != 0)
addEDE(resp, dns.ExtendedErrorCodeBlocked)
}
e.writeMsg(w, r, resp, flags)
}

169
internal/metrics/metrics.go Normal file
View file

@ -0,0 +1,169 @@
// Package metrics defines all Prometheus metrics. Hot-path series are
// resolved to concrete Counter/Observer values up front so a query costs a few
// atomic adds and no map lookups.
package metrics
import (
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// Query outcome ("result" label).
const (
ResCacheL1 = iota
ResCacheL2
ResStale
ResResolved
ResBlocked
ResOverride
ResRefused
ResInvalid
numResults
)
var resultNames = [numResults]string{"cache_local", "cache_shared", "cache_stale", "resolved", "blocked", "override", "refused", "invalid"}
// Block reasons.
const (
BlockList = iota
BlockCustom
BlockCNAME
numBlock
)
// Latency paths.
const (
PathLocal = iota // blocked / override
PathCache
PathUpstream
numPaths
)
var qtypeNames = []struct {
t uint16
n string
}{
{dns.TypeA, "A"}, {dns.TypeAAAA, "AAAA"}, {dns.TypeHTTPS, "HTTPS"}, {dns.TypeSVCB, "SVCB"}, {dns.TypePTR, "PTR"},
{dns.TypeMX, "MX"}, {dns.TypeTXT, "TXT"}, {dns.TypeSRV, "SRV"}, {dns.TypeNS, "NS"}, {dns.TypeSOA, "SOA"},
{dns.TypeCNAME, "CNAME"}, {dns.TypeDS, "DS"}, {dns.TypeDNSKEY, "DNSKEY"}, {dns.TypeANY, "ANY"},
}
// M is the process-wide metric set.
var M = newMetrics()
type Metrics struct {
Result [numResults]prometheus.Counter
Blocked [numBlock]prometheus.Counter
Rcode [6]prometheus.Counter // NOERROR..REFUSED
RcodeOther prometheus.Counter
qtype map[uint16]prometheus.Counter
qOther prometheus.Counter
Latency [numPaths]prometheus.Observer
CacheMiss prometheus.Counter
CacheEvictions prometheus.Counter
CachePrefetch prometheus.Counter
RedisErrors prometheus.Counter
RedisDropped prometheus.Counter
RedisUp prometheus.Gauge
UpstreamOK prometheus.Counter
UpstreamErr prometheus.Counter
UpstreamLatency prometheus.Histogram
Dedup prometheus.Counter
DNSSEC *prometheus.CounterVec // result: secure|insecure|bogus|indeterminate|skipped
BlockingEnabled prometheus.Gauge
ListDomains *prometheus.GaugeVec
ListUpdated *prometheus.GaugeVec
ListErrors *prometheus.CounterVec
SyncPeers prometheus.Gauge
SyncOK prometheus.Counter
SyncErr prometheus.Counter
SyncLastOK prometheus.Gauge
SyncEntries prometheus.Gauge
}
func newMetrics() *Metrics {
m := &Metrics{qtype: map[uint16]prometheus.Counter{}}
q := promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "adblockdns", Name: "queries_total", Help: "DNS queries by outcome."}, []string{"result"})
for i, n := range resultNames {
m.Result[i] = q.WithLabelValues(n)
}
b := promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "adblockdns", Name: "blocked_total", Help: "Blocked queries by reason."}, []string{"reason"})
for i, n := range [numBlock]string{"list", "custom", "cname"} {
m.Blocked[i] = b.WithLabelValues(n)
}
r := promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "adblockdns", Name: "responses_total", Help: "DNS responses by rcode."}, []string{"rcode"})
for i := range m.Rcode {
m.Rcode[i] = r.WithLabelValues(dns.RcodeToString[i])
}
m.RcodeOther = r.WithLabelValues("OTHER")
t := promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "adblockdns", Name: "queries_by_type_total", Help: "DNS queries by record type."}, []string{"type"})
for _, e := range qtypeNames {
m.qtype[e.t] = t.WithLabelValues(e.n)
}
m.qOther = t.WithLabelValues("OTHER")
lat := promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "adblockdns", Name: "request_duration_seconds", Help: "Time to answer a client query.",
Buckets: []float64{.00005, .0001, .00025, .0005, .001, .0025, .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5},
}, []string{"path"})
for i, n := range [numPaths]string{"local", "cache", "upstream"} {
m.Latency[i] = lat.WithLabelValues(n)
}
m.CacheMiss = promauto.NewCounter(prometheus.CounterOpts{Namespace: "adblockdns", Name: "cache_misses_total", Help: "Queries that missed both cache layers."})
m.CacheEvictions = promauto.NewCounter(prometheus.CounterOpts{Namespace: "adblockdns", Name: "cache_evictions_total", Help: "Local cache evictions (memory pressure)."})
m.CachePrefetch = promauto.NewCounter(prometheus.CounterOpts{Namespace: "adblockdns", Name: "cache_refreshes_total", Help: "Background refreshes (prefetch and serve-stale)."})
m.RedisErrors = promauto.NewCounter(prometheus.CounterOpts{Namespace: "adblockdns", Name: "shared_cache_errors_total", Help: "Shared (Redis) cache errors."})
m.RedisDropped = promauto.NewCounter(prometheus.CounterOpts{Namespace: "adblockdns", Name: "shared_cache_dropped_writes_total", Help: "Shared cache writes dropped because the queue was full."})
m.RedisUp = promauto.NewGauge(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "shared_cache_up", Help: "1 if the shared cache is reachable."})
u := promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "adblockdns", Name: "upstream_requests_total", Help: "Resolutions performed (cache misses that were resolved)."}, []string{"outcome"})
m.UpstreamOK, m.UpstreamErr = u.WithLabelValues("ok"), u.WithLabelValues("error")
m.UpstreamLatency = promauto.NewHistogram(prometheus.HistogramOpts{Namespace: "adblockdns", Name: "upstream_duration_seconds", Help: "Resolution time on cache misses (incl. DNSSEC validation).",
Buckets: []float64{.001, .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}})
m.Dedup = promauto.NewCounter(prometheus.CounterOpts{Namespace: "adblockdns", Name: "deduplicated_queries_total", Help: "Concurrent identical misses collapsed into one resolution."})
m.DNSSEC = promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "adblockdns", Name: "dnssec_validations_total", Help: "DNSSEC validation results."}, []string{"result"})
m.BlockingEnabled = promauto.NewGauge(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "blocking_enabled", Help: "1 if ad blocking is active."})
m.ListDomains = promauto.NewGaugeVec(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "blocklist_rules", Help: "Rules parsed per list."}, []string{"list"})
m.ListUpdated = promauto.NewGaugeVec(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "blocklist_last_success_timestamp_seconds", Help: "Last successful list download."}, []string{"list"})
m.ListErrors = promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "adblockdns", Name: "blocklist_update_errors_total", Help: "Failed list downloads."}, []string{"list"})
m.SyncPeers = promauto.NewGauge(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "sync_peers", Help: "Known sync peers."})
s := promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "adblockdns", Name: "sync_exchanges_total", Help: "State exchanges with peers."}, []string{"outcome"})
m.SyncOK, m.SyncErr = s.WithLabelValues("ok"), s.WithLabelValues("error")
m.SyncLastOK = promauto.NewGauge(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "sync_last_success_timestamp_seconds", Help: "Last successful peer exchange."})
m.SyncEntries = promauto.NewGauge(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "sync_state_entries", Help: "Runtime state entries."})
return m
}
// QType returns the counter for a query type.
func (m *Metrics) QType(t uint16) prometheus.Counter {
if c, ok := m.qtype[t]; ok {
return c
}
return m.qOther
}
// ObserveRcode counts a response code.
func (m *Metrics) ObserveRcode(rc int) {
if rc >= 0 && rc < len(m.Rcode) {
m.Rcode[rc].Inc()
return
}
m.RcodeOther.Inc()
}
// RegisterInfo exposes build/zone info and cache size gauges.
func RegisterInfo(zone, node, version string, entries func() float64, bytes func() float64) {
promauto.NewGaugeFunc(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "cache_entries", Help: "Entries in the local cache."}, entries)
promauto.NewGaugeFunc(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "cache_bytes", Help: "Approximate memory used by the local cache."}, bytes)
g := promauto.NewGaugeVec(prometheus.GaugeOpts{Namespace: "adblockdns", Name: "info", Help: "Build and deployment info."}, []string{"zone", "node", "version"})
g.WithLabelValues(zone, node, version).Set(1)
}

View file

@ -0,0 +1,130 @@
package policy
// DomainSet is an immutable set of domains stored as 64-bit hashes in an
// open-addressing table (~16 bytes/domain at 50% load). Lookups do not allocate
// and walk the query name once from right to left, producing the hash of every
// label-aligned suffix on the way (FNV-1a over the reversed string).
//
// Being hash-only the set cannot enumerate its members; the false-positive
// probability for 1M entries is ~3e-8 per lookup.
type DomainSet struct {
exact table
suffix table
}
const (
fnvOffset = 14695981039346656037
fnvPrime = 1099511628211
)
type table struct {
slots []uint64
mask uint64
n int
}
func fixHash(h uint64) uint64 {
if h == 0 {
return 1
}
return h
}
func mix(h uint64) uint64 {
h ^= h >> 33
h *= 0xff51afd7ed558ccd
h ^= h >> 33
return h
}
// hashDomain hashes a lowercase, dot-less-suffix domain.
func hashDomain(s string) uint64 {
h := uint64(fnvOffset)
for i := len(s) - 1; i >= 0; i-- {
h = (h ^ uint64(s[i])) * fnvPrime
}
return fixHash(h)
}
func (t *table) has(h uint64) bool {
if t.n == 0 {
return false
}
for i := mix(h) & t.mask; ; i = (i + 1) & t.mask {
switch t.slots[i] {
case h:
return true
case 0:
return false
}
}
}
func buildTable(hs []uint64) table {
if len(hs) == 0 {
return table{}
}
size := uint64(16)
for size < uint64(len(hs))*2 {
size <<= 1
}
t := table{slots: make([]uint64, size), mask: size - 1}
for _, h := range hs {
i := mix(h) & t.mask
for {
if t.slots[i] == h {
break
}
if t.slots[i] == 0 {
t.slots[i] = h
t.n++
break
}
i = (i + 1) & t.mask
}
}
return t
}
// Match reports whether name (lowercase, no trailing dot) is in the set,
// either exactly or because one of its parent domains is a suffix entry.
func (s *DomainSet) Match(name string) bool {
if s == nil || (s.exact.n == 0 && s.suffix.n == 0) {
return false
}
h := uint64(fnvOffset)
for i := len(name) - 1; i >= 0; i-- {
h = (h ^ uint64(name[i])) * fnvPrime
if i == 0 || name[i-1] == '.' {
fh := fixHash(h)
if s.suffix.has(fh) || (i == 0 && s.exact.has(fh)) {
return true
}
}
}
return false
}
// Len returns the number of unique entries.
func (s *DomainSet) Len() int {
if s == nil {
return 0
}
return s.exact.n + s.suffix.n
}
// SetBuilder accumulates entries before freezing them into a DomainSet.
type SetBuilder struct {
exact []uint64
suffix []uint64
}
func (b *SetBuilder) AddExact(domain string) { b.exact = append(b.exact, hashDomain(domain)) }
func (b *SetBuilder) AddSuffix(domain string) { b.suffix = append(b.suffix, hashDomain(domain)) }
func (b *SetBuilder) Len() int { return len(b.exact) + len(b.suffix) }
func (b *SetBuilder) Build() *DomainSet {
s := &DomainSet{exact: buildTable(b.exact), suffix: buildTable(b.suffix)}
b.exact, b.suffix = nil, nil
return s
}

231
internal/policy/lists.go Normal file
View file

@ -0,0 +1,231 @@
package policy
import (
"context"
"encoding/json"
"fmt"
"io"
"math/rand/v2"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/sirupsen/logrus"
"jonasled.dev/jonasled/adblock-dns/internal/config"
)
// Lists is an immutable snapshot of all loaded blocklists.
type Lists struct {
Block *DomainSet
Allow *DomainSet
Rules map[string]int // rules per list
}
// ListHooks lets the caller observe list activity (metrics).
type ListHooks struct {
Loaded func(name string, rules int)
Result func(name string, ok bool)
}
// ListManager downloads, caches on disk, parses and refreshes blocklists.
type ListManager struct {
cfgs []config.ListConfig
dir string
refresh time.Duration
client *http.Client
log *logrus.Logger
apply func(*Lists)
hooks ListHooks
ready chan struct{}
}
type listMeta struct {
ETag string `json:"etag"`
LastModified string `json:"last_modified"`
Fetched time.Time `json:"fetched"`
}
var unsafeName = regexp.MustCompile(`[^a-zA-Z0-9._-]+`)
func NewListManager(cfg *config.Config, log *logrus.Logger, apply func(*Lists), hooks ListHooks) *ListManager {
var cfgs []config.ListConfig
for _, l := range cfg.Lists {
if l.Enabled != nil && !*l.Enabled {
continue
}
if l.Name == "" {
l.Name = l.URL + l.File
}
cfgs = append(cfgs, l)
}
return &ListManager{
cfgs: cfgs, dir: filepath.Join(cfg.DataDir, "lists"), refresh: cfg.ListsRefresh, log: log, apply: apply, hooks: hooks,
client: &http.Client{Timeout: 2 * time.Minute},
ready: make(chan struct{}),
}
}
// Ready is closed once the first load attempt has finished.
func (lm *ListManager) Ready() <-chan struct{} { return lm.ready }
func (lm *ListManager) path(l config.ListConfig) string {
if l.File != "" {
return l.File
}
return filepath.Join(lm.dir, unsafeName.ReplaceAllString(l.Name, "_")+".list")
}
// Run loads cached lists immediately, then keeps them fresh until ctx ends.
func (lm *ListManager) Run(ctx context.Context) {
if len(lm.cfgs) == 0 {
close(lm.ready)
return
}
_ = os.MkdirAll(lm.dir, 0o755)
lm.rebuild() // from disk cache; serves immediately even when offline
first := true
backoff := time.Minute
for {
changed, failed := lm.fetchAll(ctx)
if changed || first {
lm.rebuild()
}
if first {
close(lm.ready)
first = false
}
wait := lm.refresh + time.Duration(rand.Int64N(int64(lm.refresh/10)+1))
if failed {
wait = min(backoff, wait)
backoff = min(backoff*2, lm.refresh)
} else {
backoff = time.Minute
}
select {
case <-ctx.Done():
return
case <-time.After(wait):
}
}
}
func (lm *ListManager) fetchAll(ctx context.Context) (changed, failed bool) {
for _, l := range lm.cfgs {
if l.URL == "" {
continue
}
c, err := lm.fetch(ctx, l)
if err != nil {
lm.log.WithError(err).Warnf("blocklist %q: download failed (keeping previous copy)", l.Name)
failed = true
}
if lm.hooks.Result != nil {
lm.hooks.Result(l.Name, err == nil)
}
changed = changed || c
}
return
}
func (lm *ListManager) fetch(ctx context.Context, l config.ListConfig) (bool, error) {
p := lm.path(l)
var meta listMeta
if raw, err := os.ReadFile(p + ".meta"); err == nil {
_ = json.Unmarshal(raw, &meta)
}
_, statErr := os.Stat(p)
if statErr == nil && time.Since(meta.Fetched) < lm.refresh/2 {
return false, nil // fresh enough; avoids hammering list hosts on every pod restart
}
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, l.URL, nil)
req.Header.Set("User-Agent", "adblock-dns")
if statErr == nil {
if meta.ETag != "" {
req.Header.Set("If-None-Match", meta.ETag)
}
if meta.LastModified != "" {
req.Header.Set("If-Modified-Since", meta.LastModified)
}
}
resp, err := lm.client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotModified {
meta.Fetched = time.Now()
writeMeta(p, meta)
return false, nil
}
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("HTTP %s", resp.Status)
}
tmp := p + ".tmp"
f, err := os.Create(tmp)
if err != nil {
return false, err
}
n, err := io.Copy(f, io.LimitReader(resp.Body, 512<<20))
if cerr := f.Close(); err == nil {
err = cerr
}
if err != nil || n == 0 {
_ = os.Remove(tmp)
if err == nil {
err = fmt.Errorf("empty response")
}
return false, err
}
if err := os.Rename(tmp, p); err != nil {
return false, err
}
writeMeta(p, listMeta{ETag: resp.Header.Get("ETag"), LastModified: resp.Header.Get("Last-Modified"), Fetched: time.Now()})
lm.log.Infof("blocklist %q updated (%d bytes)", l.Name, n)
return true, nil
}
func writeMeta(p string, m listMeta) {
raw, _ := json.Marshal(m)
_ = os.WriteFile(p+".meta", raw, 0o644)
}
func (lm *ListManager) rebuild() {
var block, allow SetBuilder
rules := map[string]int{}
for _, l := range lm.cfgs {
f, err := os.Open(lm.path(l))
if err != nil {
continue
}
format := strings.ToLower(l.Format)
if format == "" {
format = "auto"
}
n, err := ParseList(f, format, l.Subdomains, l.Allow, func(kind int, d string) {
switch kind {
case RuleBlockExact:
block.AddExact(d)
case RuleBlockSuffix:
block.AddSuffix(d)
case RuleAllowExact:
allow.AddExact(d)
case RuleAllowSuffix:
allow.AddSuffix(d)
}
})
f.Close()
if err != nil {
lm.log.WithError(err).Warnf("blocklist %q: parse error", l.Name)
}
rules[l.Name] = n
if lm.hooks.Loaded != nil {
lm.hooks.Loaded(l.Name, n)
}
}
lists := &Lists{Block: block.Build(), Allow: allow.Build(), Rules: rules}
lm.log.Infof("blocklists loaded: %d blocked, %d allowed unique domains", lists.Block.Len(), lists.Allow.Len())
lm.apply(lists)
}

142
internal/policy/parse.go Normal file
View file

@ -0,0 +1,142 @@
package policy
import (
"bufio"
"io"
"net/netip"
"strings"
)
// Rule kinds produced by the parser.
const (
RuleBlockExact = iota
RuleBlockSuffix
RuleAllowExact
RuleAllowSuffix
)
var hostsIgnore = map[string]struct{}{
"localhost": {}, "localhost.localdomain": {}, "local": {}, "broadcasthost": {},
"ip6-localhost": {}, "ip6-loopback": {}, "ip6-localnet": {}, "ip6-mcastprefix": {},
"ip6-allnodes": {}, "ip6-allrouters": {}, "ip6-allhosts": {}, "0.0.0.0": {},
}
// validDomain checks and normalises a hostname. Returns "" if unusable.
func validDomain(s string) string {
s = strings.TrimSuffix(strings.ToLower(s), ".")
if s == "" || len(s) > 253 {
return ""
}
prev := byte('.')
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case c >= 'a' && c <= 'z', c >= '0' && c <= '9', c == '-', c == '_':
case c == '.':
if prev == '.' {
return ""
}
default:
return ""
}
prev = c
}
if _, err := netip.ParseAddr(s); err == nil {
return ""
}
return s
}
// ParseList streams a list and calls add(kind, domain) for each rule.
// format is "auto", "hosts", "domains" or "adblock". subdomains overrides the
// default suffix behaviour (nil = default per rule type). allowList turns all
// block rules into allow rules. It returns the number of rules emitted.
func ParseList(r io.Reader, format string, subdomains *bool, allowList bool, add func(kind int, domain string)) (int, error) {
sc := bufio.NewScanner(r)
sc.Buffer(make([]byte, 64<<10), 1<<20)
n := 0
emit := func(allow, suffix bool, d string) {
if d = validDomain(d); d == "" {
return
}
if allowList {
allow = true
}
kind := RuleBlockExact
switch {
case allow && suffix:
kind = RuleAllowSuffix
case allow:
kind = RuleAllowExact
case suffix:
kind = RuleBlockSuffix
}
add(kind, d)
n++
}
suf := func(def bool) bool {
if subdomains != nil {
return *subdomains
}
return def
}
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if line == "" || line[0] == '#' || line[0] == '!' || line[0] == '[' {
continue
}
// adblock syntax
if strings.HasPrefix(line, "||") || strings.HasPrefix(line, "@@||") {
if format == "hosts" || format == "domains" {
continue
}
allow := line[0] == '@'
rule := strings.TrimPrefix(strings.TrimPrefix(line, "@@"), "||")
if i := strings.IndexByte(rule, '$'); i >= 0 {
opts := rule[i+1:]
rule = rule[:i]
if opts != "important" && opts != "all" {
continue // option-restricted rules cannot be expressed in DNS
}
}
rule = strings.TrimSuffix(rule, "^")
if strings.ContainsAny(rule, "/*^|:?") {
continue
}
emit(allow, suf(true), rule)
continue
}
if line[0] == '@' || line[0] == '|' || line[0] == '/' {
continue
}
if i := strings.IndexByte(line, '#'); i >= 0 {
line = strings.TrimSpace(line[:i])
}
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
if _, err := netip.ParseAddr(fields[0]); err == nil { // hosts format
if format == "domains" || format == "adblock" {
continue
}
for _, h := range fields[1:] {
if _, skip := hostsIgnore[strings.ToLower(h)]; skip {
continue
}
emit(false, suf(false), h)
}
continue
}
if len(fields) != 1 || format == "hosts" || format == "adblock" {
continue
}
d := fields[0]
wild := false
if strings.HasPrefix(d, "*.") {
d, wild = d[2:], true
}
emit(false, suf(true) || wild, d)
}
return n, sc.Err()
}

291
internal/policy/policy.go Normal file
View file

@ -0,0 +1,291 @@
// Package policy decides what happens to a name before it is resolved:
// local DNS overrides, allow/block overrides and blocklists.
package policy
import (
"encoding/json"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/miekg/dns"
"jonasled.dev/jonasled/adblock-dns/internal/config"
"jonasled.dev/jonasled/adblock-dns/internal/state"
)
// Decision is the verdict for a name.
type Decision uint8
const (
Allow Decision = iota
BlockedList // matched a blocklist
BlockedCustom // matched a user block rule / regex
)
func (d Decision) Blocked() bool { return d != Allow }
type recordSet struct {
exact map[string]map[uint16][]dns.RR
wild map[string]map[uint16][]dns.RR // keyed by the name below "*."
}
type overrides struct {
allow, block map[string]struct{}
allowRe, blockRe []*regexp.Regexp
records recordSet
}
// Manager holds the live policy. All methods are safe for concurrent use and
// lock-free on the read path.
type Manager struct {
zone string
cfg *config.Config
lists atomic.Pointer[Lists]
ov atomic.Pointer[overrides]
gen atomic.Uint64
offTil atomic.Int64 // unix nanos until which blocking is disabled (MaxInt64 = indefinitely)
}
func NewManager(cfg *config.Config) (*Manager, error) {
m := &Manager{zone: cfg.Zone, cfg: cfg}
m.lists.Store(&Lists{})
if err := m.Rebuild(nil); err != nil {
return nil, err
}
return m, nil
}
// BuildRR creates a resource record from simple text fields.
func BuildRR(name, typ, value string, ttl uint32) (dns.RR, error) {
if ttl == 0 {
ttl = 300
}
typ = strings.ToUpper(strings.TrimSpace(typ))
if typ == "TXT" && !strings.HasPrefix(value, `"`) {
value = strconv.Quote(value)
}
rr, err := dns.NewRR(fmt.Sprintf("%s %d IN %s %s", dns.Fqdn(strings.ToLower(name)), ttl, typ, value))
if err != nil {
return nil, fmt.Errorf("record %s %s %q: %w", name, typ, value, err)
}
if rr == nil {
return nil, fmt.Errorf("record %s %s %q: empty", name, typ, value)
}
return rr, nil
}
func norm(d string) string { return strings.TrimSuffix(strings.ToLower(strings.TrimSpace(d)), ".") }
func (o *overrides) addRecord(spec config.RecordConfig) error {
rr, err := BuildRR(spec.Name, spec.Type, spec.Value, spec.TTL)
if err != nil {
return err
}
n := norm(rr.Header().Name)
target := o.records.exact
if strings.HasPrefix(n, "*.") {
n, target = n[2:], o.records.wild
}
if target[n] == nil {
target[n] = map[uint16][]dns.RR{}
}
t := rr.Header().Rrtype
target[n][t] = append(target[n][t], rr)
return nil
}
// Rebuild recomputes overrides from static config plus runtime entries.
func (m *Manager) Rebuild(live []state.Entry) error {
o := &overrides{
allow: map[string]struct{}{}, block: map[string]struct{}{},
records: recordSet{exact: map[string]map[uint16][]dns.RR{}, wild: map[string]map[uint16][]dns.RR{}},
}
for _, d := range m.cfg.Allow {
o.allow[norm(d)] = struct{}{}
}
for _, d := range m.cfg.Block {
o.block[norm(d)] = struct{}{}
}
for _, p := range m.cfg.AllowRegex {
re, err := regexp.Compile(p)
if err != nil {
return fmt.Errorf("allow_regex %q: %w", p, err)
}
o.allowRe = append(o.allowRe, re)
}
for _, p := range m.cfg.BlockRegex {
re, err := regexp.Compile(p)
if err != nil {
return fmt.Errorf("block_regex %q: %w", p, err)
}
o.blockRe = append(o.blockRe, re)
}
for _, r := range m.cfg.Records {
if err := o.addRecord(r); err != nil {
return err
}
}
var off int64
for _, e := range live {
if !e.AppliesTo(m.zone) {
continue
}
switch e.Kind {
case state.KindAllow:
o.allow[norm(e.Key)] = struct{}{}
case state.KindBlock:
o.block[norm(e.Key)] = struct{}{}
case state.KindRecord:
var spec config.RecordConfig
if json.Unmarshal(e.Value, &spec) == nil {
_ = o.addRecord(spec) // runtime entries were validated on input; ignore stragglers
}
case state.KindBlocking:
var v struct {
Disabled bool `json:"disabled"`
Until int64 `json:"until"` // unix seconds, 0 = indefinitely
}
if json.Unmarshal(e.Value, &v) == nil && v.Disabled {
switch {
case v.Until == 0:
off = math.MaxInt64
case v.Until*1e9 > time.Now().UnixNano():
off = v.Until * 1e9
}
}
}
}
m.offTil.Store(off)
m.ov.Store(o)
m.gen.Add(1)
return nil
}
// SetLists swaps in freshly built blocklists.
func (m *Manager) SetLists(l *Lists) {
m.lists.Store(l)
m.gen.Add(1)
}
// Generation changes whenever the policy changes.
func (m *Manager) Generation() uint64 { return m.gen.Load() }
// BlockingEnabled reports whether blocking is currently active.
func (m *Manager) BlockingEnabled() bool {
off := m.offTil.Load()
return off == 0 || (off != math.MaxInt64 && time.Now().UnixNano() >= off)
}
func suffixIn(m map[string]struct{}, name string) bool {
if len(m) == 0 {
return false
}
for i := 0; ; {
if _, ok := m[name[i:]]; ok {
return true
}
j := strings.IndexByte(name[i:], '.')
if j < 0 {
return false
}
i += j + 1
}
}
func anyRe(res []*regexp.Regexp, name string) bool {
for _, re := range res {
if re.MatchString(name) {
return true
}
}
return false
}
// Check classifies name (lowercase, without trailing dot).
// Precedence: user allow > user block > list allow (exceptions) > list block.
func (m *Manager) Check(name string) Decision {
if !m.BlockingEnabled() {
return Allow
}
o := m.ov.Load()
if suffixIn(o.allow, name) || anyRe(o.allowRe, name) {
return Allow
}
if suffixIn(o.block, name) || anyRe(o.blockRe, name) {
return BlockedCustom
}
l := m.lists.Load()
if l.Allow.Match(name) {
return Allow
}
if l.Block.Match(name) {
return BlockedList
}
return Allow
}
// Lookup returns local override records for name/qtype. found reports that
// the name is locally defined (so an empty result means NODATA).
func (m *Manager) Lookup(name string, qtype uint16) (rrs []dns.RR, found bool) {
o := m.ov.Load()
if len(o.records.exact) == 0 && len(o.records.wild) == 0 {
return nil, false
}
return o.records.lookup(name, qtype, 0)
}
func (rs *recordSet) find(name string) (sets map[uint16][]dns.RR, wildcard bool) {
if s, ok := rs.exact[name]; ok {
return s, false
}
if len(rs.wild) == 0 {
return nil, false
}
for i := 0; ; {
j := strings.IndexByte(name[i:], '.')
if j < 0 {
return nil, false
}
i += j + 1
if s, ok := rs.wild[name[i:]]; ok {
return s, true
}
}
}
func (rs *recordSet) lookup(name string, qtype uint16, depth int) ([]dns.RR, bool) {
sets, wildcard := rs.find(name)
if sets == nil {
return nil, false
}
var src []dns.RR
switch {
case qtype == dns.TypeANY:
for _, l := range sets {
src = append(src, l...)
}
case len(sets[qtype]) > 0:
src = sets[qtype]
case len(sets[dns.TypeCNAME]) > 0:
src = sets[dns.TypeCNAME]
}
out := make([]dns.RR, 0, len(src)+1)
fq := dns.Fqdn(name)
for _, rr := range src {
c := dns.Copy(rr)
if wildcard {
c.Header().Name = fq
}
out = append(out, c)
if cn, ok := rr.(*dns.CNAME); ok && depth < 4 && qtype != dns.TypeCNAME {
more, _ := rs.lookup(norm(cn.Target), qtype, depth+1)
out = append(out, more...)
}
}
return out, true
}

View file

@ -0,0 +1,123 @@
package policy
import (
"strings"
"testing"
"github.com/miekg/dns"
"jonasled.dev/jonasled/adblock-dns/internal/config"
)
func parse(t *testing.T, in string) (b, a SetBuilder) {
t.Helper()
_, err := ParseList(strings.NewReader(in), "auto", nil, false, func(k int, d string) {
switch k {
case RuleBlockExact:
b.AddExact(d)
case RuleBlockSuffix:
b.AddSuffix(d)
case RuleAllowExact:
a.AddExact(d)
case RuleAllowSuffix:
a.AddSuffix(d)
}
})
if err != nil {
t.Fatal(err)
}
return
}
func TestParseAndMatch(t *testing.T) {
b, a := parse(t, `# comment
0.0.0.0 hosts-only.example
127.0.0.1 localhost
||adblock.example^
||opt.example^$third-party
@@||allowed.adblock.example^
plain.example
*.wild.example
! another comment
`)
blk, allow := b.Build(), a.Build()
for name, want := range map[string]bool{
"hosts-only.example": true,
"sub.hosts-only.example": false, // hosts format is exact-only
"adblock.example": true,
"x.y.adblock.example": true,
"opt.example": false, // option-restricted rule skipped
"plain.example": true,
"a.plain.example": true,
"a.wild.example": true,
"localhost": false,
"notadblock.example": false, // label-aligned suffix only
"example": false,
} {
if got := blk.Match(name); got != want {
t.Errorf("block %q = %v, want %v", name, got, want)
}
}
if !allow.Match("allowed.adblock.example") || allow.Match("adblock.example") {
t.Error("allow rule mismatch")
}
}
func TestDomainSetMany(t *testing.T) {
var b SetBuilder
for i := 0; i < 200000; i++ {
b.AddSuffix(strings.Repeat("a", i%20+1) + "." + string(rune('a'+i%26)) + ".test")
}
s := b.Build()
if !s.Match("x.aaa.a.test") || s.Match("nothing.example") {
t.Fatal("unexpected match result")
}
}
func newMgr(t *testing.T, mut func(*config.Config)) *Manager {
t.Helper()
c := &config.Config{Zone: "home"}
mut(c)
m, err := NewManager(c)
if err != nil {
t.Fatal(err)
}
return m
}
func TestPrecedenceAndRecords(t *testing.T) {
m := newMgr(t, func(c *config.Config) {
c.Allow = []string{"keep.blocked.example"}
c.Block = []string{"custom.example"}
c.BlockRegex = []string{`^ads?\d+\.`}
c.Records = []config.RecordConfig{
{Name: "nas.lan", Type: "A", Value: "192.168.1.5"},
{Name: "*.apps.lan", Type: "CNAME", Value: "nas.lan."},
{Name: "t.lan", Type: "TXT", Value: "hello world"},
}
})
var b SetBuilder
b.AddSuffix("blocked.example")
m.SetLists(&Lists{Block: b.Build()})
cases := map[string]Decision{
"x.blocked.example": BlockedList, "keep.blocked.example": Allow, "sub.keep.blocked.example": Allow,
"custom.example": BlockedCustom, "ad12.foo.com": BlockedCustom, "fine.com": Allow,
}
for n, want := range cases {
if got := m.Check(n); got != want {
t.Errorf("Check(%q)=%v want %v", n, got, want)
}
}
if rr, ok := m.Lookup("nas.lan", dns.TypeA); !ok || len(rr) != 1 {
t.Fatal("exact record")
}
if rr, ok := m.Lookup("nas.lan", dns.TypeAAAA); !ok || len(rr) != 0 {
t.Fatal("expected NODATA for AAAA")
}
// wildcard CNAME is followed to the local A record
if rr, ok := m.Lookup("web.apps.lan", dns.TypeA); !ok || len(rr) != 2 || rr[0].Header().Name != "web.apps.lan." {
t.Fatalf("wildcard cname chain: %v", rr)
}
if _, ok := m.Lookup("unknown.lan", dns.TypeA); ok {
t.Fatal("unknown name must not be local")
}
}

View file

@ -0,0 +1,301 @@
package resolver
import (
"context"
"crypto/rand"
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
"net"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
// Forwarder sends queries to a set of upstream servers. Plain DNS uses a few
// long-lived, multiplexed UDP sockets per server (no socket per query), falling
// back to pooled TCP on truncation. "tls://host:853[#servername]" uses pooled DoT.
type Forwarder struct {
ups []*upstream
timeout time.Duration
log *logrus.Logger
}
type upstream struct {
addr string
dot bool
tlsName string
muxes []*udpMux
next atomic.Uint32
pool chan *dns.Conn
srtt atomic.Int64 // smoothed RTT in ns; failures inflate it
}
func NewForwarder(servers []string, timeout time.Duration, log *logrus.Logger) (*Forwarder, error) {
f := &Forwarder{timeout: timeout, log: log}
for _, s := range servers {
u := &upstream{pool: make(chan *dns.Conn, 8)}
switch {
case strings.HasPrefix(s, "tls://"):
s = strings.TrimPrefix(s, "tls://")
u.dot = true
if h, name, ok := strings.Cut(s, "#"); ok {
s, u.tlsName = h, name
}
if _, _, err := net.SplitHostPort(s); err != nil {
s = net.JoinHostPort(s, "853")
}
if u.tlsName == "" {
u.tlsName, _, _ = net.SplitHostPort(s)
}
default:
if _, _, err := net.SplitHostPort(s); err != nil {
s = net.JoinHostPort(strings.Trim(s, "[]"), "53")
}
}
u.addr = s
if !u.dot {
for i := 0; i < 4; i++ {
u.muxes = append(u.muxes, newUDPMux(s, log))
}
}
u.srtt.Store(int64(5 * time.Millisecond))
f.ups = append(f.ups, u)
}
if len(f.ups) == 0 {
return nil, ErrNoServers
}
return f, nil
}
// order returns upstreams sorted by smoothed RTT (best first).
func (f *Forwarder) order() []*upstream {
o := append([]*upstream(nil), f.ups...)
sort.Slice(o, func(i, j int) bool { return o[i].srtt.Load() < o[j].srtt.Load() })
return o
}
func (f *Forwarder) Resolve(ctx context.Context, name string, qtype uint16, do, cd bool) (*dns.Msg, error) {
q := newQuery(name, qtype, do, cd, true)
var lastErr error
for _, u := range f.order() {
if ctx.Err() != nil {
return nil, ctx.Err()
}
actx, cancel := context.WithTimeout(ctx, f.timeout)
start := time.Now()
resp, err := u.exchange(actx, q)
cancel()
if err == nil && resp.Rcode != dns.RcodeServerFailure && resp.Rcode != dns.RcodeRefused {
u.observe(time.Since(start), true)
return resp, nil
}
u.observe(time.Since(start), false)
if err == nil {
err = fmt.Errorf("upstream %s answered %s", u.addr, dns.RcodeToString[resp.Rcode])
if resp.Rcode == dns.RcodeServerFailure { // may be a legit bogus/SERVFAIL; remember in case all fail
lastErr = err
if len(f.ups) == 1 {
return resp, nil
}
continue
}
}
lastErr = err
}
if lastErr == nil {
lastErr = ErrNoServers
}
return nil, lastErr
}
func (u *upstream) observe(d time.Duration, ok bool) {
old := u.srtt.Load()
if !ok {
u.srtt.Store(min(old*2+int64(50*time.Millisecond), int64(10*time.Second)))
return
}
u.srtt.Store((old*7 + int64(d)) / 8)
}
func (u *upstream) exchange(ctx context.Context, q *dns.Msg) (*dns.Msg, error) {
if u.dot {
return u.exchangeStream(ctx, q)
}
m := u.muxes[u.next.Add(1)%uint32(len(u.muxes))]
resp, err := m.exchange(ctx, q)
if err != nil {
return nil, err
}
if resp.Truncated {
return u.exchangeStream(ctx, q)
}
return resp, nil
}
// exchangeStream uses a pooled TCP/TLS connection, redialing once if a pooled
// connection turns out to be stale.
func (u *upstream) exchangeStream(ctx context.Context, q *dns.Msg) (*dns.Msg, error) {
for attempt := 0; attempt < 2; attempt++ {
var conn *dns.Conn
pooled := false
select {
case conn = <-u.pool:
pooled = true
default:
d := &net.Dialer{Timeout: 2 * time.Second}
var raw net.Conn
var err error
if u.dot {
raw, err = tls.DialWithDialer(d, "tcp", u.addr, &tls.Config{ServerName: u.tlsName, MinVersion: tls.VersionTLS12})
} else {
raw, err = d.DialContext(ctx, "tcp", u.addr)
}
if err != nil {
return nil, err
}
conn = &dns.Conn{Conn: raw}
}
if dl, ok := ctx.Deadline(); ok {
_ = conn.SetDeadline(dl)
}
c := dns.Client{}
resp, _, err := c.ExchangeWithConnContext(ctx, q, conn)
if err != nil {
conn.Close()
if pooled {
continue
}
return nil, err
}
_ = conn.SetDeadline(time.Time{})
select {
case u.pool <- conn:
default:
conn.Close()
}
return resp, nil
}
return nil, errors.New("stream exchange failed")
}
// ---------------------------------------------------------------------------
// udpMux multiplexes many in-flight queries over one connected UDP socket,
// matching responses by message ID.
type udpMux struct {
addr string
log *logrus.Logger
mu sync.Mutex
conn net.Conn
inflight map[uint16]chan []byte
}
func newUDPMux(addr string, log *logrus.Logger) *udpMux {
return &udpMux{addr: addr, log: log, inflight: map[uint16]chan []byte{}}
}
func (m *udpMux) connect() (net.Conn, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.conn != nil {
return m.conn, nil
}
c, err := net.DialTimeout("udp", m.addr, 2*time.Second)
if err != nil {
return nil, err
}
m.conn = c
go m.readLoop(c)
return c, nil
}
func (m *udpMux) readLoop(c net.Conn) {
buf := make([]byte, 65535)
for {
n, err := c.Read(buf)
if err != nil {
m.mu.Lock()
if m.conn == c {
m.conn = nil
}
m.mu.Unlock()
c.Close()
return
}
if n < 12 {
continue
}
id := binary.BigEndian.Uint16(buf)
m.mu.Lock()
ch := m.inflight[id]
m.mu.Unlock()
if ch != nil {
select {
case ch <- append([]byte(nil), buf[:n]...):
default:
}
}
}
}
func (m *udpMux) exchange(ctx context.Context, q *dns.Msg) (*dns.Msg, error) {
c, err := m.connect()
if err != nil {
return nil, err
}
ch := make(chan []byte, 1)
m.mu.Lock()
var id uint16
for i := 0; ; i++ {
var b [2]byte
_, _ = rand.Read(b[:])
id = binary.BigEndian.Uint16(b[:])
if _, used := m.inflight[id]; !used {
break
}
if i > 16 {
m.mu.Unlock()
return nil, errors.New("udp mux full")
}
}
m.inflight[id] = ch
m.mu.Unlock()
defer func() {
m.mu.Lock()
delete(m.inflight, id)
m.mu.Unlock()
}()
orig := q.Id
q.Id = id
wire, err := q.Pack()
q.Id = orig
if err != nil {
return nil, err
}
if _, err := c.Write(wire); err != nil {
return nil, err
}
select {
case raw := <-ch:
resp := new(dns.Msg)
if err := resp.Unpack(raw); err != nil {
return nil, err
}
if len(resp.Question) != 1 || !strings.EqualFold(resp.Question[0].Name, q.Question[0].Name) || resp.Question[0].Qtype != q.Question[0].Qtype {
return nil, errors.New("response question mismatch")
}
resp.Id = orig
return resp, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}

View file

@ -0,0 +1,426 @@
package resolver
import (
"context"
"math/rand/v2"
"net"
"sort"
"strings"
"sync"
"time"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
const (
maxHops = 24 // referrals per (sub)resolution
maxDepth = 10 // nested resolutions (CNAME chains, NS address lookups)
maxQueries = 64 // total upstream queries per client query
maxZoneCache = 8192
triesPerHop = 3
)
// Recursive is an iterative resolver: it starts at the closest known zone cut
// (the root hints by default) and follows referrals. It always asks for DNSSEC
// data (DO) so the validator can verify the result, and sanitises responses
// (bailiwick checks, CNAME-chain filtering) before returning them.
type Recursive struct {
timeout time.Duration
ipv6 bool
log *logrus.Logger
roots []string
mu sync.RWMutex
zones map[string]*zoneNS // delegation cache: zone name → servers
rttMu sync.Mutex
rtt map[string]time.Duration // server → smoothed RTT
}
type zoneNS struct {
servers []string // host:port
expires time.Time
}
type job struct{ queries int }
func NewRecursive(timeout time.Duration, ipv6 bool, log *logrus.Logger) *Recursive {
r := &Recursive{timeout: timeout, ipv6: ipv6, log: log, zones: map[string]*zoneNS{}, rtt: map[string]time.Duration{}}
for _, ip := range rootV4 {
r.roots = append(r.roots, net.JoinHostPort(ip, "53"))
}
if ipv6 {
for _, ip := range rootV6 {
r.roots = append(r.roots, net.JoinHostPort(ip, "53"))
}
}
return r
}
func (r *Recursive) Resolve(ctx context.Context, name string, qtype uint16, do, _ bool) (*dns.Msg, error) {
return r.resolve(ctx, dns.CanonicalName(name), qtype, do, 0, &job{})
}
// closest returns the best known servers for name and the zone they serve.
// DS lives in the parent zone, so its search starts one label up.
func (r *Recursive) closest(name string, qtype uint16) ([]string, string) {
if qtype == dns.TypeDS && name != "." {
name = parentOf(name)
}
r.mu.RLock()
defer r.mu.RUnlock()
now := time.Now()
for n := name; ; n = parentOf(n) {
if z, ok := r.zones[n]; ok && now.Before(z.expires) {
return z.servers, n
}
if n == "." {
return r.roots, "."
}
}
}
func parentOf(name string) string {
if i := strings.IndexByte(name, '.'); i >= 0 && i+1 < len(name) {
return name[i+1:]
}
return "."
}
func (r *Recursive) storeZone(zone string, servers []string, ttl uint32) {
ttl = min(max(ttl, 60), 6*3600)
r.mu.Lock()
if len(r.zones) >= maxZoneCache { // cheap bounded eviction
i := 0
for k := range r.zones {
delete(r.zones, k)
if i++; i > maxZoneCache/8 {
break
}
}
}
r.zones[zone] = &zoneNS{servers: servers, expires: time.Now().Add(time.Duration(ttl) * time.Second)}
r.mu.Unlock()
}
func (r *Recursive) resolve(ctx context.Context, qname string, qtype uint16, do bool, depth int, j *job) (*dns.Msg, error) {
if depth > maxDepth {
return nil, ErrDepth
}
servers, zone := r.closest(qname, qtype)
for hop := 0; hop < maxHops; hop++ {
resp, err := r.queryAny(ctx, servers, qname, qtype, do, j)
if err != nil {
return nil, err
}
switch r.classify(resp, qname, qtype, zone) {
case kindAnswer, kindNegative:
out := sanitize(resp, qname, zone)
return r.followCNAME(ctx, out, qname, qtype, do, depth, j)
case kindReferral:
cut, ns, ttl := referral(resp, zone, qname)
addrs := r.glue(resp, ns, zone)
if len(addrs) == 0 {
addrs = r.lookupNS(ctx, ns, do, depth, j)
}
if len(addrs) == 0 {
return nil, ErrNoServers
}
r.storeZone(cut, addrs, ttl)
servers, zone = addrs, cut
}
}
return nil, ErrDepth
}
type kind int
const (
kindAnswer kind = iota
kindNegative
kindReferral
)
// classify decides what an authoritative response is. Responses that are
// neither are reported as kindNegative-less errors via queryAny's validation,
// so here we only need to distinguish the three useful kinds.
func (r *Recursive) classify(m *dns.Msg, qname string, qtype uint16, zone string) kind {
if m.Rcode == dns.RcodeNameError {
return kindNegative
}
for _, rr := range m.Answer {
if rr.Header().Rrtype != dns.TypeRRSIG && dns.CanonicalName(rr.Header().Name) == qname {
return kindAnswer
}
}
if len(m.Answer) > 0 { // DNAME-synthesised chains etc.
return kindAnswer
}
if cut, _, _ := referral(m, zone, qname); cut != "" && !hasSOA(m) {
return kindReferral
}
return kindNegative
}
func hasSOA(m *dns.Msg) bool {
for _, rr := range m.Ns {
if rr.Header().Rrtype == dns.TypeSOA {
return true
}
}
return false
}
// referral extracts the delegation from a response, enforcing bailiwick: the
// new cut must lie strictly below the current zone and above (or at) qname.
func referral(m *dns.Msg, zone, qname string) (cut string, ns []string, ttl uint32) {
for _, rr := range m.Ns {
n, ok := rr.(*dns.NS)
if !ok {
continue
}
owner := dns.CanonicalName(n.Hdr.Name)
if owner == zone || !dns.IsSubDomain(zone, owner) || !dns.IsSubDomain(owner, qname) {
continue
}
if cut == "" || len(owner) > len(cut) {
cut, ns, ttl = owner, nil, n.Hdr.Ttl
}
if owner == cut {
ns = append(ns, dns.CanonicalName(n.Ns))
}
}
return
}
// glue returns server addresses from the additional section; only names that
// are in bailiwick of the delegating zone are trusted.
func (r *Recursive) glue(m *dns.Msg, ns []string, zone string) []string {
want := map[string]bool{}
for _, n := range ns {
want[n] = true
}
var out []string
for _, rr := range m.Extra {
owner := dns.CanonicalName(rr.Header().Name)
if !want[owner] || !dns.IsSubDomain(zone, owner) {
continue
}
switch a := rr.(type) {
case *dns.A:
out = append(out, net.JoinHostPort(a.A.String(), "53"))
case *dns.AAAA:
if r.ipv6 {
out = append(out, net.JoinHostPort(a.AAAA.String(), "53"))
}
}
}
return out
}
// lookupNS resolves nameserver names that came without glue.
func (r *Recursive) lookupNS(ctx context.Context, ns []string, do bool, depth int, j *job) []string {
rand.Shuffle(len(ns), func(a, b int) { ns[a], ns[b] = ns[b], ns[a] })
var out []string
for _, n := range ns {
types := []uint16{dns.TypeA}
if r.ipv6 {
types = append(types, dns.TypeAAAA)
}
for _, t := range types {
resp, err := r.resolve(ctx, n, t, false, depth+1, j)
if err != nil {
continue
}
for _, rr := range resp.Answer {
switch a := rr.(type) {
case *dns.A:
out = append(out, net.JoinHostPort(a.A.String(), "53"))
case *dns.AAAA:
out = append(out, net.JoinHostPort(a.AAAA.String(), "53"))
}
}
}
if len(out) >= 2 {
break
}
}
return out
}
// followCNAME completes a chain whose last CNAME points outside the zone we
// just asked.
func (r *Recursive) followCNAME(ctx context.Context, m *dns.Msg, qname string, qtype uint16, do bool, depth int, j *job) (*dns.Msg, error) {
if qtype == dns.TypeCNAME || qtype == dns.TypeANY || m.Rcode != dns.RcodeSuccess {
return m, nil
}
last := ""
for _, rr := range m.Answer {
switch v := rr.(type) {
case *dns.CNAME:
last = dns.CanonicalName(v.Target)
default:
if rr.Header().Rrtype == qtype {
return m, nil // got final data
}
}
}
if last == "" {
return m, nil
}
sub, err := r.resolve(ctx, last, qtype, do, depth+1, j)
if err != nil {
return nil, err
}
m.Answer = append(m.Answer, sub.Answer...)
m.Ns = sub.Ns
m.Rcode = sub.Rcode
return m, nil
}
// sanitize keeps only records that belong to the answer to qname: the CNAME
// chain, its final RRsets and the matching RRSIGs, plus the SOA/NSEC/NSEC3
// proofs from the authority section. Everything else (including glue and
// unrelated additional data) is dropped to prevent cache poisoning.
func sanitize(m *dns.Msg, qname, zone string) *dns.Msg {
out := new(dns.Msg)
out.Rcode = m.Rcode
out.Authoritative = false
out.RecursionAvailable = true
names := map[string]bool{qname: true}
for changed := true; changed; {
changed = false
for _, rr := range m.Answer {
own := dns.CanonicalName(rr.Header().Name)
if !names[own] {
continue
}
switch v := rr.(type) {
case *dns.CNAME:
if t := dns.CanonicalName(v.Target); !names[t] {
names[t], changed = true, true
}
}
}
}
for _, rr := range m.Answer {
own := dns.CanonicalName(rr.Header().Name)
if names[own] {
out.Answer = append(out.Answer, rr)
continue
}
if _, ok := rr.(*dns.DNAME); ok { // synthesised CNAMEs live under a DNAME owner
for n := range names {
if dns.IsSubDomain(own, n) {
out.Answer = append(out.Answer, rr)
break
}
}
}
}
for _, rr := range m.Ns {
if !dns.IsSubDomain(zone, dns.CanonicalName(rr.Header().Name)) {
continue
}
switch v := rr.(type) {
case *dns.SOA, *dns.NSEC, *dns.NSEC3:
out.Ns = append(out.Ns, rr)
case *dns.RRSIG:
if t := v.TypeCovered; t == dns.TypeSOA || t == dns.TypeNSEC || t == dns.TypeNSEC3 {
out.Ns = append(out.Ns, rr)
}
}
}
return out
}
// queryAny tries servers (best RTT first, with exploration) until one gives a
// usable authoritative response.
func (r *Recursive) queryAny(ctx context.Context, servers []string, qname string, qtype uint16, do bool, j *job) (*dns.Msg, error) {
order := r.rank(servers)
var lastErr error = ErrNoServers
for i, s := range order {
if i >= triesPerHop {
break
}
if j.queries++; j.queries > maxQueries {
return nil, ErrBudget
}
if ctx.Err() != nil {
return nil, ctx.Err()
}
tctx, cancel := context.WithTimeout(ctx, r.timeout)
start := time.Now()
resp, err := r.exchange(tctx, s, qname, qtype, do)
cancel()
if err != nil {
r.observe(s, r.timeout*2)
lastErr = err
continue
}
r.observe(s, time.Since(start))
if resp.Rcode != dns.RcodeSuccess && resp.Rcode != dns.RcodeNameError {
lastErr = &rcodeError{resp.Rcode}
continue
}
return resp, nil
}
return nil, lastErr
}
type rcodeError struct{ rc int }
func (e *rcodeError) Error() string { return "upstream rcode " + dns.RcodeToString[e.rc] }
func (r *Recursive) exchange(ctx context.Context, server, qname string, qtype uint16, do bool) (*dns.Msg, error) {
q := newQuery(qname, qtype, do, false, false)
c := &dns.Client{Net: "udp", UDPSize: ednsSize, Timeout: r.timeout}
resp, _, err := c.ExchangeContext(ctx, q, server)
if err == nil && resp.Truncated {
c.Net = "tcp"
resp, _, err = c.ExchangeContext(ctx, q, server)
}
if err != nil {
return nil, err
}
if len(resp.Question) != 1 || !strings.EqualFold(resp.Question[0].Name, q.Question[0].Name) || resp.Question[0].Qtype != qtype {
return nil, &rcodeError{dns.RcodeFormatError}
}
return resp, nil
}
func (r *Recursive) rank(servers []string) []string {
type s struct {
addr string
cost time.Duration
}
r.rttMu.Lock()
l := make([]s, len(servers))
for i, a := range servers {
c, ok := r.rtt[a]
if !ok {
c = time.Duration(rand.Int64N(int64(20 * time.Millisecond))) // explore unknown servers
}
l[i] = s{a, c}
}
r.rttMu.Unlock()
sort.Slice(l, func(i, j int) bool { return l[i].cost < l[j].cost })
out := make([]string, len(l))
for i := range l {
out[i] = l[i].addr
}
return out
}
func (r *Recursive) observe(server string, d time.Duration) {
r.rttMu.Lock()
if len(r.rtt) > 50000 {
r.rtt = map[string]time.Duration{}
}
if old, ok := r.rtt[server]; ok {
d = (old*7 + d) / 8
}
r.rtt[server] = d
r.rttMu.Unlock()
}

View file

@ -0,0 +1,70 @@
// Package resolver contains the upstream resolution strategies: forwarding to
// custom servers and full iterative recursion from the root.
package resolver
import (
"context"
"errors"
"strings"
"github.com/miekg/dns"
)
// Resolver resolves one question and returns the raw response. do requests
// DNSSEC records (EDNS DO bit); cd sets the CD bit when forwarding.
type Resolver interface {
Resolve(ctx context.Context, name string, qtype uint16, do, cd bool) (*dns.Msg, error)
}
var (
ErrNoServers = errors.New("no usable upstream server")
ErrBudget = errors.New("resolution exceeded query budget")
ErrDepth = errors.New("resolution exceeded recursion depth")
)
const ednsSize = 1232 // avoids IP fragmentation (DNS flag day 2020)
func newQuery(name string, qtype uint16, do, cd, rd bool) *dns.Msg {
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(name), qtype)
m.RecursionDesired = rd
m.CheckingDisabled = cd
m.SetEdns0(ednsSize, do)
return m
}
// Router sends names under configured zones to dedicated resolvers
// (conditional forwarding) and everything else to the default.
type Router struct {
Default Resolver
zones map[string]Resolver
}
func NewRouter(def Resolver) *Router { return &Router{Default: def, zones: map[string]Resolver{}} }
func (r *Router) Add(zone string, res Resolver) {
r.zones[strings.ToLower(strings.TrimSuffix(zone, "."))] = res
}
// Pick returns the resolver for name and whether it is a conditional zone
// (for which DNSSEC validation is skipped).
func (r *Router) Pick(name string) (Resolver, bool) {
if len(r.zones) > 0 {
for i := 0; ; {
if res, ok := r.zones[name[i:]]; ok {
return res, true
}
j := strings.IndexByte(name[i:], '.')
if j < 0 {
break
}
i += j + 1
}
}
return r.Default, false
}
func (r *Router) Resolve(ctx context.Context, name string, qtype uint16, do, cd bool) (*dns.Msg, error) {
res, _ := r.Pick(strings.TrimSuffix(strings.ToLower(name), "."))
return res.Resolve(ctx, name, qtype, do, cd)
}

View file

@ -0,0 +1,12 @@
package resolver
// Root server addresses (https://www.iana.org/domains/root/files).
var rootV4 = []string{
"198.41.0.4", "170.247.170.2", "192.33.4.12", "199.7.91.13", "192.203.230.10", "192.5.5.241", "192.112.36.4",
"198.97.190.53", "192.36.148.17", "192.58.128.30", "193.0.14.129", "199.7.83.42", "202.12.27.33",
}
var rootV6 = []string{
"2001:503:ba3e::2:30", "2801:1b8:10::b", "2001:500:2::c", "2001:500:2d::d", "2001:500:a8::e", "2001:500:2f::f",
"2001:500:12::d0d", "2001:500:1::53", "2001:7fe::53", "2001:503:c27::2:30", "2001:7fd::1", "2001:500:9f::42", "2001:dc3::35",
}

57
internal/server/server.go Normal file
View file

@ -0,0 +1,57 @@
// Package server runs the DNS listeners.
package server
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
// Run starts n UDP and n TCP listeners (SO_REUSEPORT, so the kernel spreads
// load across them) and blocks until ctx ends or a listener fails. started is
// closed once every listener is accepting.
func Run(ctx context.Context, addr string, n int, h dns.Handler, log *logrus.Logger, started chan<- struct{}) error {
var servers []*dns.Server
var ready atomic.Int32
total := int32(2 * n)
notify := func() {
if ready.Add(1) == total {
close(started)
}
}
for i := 0; i < n; i++ {
for _, nw := range []string{"udp", "tcp"} {
servers = append(servers, &dns.Server{
Addr: addr, Net: nw, Handler: h, ReusePort: true, NotifyStartedFunc: notify,
UDPSize: 65535, ReadTimeout: 5 * time.Second, WriteTimeout: 5 * time.Second,
IdleTimeout: func() time.Duration { return 10 * time.Second }, MaxTCPQueries: 256,
})
}
}
errc := make(chan error, len(servers))
var wg sync.WaitGroup
for _, s := range servers {
wg.Add(1)
go func() {
defer wg.Done()
if err := s.ListenAndServe(); err != nil {
errc <- err
}
}()
}
log.Infof("DNS listening on %s (%d UDP + %d TCP listeners)", addr, n, n)
var err error
select {
case <-ctx.Done():
case err = <-errc:
}
for _, s := range servers {
_ = s.Shutdown()
}
wg.Wait()
return err
}

190
internal/state/store.go Normal file
View file

@ -0,0 +1,190 @@
// Package state holds the small, mutable runtime state (API-made overrides)
// that replicas synchronise. It is a last-write-wins map with tombstones; every
// entry carries a hybrid timestamp and the writing node as tie breaker, so
// merges are commutative, associative and idempotent (a state-based CRDT).
package state
import (
"encoding/json"
"os"
"path/filepath"
"sync"
"time"
)
const (
KindAllow = "allow"
KindBlock = "block"
KindRecord = "record"
KindBlocking = "blocking"
)
const tombstoneTTL = 7 * 24 * time.Hour
type Entry struct {
Kind string `json:"kind"`
Key string `json:"key"`
Zones []string `json:"zones,omitempty"` // empty = all zones
Value json.RawMessage `json:"value,omitempty"`
TS int64 `json:"ts"` // unix nanoseconds
Node string `json:"node"`
Deleted bool `json:"deleted,omitempty"`
}
func (e *Entry) id() string { return e.Kind + "\x00" + e.Key }
func newer(a, b *Entry) bool { return a.TS > b.TS || (a.TS == b.TS && a.Node > b.Node) }
// AppliesTo reports whether the entry is relevant for zone.
func (e *Entry) AppliesTo(zone string) bool {
if len(e.Zones) == 0 {
return true
}
for _, z := range e.Zones {
if z == zone {
return true
}
}
return false
}
type Store struct {
node string
path string
mu sync.RWMutex
m map[string]Entry
lastTS int64
subs []func()
dirty chan struct{}
version uint64
}
// New creates a store; if path is non-empty the state is loaded from and
// persisted to it.
func New(node, path string) *Store {
s := &Store{node: node, path: path, m: map[string]Entry{}, dirty: make(chan struct{}, 1)}
if path != "" {
if raw, err := os.ReadFile(path); err == nil {
var es []Entry
if json.Unmarshal(raw, &es) == nil {
for _, e := range es {
s.m[e.id()] = e
}
}
}
go s.persistLoop()
}
return s
}
// Subscribe registers a callback invoked (outside locks) after every change.
func (s *Store) Subscribe(fn func()) {
s.mu.Lock()
s.subs = append(s.subs, fn)
s.mu.Unlock()
}
func (s *Store) tick() int64 {
now := time.Now().UnixNano()
if now <= s.lastTS {
now = s.lastTS + 1
}
s.lastTS = now
return now
}
func (s *Store) changed() {
s.mu.Lock()
s.version++
subs := append([]func(){}, s.subs...)
s.mu.Unlock()
select {
case s.dirty <- struct{}{}:
default:
}
for _, fn := range subs {
fn()
}
}
// Version increases on every change.
func (s *Store) Version() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.version
}
// Put sets (or, with deleted, tombstones) an entry.
func (s *Store) Put(kind, key string, zones []string, value json.RawMessage, deleted bool) Entry {
s.mu.Lock()
e := Entry{Kind: kind, Key: key, Zones: zones, Value: value, TS: s.tick(), Node: s.node, Deleted: deleted}
s.m[e.id()] = e
s.mu.Unlock()
s.changed()
return e
}
// Merge applies remote entries and reports whether anything changed locally.
func (s *Store) Merge(in []Entry) bool {
changed := false
s.mu.Lock()
for i := range in {
e := in[i]
if e.Kind == "" || e.Key == "" {
continue
}
if cur, ok := s.m[e.id()]; !ok || newer(&e, &cur) {
s.m[e.id()] = e
changed = true
if e.TS > s.lastTS { // keep our clock ahead of anything we've seen
s.lastTS = e.TS
}
}
}
s.mu.Unlock()
if changed {
s.changed()
}
return changed
}
// Snapshot returns all entries including tombstones (used for sync).
func (s *Store) Snapshot() []Entry {
s.mu.Lock()
defer s.mu.Unlock()
cut := time.Now().Add(-tombstoneTTL).UnixNano()
out := make([]Entry, 0, len(s.m))
for k, e := range s.m {
if e.Deleted && e.TS < cut {
delete(s.m, k)
continue
}
out = append(out, e)
}
return out
}
// Live returns the non-deleted entries.
func (s *Store) Live() []Entry {
s.mu.RLock()
defer s.mu.RUnlock()
out := make([]Entry, 0, len(s.m))
for _, e := range s.m {
if !e.Deleted {
out = append(out, e)
}
}
return out
}
func (s *Store) persistLoop() {
for range s.dirty {
time.Sleep(500 * time.Millisecond) // debounce
raw, _ := json.Marshal(s.Snapshot())
tmp := s.path + ".tmp"
if os.MkdirAll(filepath.Dir(s.path), 0o755) == nil && os.WriteFile(tmp, raw, 0o600) == nil {
_ = os.Rename(tmp, s.path)
}
}
}

218
internal/state/sync.go Normal file
View file

@ -0,0 +1,218 @@
package state
import (
"bytes"
"context"
"crypto/subtle"
"crypto/tls"
"crypto/x509"
"encoding/json"
"io"
"math/rand/v2"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
"jonasled.dev/jonasled/adblock-dns/internal/metrics"
)
// SyncOptions configures peer synchronisation.
type SyncOptions struct {
Token string
Peers []string // static host:port
PeersDNS string // headless service host:port, resolved to pod IPs
SelfIPs []string
Interval time.Duration
TLS bool
CAFile string
}
// Syncer exchanges the runtime state with peer replicas (anti-entropy plus
// immediate push on change). Peers that cannot be reached are simply skipped:
// every replica keeps serving from its own state, and state converges as soon
// as connectivity returns.
type Syncer struct {
st *Store
opt SyncOptions
log *logrus.Logger
client *http.Client
kick chan struct{}
mu sync.Mutex
seen map[string]bool // peer → last exchange succeeded
peers int
}
func NewSyncer(st *Store, opt SyncOptions, log *logrus.Logger) (*Syncer, error) {
tr := &http.Transport{
DialContext: (&net.Dialer{Timeout: 2 * time.Second}).DialContext,
TLSHandshakeTimeout: 3 * time.Second, MaxIdleConnsPerHost: 2, IdleConnTimeout: 30 * time.Second,
}
if opt.TLS {
tc := &tls.Config{MinVersion: tls.VersionTLS12}
if opt.CAFile != "" {
pem, err := os.ReadFile(opt.CAFile)
if err != nil {
return nil, err
}
tc.RootCAs = x509.NewCertPool()
tc.RootCAs.AppendCertsFromPEM(pem)
}
tr.TLSClientConfig = tc
}
s := &Syncer{st: st, opt: opt, log: log, client: &http.Client{Transport: tr, Timeout: 5 * time.Second}, kick: make(chan struct{}, 1), seen: map[string]bool{}}
st.Subscribe(func() {
select {
case s.kick <- struct{}{}:
default:
}
})
return s, nil
}
type exchangeMsg struct {
Entries []Entry `json:"entries"`
}
// Handler serves POST /sync/exchange (bearer-token protected).
func (s *Syncer) Handler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !CheckToken(r, s.opt.Token) {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
var in exchangeMsg
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 8<<20)).Decode(&in); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
s.st.Merge(in.Entries)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(exchangeMsg{Entries: s.st.Snapshot()})
}
}
// CheckToken validates the bearer token in constant time.
func CheckToken(r *http.Request, token string) bool {
if token == "" {
return false
}
got := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
return subtle.ConstantTimeCompare([]byte(got), []byte(token)) == 1
}
func (s *Syncer) discover(ctx context.Context) []string {
out := append([]string(nil), s.opt.Peers...)
if s.opt.PeersDNS != "" {
host, port, err := net.SplitHostPort(s.opt.PeersDNS)
if err == nil {
rctx, cancel := context.WithTimeout(ctx, 2*time.Second)
ips, err := net.DefaultResolver.LookupHost(rctx, host)
cancel()
if err != nil {
s.log.WithError(err).Debug("sync: peer discovery failed")
}
for _, ip := range ips {
self := false
for _, me := range s.opt.SelfIPs {
if me == ip {
self = true
}
}
if !self {
out = append(out, net.JoinHostPort(ip, port))
}
}
}
}
return out
}
// Run loops until ctx is done.
func (s *Syncer) Run(ctx context.Context) {
iv := s.opt.Interval
if iv <= 0 {
iv = 10 * time.Second
}
timer := time.NewTimer(time.Second)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return
case <-s.kick:
time.Sleep(50 * time.Millisecond) // coalesce bursts
case <-timer.C:
}
s.round(ctx)
timer.Reset(iv + time.Duration(rand.Int64N(int64(iv/5)+1)))
}
}
func (s *Syncer) round(ctx context.Context) {
peers := s.discover(ctx)
metrics.M.SyncPeers.Set(float64(len(peers)))
metrics.M.SyncEntries.Set(float64(len(s.st.Snapshot())))
var wg sync.WaitGroup
for _, p := range peers {
wg.Add(1)
go func() {
defer wg.Done()
err := s.exchange(ctx, p)
s.mu.Lock()
prev, known := s.seen[p]
s.seen[p] = err == nil
s.mu.Unlock()
if err != nil {
metrics.M.SyncErr.Inc()
if !known || prev { // log on state change only
s.log.WithError(err).Warnf("sync: peer %s unreachable (continuing standalone)", p)
}
return
}
metrics.M.SyncOK.Inc()
metrics.M.SyncLastOK.SetToCurrentTime()
if known && !prev {
s.log.Infof("sync: peer %s reachable again", p)
}
}()
}
wg.Wait()
}
func (s *Syncer) exchange(ctx context.Context, peer string) error {
scheme := "http"
if s.opt.TLS {
scheme = "https"
}
body, _ := json.Marshal(exchangeMsg{Entries: s.st.Snapshot()})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, scheme+"://"+peer+"/sync/exchange", bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+s.opt.Token)
req.Header.Set("Content-Type", "application/json")
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, resp.Body)
return &httpError{resp.Status}
}
var in exchangeMsg
if err := json.NewDecoder(io.LimitReader(resp.Body, 8<<20)).Decode(&in); err != nil {
return err
}
s.st.Merge(in.Entries)
return nil
}
type httpError struct{ s string }
func (e *httpError) Error() string { return "peer answered " + e.s }