2019-10-09 09:51:26 -07:00
|
|
|
// DNS Rewrites
|
|
|
|
|
|
|
|
package dnsfilter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-01-16 02:51:35 -07:00
|
|
|
"net"
|
2019-10-09 09:51:26 -07:00
|
|
|
"net/http"
|
2020-01-16 02:51:35 -07:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-10-09 09:51:26 -07:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-01-16 02:51:35 -07:00
|
|
|
"github.com/miekg/dns"
|
2019-10-09 09:51:26 -07:00
|
|
|
)
|
|
|
|
|
2020-01-16 02:51:35 -07:00
|
|
|
// RewriteEntry is a rewrite array element
|
|
|
|
type RewriteEntry struct {
|
|
|
|
Domain string `yaml:"domain"`
|
|
|
|
Answer string `yaml:"answer"` // IP address or canonical name
|
|
|
|
Type uint16 `yaml:"-"` // DNS record type: CNAME, A or AAAA
|
|
|
|
IP net.IP `yaml:"-"` // Parsed IP address (if Type is A or AAAA)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RewriteEntry) equals(b RewriteEntry) bool {
|
|
|
|
return r.Domain == b.Domain && r.Answer == b.Answer
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWildcard(host string) bool {
|
|
|
|
return len(host) >= 2 &&
|
|
|
|
host[0] == '*' && host[1] == '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return TRUE of host name matches a wildcard pattern
|
|
|
|
func matchDomainWildcard(host, wildcard string) bool {
|
|
|
|
return isWildcard(wildcard) &&
|
|
|
|
strings.HasSuffix(host, wildcard[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
type rewritesArray []RewriteEntry
|
|
|
|
|
|
|
|
func (a rewritesArray) Len() int { return len(a) }
|
|
|
|
|
|
|
|
func (a rewritesArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
|
2020-04-27 07:24:55 -07:00
|
|
|
// Priority:
|
2020-05-26 01:42:42 -07:00
|
|
|
// . CNAME < A/AAAA;
|
|
|
|
// . exact < wildcard;
|
|
|
|
// . higher level wildcard < lower level wildcard
|
2020-01-16 02:51:35 -07:00
|
|
|
func (a rewritesArray) Less(i, j int) bool {
|
|
|
|
if a[i].Type == dns.TypeCNAME && a[j].Type != dns.TypeCNAME {
|
|
|
|
return true
|
2020-05-26 01:42:42 -07:00
|
|
|
} else if a[i].Type != dns.TypeCNAME && a[j].Type == dns.TypeCNAME {
|
|
|
|
return false
|
2020-01-16 02:51:35 -07:00
|
|
|
}
|
|
|
|
|
2020-04-27 07:24:55 -07:00
|
|
|
if isWildcard(a[i].Domain) {
|
|
|
|
if !isWildcard(a[j].Domain) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if isWildcard(a[j].Domain) {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-16 02:51:35 -07:00
|
|
|
}
|
|
|
|
|
2020-04-27 07:24:55 -07:00
|
|
|
// both are wildcards
|
|
|
|
return len(a[i].Domain) > len(a[j].Domain)
|
2020-01-16 02:51:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare entry for use
|
|
|
|
func (r *RewriteEntry) prepare() {
|
2020-06-03 02:04:23 -07:00
|
|
|
if r.Answer == "AAAA" {
|
|
|
|
r.IP = nil
|
|
|
|
r.Type = dns.TypeAAAA
|
|
|
|
return
|
|
|
|
} else if r.Answer == "A" {
|
|
|
|
r.IP = nil
|
|
|
|
r.Type = dns.TypeA
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:51:35 -07:00
|
|
|
ip := net.ParseIP(r.Answer)
|
|
|
|
if ip == nil {
|
|
|
|
r.Type = dns.TypeCNAME
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.IP = ip
|
|
|
|
r.Type = dns.TypeAAAA
|
|
|
|
|
|
|
|
ip4 := ip.To4()
|
|
|
|
if ip4 != nil {
|
|
|
|
r.IP = ip4
|
|
|
|
r.Type = dns.TypeA
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 03:32:46 -07:00
|
|
|
func (d *DNSFilter) prepareRewrites() {
|
2020-01-16 02:51:35 -07:00
|
|
|
for i := range d.Rewrites {
|
|
|
|
d.Rewrites[i].prepare()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the list of matched rewrite entries.
|
|
|
|
// Priority: CNAME, A/AAAA; exact, wildcard.
|
|
|
|
// If matched exactly, don't return wildcard entries.
|
2020-04-27 07:24:55 -07:00
|
|
|
// If matched by several wildcards, select the more specific one
|
2020-01-16 02:51:35 -07:00
|
|
|
func findRewrites(a []RewriteEntry, host string) []RewriteEntry {
|
|
|
|
rr := rewritesArray{}
|
|
|
|
for _, r := range a {
|
|
|
|
if r.Domain != host {
|
|
|
|
if !matchDomainWildcard(host, r.Domain) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rr = append(rr, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(rr)
|
|
|
|
|
|
|
|
isWC := isWildcard(rr[0].Domain)
|
|
|
|
if !isWC {
|
|
|
|
for i, r := range rr {
|
|
|
|
if isWildcard(r.Domain) {
|
|
|
|
rr = rr[:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-04-27 07:24:55 -07:00
|
|
|
} else {
|
|
|
|
rr = rr[:1]
|
2020-01-16 02:51:35 -07:00
|
|
|
}
|
2020-04-27 07:24:55 -07:00
|
|
|
|
2020-01-16 02:51:35 -07:00
|
|
|
return rr
|
|
|
|
}
|
|
|
|
|
|
|
|
func rewriteArrayDup(a []RewriteEntry) []RewriteEntry {
|
|
|
|
a2 := make([]RewriteEntry, len(a))
|
|
|
|
copy(a2, a)
|
|
|
|
return a2
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:51:26 -07:00
|
|
|
type rewriteEntryJSON struct {
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
Answer string `json:"answer"`
|
|
|
|
}
|
|
|
|
|
2020-12-17 03:32:46 -07:00
|
|
|
func (d *DNSFilter) handleRewriteList(w http.ResponseWriter, r *http.Request) {
|
2019-10-09 09:51:26 -07: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 03:32:46 -07:00
|
|
|
func (d *DNSFilter) handleRewriteAdd(w http.ResponseWriter, r *http.Request) {
|
2019-10-09 09:51:26 -07: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,
|
|
|
|
}
|
2020-01-16 02:51:35 -07:00
|
|
|
ent.prepare()
|
2019-10-09 09:51:26 -07: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 03:32:46 -07:00
|
|
|
func (d *DNSFilter) handleRewriteDelete(w http.ResponseWriter, r *http.Request) {
|
2019-10-09 09:51:26 -07: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 {
|
2020-01-16 02:51:35 -07:00
|
|
|
if ent.equals(entDel) {
|
2019-10-09 09:51:26 -07: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 03:32:46 -07:00
|
|
|
func (d *DNSFilter) registerRewritesHandlers() {
|
2019-10-09 09:51:26 -07:00
|
|
|
d.Config.HTTPRegister("GET", "/control/rewrite/list", d.handleRewriteList)
|
|
|
|
d.Config.HTTPRegister("POST", "/control/rewrite/add", d.handleRewriteAdd)
|
|
|
|
d.Config.HTTPRegister("POST", "/control/rewrite/delete", d.handleRewriteDelete)
|
|
|
|
}
|