core/dnsserver: add Config.UDPDecorateWriterFunc for external plugins (#8257)

* core/dnsserver: add Config.UDPDecorateWriterFunc for external plugins

Allow external plugins to install a dns.DecorateWriter (an existing
miekg/dns Server field CoreDNS never sets) on plain-UDP servers, e.g.
for per-socket write disciplines or overload protection; UDP is the
only transport without a Max*-style concurrency setting.

The field is a factory called once per Server in ServePacket, so each
socket gets its own decorator under multisocket; the *Server argument
is the same value handlers see via ctx.Value(dnsserver.Key{}). Follows
the Config.HTTPRequestValidateFunc precedent, including server-block
propagation and the ServePacket-time lookup over s.zones. Nil (the
default) changes nothing.

Signed-off-by: Ryan Brewster <rpb@anthropic.com>

* core/dnsserver: select UDP writer decorator deterministically in NewServer

Review feedback: s.zones is a map, so choosing the decorator in
ServePacket was non-deterministic when several server blocks share a
listener. Select it in NewServer from the group slice instead (stable
config order, last one set wins) and document the resolution order.

Signed-off-by: Ryan Brewster <rpb@anthropic.com>

* core/dnsserver: group decorator selection with the other last-writer-wins fields

Pure move: place it next to ProxyProtoConnPolicy, the existing field
resolved the same way at the same point.

Signed-off-by: Ryan Brewster <rpb@anthropic.com>

---------

Signed-off-by: Ryan Brewster <rpb@anthropic.com>
This commit is contained in:
rpb-ant
2026-07-09 13:15:13 -04:00
committed by GitHub
parent 7df9238255
commit 2d106be341
4 changed files with 87 additions and 1 deletions

View File

@@ -55,6 +55,11 @@ type Server struct {
tsigSecret map[string]string
// udpDecorateWriterFunc is selected in NewServer from the group configs in
// stable order (last one set wins), so the choice is deterministic when
// several server blocks share a listener. See Config.UDPDecorateWriterFunc.
udpDecorateWriterFunc func(*Server) dns.DecorateWriter
// Ensure Stop is idempotent when invoked concurrently (e.g., during reload and SIGTERM).
stopOnce sync.Once
stopErr error
@@ -138,6 +143,9 @@ func NewServer(addr string, group []*Config) (*Server, error) {
if site.ProxyProtoUDPSessionTrackingMaxSessions > 0 {
s.udpSessionTrackingMaxSessions = site.ProxyProtoUDPSessionTrackingMaxSessions
}
if site.UDPDecorateWriterFunc != nil {
s.udpDecorateWriterFunc = site.UDPDecorateWriterFunc
}
}
if !s.debug {
@@ -179,12 +187,17 @@ func (s *Server) Serve(l net.Listener) error {
// ServePacket starts the server with an existing packetconn. It blocks until the server stops.
// This implements caddy.UDPServer interface.
func (s *Server) ServePacket(p net.PacketConn) error {
// Use a custom writer decorator if one was configured.
var dw dns.DecorateWriter
if s.udpDecorateWriterFunc != nil {
dw = s.udpDecorateWriterFunc(s)
}
s.m.Lock()
s.server[udp] = &dns.Server{PacketConn: p, Net: "udp", Handler: dns.HandlerFunc(func(w dns.ResponseWriter, r *dns.Msg) {
ctx := context.WithValue(context.Background(), Key{}, s)
ctx = context.WithValue(ctx, LoopKey{}, 0)
s.ServeDNS(ctx, w, r)
}), TsigSecret: s.tsigSecret}
}), TsigSecret: s.tsigSecret, DecorateWriter: dw}
s.m.Unlock()
return s.server[udp].ActivateAndServe()