2019-10-09 16:51:26 +00:00
|
|
|
// DNS Rewrites
|
|
|
|
|
2021-05-21 13:15:47 +00:00
|
|
|
package filtering
|
2019-10-09 16:51:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-12-27 16:40:39 +00:00
|
|
|
"fmt"
|
2020-01-16 09:51:35 +00:00
|
|
|
"net"
|
2019-10-09 16:51:26 +00:00
|
|
|
"net/http"
|
2020-01-16 09:51:35 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-10-09 16:51:26 +00:00
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2021-12-27 16:40:39 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-10-09 16:51:26 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-12-27 16:40:39 +00:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2020-01-16 09:51:35 +00:00
|
|
|
"github.com/miekg/dns"
|
2019-10-09 16:51:26 +00:00
|
|
|
)
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
// LegacyRewrite is a single legacy DNS rewrite record.
|
|
|
|
//
|
|
|
|
// Instances of *LegacyRewrite must never be nil.
|
|
|
|
type LegacyRewrite struct {
|
|
|
|
// Domain is the domain pattern for which this rewrite should work.
|
2020-01-16 09:51:35 +00:00
|
|
|
Domain string `yaml:"domain"`
|
2021-12-24 17:14:36 +00:00
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// Answer is the IP address, canonical name, or one of the special
|
|
|
|
// values: "A" or "AAAA".
|
|
|
|
Answer string `yaml:"answer"`
|
2021-12-24 17:14:36 +00:00
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// IP is the IP address that should be used in the response if Type is
|
2021-12-27 16:40:39 +00:00
|
|
|
// dns.TypeA or dns.TypeAAAA.
|
2021-07-12 18:10:39 +00:00
|
|
|
IP net.IP `yaml:"-"`
|
2021-12-24 17:14:36 +00:00
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// Type is the DNS record type: A, AAAA, or CNAME.
|
|
|
|
Type uint16 `yaml:"-"`
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
// clone returns a deep clone of rw.
|
|
|
|
func (rw *LegacyRewrite) clone() (cloneRW *LegacyRewrite) {
|
|
|
|
return &LegacyRewrite{
|
|
|
|
Domain: rw.Domain,
|
|
|
|
Answer: rw.Answer,
|
|
|
|
IP: netutil.CloneIP(rw.IP),
|
|
|
|
Type: rw.Type,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// equal returns true if the rw is equal to the other.
|
|
|
|
func (rw *LegacyRewrite) equal(other *LegacyRewrite) (ok bool) {
|
|
|
|
return rw.Domain == other.Domain && rw.Answer == other.Answer
|
2021-09-17 11:37:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
// matchesQType returns true if the entry matches the question type qt.
|
|
|
|
func (rw *LegacyRewrite) matchesQType(qt uint16) (ok bool) {
|
2021-09-17 11:37:55 +00:00
|
|
|
// Add CNAMEs, since they match for all types requests.
|
2021-12-27 16:40:39 +00:00
|
|
|
if rw.Type == dns.TypeCNAME {
|
2021-09-17 11:37:55 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject types other than A and AAAA.
|
2021-12-27 16:40:39 +00:00
|
|
|
if qt != dns.TypeA && qt != dns.TypeAAAA {
|
2021-09-17 11:37:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the types match or the entry is set to allow only the other type,
|
|
|
|
// include them.
|
2021-12-27 16:40:39 +00:00
|
|
|
return rw.Type == qt || rw.IP == nil
|
2021-09-17 11:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// normalize makes sure that the a new or decoded entry is normalized with
|
|
|
|
// regards to domain name case, IP length, and so on.
|
2021-12-27 16:40:39 +00:00
|
|
|
//
|
|
|
|
// If rw is nil, it returns an errors.
|
|
|
|
func (rw *LegacyRewrite) normalize() (err error) {
|
|
|
|
if rw == nil {
|
|
|
|
return errors.Error("nil rewrite entry")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(a.garipov): Write a case-agnostic version of strings.HasSuffix and
|
|
|
|
// use it in matchDomainWildcard instead of using strings.ToLower
|
2021-09-17 11:37:55 +00:00
|
|
|
// everywhere.
|
2021-12-27 16:40:39 +00:00
|
|
|
rw.Domain = strings.ToLower(rw.Domain)
|
2021-09-17 11:37:55 +00:00
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
switch rw.Answer {
|
2021-09-17 11:37:55 +00:00
|
|
|
case "AAAA":
|
2021-12-27 16:40:39 +00:00
|
|
|
rw.IP = nil
|
|
|
|
rw.Type = dns.TypeAAAA
|
2021-09-17 11:37:55 +00:00
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
return nil
|
2021-09-17 11:37:55 +00:00
|
|
|
case "A":
|
2021-12-27 16:40:39 +00:00
|
|
|
rw.IP = nil
|
|
|
|
rw.Type = dns.TypeA
|
2021-09-17 11:37:55 +00:00
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
return nil
|
2021-09-17 11:37:55 +00:00
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
ip := net.ParseIP(rw.Answer)
|
2021-09-17 11:37:55 +00:00
|
|
|
if ip == nil {
|
2021-12-27 16:40:39 +00:00
|
|
|
rw.Type = dns.TypeCNAME
|
2021-09-17 11:37:55 +00:00
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
return nil
|
2021-09-17 11:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ip4 := ip.To4()
|
|
|
|
if ip4 != nil {
|
2021-12-27 16:40:39 +00:00
|
|
|
rw.IP = ip4
|
|
|
|
rw.Type = dns.TypeA
|
2021-09-17 11:37:55 +00:00
|
|
|
} else {
|
2021-12-27 16:40:39 +00:00
|
|
|
rw.IP = ip
|
|
|
|
rw.Type = dns.TypeAAAA
|
2021-09-17 11:37:55 +00:00
|
|
|
}
|
2021-12-27 16:40:39 +00:00
|
|
|
|
|
|
|
return nil
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
// isWildcard returns true if pat is a wildcard domain pattern.
|
|
|
|
func isWildcard(pat string) bool {
|
|
|
|
return len(pat) > 1 && pat[0] == '*' && pat[1] == '.'
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// matchDomainWildcard returns true if host matches the wildcard pattern.
|
|
|
|
func matchDomainWildcard(host, wildcard string) (ok bool) {
|
|
|
|
return isWildcard(wildcard) && strings.HasSuffix(host, wildcard[1:])
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// rewritesSorted is a slice of legacy rewrites for sorting.
|
|
|
|
//
|
|
|
|
// The sorting priority:
|
|
|
|
//
|
|
|
|
// A and AAAA > CNAME
|
|
|
|
// wildcard > exact
|
|
|
|
// lower level wildcard > higher level wildcard
|
|
|
|
//
|
2021-12-27 16:40:39 +00:00
|
|
|
type rewritesSorted []*LegacyRewrite
|
2020-01-16 09:51:35 +00:00
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// Len implements the sort.Interface interface for legacyRewritesSorted.
|
2021-12-27 16:40:39 +00:00
|
|
|
func (a rewritesSorted) Len() (l int) { return len(a) }
|
2020-01-16 09:51:35 +00:00
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// Swap implements the sort.Interface interface for legacyRewritesSorted.
|
|
|
|
func (a rewritesSorted) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
2020-01-16 09:51:35 +00:00
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// Less implements the sort.Interface interface for legacyRewritesSorted.
|
2021-12-27 16:40:39 +00:00
|
|
|
func (a rewritesSorted) Less(i, j int) (less bool) {
|
2020-01-16 09:51:35 +00:00
|
|
|
if a[i].Type == dns.TypeCNAME && a[j].Type != dns.TypeCNAME {
|
|
|
|
return true
|
2020-05-26 08:42:42 +00:00
|
|
|
} else if a[i].Type != dns.TypeCNAME && a[j].Type == dns.TypeCNAME {
|
|
|
|
return false
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:24:55 +00:00
|
|
|
if isWildcard(a[i].Domain) {
|
|
|
|
if !isWildcard(a[j].Domain) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if isWildcard(a[j].Domain) {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
// Both are wildcards.
|
2020-04-27 14:24:55 +00:00
|
|
|
return len(a[i].Domain) > len(a[j].Domain)
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
// prepareRewrites normalizes and validates all legacy DNS rewrites.
|
|
|
|
func (d *DNSFilter) prepareRewrites() (err error) {
|
|
|
|
for i, r := range d.Rewrites {
|
|
|
|
err = r.normalize()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("at index %d: %w", i, err)
|
|
|
|
}
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
2021-12-27 16:40:39 +00:00
|
|
|
|
|
|
|
return nil
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 17:14:36 +00:00
|
|
|
// findRewrites returns the list of matched rewrite entries. If rewrites are
|
|
|
|
// empty, but matched is true, the domain is found among the rewrite rules but
|
|
|
|
// not for this question type.
|
|
|
|
//
|
|
|
|
// The result priority is: CNAME, then A and AAAA; exact, then wildcard. If the
|
|
|
|
// host is matched exactly, wildcard entries aren't returned. If the host
|
|
|
|
// matched by wildcards, return the most specific for the question type.
|
|
|
|
func findRewrites(
|
2021-12-27 16:40:39 +00:00
|
|
|
entries []*LegacyRewrite,
|
2021-12-24 17:14:36 +00:00
|
|
|
host string,
|
|
|
|
qtype uint16,
|
2021-12-27 16:40:39 +00:00
|
|
|
) (rewrites []*LegacyRewrite, matched bool) {
|
2021-09-17 11:37:55 +00:00
|
|
|
for _, e := range entries {
|
|
|
|
if e.Domain != host && !matchDomainWildcard(host, e.Domain) {
|
2021-07-12 18:10:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-24 17:14:36 +00:00
|
|
|
matched = true
|
2021-09-17 11:37:55 +00:00
|
|
|
if e.matchesQType(qtype) {
|
2021-12-24 17:14:36 +00:00
|
|
|
rewrites = append(rewrites, e)
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 17:14:36 +00:00
|
|
|
if len(rewrites) == 0 {
|
|
|
|
return nil, matched
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 17:14:36 +00:00
|
|
|
sort.Sort(rewritesSorted(rewrites))
|
2020-01-16 09:51:35 +00:00
|
|
|
|
2021-12-24 17:14:36 +00:00
|
|
|
for i, r := range rewrites {
|
2021-03-29 16:35:35 +00:00
|
|
|
if isWildcard(r.Domain) {
|
2021-12-24 17:14:36 +00:00
|
|
|
// Don't use rewrites[:0], because we need to return at least one
|
|
|
|
// item here.
|
|
|
|
rewrites = rewrites[:max(1, i)]
|
2021-03-29 16:35:35 +00:00
|
|
|
|
|
|
|
break
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-27 14:24:55 +00:00
|
|
|
|
2021-12-24 17:14:36 +00:00
|
|
|
return rewrites, matched
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 16:35:35 +00:00
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
type rewriteEntryJSON struct {
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
Answer string `json:"answer"`
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) handleRewriteList(w http.ResponseWriter, r *http.Request) {
|
2019-10-09 16:51:26 +00:00
|
|
|
arr := []*rewriteEntryJSON{}
|
|
|
|
|
|
|
|
d.confLock.Lock()
|
|
|
|
for _, ent := range d.Config.Rewrites {
|
|
|
|
jsent := rewriteEntryJSON{
|
|
|
|
Domain: ent.Domain,
|
|
|
|
Answer: ent.Answer,
|
|
|
|
}
|
|
|
|
arr = append(arr, &jsent)
|
|
|
|
}
|
|
|
|
d.confLock.Unlock()
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode(arr)
|
|
|
|
if err != nil {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "json.Encode: %s", err)
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) handleRewriteAdd(w http.ResponseWriter, r *http.Request) {
|
2021-12-27 16:40:39 +00:00
|
|
|
rwJSON := rewriteEntryJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&rwJSON)
|
2019-10-09 16:51:26 +00:00
|
|
|
if err != nil {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
rw := &LegacyRewrite{
|
|
|
|
Domain: rwJSON.Domain,
|
|
|
|
Answer: rwJSON.Answer,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rw.normalize()
|
|
|
|
if err != nil {
|
|
|
|
// Shouldn't happen currently, since normalize only returns a non-nil
|
|
|
|
// error when a rewrite is nil, but be change-proof.
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "normalizing: %s", err)
|
|
|
|
|
|
|
|
return
|
2019-10-09 16:51:26 +00:00
|
|
|
}
|
2021-12-27 16:40:39 +00:00
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
d.confLock.Lock()
|
2021-12-27 16:40:39 +00:00
|
|
|
d.Config.Rewrites = append(d.Config.Rewrites, rw)
|
2019-10-09 16:51:26 +00:00
|
|
|
d.confLock.Unlock()
|
2021-12-27 16:40:39 +00:00
|
|
|
log.Debug("rewrite: added element: %s -> %s [%d]", rw.Domain, rw.Answer, len(d.Config.Rewrites))
|
2019-10-09 16:51:26 +00:00
|
|
|
|
|
|
|
d.Config.ConfigModified()
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) handleRewriteDelete(w http.ResponseWriter, r *http.Request) {
|
2019-10-09 16:51:26 +00:00
|
|
|
jsent := rewriteEntryJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&jsent)
|
|
|
|
if err != nil {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
entDel := &LegacyRewrite{
|
2019-10-09 16:51:26 +00:00
|
|
|
Domain: jsent.Domain,
|
|
|
|
Answer: jsent.Answer,
|
|
|
|
}
|
2021-12-27 16:40:39 +00:00
|
|
|
arr := []*LegacyRewrite{}
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
d.confLock.Lock()
|
|
|
|
for _, ent := range d.Config.Rewrites {
|
2021-09-17 11:37:55 +00:00
|
|
|
if ent.equal(entDel) {
|
2021-12-24 17:14:36 +00:00
|
|
|
log.Debug("rewrite: removed element: %s -> %s", ent.Domain, ent.Answer)
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-12-24 17:14:36 +00:00
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
arr = append(arr, ent)
|
|
|
|
}
|
|
|
|
d.Config.Rewrites = arr
|
|
|
|
d.confLock.Unlock()
|
|
|
|
|
|
|
|
d.Config.ConfigModified()
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) registerRewritesHandlers() {
|
2021-02-04 11:15:34 +00:00
|
|
|
d.Config.HTTPRegister(http.MethodGet, "/control/rewrite/list", d.handleRewriteList)
|
|
|
|
d.Config.HTTPRegister(http.MethodPost, "/control/rewrite/add", d.handleRewriteAdd)
|
|
|
|
d.Config.HTTPRegister(http.MethodPost, "/control/rewrite/delete", d.handleRewriteDelete)
|
2019-10-09 16:51:26 +00:00
|
|
|
}
|