server

package
v0.8.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: MIT Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DoTServer

type DoTServer struct {
	// contains filtered or unexported fields
}

DoTServer handles DNS queries over TLS (RFC 7858).

func NewDoTServer

func NewDoTServer(addr string, handler Handler, certFile, keyFile string, timeout time.Duration, maxConns int, logger *slog.Logger) (*DoTServer, error)

NewDoTServer creates a new DNS-over-TLS server. It loads the TLS certificate/key pair and wraps a TCP listener with TLS.

func NewDoTServerWithListener

func NewDoTServerWithListener(ln net.Listener, handler Handler, timeout time.Duration, maxConns int, logger *slog.Logger) *DoTServer

NewDoTServerWithListener creates a DoT server using an existing TLS listener. This is primarily useful for testing.

func NewDoTServerWithTLSConfig added in v0.5.5

func NewDoTServerWithTLSConfig(addr string, handler Handler, tlsCfg *tls.Config, timeout time.Duration, maxConns int, logger *slog.Logger) (*DoTServer, error)

NewDoTServerWithTLSConfig creates a DoT server using a pre-built tls.Config. This is used when auto-TLS provides the certificate dynamically.

func (*DoTServer) Addr

func (s *DoTServer) Addr() net.Addr

Addr returns the listener's network address.

func (*DoTServer) Close

func (s *DoTServer) Close() error

Close closes the DoT server listener.

func (*DoTServer) Serve

func (s *DoTServer) Serve(ctx context.Context) error

Serve starts the DoT server loop. It uses the same semaphore-based concurrency limiting pattern as TCPServer.

type Handler

type Handler interface {
	Handle(query []byte, clientAddr net.Addr) ([]byte, error)
}

Handler processes a raw DNS query and returns a raw DNS response.

type MainHandler

type MainHandler struct {

	// OnQuery is an optional callback invoked after each query is resolved.
	// Parameters: client IP, qname, qtype, rcode (may be "BLOCKED"), whether served from cache, duration in ms.
	OnQuery func(client, qname, qtype, rcode string, cached bool, durationMs float64)
	// contains filtered or unexported fields
}

MainHandler ties together parsing, resolution, and response assembly.

func NewMainHandler

func NewMainHandler(
	res *resolver.Resolver,
	c *cache.Cache,
	rl *security.RateLimiter,
	rrl *security.RRL,
	acl *security.ACL,
	m *metrics.Metrics,
	logger *slog.Logger,
) *MainHandler

NewMainHandler creates a new MainHandler.

func (*MainHandler) CookiesEnforce added in v0.7.3

func (h *MainHandler) CookiesEnforce() bool

CookiesEnforce reports the current §5.4 strict-mode setting.

func (*MainHandler) EnableCookies

func (h *MainHandler) EnableCookies() error

EnableCookies enables DNS cookie support (RFC 7873). A random 16-byte server secret is generated at startup. M-6: if the OS RNG fails we refuse to enable cookies rather than fall back to a public literal (which would defeat the anti-spoofing property entirely).

func (*MainHandler) EnableCookiesWithSecret

func (h *MainHandler) EnableCookiesWithSecret(secret []byte)

EnableCookiesWithSecret enables DNS cookies with a specific secret (for testing).

func (*MainHandler) Handle

func (h *MainHandler) Handle(query []byte, clientAddr net.Addr) (resp []byte, err error)

func (*MainHandler) RotateCookieSecret added in v0.6.19

func (h *MainHandler) RotateCookieSecret() error

RotateCookieSecret generates a fresh random 16-byte server cookie secret. The previous secret is retained for one hour (the cookie validity window per RFC 9018 §4.3) so cookies the client minted moments before rotation are not invalidated — RFC 7873 §5.2.5 explicitly requires this grace ("Cookies created with old secrets remain valid until they expire"). Without grace every rotation would force every active client through a BADCOOKIE round-trip, turning a routine operational action into a service blip.

