dns: return empty reply for non-A queries for DNS hostnames
instead of NXDOMAIN, which is incorrect
This commit is contained in:
parent
c7be96ac2e
commit
92d995bf79
@ -16,6 +16,7 @@
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -193,7 +194,9 @@ func isLocalInAddrArpa(q string) bool {
|
|||||||
return local
|
return local
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) resolve(q dns.Question) (dns.RR, error) {
|
var sentinelEmpty = errors.New("no answers")
|
||||||
|
|
||||||
|
func (s *Server) resolve(q dns.Question) (rr dns.RR, err error) {
|
||||||
if q.Qclass != dns.ClassINET {
|
if q.Qclass != dns.ClassINET {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@ -205,12 +208,17 @@ func (s *Server) resolve(q dns.Question) (dns.RR, error) {
|
|||||||
return dns.NewRR(q.Name + " 3600 IN A 127.0.0.1")
|
return dns.NewRR(q.Name + " 3600 IN A 127.0.0.1")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if q.Qtype == dns.TypeA {
|
if q.Qtype == dns.TypeA ||
|
||||||
|
q.Qtype == dns.TypeAAAA ||
|
||||||
|
q.Qtype == dns.TypeMX {
|
||||||
name := strings.TrimSuffix(q.Name, ".")
|
name := strings.TrimSuffix(q.Name, ".")
|
||||||
name = strings.TrimSuffix(name, "."+s.domain)
|
name = strings.TrimSuffix(name, "."+s.domain)
|
||||||
if host, ok := s.hostByName(name); ok {
|
if host, ok := s.hostByName(name); ok {
|
||||||
|
if q.Qtype == dns.TypeA {
|
||||||
return dns.NewRR(q.Name + " 3600 IN A " + host)
|
return dns.NewRR(q.Name + " 3600 IN A " + host)
|
||||||
}
|
}
|
||||||
|
return nil, sentinelEmpty
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if q.Qtype == dns.TypePTR {
|
if q.Qtype == dns.TypePTR {
|
||||||
if host, ok := s.hostByIP(q.Name); ok {
|
if host, ok := s.hostByIP(q.Name); ok {
|
||||||
@ -232,6 +240,12 @@ func (s *Server) handleInternal(w dns.ResponseWriter, r *dns.Msg) {
|
|||||||
}
|
}
|
||||||
rr, err := s.resolve(r.Question[0])
|
rr, err := s.resolve(r.Question[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == sentinelEmpty {
|
||||||
|
m := new(dns.Msg)
|
||||||
|
m.SetReply(r)
|
||||||
|
w.WriteMsg(m)
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if rr != nil {
|
if rr != nil {
|
||||||
|
@ -208,6 +208,18 @@ func TestHostnameDHCP(t *testing.T) {
|
|||||||
t.Fatalf("unexpected response record: got %q, want %q", got, want)
|
t.Fatalf("unexpected response record: got %q, want %q", got, want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("AAAA", func(t *testing.T) {
|
||||||
|
m := new(dns.Msg)
|
||||||
|
m.SetQuestion(hostname+".lan.", dns.TypeAAAA)
|
||||||
|
s.Mux.ServeDNS(r, m)
|
||||||
|
if got, want := r.response.MsgHdr.Rcode, dns.RcodeSuccess; got != want {
|
||||||
|
t.Fatalf("unexpected rcode: got %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
if got, want := len(r.response.Answer), 0; got != want {
|
||||||
|
t.Fatalf("unexpected number of answers: got %d, want %d", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalhost(t *testing.T) {
|
func TestLocalhost(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user