2020-12-21 17:48:07 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-08-23 16:58:24 +03:00
|
|
|
"net/netip"
|
2020-12-21 17:48:07 +03:00
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-12-21 17:48:07 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-12-21 17:48:07 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
// filterDNSRewriteResponse handles a single DNS rewrite response entry. It
|
|
|
|
// returns the properly constructed answer resource record.
|
|
|
|
func (s *Server) filterDNSRewriteResponse(
|
|
|
|
req *dns.Msg,
|
|
|
|
rr rules.RRType,
|
|
|
|
v rules.RRValue,
|
|
|
|
) (ans dns.RR, err error) {
|
2020-12-21 17:48:07 +03:00
|
|
|
switch rr {
|
2022-10-28 17:42:45 +03:00
|
|
|
case dns.TypeA, dns.TypeAAAA:
|
2022-09-06 19:03:47 +03:00
|
|
|
return s.ansFromDNSRewriteIP(v, rr, req)
|
2022-10-28 17:42:45 +03:00
|
|
|
case dns.TypePTR, dns.TypeTXT:
|
2022-09-06 19:03:47 +03:00
|
|
|
return s.ansFromDNSRewriteText(v, rr, req)
|
|
|
|
case dns.TypeMX:
|
|
|
|
return s.ansFromDNSRewriteMX(v, rr, req)
|
2022-10-28 17:42:45 +03:00
|
|
|
case dns.TypeHTTPS, dns.TypeSVCB:
|
2022-09-06 19:03:47 +03:00
|
|
|
return s.ansFromDNSRewriteSVCB(v, rr, req)
|
|
|
|
case dns.TypeSRV:
|
|
|
|
return s.ansFromDNSRewriteSRV(v, rr, req)
|
|
|
|
default:
|
|
|
|
log.Debug("don't know how to handle dns rr type %d, skipping", rr)
|
2020-12-21 17:48:07 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 17:48:07 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
// ansFromDNSRewriteIP creates a new answer resource record from the A/AAAA
|
|
|
|
// dnsrewrite rule data.
|
|
|
|
func (s *Server) ansFromDNSRewriteIP(
|
|
|
|
v rules.RRValue,
|
|
|
|
rr rules.RRType,
|
|
|
|
req *dns.Msg,
|
|
|
|
) (ans dns.RR, err error) {
|
2023-08-23 16:58:24 +03:00
|
|
|
ip, ok := v.(netip.Addr)
|
2022-09-06 19:03:47 +03:00
|
|
|
if !ok {
|
2023-08-23 16:58:24 +03:00
|
|
|
return nil, fmt.Errorf("value for rr type %s has type %T, not netip.Addr", dns.Type(rr), v)
|
2022-09-06 19:03:47 +03:00
|
|
|
}
|
2020-12-23 12:35:07 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
if rr == dns.TypeA {
|
2023-08-23 16:58:24 +03:00
|
|
|
return s.genAnswerA(req, ip), nil
|
2022-09-06 19:03:47 +03:00
|
|
|
}
|
2020-12-23 12:35:07 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
return s.genAnswerAAAA(req, ip), nil
|
|
|
|
}
|
2020-12-23 12:35:07 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
// ansFromDNSRewriteText creates a new answer resource record from the TXT/PTR
|
|
|
|
// dnsrewrite rule data.
|
|
|
|
func (s *Server) ansFromDNSRewriteText(
|
|
|
|
v rules.RRValue,
|
|
|
|
rr rules.RRType,
|
|
|
|
req *dns.Msg,
|
|
|
|
) (ans dns.RR, err error) {
|
|
|
|
str, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("value for rr type %s has type %T, not string", dns.Type(rr), v)
|
|
|
|
}
|
2020-12-23 12:35:07 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
if rr == dns.TypeTXT {
|
|
|
|
return s.genAnswerTXT(req, []string{str}), nil
|
|
|
|
}
|
2020-12-21 17:48:07 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
return s.genAnswerPTR(req, str), nil
|
|
|
|
}
|
2021-03-15 13:08:13 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
// ansFromDNSRewriteMX creates a new answer resource record from the MX
|
|
|
|
// dnsrewrite rule data.
|
|
|
|
func (s *Server) ansFromDNSRewriteMX(
|
|
|
|
v rules.RRValue,
|
|
|
|
rr rules.RRType,
|
|
|
|
req *dns.Msg,
|
|
|
|
) (ans dns.RR, err error) {
|
|
|
|
mx, ok := v.(*rules.DNSMX)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"value for rr type %s has type %T, not *rules.DNSMX",
|
|
|
|
dns.Type(rr),
|
|
|
|
v,
|
|
|
|
)
|
|
|
|
}
|
2020-12-21 17:48:07 +03:00
|
|
|
|
2022-09-06 19:03:47 +03:00
|
|
|
return s.genAnswerMX(req, mx), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ansFromDNSRewriteSVCB creates a new answer resource record from the
|
|
|
|
// SVCB/HTTPS dnsrewrite rule data.
|
|
|
|
func (s *Server) ansFromDNSRewriteSVCB(
|
|
|
|
v rules.RRValue,
|
|
|
|
rr rules.RRType,
|
|
|
|
req *dns.Msg,
|
|
|
|
) (ans dns.RR, err error) {
|
|
|
|
svcb, ok := v.(*rules.DNSSVCB)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"value for rr type %s has type %T, not *rules.DNSSVCB",
|
|
|
|
dns.Type(rr),
|
|
|
|
v,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rr == dns.TypeHTTPS {
|
|
|
|
return s.genAnswerHTTPS(req, svcb), nil
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
2022-09-06 19:03:47 +03:00
|
|
|
|
|
|
|
return s.genAnswerSVCB(req, svcb), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ansFromDNSRewriteSRV creates a new answer resource record from the SRV
|
|
|
|
// dnsrewrite rule data.
|
|
|
|
func (s *Server) ansFromDNSRewriteSRV(
|
|
|
|
v rules.RRValue,
|
|
|
|
rr rules.RRType,
|
|
|
|
req *dns.Msg,
|
|
|
|
) (dns.RR, error) {
|
|
|
|
srv, ok := v.(*rules.DNSSRV)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"value for rr type %s has type %T, not *rules.DNSSRV",
|
|
|
|
dns.Type(rr),
|
|
|
|
v,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.genAnswerSRV(req, srv), nil
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
// filterDNSRewrite handles dnsrewrite filters. It constructs a DNS response
|
|
|
|
// and sets it into pctx.Res. All parameters must not be nil.
|
|
|
|
func (s *Server) filterDNSRewrite(
|
|
|
|
req *dns.Msg,
|
|
|
|
res *filtering.Result,
|
|
|
|
pctx *proxy.DNSContext,
|
|
|
|
) (err error) {
|
2024-04-11 14:03:37 +03:00
|
|
|
resp := s.replyCompressed(req)
|
2020-12-21 17:48:07 +03:00
|
|
|
dnsrr := res.DNSRewriteResult
|
|
|
|
if dnsrr == nil {
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("no dns rewrite rule content")
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.Rcode = dnsrr.RCode
|
|
|
|
if resp.Rcode != dns.RcodeSuccess {
|
2022-09-02 14:52:19 +03:00
|
|
|
pctx.Res = resp
|
2020-12-21 17:48:07 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if dnsrr.Response == nil {
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("no dns rewrite rule responses")
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 16:58:24 +03:00
|
|
|
qtype := req.Question[0].Qtype
|
|
|
|
values := dnsrr.Response[qtype]
|
2020-12-21 17:48:07 +03:00
|
|
|
for i, v := range values {
|
|
|
|
var ans dns.RR
|
2023-08-23 16:58:24 +03:00
|
|
|
ans, err = s.filterDNSRewriteResponse(req, qtype, v)
|
2020-12-21 17:48:07 +03:00
|
|
|
if err != nil {
|
2023-08-23 16:58:24 +03:00
|
|
|
return fmt.Errorf("dns rewrite response for %s[%d]: %w", dns.Type(qtype), i, err)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:52:19 +03:00
|
|
|
pctx.Res = resp
|
2020-12-21 17:48:07 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|