2019-10-30 12:11:16 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-03-20 11:41:13 +00:00
|
|
|
"fmt"
|
2019-10-30 12:11:16 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-03-20 11:41:13 +00:00
|
|
|
"strings"
|
2019-10-30 12:11:16 +00:00
|
|
|
"sync"
|
|
|
|
|
2021-04-07 17:16:06 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghstrings"
|
2019-10-30 12:11:16 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-03-20 11:41:13 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter"
|
|
|
|
"github.com/AdguardTeam/urlfilter/filterlist"
|
2019-10-30 12:11:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type accessCtx struct {
|
|
|
|
lock sync.Mutex
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
// allowedClients are the IP addresses of clients in the allowlist.
|
|
|
|
allowedClients *aghstrings.Set
|
|
|
|
|
|
|
|
// disallowedClients are the IP addresses of clients in the blocklist.
|
|
|
|
disallowedClients *aghstrings.Set
|
2019-10-30 12:11:16 +00:00
|
|
|
|
|
|
|
allowedClientsIPNet []net.IPNet // CIDRs of whitelist clients
|
|
|
|
disallowedClientsIPNet []net.IPNet // CIDRs of clients that should be blocked
|
|
|
|
|
2020-03-20 11:41:13 +00:00
|
|
|
blockedHostsEngine *urlfilter.DNSEngine // finds hosts that should be blocked
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
func newAccessCtx(allowedClients, disallowedClients, blockedHosts []string) (a *accessCtx, err error) {
|
|
|
|
a = &accessCtx{
|
|
|
|
allowedClients: aghstrings.NewSet(),
|
|
|
|
disallowedClients: aghstrings.NewSet(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = processIPCIDRArray(a.allowedClients, &a.allowedClientsIPNet, allowedClients)
|
2019-10-30 12:11:16 +00:00
|
|
|
if err != nil {
|
2021-04-20 13:26:19 +00:00
|
|
|
return nil, fmt.Errorf("processing allowed clients: %w", err)
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
err = processIPCIDRArray(a.disallowedClients, &a.disallowedClientsIPNet, disallowedClients)
|
2019-10-30 12:11:16 +00:00
|
|
|
if err != nil {
|
2021-04-20 13:26:19 +00:00
|
|
|
return nil, fmt.Errorf("processing disallowed clients: %w", err)
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:16:06 +00:00
|
|
|
b := &strings.Builder{}
|
2020-03-20 11:41:13 +00:00
|
|
|
for _, s := range blockedHosts {
|
2021-05-11 17:33:02 +00:00
|
|
|
aghstrings.WriteToBuilder(b, strings.ToLower(s), "\n")
|
2020-03-20 11:41:13 +00:00
|
|
|
}
|
2019-10-30 12:11:16 +00:00
|
|
|
|
2020-03-20 11:41:13 +00:00
|
|
|
listArray := []filterlist.RuleList{}
|
|
|
|
list := &filterlist.StringRuleList{
|
|
|
|
ID: int(0),
|
2021-04-07 17:16:06 +00:00
|
|
|
RulesText: b.String(),
|
2020-03-20 11:41:13 +00:00
|
|
|
IgnoreCosmetic: true,
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
2020-03-20 11:41:13 +00:00
|
|
|
listArray = append(listArray, list)
|
|
|
|
rulesStorage, err := filterlist.NewRuleStorage(listArray)
|
|
|
|
if err != nil {
|
2021-04-20 13:26:19 +00:00
|
|
|
return nil, fmt.Errorf("filterlist.NewRuleStorage(): %w", err)
|
2020-03-20 11:41:13 +00:00
|
|
|
}
|
|
|
|
a.blockedHostsEngine = urlfilter.NewDNSEngine(rulesStorage)
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
return a, nil
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split array of IP or CIDR into 2 containers for fast search
|
2021-04-20 13:26:19 +00:00
|
|
|
func processIPCIDRArray(dst *aghstrings.Set, dstIPNet *[]net.IPNet, src []string) error {
|
2019-10-30 12:11:16 +00:00
|
|
|
for _, s := range src {
|
|
|
|
ip := net.ParseIP(s)
|
|
|
|
if ip != nil {
|
2021-04-20 13:26:19 +00:00
|
|
|
dst.Add(s)
|
|
|
|
|
2019-10-30 12:11:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ipnet, err := net.ParseCIDR(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-20 13:26:19 +00:00
|
|
|
|
2019-10-30 12:11:16 +00:00
|
|
|
*dstIPNet = append(*dstIPNet, *ipnet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-08 11:56:19 +00:00
|
|
|
// IsBlockedIP - return TRUE if this client should be blocked
|
2020-09-08 10:56:45 +00:00
|
|
|
// Returns the item from the "disallowedClients" list that lead to blocking IP.
|
|
|
|
// If it returns TRUE and an empty string, it means that the "allowedClients" is not empty,
|
|
|
|
// but the ip does not belong to it.
|
2021-01-20 14:27:53 +00:00
|
|
|
func (a *accessCtx) IsBlockedIP(ip net.IP) (bool, string) {
|
|
|
|
ipStr := ip.String()
|
|
|
|
|
2019-10-30 12:11:16 +00:00
|
|
|
a.lock.Lock()
|
|
|
|
defer a.lock.Unlock()
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
if a.allowedClients.Len() != 0 || len(a.allowedClientsIPNet) != 0 {
|
|
|
|
if a.allowedClients.Has(ipStr) {
|
2020-07-24 11:30:29 +00:00
|
|
|
return false, ""
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.allowedClientsIPNet) != 0 {
|
|
|
|
for _, ipnet := range a.allowedClientsIPNet {
|
2021-01-20 14:27:53 +00:00
|
|
|
if ipnet.Contains(ip) {
|
2020-07-24 11:30:29 +00:00
|
|
|
return false, ""
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 10:56:45 +00:00
|
|
|
return true, ""
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
if a.disallowedClients.Has(ipStr) {
|
2021-01-20 14:27:53 +00:00
|
|
|
return true, ipStr
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.disallowedClientsIPNet) != 0 {
|
|
|
|
for _, ipnet := range a.disallowedClientsIPNet {
|
2021-01-20 14:27:53 +00:00
|
|
|
if ipnet.Contains(ip) {
|
2020-07-24 11:30:29 +00:00
|
|
|
return true, ipnet.String()
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 11:30:29 +00:00
|
|
|
return false, ""
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 11:56:19 +00:00
|
|
|
// IsBlockedDomain - return TRUE if this domain should be blocked
|
2019-10-30 12:11:16 +00:00
|
|
|
func (a *accessCtx) IsBlockedDomain(host string) bool {
|
|
|
|
a.lock.Lock()
|
2020-06-23 11:36:26 +00:00
|
|
|
_, ok := a.blockedHostsEngine.Match(host)
|
2019-10-30 12:11:16 +00:00
|
|
|
a.lock.Unlock()
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
type accessListJSON struct {
|
|
|
|
AllowedClients []string `json:"allowed_clients"`
|
|
|
|
DisallowedClients []string `json:"disallowed_clients"`
|
|
|
|
BlockedHosts []string `json:"blocked_hosts"`
|
|
|
|
}
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
func (s *Server) accessListJSON() (j accessListJSON) {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
|
|
|
return accessListJSON{
|
|
|
|
AllowedClients: aghstrings.CloneSlice(s.conf.AllowedClients),
|
|
|
|
DisallowedClients: aghstrings.CloneSlice(s.conf.DisallowedClients),
|
|
|
|
BlockedHosts: aghstrings.CloneSlice(s.conf.BlockedHosts),
|
2019-10-30 12:11:16 +00:00
|
|
|
}
|
2021-05-26 14:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccessList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
j := s.accessListJSON()
|
2019-10-30 12:11:16 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode(j)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "json.Encode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkIPCIDRArray(src []string) error {
|
|
|
|
for _, s := range src {
|
|
|
|
ip := net.ParseIP(s)
|
|
|
|
if ip != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := net.ParseCIDR(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleAccessSet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
j := accessListJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&j)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checkIPCIDRArray(j.AllowedClients)
|
|
|
|
if err == nil {
|
|
|
|
err = checkIPCIDRArray(j.DisallowedClients)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
var a *accessCtx
|
|
|
|
a, err = newAccessCtx(j.AllowedClients, j.DisallowedClients, j.BlockedHosts)
|
2019-10-30 12:11:16 +00:00
|
|
|
if err != nil {
|
2021-04-20 13:26:19 +00:00
|
|
|
httpError(r, w, http.StatusBadRequest, "creating access ctx: %s", err)
|
|
|
|
|
2019-10-30 12:11:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
defer log.Debug("Access: updated lists: %d, %d, %d",
|
|
|
|
len(j.AllowedClients), len(j.DisallowedClients), len(j.BlockedHosts))
|
|
|
|
|
|
|
|
defer s.conf.ConfigModified()
|
|
|
|
|
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
2019-10-30 12:11:16 +00:00
|
|
|
s.conf.AllowedClients = j.AllowedClients
|
|
|
|
s.conf.DisallowedClients = j.DisallowedClients
|
|
|
|
s.conf.BlockedHosts = j.BlockedHosts
|
|
|
|
s.access = a
|
|
|
|
}
|