Returns the error from the OS RNG; on error the existing secrets are left intact (better to keep using a known-good secret than land on an all-zero replacement).

func (*MainHandler) SetBlocklist

func (h *MainHandler) SetBlocklist(bl interface {
	IsBlocked(string) bool
	BlockingMode() string
	CustomIP() string
})

SetBlocklist configures an optional blocklist for the handler.

func (*MainHandler) SetCookiesEnforce added in v0.7.3

func (h *MainHandler) SetCookiesEnforce(enforce bool)

SetCookiesEnforce toggles RFC 7873 §5.4 strict mode: cookie-less UDP queries are rejected with BADCOOKIE. No-op unless cookies themselves are also enabled (EnableCookies / EnableCookiesWithSecret).

func (*MainHandler) SetDownstreamUDPBufferSize added in v0.6.2

func (h *MainHandler) SetDownstreamUDPBufferSize(size int)

SetDownstreamUDPBufferSize configures the EDNS0 UDP payload size this server advertises to clients in outgoing OPT records. Per RFC 9018 / DNS Flag Day 2020, the safe default is 1232 (IPv6 minimum MTU 1280 minus 40-byte IPv6 header minus 8-byte UDP header). Values outside [512, 65535] are silently clamped to 1232 by advertisedUDPBufferSize.

func (*MainHandler) SetECS

func (h *MainHandler) SetECS(enabled bool, maxPrefix int)

SetECS enables or disables ECS forwarding. maxPrefix and maxPrefixV6 cap the source-prefix length we forward upstream for IPv4 and IPv6 clients respectively. Pass 0 or out-of-range values to accept the RFC 7871 §11.1 recommended defaults (/24 IPv4, /56 IPv6).

func (*MainHandler) SetECSPrefixes added in v0.6.9

func (h *MainHandler) SetECSPrefixes(enabled bool, maxPrefixV4, maxPrefixV6 int)

SetECSPrefixes is the IPv6-aware extension of SetECS. The legacy SetECS is preserved as a convenience wrapper for callers that only care about IPv4 limits.

func (*MainHandler) SetNoCacheClients

func (h *MainHandler) SetNoCacheClients(cidrs []string)

SetNoCacheClients configures the list of client IPs/CIDRs that should bypass the cache.

func (*MainHandler) SetPrivateFilter

func (h *MainHandler) SetPrivateFilter(enabled bool)

SetPrivateFilter enables or disables private address filtering.

type TCPOption

type TCPOption func(*TCPServer)

TCPOption configures optional TCPServer parameters.

func WithIdleTimeout

func WithIdleTimeout(d time.Duration) TCPOption

WithIdleTimeout sets the idle timeout between pipelined queries.

func WithPipelineMax

func WithPipelineMax(n int) TCPOption

WithPipelineMax sets the maximum number of queries per TCP connection.

type TCPServer

type TCPServer struct {
	// contains filtered or unexported fields
}

TCPServer handles DNS queries over TCP.

func NewTCPServer

func NewTCPServer(addr string, handler Handler, timeout time.Duration, maxConns int, logger *slog.Logger, opts ...TCPOption) (*TCPServer, error)

NewTCPServer creates a new TCP DNS server.

func (*TCPServer) Close

func (s *TCPServer) Close() error

Close closes the TCP server.

func (*TCPServer) Serve

func (s *TCPServer) Serve(ctx context.Context) error

Serve starts the TCP server loop.

type UDPServer

type UDPServer struct {
	// contains filtered or unexported fields
}

UDPServer handles DNS queries over UDP.

func NewUDPServer

func NewUDPServer(addr string, handler Handler, maxWorkers int, logger *slog.Logger) (*UDPServer, error)

NewUDPServer creates a new UDP DNS server.

func (*UDPServer) Close

func (s *UDPServer) Close() error

Close closes the UDP server.

func (*UDPServer) Serve

func (s *UDPServer) Serve(ctx context.Context) error

Serve starts the UDP server loop.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL