dnsserver

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package dnsserver is a scriptable local DNS server (#67). Each query is answered by a Handler, which decides per name and type whether to answer with records, return an error RCODE or NODATA, delay, truncate, or drop it. The server counts every query it receives, in total and per name and type.

It backs the hermetic tests (through internal/dnstest) and -simulate-zone labs (#76), which point the real resolver at a loopback server.

Index

Constants

This section is empty.

Variables

View Source
var (
	NXDomain = Reply{RCode: dnsmessage.RCodeNameError}
	ServFail = Reply{RCode: dnsmessage.RCodeServerFailure}
	Refused  = Reply{RCode: dnsmessage.RCodeRefused}
	Dropped  = Reply{Drop: true}
)

Common replies.

View Source
var ErrNoIPv6 = errors.New("no IPv6 loopback")

ErrNoIPv6 is returned by Listen when IPv6 is requested and the host has no IPv6 loopback.

Functions

This section is empty.

Types

type Handler

type Handler func(Query) Reply

Handler answers one query. It may be called concurrently unless the server is serial.

type Options

type Options struct {
	// Serial answers one UDP query at a time, like a resolver at its rate
	// limit; a Delay then throttles the whole server.
	Serial bool
	// IPv6 listens on [::1] instead of 127.0.0.1.
	IPv6 bool
}

Options configure a server.

type Query

type Query struct {
	Name string          // lowercase, without the trailing dot
	Type dnsmessage.Type // dnsmessage.TypeA, TypeAAAA, TypeCNAME, ...
	TCP  bool            // the query arrived over TCP
}

Query is one question the server received.

type Reply

type Reply struct {
	A     []string // IPv4 answers for A queries
	AAAA  []string // IPv6 answers for AAAA queries
	CNAME string   // alias answered for every qtype; A/AAAA above are then owned by the target

	RCode    dnsmessage.RCode // e.g. RCodeNameError (NXDOMAIN), RCodeServerFailure, RCodeRefused
	Drop     bool             // send nothing; the client times out
	Delay    time.Duration    // wait before answering
	Truncate bool             // over UDP, answer with TC=1 and no records so the client retries over TCP
}

Reply scripts the answer to a Query. The zero Reply is NOERROR with no records, which the server sends as NODATA (an SOA in the authority section).

type Server

type Server struct {
	Addr string // host:port, for -dns-server or dns.NewResolver

	TLSAddr string         // DoT listener after ListenTLS, for tls://<TLSAddr>
	RootCAs *x509.CertPool // trusts the ListenTLS certificate
	// contains filtered or unexported fields
}

Server is a running DNS server.

func Listen

func Listen(opts Options, h Handler) (*Server, error)

Listen runs a server on a free loopback port answering with h, until Close.

func (*Server) Close

func (s *Server) Close()

Close stops the server: it closes the listeners and every open stream connection, and waits until no handler is running, including delayed UDP answers and connections parked on a dropped query.

func (*Server) CloseStreamsAfter

func (s *Server) CloseStreamsAfter(n int)

CloseStreamsAfter makes the server close each TCP or TLS connection after answering n queries on it, as servers with a short idle timeout or a per-connection query limit do. 0 restores the default (never).

func (*Server) Connections

func (s *Server) Connections() int64

Connections returns how many TCP or TLS connections clients opened.

func (*Server) DoHHandler

func (s *Server) DoHHandler(h Handler) http.Handler

DoHHandler serves DNS over HTTPS (RFC 8484, POST) answering with h. Run it with httptest.NewUnstartedServer(...).StartTLS().

func (*Server) ListenTLS

func (s *Server) ListenTLS(h Handler) error

ListenTLS adds a DNS-over-TLS listener (RFC 7858) answering with h, using a freshly generated certificate for 127.0.0.1. Use TLSAddr as tls://<TLSAddr> and RootCAs to trust it. Close stops it too.

func (*Server) Queries

func (s *Server) Queries() int64

Queries returns how many queries the server has received.

func (*Server) QueriesFor

func (s *Server) QueriesFor(name string, qtype dnsmessage.Type) int64

QueriesFor returns how many queries for name and qtype the server has received. name is matched case-insensitively, with or without a trailing dot.

Jump to

Keyboard shortcuts

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