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 ¶
- Variables
- type Handler
- type Options
- type Query
- type Reply
- type Server
- func (s *Server) Close()
- func (s *Server) CloseStreamsAfter(n int)
- func (s *Server) Connections() int64
- func (s *Server) DoHHandler(h Handler) http.Handler
- func (s *Server) ListenTLS(h Handler) error
- func (s *Server) Queries() int64
- func (s *Server) QueriesFor(name string, qtype dnsmessage.Type) int64
Constants ¶
This section is empty.
Variables ¶
var ( NXDomain = Reply{RCode: dnsmessage.RCodeNameError} ServFail = Reply{RCode: dnsmessage.RCodeServerFailure} Refused = Reply{RCode: dnsmessage.RCodeRefused} Dropped = Reply{Drop: true} )
Common replies.
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 ¶
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 (*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 ¶
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 ¶
Connections returns how many TCP or TLS connections clients opened.
func (*Server) DoHHandler ¶
DoHHandler serves DNS over HTTPS (RFC 8484, POST) answering with h. Run it with httptest.NewUnstartedServer(...).StartTLS().
func (*Server) ListenTLS ¶
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) 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.