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"
|
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
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-01-16 09:51:35 +00:00
|
|
|
"github.com/miekg/dns"
|
2019-10-09 16:51:26 +00:00
|
|
|
)
|
|
|
|
|
2020-01-16 09:51:35 +00:00
|
|
|
// RewriteEntry is a rewrite array element
|
|
|
|
type RewriteEntry struct {
|
2021-07-12 18:10:39 +00:00
|
|
|
// Domain is the domain for which this rewrite should work.
|
2020-01-16 09:51:35 +00:00
|
|
|
Domain string `yaml:"domain"`
|
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"`
|
|
|
|
// IP is the IP address that should be used in the response if Type is
|
|
|
|
// A or AAAA.
|
|
|
|
IP net.IP `yaml:"-"`
|
|
|
|
// Type is the DNS record type: A, AAAA, or CNAME.
|
|
|
|
Type uint16 `yaml:"-"`
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 11:37:55 +00:00
|
|
|
// equal returns true if the entry is considered equal to the other.
|
|
|
|
func (e *RewriteEntry) equal(other RewriteEntry) (ok bool) {
|
|
|
|
return e.Domain == other.Domain && e.Answer == other.Answer
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchesQType returns true if the entry matched qtype.
|
|
|
|
func (e *RewriteEntry) matchesQType(qtype uint16) (ok bool) {
|
|
|
|
// Add CNAMEs, since they match for all types requests.
|
|
|
|
if e.Type == dns.TypeCNAME {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject types other than A and AAAA.
|
|
|
|
if qtype != dns.TypeA && qtype != dns.TypeAAAA {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the types match or the entry is set to allow only the other type,
|
|
|
|
// include them.
|
|
|
|
return e.Type == qtype || e.IP == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize makes sure that the a new or decoded entry is normalized with
|
|
|
|
// regards to domain name case, IP length, and so on.
|
|
|
|
func (e *RewriteEntry) normalize() {
|
|
|
|
// TODO(a.garipov): Write a case-agnostic version of strings.HasSuffix
|
|
|
|
// and use it in matchDomainWildcard instead of using strings.ToLower
|
|
|
|
// everywhere.
|
|
|
|
e.Domain = strings.ToLower(e.Domain)
|
|
|
|
|
|
|
|
switch e.Answer {
|
|
|
|
case "AAAA":
|
|
|
|
e.IP = nil
|
|
|
|
e.Type = dns.TypeAAAA
|
|
|
|
|
|
|
|
return
|
|
|
|
case "A":
|
|
|
|
e.IP = nil
|
|
|
|
e.Type = dns.TypeA
|
|
|
|
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.ParseIP(e.Answer)
|
|
|
|
if ip == nil {
|
|
|
|
e.Type = dns.TypeCNAME
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ip4 := ip.To4()
|
|
|
|
if ip4 != nil {
|
|
|
|
e.IP = ip4
|
|
|
|
e.Type = dns.TypeA
|
|
|
|
} else {
|
|
|
|
e.IP = ip
|
|
|
|
e.Type = dns.TypeAAAA
|
|
|
|
}
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isWildcard(host string) bool {
|
2021-07-12 18:10:39 +00:00
|
|
|
return len(host) > 1 && host[0] == '*' && host[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
|
|
|
|
//
|
|
|
|
type rewritesSorted []RewriteEntry
|
2020-01-16 09:51:35 +00:00
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// Len implements the sort.Interface interface for legacyRewritesSorted.
|
|
|
|
func (a rewritesSorted) Len() 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.
|
|
|
|
func (a rewritesSorted) Less(i, j int) 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
|
|
|
}
|
|
|
|
|
2020-04-27 14:24:55 +00:00
|
|
|
// both are wildcards
|
|
|
|
return len(a[i].Domain) > len(a[j].Domain)
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) prepareRewrites() {
|
2020-01-16 09:51:35 +00:00
|
|
|
for i := range d.Rewrites {
|
2021-09-17 11:37:55 +00:00
|
|
|
d.Rewrites[i].normalize()
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
// findRewrites returns the list of matched rewrite entries. The 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.
|
2021-09-17 11:37:55 +00:00
|
|
|
func findRewrites(entries []RewriteEntry, host string, qtype uint16) (matched []RewriteEntry) {
|
2021-07-12 18:10:39 +00:00
|
|
|
rr := rewritesSorted{}
|
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-09-17 11:37:55 +00:00
|
|
|
if e.matchesQType(qtype) {
|
|
|
|
rr = append(rr, e)
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(rr)
|
|
|
|
|
2021-03-29 16:35:35 +00:00
|
|
|
for i, r := range rr {
|
|
|
|
if isWildcard(r.Domain) {
|
|
|
|
// Don't use rr[:0], because we need to return at least
|
|
|
|
// one item here.
|
|
|
|
rr = rr[:max(1, i)]
|
|
|
|
|
|
|
|
break
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-27 14:24:55 +00:00
|
|
|
|
2020-01-16 09:51:35 +00:00
|
|
|
return rr
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "json.Encode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) handleRewriteAdd(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 {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ent := RewriteEntry{
|
|
|
|
Domain: jsent.Domain,
|
|
|
|
Answer: jsent.Answer,
|
|
|
|
}
|
2021-09-17 11:37:55 +00:00
|
|
|
ent.normalize()
|
2019-10-09 16:51:26 +00:00
|
|
|
d.confLock.Lock()
|
|
|
|
d.Config.Rewrites = append(d.Config.Rewrites, ent)
|
|
|
|
d.confLock.Unlock()
|
|
|
|
log.Debug("Rewrites: added element: %s -> %s [%d]",
|
|
|
|
ent.Domain, ent.Answer, len(d.Config.Rewrites))
|
|
|
|
|
|
|
|
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 {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
entDel := RewriteEntry{
|
|
|
|
Domain: jsent.Domain,
|
|
|
|
Answer: jsent.Answer,
|
|
|
|
}
|
|
|
|
arr := []RewriteEntry{}
|
|
|
|
d.confLock.Lock()
|
|
|
|
for _, ent := range d.Config.Rewrites {
|
2021-09-17 11:37:55 +00:00
|
|
|
if ent.equal(entDel) {
|
2019-10-09 16:51:26 +00:00
|
|
|
log.Debug("Rewrites: removed element: %s -> %s", ent.Domain, ent.Answer)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|