A simple non-recursive DNS server written in Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

46 lines
1.3 KiB

package realm
import "github.com/miekg/dns"
// A Server listens for DNS requests over UDP and responds with answers from the provided Zone.
type Server struct {
server *dns.Server
zones Zones
}
// NewServer returns a new initialized *Server that will bind to listen and will look up answers from zone.
func NewServer(listen string, zones Zones) *Server {
var s *Server
s = &Server{zones: zones}
s.server = &dns.Server{
Addr: listen,
Net: "udp",
Handler: s,
}
return s
}
// ListenAndServe will start the nameserver on the configured address.
func (s *Server) ListenAndServe() error {
return s.server.ListenAndServe()
}
// ServeDNS will be called for every DNS request to this server.
// It will attempt to provide answers to all questions from the configured zone.
func (s *Server) ServeDNS(w dns.ResponseWriter, request *dns.Msg) {
// Setup the default response
var response *dns.Msg
response = &dns.Msg{}
response.SetReply(request)
response.Compress = true
// Lookup answers to any of the questions
for _, question := range request.Question {
var records []dns.RR
records = s.zones.Lookup(question.Name, question.Qtype, question.Qclass)
response.Answer = append(response.Answer, records...)
}
// Respond to the request
w.WriteMsg(response)
}