mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-27 09:18:58 +03:00
a5b9d1d74c
Close #1469
Squashed commit of the following:
commit b07f408550a526726c8e38886ce0fcc1f4c3d93d
Merge: 56aa0dfc c5a39b94
Author: Simon Zolin <s.zolin@adguard.com>
Date: Fri Mar 20 14:32:21 2020 +0300
Merge remote-tracking branch 'origin/master' into 1469-wildcards
commit 56aa0dfcca8265fed35e9b92f85dc54de947cc11
Author: Simon Zolin <s.zolin@adguard.com>
Date: Thu Mar 19 12:02:17 2020 +0300
update text
commit 468bcd9ea6e57eedd96bbb4b8414d86a60abcdc3
Author: Simon Zolin <s.zolin@adguard.com>
Date: Thu Mar 19 11:51:01 2020 +0300
* access: use urlfilter for blocked hosts processing
commit 1b157d39dfb32dd74d527274e5466b1642cd6916
Author: Simon Zolin <s.zolin@adguard.com>
Date: Wed Mar 18 13:13:06 2020 +0300
minor
commit 34811eb08ab690104d29710b0e1bc9fb003a4cd4
Author: Ildar Kamalov <i.kamalov@adguard.com>
Date: Fri Mar 13 19:05:12 2020 +0300
* client: change field description
commit fe0acec1bbd0b810f00481412efcc9776022b694
Author: Simon Zolin <s.zolin@adguard.com>
Date: Fri Mar 13 16:51:17 2020 +0300
+ DNS: blocked hosts: support wildcards
203 lines
4.5 KiB
Go
203 lines
4.5 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/urlfilter"
|
|
"github.com/AdguardTeam/urlfilter/filterlist"
|
|
)
|
|
|
|
type accessCtx struct {
|
|
lock sync.Mutex
|
|
|
|
allowedClients map[string]bool // IP addresses of whitelist clients
|
|
disallowedClients map[string]bool // IP addresses of clients that should be blocked
|
|
|
|
allowedClientsIPNet []net.IPNet // CIDRs of whitelist clients
|
|
disallowedClientsIPNet []net.IPNet // CIDRs of clients that should be blocked
|
|
|
|
blockedHostsEngine *urlfilter.DNSEngine // finds hosts that should be blocked
|
|
}
|
|
|
|
func (a *accessCtx) Init(allowedClients, disallowedClients, blockedHosts []string) error {
|
|
err := processIPCIDRArray(&a.allowedClients, &a.allowedClientsIPNet, allowedClients)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = processIPCIDRArray(&a.disallowedClients, &a.disallowedClientsIPNet, disallowedClients)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
buf := strings.Builder{}
|
|
for _, s := range blockedHosts {
|
|
buf.WriteString(s)
|
|
buf.WriteString("\n")
|
|
}
|
|
|
|
listArray := []filterlist.RuleList{}
|
|
list := &filterlist.StringRuleList{
|
|
ID: int(0),
|
|
RulesText: buf.String(),
|
|
IgnoreCosmetic: true,
|
|
}
|
|
listArray = append(listArray, list)
|
|
rulesStorage, err := filterlist.NewRuleStorage(listArray)
|
|
if err != nil {
|
|
return fmt.Errorf("filterlist.NewRuleStorage(): %s", err)
|
|
}
|
|
a.blockedHostsEngine = urlfilter.NewDNSEngine(rulesStorage)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Split array of IP or CIDR into 2 containers for fast search
|
|
func processIPCIDRArray(dst *map[string]bool, dstIPNet *[]net.IPNet, src []string) error {
|
|
*dst = make(map[string]bool)
|
|
|
|
for _, s := range src {
|
|
ip := net.ParseIP(s)
|
|
if ip != nil {
|
|
(*dst)[s] = true
|
|
continue
|
|
}
|
|
|
|
_, ipnet, err := net.ParseCIDR(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*dstIPNet = append(*dstIPNet, *ipnet)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsBlockedIP - return TRUE if this client should be blocked
|
|
func (a *accessCtx) IsBlockedIP(ip string) bool {
|
|
a.lock.Lock()
|
|
defer a.lock.Unlock()
|
|
|
|
if len(a.allowedClients) != 0 || len(a.allowedClientsIPNet) != 0 {
|
|
_, ok := a.allowedClients[ip]
|
|
if ok {
|
|
return false
|
|
}
|
|
|
|
if len(a.allowedClientsIPNet) != 0 {
|
|
ipAddr := net.ParseIP(ip)
|
|
for _, ipnet := range a.allowedClientsIPNet {
|
|
if ipnet.Contains(ipAddr) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
_, ok := a.disallowedClients[ip]
|
|
if ok {
|
|
return true
|
|
}
|
|
|
|
if len(a.disallowedClientsIPNet) != 0 {
|
|
ipAddr := net.ParseIP(ip)
|
|
for _, ipnet := range a.disallowedClientsIPNet {
|
|
if ipnet.Contains(ipAddr) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// IsBlockedDomain - return TRUE if this domain should be blocked
|
|
func (a *accessCtx) IsBlockedDomain(host string) bool {
|
|
a.lock.Lock()
|
|
_, ok := a.blockedHostsEngine.Match(host, nil)
|
|
a.lock.Unlock()
|
|
return ok
|
|
}
|
|
|
|
type accessListJSON struct {
|
|
AllowedClients []string `json:"allowed_clients"`
|
|
DisallowedClients []string `json:"disallowed_clients"`
|
|
BlockedHosts []string `json:"blocked_hosts"`
|
|
}
|
|
|
|
func (s *Server) handleAccessList(w http.ResponseWriter, r *http.Request) {
|
|
s.RLock()
|
|
j := accessListJSON{
|
|
AllowedClients: s.conf.AllowedClients,
|
|
DisallowedClients: s.conf.DisallowedClients,
|
|
BlockedHosts: s.conf.BlockedHosts,
|
|
}
|
|
s.RUnlock()
|
|
|
|
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
|
|
}
|
|
|
|
a := &accessCtx{}
|
|
err = a.Init(j.AllowedClients, j.DisallowedClients, j.BlockedHosts)
|
|
if err != nil {
|
|
httpError(r, w, http.StatusBadRequest, "access.Init: %s", err)
|
|
return
|
|
}
|
|
|
|
s.Lock()
|
|
s.conf.AllowedClients = j.AllowedClients
|
|
s.conf.DisallowedClients = j.DisallowedClients
|
|
s.conf.BlockedHosts = j.BlockedHosts
|
|
s.access = a
|
|
s.Unlock()
|
|
s.conf.ConfigModified()
|
|
|
|
log.Debug("Access: updated lists: %d, %d, %d",
|
|
len(j.AllowedClients), len(j.DisallowedClients), len(j.BlockedHosts))
|
|
}
|