2020-05-08 16:39:37 +01:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2023-08-23 14:58:24 +01:00
|
|
|
"net/netip"
|
2024-02-08 17:39:18 +00:00
|
|
|
"slices"
|
2020-05-08 16:39:37 +01:00
|
|
|
|
2021-05-21 14:15:47 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-08 16:39:37 +01: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 16:39:37 +01:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
// TODO(e.burkov): Name all the methods by a [proxy.MessageConstructor]
|
|
|
|
// template. Also extract all the methods to a separate entity.
|
2020-12-21 14:48:07 +00:00
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
// reply creates a DNS response for req.
|
|
|
|
func (*Server) reply(req *dns.Msg, code int) (resp *dns.Msg) {
|
|
|
|
resp = (&dns.Msg{}).SetRcode(req, code)
|
|
|
|
resp.RecursionAvailable = true
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// replyCompressed creates a DNS response for req and sets the compress flag.
|
|
|
|
func (s *Server) replyCompressed(req *dns.Msg) (resp *dns.Msg) {
|
|
|
|
resp = s.reply(req, dns.RcodeSuccess)
|
|
|
|
resp.Compress = true
|
2020-12-21 14:48:07 +00:00
|
|
|
|
|
|
|
return resp
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:46:01 +01:00
|
|
|
// ipsFromRules extracts unique non-IP addresses from the filtering result
|
|
|
|
// rules.
|
2023-08-23 14:58:24 +01:00
|
|
|
func ipsFromRules(resRules []*filtering.ResultRule) (ips []netip.Addr) {
|
2021-06-17 12:09:16 +01:00
|
|
|
for _, r := range resRules {
|
2023-07-11 13:46:01 +01:00
|
|
|
// len(resRules) and len(ips) are actually small enough for O(n^2) to do
|
|
|
|
// not raise performance questions.
|
2023-08-23 14:58:24 +01:00
|
|
|
if ip := r.IP; ip != (netip.Addr{}) && !slices.Contains(ips, ip) {
|
2023-07-11 13:46:01 +01:00
|
|
|
ips = append(ips, ip)
|
2021-06-17 12:09:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:09:54 +01:00
|
|
|
// genDNSFilterMessage generates a filtered response to req for the filtering
|
|
|
|
// result res.
|
|
|
|
func (s *Server) genDNSFilterMessage(
|
|
|
|
dctx *proxy.DNSContext,
|
|
|
|
res *filtering.Result,
|
|
|
|
) (resp *dns.Msg) {
|
|
|
|
req := dctx.Req
|
2023-08-09 14:27:21 +01:00
|
|
|
qt := req.Question[0].Qtype
|
2024-04-22 08:48:26 +01:00
|
|
|
if qt != dns.TypeA && qt != dns.TypeAAAA && qt != dns.TypeHTTPS {
|
2023-09-04 15:18:43 +01:00
|
|
|
m, _, _ := s.dnsFilter.BlockingMode()
|
|
|
|
if m == filtering.BlockingModeNullIP {
|
2024-04-11 12:03:37 +01:00
|
|
|
return s.replyCompressed(req)
|
2020-10-02 10:51:55 +01:00
|
|
|
}
|
2022-09-06 15:09:54 +01:00
|
|
|
|
2023-08-09 14:27:21 +01:00
|
|
|
return s.newMsgNODATA(req)
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 15:09:54 +01:00
|
|
|
switch res.Reason {
|
2021-05-21 14:15:47 +01:00
|
|
|
case filtering.FilteredSafeBrowsing:
|
2023-09-04 15:18:43 +01:00
|
|
|
return s.genBlockedHost(req, s.dnsFilter.SafeBrowsingBlockHost(), dctx)
|
2021-05-21 14:15:47 +01:00
|
|
|
case filtering.FilteredParental:
|
2023-09-04 15:18:43 +01:00
|
|
|
return s.genBlockedHost(req, s.dnsFilter.ParentalBlockHost(), dctx)
|
2023-06-19 13:45:01 +01:00
|
|
|
case filtering.FilteredSafeSearch:
|
|
|
|
// If Safe Search generated the necessary IP addresses, use them.
|
|
|
|
// Otherwise, if there were no errors, there are no addresses for the
|
|
|
|
// requested IP version, so produce a NODATA response.
|
2023-12-12 13:20:55 +00:00
|
|
|
return s.getCNAMEWithIPs(req, ipsFromRules(res.Rules), res.CanonName)
|
2020-05-08 16:39:37 +01:00
|
|
|
default:
|
2023-06-19 13:45:01 +01:00
|
|
|
return s.genForBlockingMode(req, ipsFromRules(res.Rules))
|
2022-09-06 15:09:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 13:20:55 +00:00
|
|
|
// getCNAMEWithIPs generates a filtered response to req for with CNAME record
|
|
|
|
// and provided ips.
|
|
|
|
func (s *Server) getCNAMEWithIPs(req *dns.Msg, ips []netip.Addr, cname string) (resp *dns.Msg) {
|
2024-04-11 12:03:37 +01:00
|
|
|
resp = s.replyCompressed(req)
|
2023-12-12 13:20:55 +00:00
|
|
|
|
|
|
|
originalName := req.Question[0].Name
|
|
|
|
|
|
|
|
var ans []dns.RR
|
|
|
|
if cname != "" {
|
|
|
|
ans = append(ans, s.genAnswerCNAME(req, cname))
|
|
|
|
|
|
|
|
// The given IPs actually are resolved for this cname.
|
|
|
|
req.Question[0].Name = dns.Fqdn(cname)
|
|
|
|
defer func() { req.Question[0].Name = originalName }()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Question[0].Qtype {
|
|
|
|
case dns.TypeA:
|
|
|
|
ans = append(ans, s.genAnswersWithIPv4s(req, ips)...)
|
|
|
|
case dns.TypeAAAA:
|
|
|
|
for _, ip := range ips {
|
|
|
|
if ip.Is6() {
|
|
|
|
ans = append(ans, s.genAnswerAAAA(req, ip))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Go on and return an empty response.
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = ans
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:09:54 +01:00
|
|
|
// genForBlockingMode generates a filtered response to req based on the server's
|
|
|
|
// blocking mode.
|
2023-08-23 14:58:24 +01:00
|
|
|
func (s *Server) genForBlockingMode(req *dns.Msg, ips []netip.Addr) (resp *dns.Msg) {
|
2023-09-04 15:18:43 +01:00
|
|
|
switch mode, bIPv4, bIPv6 := s.dnsFilter.BlockingMode(); mode {
|
2023-08-30 16:26:02 +01:00
|
|
|
case filtering.BlockingModeCustomIP:
|
2023-09-04 15:18:43 +01:00
|
|
|
return s.makeResponseCustomIP(req, bIPv4, bIPv6)
|
2023-08-30 16:26:02 +01:00
|
|
|
case filtering.BlockingModeDefault:
|
2022-09-06 15:09:54 +01:00
|
|
|
if len(ips) > 0 {
|
|
|
|
return s.genResponseWithIPs(req, ips)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.makeResponseNullIP(req)
|
2023-08-30 16:26:02 +01:00
|
|
|
case filtering.BlockingModeNullIP:
|
2022-09-06 15:09:54 +01:00
|
|
|
return s.makeResponseNullIP(req)
|
2023-08-30 16:26:02 +01:00
|
|
|
case filtering.BlockingModeNXDOMAIN:
|
2024-04-11 12:03:37 +01:00
|
|
|
return s.NewMsgNXDOMAIN(req)
|
2023-08-30 16:26:02 +01:00
|
|
|
case filtering.BlockingModeREFUSED:
|
2022-09-06 15:09:54 +01:00
|
|
|
return s.makeResponseREFUSED(req)
|
|
|
|
default:
|
2023-11-20 15:07:23 +00:00
|
|
|
log.Error("dnsforward: invalid blocking mode %q", mode)
|
2023-09-04 15:18:43 +01:00
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
return s.replyCompressed(req)
|
2023-09-04 15:18:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeResponseCustomIP generates a DNS response message for Custom IP blocking
|
|
|
|
// mode with the provided IP addresses and an appropriate resource record type.
|
|
|
|
func (s *Server) makeResponseCustomIP(
|
|
|
|
req *dns.Msg,
|
|
|
|
bIPv4 netip.Addr,
|
|
|
|
bIPv6 netip.Addr,
|
|
|
|
) (resp *dns.Msg) {
|
|
|
|
switch qt := req.Question[0].Qtype; qt {
|
|
|
|
case dns.TypeA:
|
|
|
|
return s.genARecord(req, bIPv4)
|
|
|
|
case dns.TypeAAAA:
|
|
|
|
return s.genAAAARecord(req, bIPv6)
|
|
|
|
default:
|
|
|
|
// Generally shouldn't happen, since the types are checked in
|
|
|
|
// genDNSFilterMessage.
|
2023-11-20 15:07:23 +00:00
|
|
|
log.Error("dnsforward: invalid msg type %s for custom IP blocking mode", dns.Type(qt))
|
2022-09-06 15:09:54 +01:00
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
return s.replyCompressed(req)
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:58:24 +01:00
|
|
|
func (s *Server) genARecord(request *dns.Msg, ip netip.Addr) *dns.Msg {
|
2024-04-11 12:03:37 +01:00
|
|
|
resp := s.replyCompressed(request)
|
2020-12-23 09:35:07 +00:00
|
|
|
resp.Answer = append(resp.Answer, s.genAnswerA(request, ip))
|
2020-05-08 16:39:37 +01:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:58:24 +01:00
|
|
|
func (s *Server) genAAAARecord(request *dns.Msg, ip netip.Addr) *dns.Msg {
|
2024-04-11 12:03:37 +01:00
|
|
|
resp := s.replyCompressed(request)
|
2020-12-23 09:35:07 +00:00
|
|
|
resp.Answer = append(resp.Answer, s.genAnswerAAAA(request, ip))
|
2020-05-08 16:39:37 +01: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 16:39:37 +01:00
|
|
|
Name: req.Question[0].Name,
|
2020-12-23 09:35:07 +00:00
|
|
|
Rrtype: rrType,
|
2023-09-04 15:18:43 +01:00
|
|
|
Ttl: s.dnsFilter.BlockedResponseTTL(),
|
2020-05-08 16:39:37 +01:00
|
|
|
Class: dns.ClassINET,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:58:24 +01:00
|
|
|
func (s *Server) genAnswerA(req *dns.Msg, ip netip.Addr) (ans *dns.A) {
|
2020-12-23 09:35:07 +00:00
|
|
|
return &dns.A{
|
|
|
|
Hdr: s.hdr(req, dns.TypeA),
|
2023-08-23 14:58:24 +01:00
|
|
|
A: ip.AsSlice(),
|
2020-12-23 09:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:58:24 +01:00
|
|
|
func (s *Server) genAnswerAAAA(req *dns.Msg, ip netip.Addr) (ans *dns.AAAA) {
|
2020-12-23 09:35:07 +00:00
|
|
|
return &dns.AAAA{
|
|
|
|
Hdr: s.hdr(req, dns.TypeAAAA),
|
2023-08-23 14:58:24 +01:00
|
|
|
AAAA: ip.AsSlice(),
|
2020-12-23 09:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2021-03-15 10:08:13 +00:00
|
|
|
Hdr: s.hdr(req, dns.TypeMX),
|
2020-12-23 09:35:07 +00:00
|
|
|
Preference: mx.Preference,
|
2021-03-15 14:17:16 +00:00
|
|
|
Mx: dns.Fqdn(mx.Exchange),
|
2020-12-23 09:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) genAnswerPTR(req *dns.Msg, ptr string) (ans *dns.PTR) {
|
|
|
|
return &dns.PTR{
|
|
|
|
Hdr: s.hdr(req, dns.TypePTR),
|
2021-03-15 14:17:16 +00:00
|
|
|
Ptr: dns.Fqdn(ptr),
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 10:08:13 +00:00
|
|
|
func (s *Server) genAnswerSRV(req *dns.Msg, srv *rules.DNSSRV) (ans *dns.SRV) {
|
|
|
|
return &dns.SRV{
|
|
|
|
Hdr: s.hdr(req, dns.TypeSRV),
|
|
|
|
Priority: srv.Priority,
|
|
|
|
Weight: srv.Weight,
|
|
|
|
Port: srv.Port,
|
2021-03-15 14:17:16 +00:00
|
|
|
Target: dns.Fqdn(srv.Target),
|
2021-03-15 10:08:13 +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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 12:09:16 +01:00
|
|
|
// genResponseWithIPs generates a DNS response message with the provided IP
|
|
|
|
// addresses and an appropriate resource record type. If any of the IPs cannot
|
|
|
|
// be converted to the correct protocol, genResponseWithIPs returns an empty
|
|
|
|
// response.
|
2023-08-23 14:58:24 +01:00
|
|
|
func (s *Server) genResponseWithIPs(req *dns.Msg, ips []netip.Addr) (resp *dns.Msg) {
|
2021-06-17 12:09:16 +01:00
|
|
|
var ans []dns.RR
|
|
|
|
switch req.Question[0].Qtype {
|
|
|
|
case dns.TypeA:
|
2023-11-20 15:07:23 +00:00
|
|
|
ans = s.genAnswersWithIPv4s(req, ips)
|
2021-06-17 12:09:16 +01:00
|
|
|
case dns.TypeAAAA:
|
|
|
|
for _, ip := range ips {
|
2023-08-23 14:58:24 +01:00
|
|
|
if ip.Is6() {
|
|
|
|
ans = append(ans, s.genAnswerAAAA(req, ip))
|
|
|
|
}
|
2021-06-17 12:09:16 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Go on and return an empty response.
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
resp = s.replyCompressed(req)
|
2021-06-17 12:09:16 +01:00
|
|
|
resp.Answer = ans
|
|
|
|
|
2020-05-08 16:39:37 +01:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:07:23 +00:00
|
|
|
// genAnswersWithIPv4s generates DNS A answers provided IPv4 addresses. If any
|
|
|
|
// of the IPs isn't an IPv4 address, genAnswersWithIPv4s logs a warning and
|
|
|
|
// returns nil,
|
|
|
|
func (s *Server) genAnswersWithIPv4s(req *dns.Msg, ips []netip.Addr) (ans []dns.RR) {
|
|
|
|
for _, ip := range ips {
|
|
|
|
if !ip.Is4() {
|
|
|
|
log.Info("dnsforward: warning: ip %s is not ipv4 address", ip)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ans = append(ans, s.genAnswerA(req, ip))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
2021-06-17 12:09:16 +01:00
|
|
|
// makeResponseNullIP creates a response with 0.0.0.0 for A requests, :: for
|
|
|
|
// AAAA requests, and an empty response for other types.
|
|
|
|
func (s *Server) makeResponseNullIP(req *dns.Msg) (resp *dns.Msg) {
|
|
|
|
// Respond with the corresponding zero IP type as opposed to simply
|
|
|
|
// using one or the other in both cases, because the IPv4 zero IP is
|
|
|
|
// converted to a IPV6-mapped IPv4 address, while the IPv6 zero IP is
|
|
|
|
// converted into an empty slice instead of the zero IPv4.
|
|
|
|
switch req.Question[0].Qtype {
|
|
|
|
case dns.TypeA:
|
2023-08-23 14:58:24 +01:00
|
|
|
resp = s.genResponseWithIPs(req, []netip.Addr{netip.IPv4Unspecified()})
|
2021-06-17 12:09:16 +01:00
|
|
|
case dns.TypeAAAA:
|
2023-08-23 14:58:24 +01:00
|
|
|
resp = s.genResponseWithIPs(req, []netip.Addr{netip.IPv6Unspecified()})
|
2021-06-17 12:09:16 +01:00
|
|
|
default:
|
2024-04-11 12:03:37 +01:00
|
|
|
resp = s.replyCompressed(req)
|
2020-10-02 10:51:55 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 12:09:16 +01:00
|
|
|
return resp
|
2020-10-02 10:51:55 +01:00
|
|
|
}
|
|
|
|
|
2020-05-08 16:39:37 +01:00
|
|
|
func (s *Server) genBlockedHost(request *dns.Msg, newAddr string, d *proxy.DNSContext) *dns.Msg {
|
2023-08-30 16:26:02 +01:00
|
|
|
if newAddr == "" {
|
2023-11-20 15:07:23 +00:00
|
|
|
log.Info("dnsforward: block host is not specified")
|
2023-08-30 16:26:02 +01:00
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
return s.NewMsgSERVFAIL(request)
|
2023-08-30 16:26:02 +01:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:58:24 +01:00
|
|
|
ip, err := netip.ParseAddr(newAddr)
|
|
|
|
if err == nil {
|
|
|
|
return s.genResponseWithIPs(request, []netip.Addr{ip})
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// look up the hostname, TODO: cache
|
|
|
|
replReq := dns.Msg{}
|
|
|
|
replReq.SetQuestion(dns.Fqdn(newAddr), request.Question[0].Qtype)
|
|
|
|
replReq.RecursionDesired = true
|
|
|
|
|
|
|
|
newContext := &proxy.DNSContext{
|
2023-11-09 13:14:05 +00:00
|
|
|
Proto: d.Proto,
|
|
|
|
Addr: d.Addr,
|
|
|
|
Req: &replReq,
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
2021-10-06 11:14:41 +01:00
|
|
|
prx := s.proxy()
|
|
|
|
if prx == nil {
|
2023-11-20 15:07:23 +00:00
|
|
|
log.Debug("dnsforward: %s", srvClosedErr)
|
2021-10-06 11:14:41 +01:00
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
return s.NewMsgSERVFAIL(request)
|
2021-10-06 11:14:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:58:24 +01:00
|
|
|
err = prx.Resolve(newContext)
|
2020-05-08 16:39:37 +01:00
|
|
|
if err != nil {
|
2023-11-20 15:07:23 +00:00
|
|
|
log.Info("dnsforward: looking up replacement host %q: %s", newAddr, err)
|
2021-10-06 11:14:41 +01:00
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
return s.NewMsgSERVFAIL(request)
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:03:37 +01:00
|
|
|
resp := s.replyCompressed(request)
|
2020-05-08 16:39:37 +01:00
|
|
|
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 12:26:01 +01:00
|
|
|
// Create REFUSED DNS response
|
2024-04-11 12:03:37 +01:00
|
|
|
func (s *Server) makeResponseREFUSED(req *dns.Msg) *dns.Msg {
|
|
|
|
return s.reply(req, dns.RcodeRefused)
|
2020-08-18 12:26:01 +01:00
|
|
|
}
|
|
|
|
|
2023-08-09 14:27:21 +01:00
|
|
|
// newMsgNODATA returns a properly initialized NODATA response.
|
|
|
|
//
|
|
|
|
// See https://www.rfc-editor.org/rfc/rfc2308#section-2.2.
|
|
|
|
func (s *Server) newMsgNODATA(req *dns.Msg) (resp *dns.Msg) {
|
2024-04-11 12:03:37 +01:00
|
|
|
resp = s.reply(req, dns.RcodeSuccess)
|
2023-08-09 14:27:21 +01:00
|
|
|
resp.Ns = s.genSOA(req)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2020-05-08 16:39:37 +01:00
|
|
|
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,
|
2023-09-04 15:18:43 +01:00
|
|
|
Ttl: s.dnsFilter.BlockedResponseTTL(),
|
2020-05-08 16:39:37 +01:00
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Mbox: "hostmaster.", // zone will be appended later if it's not empty or "."
|
|
|
|
}
|
|
|
|
if soa.Hdr.Ttl == 0 {
|
2023-08-30 16:26:02 +01:00
|
|
|
soa.Hdr.Ttl = defaultBlockedResponseTTL
|
2020-05-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
if len(zone) > 0 && zone[0] != '.' {
|
|
|
|
soa.Mbox += zone
|
|
|
|
}
|
2024-04-11 12:03:37 +01:00
|
|
|
|
2020-05-08 16:39:37 +01:00
|
|
|
return []dns.RR{&soa}
|
|
|
|
}
|
2024-04-11 12:03:37 +01:00
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ proxy.MessageConstructor = (*Server)(nil)
|
|
|
|
|
|
|
|
// NewMsgNXDOMAIN implements the [proxy.MessageConstructor] interface for
|
|
|
|
// *Server.
|
|
|
|
func (s *Server) NewMsgNXDOMAIN(req *dns.Msg) (resp *dns.Msg) {
|
|
|
|
resp = s.reply(req, dns.RcodeNameError)
|
|
|
|
resp.Ns = s.genSOA(req)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgSERVFAIL implements the [proxy.MessageConstructor] interface for
|
|
|
|
// *Server.
|
|
|
|
func (s *Server) NewMsgSERVFAIL(req *dns.Msg) (resp *dns.Msg) {
|
|
|
|
return s.reply(req, dns.RcodeServerFailure)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgNOTIMPLEMENTED implements the [proxy.MessageConstructor] interface for
|
|
|
|
// *Server.
|
|
|
|
func (s *Server) NewMsgNOTIMPLEMENTED(req *dns.Msg) (resp *dns.Msg) {
|
|
|
|
resp = s.reply(req, dns.RcodeNotImplemented)
|
|
|
|
|
|
|
|
// Most of the Internet and especially the inner core has an MTU of at least
|
|
|
|
// 1500 octets. Maximum DNS/UDP payload size for IPv6 on MTU 1500 ethernet
|
|
|
|
// is 1452 (1500 minus 40 (IPv6 header size) minus 8 (UDP header size)).
|
|
|
|
//
|
|
|
|
// See appendix A of https://datatracker.ietf.org/doc/draft-ietf-dnsop-avoid-fragmentation/17.
|
|
|
|
const maxUDPPayload = 1452
|
|
|
|
|
|
|
|
// NOTIMPLEMENTED without EDNS is treated as 'we don't support EDNS', so
|
|
|
|
// explicitly set it.
|
|
|
|
resp.SetEdns0(maxUDPPayload, false)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|