2019-10-30 15:11:16 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-03-20 14:41:13 +03:00
|
|
|
"fmt"
|
2019-10-30 15:11:16 +03:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-03-20 14:41:13 +03:00
|
|
|
"strings"
|
2019-10-30 15:11:16 +03:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-03-20 14:41:13 +03:00
|
|
|
"github.com/AdguardTeam/urlfilter"
|
|
|
|
"github.com/AdguardTeam/urlfilter/filterlist"
|
2019-10-30 15:11:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-03-20 14:41:13 +03:00
|
|
|
blockedHostsEngine *urlfilter.DNSEngine // finds hosts that should be blocked
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-20 14:41:13 +03:00
|
|
|
buf := strings.Builder{}
|
|
|
|
for _, s := range blockedHosts {
|
|
|
|
buf.WriteString(s)
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
2019-10-30 15:11:16 +03:00
|
|
|
|
2020-03-20 14:41:13 +03:00
|
|
|
listArray := []filterlist.RuleList{}
|
|
|
|
list := &filterlist.StringRuleList{
|
|
|
|
ID: int(0),
|
|
|
|
RulesText: buf.String(),
|
|
|
|
IgnoreCosmetic: true,
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
2020-03-20 14:41:13 +03:00
|
|
|
listArray = append(listArray, list)
|
|
|
|
rulesStorage, err := filterlist.NewRuleStorage(listArray)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("filterlist.NewRuleStorage(): %w", err)
|
2020-03-20 14:41:13 +03:00
|
|
|
}
|
|
|
|
a.blockedHostsEngine = urlfilter.NewDNSEngine(rulesStorage)
|
|
|
|
|
|
|
|
return nil
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:56:19 +03:00
|
|
|
// IsBlockedIP - return TRUE if this client should be blocked
|
2020-09-08 13:56:45 +03: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.
|
2020-07-24 14:30:29 +03:00
|
|
|
func (a *accessCtx) IsBlockedIP(ip string) (bool, string) {
|
2019-10-30 15:11:16 +03:00
|
|
|
a.lock.Lock()
|
|
|
|
defer a.lock.Unlock()
|
|
|
|
|
|
|
|
if len(a.allowedClients) != 0 || len(a.allowedClientsIPNet) != 0 {
|
|
|
|
_, ok := a.allowedClients[ip]
|
|
|
|
if ok {
|
2020-07-24 14:30:29 +03:00
|
|
|
return false, ""
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.allowedClientsIPNet) != 0 {
|
|
|
|
ipAddr := net.ParseIP(ip)
|
|
|
|
for _, ipnet := range a.allowedClientsIPNet {
|
|
|
|
if ipnet.Contains(ipAddr) {
|
2020-07-24 14:30:29 +03:00
|
|
|
return false, ""
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 13:56:45 +03:00
|
|
|
return true, ""
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := a.disallowedClients[ip]
|
|
|
|
if ok {
|
2020-07-24 14:30:29 +03:00
|
|
|
return true, ip
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.disallowedClientsIPNet) != 0 {
|
|
|
|
ipAddr := net.ParseIP(ip)
|
|
|
|
for _, ipnet := range a.disallowedClientsIPNet {
|
|
|
|
if ipnet.Contains(ipAddr) {
|
2020-07-24 14:30:29 +03:00
|
|
|
return true, ipnet.String()
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:29 +03:00
|
|
|
return false, ""
|
2019-10-30 15:11:16 +03:00
|
|
|
}
|
|
|
|
|
2019-11-08 14:56:19 +03:00
|
|
|
// IsBlockedDomain - return TRUE if this domain should be blocked
|
2019-10-30 15:11:16 +03:00
|
|
|
func (a *accessCtx) IsBlockedDomain(host string) bool {
|
|
|
|
a.lock.Lock()
|
2020-06-23 14:36:26 +03:00
|
|
|
_, ok := a.blockedHostsEngine.Match(host)
|
2019-10-30 15:11:16 +03:00
|
|
|
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) {
|
2019-12-17 13:09:03 +03:00
|
|
|
s.RLock()
|
2019-10-30 15:11:16 +03:00
|
|
|
j := accessListJSON{
|
|
|
|
AllowedClients: s.conf.AllowedClients,
|
|
|
|
DisallowedClients: s.conf.DisallowedClients,
|
|
|
|
BlockedHosts: s.conf.BlockedHosts,
|
|
|
|
}
|
2019-12-17 13:09:03 +03:00
|
|
|
s.RUnlock()
|
2019-10-30 15:11:16 +03: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
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|