2020-05-08 15:39:37 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
2020-05-08 15:39:37 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2020-12-07 12:38:05 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-12-23 09:35:07 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
2020-05-08 15:39:37 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create a DNS response by DNS request and set necessary flags
|
2020-12-21 14:48:07 +00:00
|
|
|
func (s *Server) makeResponse(req *dns.Msg) (resp *dns.Msg) {
|
|
|
|
resp = &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
RecursionAvailable: true,
|
|
|
|
},
|
|
|
|
Compress: true,
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
resp.SetReply(req)
|
2020-12-21 14:48:07 +00:00
|
|
|
|
|
|
|
return resp
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// genDNSFilterMessage generates a DNS message corresponding to the filtering result
|
|
|
|
func (s *Server) genDNSFilterMessage(d *proxy.DNSContext, result *dnsfilter.Result) *dns.Msg {
|
|
|
|
m := d.Req
|
|
|
|
|
|
|
|
if m.Question[0].Qtype != dns.TypeA && m.Question[0].Qtype != dns.TypeAAAA {
|
2020-10-02 09:51:55 +00:00
|
|
|
if s.conf.BlockingMode == "null_ip" {
|
|
|
|
return s.makeResponse(m)
|
|
|
|
}
|
|
|
|
return s.genNXDomain(m)
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch result.Reason {
|
|
|
|
case dnsfilter.FilteredSafeBrowsing:
|
|
|
|
return s.genBlockedHost(m, s.conf.SafeBrowsingBlockHost, d)
|
|
|
|
case dnsfilter.FilteredParental:
|
|
|
|
return s.genBlockedHost(m, s.conf.ParentalBlockHost, d)
|
|
|
|
default:
|
|
|
|
// If the query was filtered by "Safe search", dnsfilter also must return
|
|
|
|
// the IP address that must be used in response.
|
|
|
|
// In this case regardless of the filtering method, we should return it
|
2020-12-17 10:32:46 +00:00
|
|
|
if result.Reason == dnsfilter.FilteredSafeSearch &&
|
|
|
|
len(result.Rules) > 0 &&
|
|
|
|
result.Rules[0].IP != nil {
|
|
|
|
return s.genResponseWithIP(m, result.Rules[0].IP)
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.conf.BlockingMode == "null_ip" {
|
|
|
|
// it means that we should return 0.0.0.0 or :: for any blocked request
|
2020-10-02 09:51:55 +00:00
|
|
|
return s.makeResponseNullIP(m)
|
2020-05-08 15:39:37 +00:00
|
|
|
} else if s.conf.BlockingMode == "custom_ip" {
|
|
|
|
// means that we should return custom IP for any blocked request
|
|
|
|
|
|
|
|
switch m.Question[0].Qtype {
|
|
|
|
case dns.TypeA:
|
|
|
|
return s.genARecord(m, s.conf.BlockingIPAddrv4)
|
|
|
|
case dns.TypeAAAA:
|
|
|
|
return s.genAAAARecord(m, s.conf.BlockingIPAddrv6)
|
|
|
|
}
|
|
|
|
} else if s.conf.BlockingMode == "nxdomain" {
|
|
|
|
// means that we should return NXDOMAIN for any blocked request
|
|
|
|
|
|
|
|
return s.genNXDomain(m)
|
2020-08-19 11:13:16 +00:00
|
|
|
} else if s.conf.BlockingMode == "refused" {
|
|
|
|
// means that we should return NXDOMAIN for any blocked request
|
|
|
|
|
|
|
|
return s.makeResponseREFUSED(m)
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default blocking mode
|
|
|
|
// If there's an IP specified in the rule, return it
|
2020-10-02 09:51:55 +00:00
|
|
|
// For host-type rules, return null IP
|
2020-12-17 10:32:46 +00:00
|
|
|
if len(result.Rules) > 0 && result.Rules[0].IP != nil {
|
|
|
|
return s.genResponseWithIP(m, result.Rules[0].IP)
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
2020-10-02 09:51:55 +00:00
|
|
|
|
|
|
|
return s.makeResponseNullIP(m)
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genServerFailure(request *dns.Msg) *dns.Msg {
|
|
|
|
resp := dns.Msg{}
|
|
|
|
resp.SetRcode(request, dns.RcodeServerFailure)
|
|
|
|
resp.RecursionAvailable = true
|
|
|
|
return &resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genARecord(request *dns.Msg, ip net.IP) *dns.Msg {
|
|
|
|
resp := s.makeResponse(request)
|
2020-12-23 09:35:07 +00:00
|
|
|
resp.Answer = append(resp.Answer, s.genAnswerA(request, ip))
|
2020-05-08 15:39:37 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genAAAARecord(request *dns.Msg, ip net.IP) *dns.Msg {
|
|
|
|
resp := s.makeResponse(request)
|
2020-12-23 09:35:07 +00:00
|
|
|
resp.Answer = append(resp.Answer, s.genAnswerAAAA(request, ip))
|
2020-05-08 15:39:37 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:35:07 +00:00
|
|
|
func (s *Server) hdr(req *dns.Msg, rrType rules.RRType) (h dns.RR_Header) {
|
|
|
|
return dns.RR_Header{
|
2020-05-08 15:39:37 +00:00
|
|
|
Name: req.Question[0].Name,
|
2020-12-23 09:35:07 +00:00
|
|
|
Rrtype: rrType,
|
2020-05-08 15:39:37 +00:00
|
|
|
Ttl: s.conf.BlockedResponseTTL,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:35:07 +00:00
|
|
|
func (s *Server) genAnswerA(req *dns.Msg, ip net.IP) (ans *dns.A) {
|
|
|
|
return &dns.A{
|
|
|
|
Hdr: s.hdr(req, dns.TypeA),
|
|
|
|
A: ip,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genAnswerAAAA(req *dns.Msg, ip net.IP) (ans *dns.AAAA) {
|
|
|
|
return &dns.AAAA{
|
|
|
|
Hdr: s.hdr(req, dns.TypeAAAA),
|
|
|
|
AAAA: ip,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genAnswerCNAME(req *dns.Msg, cname string) (ans *dns.CNAME) {
|
|
|
|
return &dns.CNAME{
|
|
|
|
Hdr: s.hdr(req, dns.TypeCNAME),
|
|
|
|
Target: dns.Fqdn(cname),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genAnswerMX(req *dns.Msg, mx *rules.DNSMX) (ans *dns.MX) {
|
|
|
|
return &dns.MX{
|
|
|
|
Hdr: s.hdr(req, dns.TypePTR),
|
|
|
|
Preference: mx.Preference,
|
|
|
|
Mx: mx.Exchange,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genAnswerPTR(req *dns.Msg, ptr string) (ans *dns.PTR) {
|
|
|
|
return &dns.PTR{
|
|
|
|
Hdr: s.hdr(req, dns.TypePTR),
|
|
|
|
Ptr: ptr,
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:35:07 +00:00
|
|
|
func (s *Server) genAnswerTXT(req *dns.Msg, strs []string) (ans *dns.TXT) {
|
2020-12-21 14:48:07 +00:00
|
|
|
return &dns.TXT{
|
2020-12-23 09:35:07 +00:00
|
|
|
Hdr: s.hdr(req, dns.TypeTXT),
|
2020-12-21 14:48:07 +00:00
|
|
|
Txt: strs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
// generate DNS response message with an IP address
|
|
|
|
func (s *Server) genResponseWithIP(req *dns.Msg, ip net.IP) *dns.Msg {
|
|
|
|
if req.Question[0].Qtype == dns.TypeA && ip.To4() != nil {
|
|
|
|
return s.genARecord(req, ip.To4())
|
|
|
|
} else if req.Question[0].Qtype == dns.TypeAAAA &&
|
|
|
|
len(ip) == net.IPv6len && ip.To4() == nil {
|
|
|
|
return s.genAAAARecord(req, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty response
|
|
|
|
resp := s.makeResponse(req)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2020-10-02 09:51:55 +00:00
|
|
|
// Respond with 0.0.0.0 for A, :: for AAAA, empty response for other types
|
|
|
|
func (s *Server) makeResponseNullIP(req *dns.Msg) *dns.Msg {
|
|
|
|
if req.Question[0].Qtype == dns.TypeA {
|
|
|
|
return s.genARecord(req, []byte{0, 0, 0, 0})
|
|
|
|
} else if req.Question[0].Qtype == dns.TypeAAAA {
|
|
|
|
return s.genAAAARecord(req, net.IPv6zero)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.makeResponse(req)
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
func (s *Server) genBlockedHost(request *dns.Msg, newAddr string, d *proxy.DNSContext) *dns.Msg {
|
|
|
|
ip := net.ParseIP(newAddr)
|
|
|
|
if ip != nil {
|
|
|
|
return s.genResponseWithIP(request, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// look up the hostname, TODO: cache
|
|
|
|
replReq := dns.Msg{}
|
|
|
|
replReq.SetQuestion(dns.Fqdn(newAddr), request.Question[0].Qtype)
|
|
|
|
replReq.RecursionDesired = true
|
|
|
|
|
|
|
|
newContext := &proxy.DNSContext{
|
|
|
|
Proto: d.Proto,
|
|
|
|
Addr: d.Addr,
|
|
|
|
StartTime: time.Now(),
|
|
|
|
Req: &replReq,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.dnsProxy.Resolve(newContext)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Printf("Couldn't look up replacement host %q: %s", newAddr, err)
|
2020-05-08 15:39:37 +00:00
|
|
|
return s.genServerFailure(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := s.makeResponse(request)
|
|
|
|
if newContext.Res != nil {
|
|
|
|
for _, answer := range newContext.Res.Answer {
|
|
|
|
answer.Header().Name = request.Question[0].Name
|
|
|
|
resp.Answer = append(resp.Answer, answer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:26:01 +00:00
|
|
|
// Create REFUSED DNS response
|
|
|
|
func (s *Server) makeResponseREFUSED(request *dns.Msg) *dns.Msg {
|
|
|
|
resp := dns.Msg{}
|
|
|
|
resp.SetRcode(request, dns.RcodeRefused)
|
|
|
|
resp.RecursionAvailable = true
|
|
|
|
return &resp
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
func (s *Server) genNXDomain(request *dns.Msg) *dns.Msg {
|
|
|
|
resp := dns.Msg{}
|
|
|
|
resp.SetRcode(request, dns.RcodeNameError)
|
|
|
|
resp.RecursionAvailable = true
|
|
|
|
resp.Ns = s.genSOA(request)
|
|
|
|
return &resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genSOA(request *dns.Msg) []dns.RR {
|
|
|
|
zone := ""
|
|
|
|
if len(request.Question) > 0 {
|
|
|
|
zone = request.Question[0].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
soa := dns.SOA{
|
|
|
|
// values copied from verisign's nonexistent .com domain
|
|
|
|
// their exact values are not important in our use case because they are used for domain transfers between primary/secondary DNS servers
|
|
|
|
Refresh: 1800,
|
|
|
|
Retry: 900,
|
|
|
|
Expire: 604800,
|
|
|
|
Minttl: 86400,
|
|
|
|
// copied from AdGuard DNS
|
|
|
|
Ns: "fake-for-negative-caching.adguard.com.",
|
|
|
|
Serial: 100500,
|
|
|
|
// rest is request-specific
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: zone,
|
|
|
|
Rrtype: dns.TypeSOA,
|
|
|
|
Ttl: s.conf.BlockedResponseTTL,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Mbox: "hostmaster.", // zone will be appended later if it's not empty or "."
|
|
|
|
}
|
|
|
|
if soa.Hdr.Ttl == 0 {
|
|
|
|
soa.Hdr.Ttl = defaultValues.BlockedResponseTTL
|
|
|
|
}
|
|
|
|
if len(zone) > 0 && zone[0] != '.' {
|
|
|
|
soa.Mbox += zone
|
|
|
|
}
|
|
|
|
return []dns.RR{&soa}
|
|
|
|
}
|