2021-05-21 13:15:47 +00:00
|
|
|
// Package filtering implements a DNS request and response filter.
|
|
|
|
package filtering
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-29 13:09:31 +00:00
|
|
|
"context"
|
2018-08-30 14:25:33 +00:00
|
|
|
"fmt"
|
2021-11-26 15:25:43 +00:00
|
|
|
"io/fs"
|
2018-10-29 12:46:58 +00:00
|
|
|
"net"
|
2018-08-30 14:25:33 +00:00
|
|
|
"net/http"
|
2019-07-05 14:35:40 +00:00
|
|
|
"os"
|
2019-10-22 11:58:20 +00:00
|
|
|
"runtime"
|
2020-05-12 21:46:35 +00:00
|
|
|
"runtime/debug"
|
2018-08-30 14:25:33 +00:00
|
|
|
"strings"
|
2019-10-09 16:51:26 +00:00
|
|
|
"sync"
|
2021-05-24 11:48:42 +00:00
|
|
|
"sync/atomic"
|
2019-06-18 13:18:13 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2019-04-18 11:31:13 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2019-08-22 12:09:43 +00:00
|
|
|
"github.com/AdguardTeam/golibs/cache"
|
2021-11-26 15:25:43 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-07-29 14:40:31 +00:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2019-05-15 13:46:11 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter"
|
2019-11-27 12:11:46 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter/filterlist"
|
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
2019-05-22 09:38:17 +00:00
|
|
|
"github.com/miekg/dns"
|
2018-08-30 14:25:33 +00:00
|
|
|
)
|
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
// The IDs of built-in filter lists.
|
|
|
|
//
|
|
|
|
// Keep in sync with client/src/helpers/contants.js.
|
|
|
|
const (
|
|
|
|
CustomListID = -iota
|
|
|
|
SysHostsListID
|
|
|
|
BlockedSvcsListID
|
|
|
|
ParentalListID
|
|
|
|
SafeBrowsingListID
|
|
|
|
SafeSearchListID
|
|
|
|
)
|
|
|
|
|
2019-07-23 09:21:37 +00:00
|
|
|
// ServiceEntry - blocked service array element
|
|
|
|
type ServiceEntry struct {
|
|
|
|
Name string
|
2019-11-27 12:11:46 +00:00
|
|
|
Rules []*rules.NetworkRule
|
2019-07-23 09:21:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 13:15:47 +00:00
|
|
|
// Settings are custom filtering settings for a client.
|
|
|
|
type Settings struct {
|
2020-06-23 11:36:26 +00:00
|
|
|
ClientName string
|
2021-01-20 14:27:53 +00:00
|
|
|
ClientIP net.IP
|
2020-06-23 11:36:26 +00:00
|
|
|
ClientTags []string
|
|
|
|
|
|
|
|
ServicesRules []ServiceEntry
|
2021-03-25 17:30:30 +00:00
|
|
|
|
2021-10-20 16:52:13 +00:00
|
|
|
ProtectionEnabled bool
|
2021-03-25 17:30:30 +00:00
|
|
|
FilteringEnabled bool
|
|
|
|
SafeSearchEnabled bool
|
|
|
|
SafeBrowsingEnabled bool
|
|
|
|
ParentalEnabled bool
|
2019-05-28 11:14:12 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 17:35:13 +00:00
|
|
|
// Resolver is the interface for net.Resolver to simplify testing.
|
|
|
|
type Resolver interface {
|
2021-03-11 09:17:54 +00:00
|
|
|
LookupIP(ctx context.Context, network, host string) (ips []net.IP, err error)
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 10:32:51 +00:00
|
|
|
// Config allows you to configure DNS filtering with New() or just change variables directly.
|
|
|
|
type Config struct {
|
2021-05-24 11:48:42 +00:00
|
|
|
// enabled is used to be returned within Settings.
|
|
|
|
//
|
|
|
|
// It is of type uint32 to be accessed by atomic.
|
|
|
|
enabled uint32
|
|
|
|
|
2021-03-23 09:32:07 +00:00
|
|
|
ParentalEnabled bool `yaml:"parental_enabled"`
|
|
|
|
SafeSearchEnabled bool `yaml:"safesearch_enabled"`
|
|
|
|
SafeBrowsingEnabled bool `yaml:"safebrowsing_enabled"`
|
2019-05-28 11:14:12 +00:00
|
|
|
|
2019-08-22 12:09:43 +00:00
|
|
|
SafeBrowsingCacheSize uint `yaml:"safebrowsing_cache_size"` // (in bytes)
|
|
|
|
SafeSearchCacheSize uint `yaml:"safesearch_cache_size"` // (in bytes)
|
|
|
|
ParentalCacheSize uint `yaml:"parental_cache_size"` // (in bytes)
|
|
|
|
CacheTime uint `yaml:"cache_time"` // Element's TTL (in minutes)
|
|
|
|
|
2019-07-29 08:37:16 +00:00
|
|
|
Rewrites []RewriteEntry `yaml:"rewrites"`
|
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
// Names of services to block (globally).
|
|
|
|
// Per-client settings can override this configuration.
|
|
|
|
BlockedServices []string `yaml:"blocked_services"`
|
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
// EtcHosts is a container of IP-hostname pairs taken from the operating
|
|
|
|
// system configuration files (e.g. /etc/hosts).
|
2021-10-14 16:39:21 +00:00
|
|
|
EtcHosts *aghnet.HostsContainer `yaml:"-"`
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
// Called when the configuration is changed by HTTP request
|
|
|
|
ConfigModified func() `yaml:"-"`
|
2018-11-30 10:32:51 +00:00
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
// Register an HTTP handler
|
|
|
|
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `yaml:"-"`
|
2021-02-04 17:35:13 +00:00
|
|
|
|
|
|
|
// CustomResolver is the resolver used by DNSFilter.
|
2021-04-07 17:16:06 +00:00
|
|
|
CustomResolver Resolver `yaml:"-"`
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 13:50:56 +00:00
|
|
|
// LookupStats store stats collected during safebrowsing or parental checks
|
2018-08-30 14:25:33 +00:00
|
|
|
type LookupStats struct {
|
|
|
|
Requests uint64 // number of HTTP requests that were sent
|
|
|
|
CacheHits uint64 // number of lookups that didn't need HTTP requests
|
|
|
|
Pending int64 // number of currently pending HTTP requests
|
|
|
|
PendingMax int64 // maximum number of pending HTTP requests
|
|
|
|
}
|
|
|
|
|
2019-02-22 13:34:36 +00:00
|
|
|
// Stats store LookupStats for safebrowsing, parental and safesearch
|
2018-08-30 14:25:33 +00:00
|
|
|
type Stats struct {
|
|
|
|
Safebrowsing LookupStats
|
|
|
|
Parental LookupStats
|
2019-02-22 13:34:36 +00:00
|
|
|
Safesearch LookupStats
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
// Parameters to pass to filters-initializer goroutine
|
|
|
|
type filtersInitializerParams struct {
|
2020-02-26 16:58:25 +00:00
|
|
|
allowFilters []Filter
|
|
|
|
blockFilters []Filter
|
2019-10-09 16:51:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
type hostChecker struct {
|
2021-05-21 13:15:47 +00:00
|
|
|
check func(host string, qtype uint16, setts *Settings) (res Result, err error)
|
2021-03-25 17:30:30 +00:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// DNSFilter matches hostnames and DNS requests against filtering rules.
|
|
|
|
type DNSFilter struct {
|
2020-02-26 16:58:25 +00:00
|
|
|
rulesStorage *filterlist.RuleStorage
|
|
|
|
filteringEngine *urlfilter.DNSEngine
|
2020-12-21 14:48:07 +00:00
|
|
|
rulesStorageAllow *filterlist.RuleStorage
|
|
|
|
filteringEngineAllow *urlfilter.DNSEngine
|
2020-02-26 16:58:25 +00:00
|
|
|
engineLock sync.RWMutex
|
2019-05-15 13:46:11 +00:00
|
|
|
|
2019-10-16 09:57:49 +00:00
|
|
|
parentalServer string // access via methods
|
|
|
|
safeBrowsingServer string // access via methods
|
|
|
|
parentalUpstream upstream.Upstream
|
|
|
|
safeBrowsingUpstream upstream.Upstream
|
2019-10-09 16:51:26 +00:00
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
safebrowsingCache cache.Cache
|
|
|
|
parentalCache cache.Cache
|
|
|
|
safeSearchCache cache.Cache
|
|
|
|
|
2021-05-24 11:48:42 +00:00
|
|
|
Config // for direct access by library users, even a = assignment
|
|
|
|
// confLock protects Config.
|
2019-10-09 16:51:26 +00:00
|
|
|
confLock sync.RWMutex
|
|
|
|
|
|
|
|
// Channel for passing data to filters-initializer goroutine
|
|
|
|
filtersInitializerChan chan filtersInitializerParams
|
|
|
|
filtersInitializerLock sync.Mutex
|
2021-01-29 13:09:31 +00:00
|
|
|
|
|
|
|
// resolver only looks up the IP address of the host while safe search.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Use upstream that configured in dnsforward instead.
|
|
|
|
resolver Resolver
|
2021-03-25 17:30:30 +00:00
|
|
|
|
|
|
|
hostCheckers []hostChecker
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 17:11:01 +00:00
|
|
|
// Filter represents a filter list
|
2018-11-30 10:24:42 +00:00
|
|
|
type Filter struct {
|
2019-09-04 11:12:00 +00:00
|
|
|
ID int64 // auto-assigned when filter is added (see nextFilterID)
|
|
|
|
Data []byte `yaml:"-"` // List of rules divided by '\n'
|
|
|
|
FilePath string `yaml:"-"` // Path to a filtering rules file
|
2018-11-30 10:24:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 13:50:56 +00:00
|
|
|
// Reason holds an enum detailing why it was filtered or not filtered
|
2018-08-30 14:25:33 +00:00
|
|
|
type Reason int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// reasons for not filtering
|
2019-01-24 17:11:01 +00:00
|
|
|
|
|
|
|
// NotFilteredNotFound - host was not find in any checks, default value for result
|
|
|
|
NotFilteredNotFound Reason = iota
|
2020-12-21 14:48:07 +00:00
|
|
|
// NotFilteredAllowList - the host is explicitly allowed
|
|
|
|
NotFilteredAllowList
|
2020-12-17 10:32:46 +00:00
|
|
|
// NotFilteredError is returned when there was an error during
|
2020-12-14 17:12:57 +00:00
|
|
|
// checking. Reserved, currently unused.
|
|
|
|
NotFilteredError
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
|
|
// reasons for filtering
|
2019-01-24 17:11:01 +00:00
|
|
|
|
2020-12-21 14:48:07 +00:00
|
|
|
// FilteredBlockList - the host was matched to be advertising host
|
|
|
|
FilteredBlockList
|
2019-01-24 17:11:01 +00:00
|
|
|
// FilteredSafeBrowsing - the host was matched to be malicious/phishing
|
|
|
|
FilteredSafeBrowsing
|
|
|
|
// FilteredParental - the host was matched to be outside of parental control settings
|
|
|
|
FilteredParental
|
|
|
|
// FilteredInvalid - the request was invalid and was not processed
|
|
|
|
FilteredInvalid
|
|
|
|
// FilteredSafeSearch - the host was replaced with safesearch variant
|
|
|
|
FilteredSafeSearch
|
2019-07-23 09:21:37 +00:00
|
|
|
// FilteredBlockedService - the host is blocked by "blocked services" settings
|
|
|
|
FilteredBlockedService
|
2019-07-29 08:37:16 +00:00
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
// Rewritten is returned when there was a rewrite by a legacy DNS rewrite
|
|
|
|
// rule.
|
2020-12-28 15:41:50 +00:00
|
|
|
Rewritten
|
|
|
|
|
|
|
|
// RewrittenAutoHosts is returned when there was a rewrite by autohosts
|
|
|
|
// rules (/etc/hosts and so on).
|
|
|
|
RewrittenAutoHosts
|
|
|
|
|
|
|
|
// RewrittenRule is returned when a $dnsrewrite filter rule was applied.
|
|
|
|
//
|
2021-10-14 16:39:21 +00:00
|
|
|
// TODO(a.garipov): Remove Rewritten and RewrittenAutoHosts by merging their
|
|
|
|
// functionality into RewrittenRule.
|
2020-12-28 15:41:50 +00:00
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/2499.
|
|
|
|
RewrittenRule
|
2018-08-30 14:25:33 +00:00
|
|
|
)
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// TODO(a.garipov): Resync with actual code names or replace completely
|
|
|
|
// in HTTP API v1.
|
2019-10-09 16:51:26 +00:00
|
|
|
var reasonNames = []string{
|
2020-12-17 10:32:46 +00:00
|
|
|
NotFilteredNotFound: "NotFilteredNotFound",
|
2020-12-21 14:48:07 +00:00
|
|
|
NotFilteredAllowList: "NotFilteredWhiteList",
|
2020-12-17 10:32:46 +00:00
|
|
|
NotFilteredError: "NotFilteredError",
|
2019-10-09 16:51:26 +00:00
|
|
|
|
2020-12-21 14:48:07 +00:00
|
|
|
FilteredBlockList: "FilteredBlackList",
|
2020-12-17 10:32:46 +00:00
|
|
|
FilteredSafeBrowsing: "FilteredSafeBrowsing",
|
|
|
|
FilteredParental: "FilteredParental",
|
|
|
|
FilteredInvalid: "FilteredInvalid",
|
|
|
|
FilteredSafeSearch: "FilteredSafeSearch",
|
|
|
|
FilteredBlockedService: "FilteredBlockedService",
|
2019-10-09 16:51:26 +00:00
|
|
|
|
2020-12-28 15:41:50 +00:00
|
|
|
Rewritten: "Rewrite",
|
|
|
|
RewrittenAutoHosts: "RewriteEtcHosts",
|
|
|
|
RewrittenRule: "RewriteRule",
|
2019-10-09 16:51:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 21:55:32 +00:00
|
|
|
func (r Reason) String() string {
|
2020-12-21 14:48:07 +00:00
|
|
|
if r < 0 || int(r) >= len(reasonNames) {
|
2019-10-09 16:51:26 +00:00
|
|
|
return ""
|
|
|
|
}
|
2020-12-21 14:48:07 +00:00
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
return reasonNames[r]
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
// In returns true if reasons include r.
|
2021-10-20 16:52:13 +00:00
|
|
|
func (r Reason) In(reasons ...Reason) (ok bool) {
|
2020-11-20 14:32:41 +00:00
|
|
|
for _, reason := range reasons {
|
|
|
|
if r == reason {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-10-20 16:52:13 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:48:42 +00:00
|
|
|
// SetEnabled sets the status of the *DNSFilter.
|
|
|
|
func (d *DNSFilter) SetEnabled(enabled bool) {
|
|
|
|
var i int32
|
|
|
|
if enabled {
|
|
|
|
i = 1
|
|
|
|
}
|
|
|
|
atomic.StoreUint32(&d.enabled, uint32(i))
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
// GetConfig - get configuration
|
2021-05-24 11:48:42 +00:00
|
|
|
func (d *DNSFilter) GetConfig() (s Settings) {
|
|
|
|
d.confLock.RLock()
|
|
|
|
defer d.confLock.RUnlock()
|
|
|
|
|
|
|
|
return Settings{
|
2021-10-20 16:52:13 +00:00
|
|
|
FilteringEnabled: atomic.LoadUint32(&d.Config.enabled) != 0,
|
2021-05-24 11:48:42 +00:00
|
|
|
SafeSearchEnabled: d.Config.SafeSearchEnabled,
|
|
|
|
SafeBrowsingEnabled: d.Config.SafeBrowsingEnabled,
|
|
|
|
ParentalEnabled: d.Config.ParentalEnabled,
|
|
|
|
}
|
2019-10-09 16:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDiskConfig - write configuration
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) WriteDiskConfig(c *Config) {
|
2020-01-16 09:51:35 +00:00
|
|
|
d.confLock.Lock()
|
2021-09-17 11:37:55 +00:00
|
|
|
defer d.confLock.Unlock()
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
*c = d.Config
|
2021-09-17 11:37:55 +00:00
|
|
|
c.Rewrites = cloneRewrites(c.Rewrites)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneRewrites(entries []RewriteEntry) (clone []RewriteEntry) {
|
|
|
|
return append([]RewriteEntry(nil), entries...)
|
2019-10-09 16:51:26 +00:00
|
|
|
}
|
2019-07-29 08:37:16 +00:00
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
// SetFilters - set new filters (synchronously or asynchronously)
|
|
|
|
// When filters are set asynchronously, the old filters continue working until the new filters are ready.
|
|
|
|
// In this case the caller must ensure that the old filter files are intact.
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) SetFilters(blockFilters, allowFilters []Filter, async bool) error {
|
2019-10-09 16:51:26 +00:00
|
|
|
if async {
|
|
|
|
params := filtersInitializerParams{
|
2020-02-26 16:58:25 +00:00
|
|
|
allowFilters: allowFilters,
|
|
|
|
blockFilters: blockFilters,
|
2019-10-09 16:51:26 +00:00
|
|
|
}
|
2019-07-29 08:37:16 +00:00
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
d.filtersInitializerLock.Lock() // prevent multiple writers from adding more than 1 task
|
|
|
|
// remove all pending tasks
|
|
|
|
stop := false
|
|
|
|
for !stop {
|
|
|
|
select {
|
|
|
|
case <-d.filtersInitializerChan:
|
|
|
|
//
|
|
|
|
default:
|
|
|
|
stop = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.filtersInitializerChan <- params
|
|
|
|
d.filtersInitializerLock.Unlock()
|
|
|
|
return nil
|
2019-07-23 08:43:30 +00:00
|
|
|
}
|
2019-10-09 16:51:26 +00:00
|
|
|
|
2020-02-26 16:58:25 +00:00
|
|
|
err := d.initFiltering(allowFilters, blockFilters)
|
2019-10-09 16:51:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't initialize filtering subsystem: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts initializing new filters by signal from channel
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) filtersInitializer() {
|
2019-10-09 16:51:26 +00:00
|
|
|
for {
|
|
|
|
params := <-d.filtersInitializerChan
|
2020-02-26 16:58:25 +00:00
|
|
|
err := d.initFiltering(params.allowFilters, params.blockFilters)
|
2019-10-09 16:51:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't initialize filtering subsystem: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close - close the object
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) Close() {
|
2020-03-05 10:12:21 +00:00
|
|
|
d.engineLock.Lock()
|
|
|
|
defer d.engineLock.Unlock()
|
2020-02-26 16:58:25 +00:00
|
|
|
d.reset()
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) reset() {
|
2020-12-07 12:38:05 +00:00
|
|
|
var err error
|
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
if d.rulesStorage != nil {
|
2020-12-07 12:38:05 +00:00
|
|
|
err = d.rulesStorage.Close()
|
|
|
|
if err != nil {
|
2021-05-21 13:15:47 +00:00
|
|
|
log.Error("filtering: rulesStorage.Close: %s", err)
|
2020-12-07 12:38:05 +00:00
|
|
|
}
|
2019-07-23 08:43:30 +00:00
|
|
|
}
|
2020-12-07 12:38:05 +00:00
|
|
|
|
2020-12-21 14:48:07 +00:00
|
|
|
if d.rulesStorageAllow != nil {
|
|
|
|
err = d.rulesStorageAllow.Close()
|
2020-12-07 12:38:05 +00:00
|
|
|
if err != nil {
|
2021-05-21 13:15:47 +00:00
|
|
|
log.Error("filtering: rulesStorageAllow.Close: %s", err)
|
2020-12-07 12:38:05 +00:00
|
|
|
}
|
2020-02-26 16:58:25 +00:00
|
|
|
}
|
2019-07-23 08:43:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// ResultRule contains information about applied rules.
|
|
|
|
type ResultRule struct {
|
|
|
|
// Text is the text of the rule.
|
|
|
|
Text string `json:",omitempty"`
|
|
|
|
// IP is the host IP. It is nil unless the rule uses the
|
|
|
|
// /etc/hosts syntax or the reason is FilteredSafeSearch.
|
|
|
|
IP net.IP `json:",omitempty"`
|
2021-06-17 11:09:16 +00:00
|
|
|
// FilterListID is the ID of the rule's filter list.
|
|
|
|
FilterListID int64 `json:",omitempty"`
|
2020-12-17 10:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Result contains the result of a request check.
|
|
|
|
//
|
|
|
|
// All fields transitively have omitempty tags so that the query log
|
|
|
|
// doesn't become too large.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Clarify relationships between fields. Perhaps
|
|
|
|
// replace with a sum type or an interface?
|
2018-08-30 14:25:33 +00:00
|
|
|
type Result struct {
|
2020-12-17 10:32:46 +00:00
|
|
|
// IsFiltered is true if the request is filtered.
|
|
|
|
IsFiltered bool `json:",omitempty"`
|
2019-07-29 08:37:16 +00:00
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// Reason is the reason for blocking or unblocking the request.
|
|
|
|
Reason Reason `json:",omitempty"`
|
2020-04-16 15:56:47 +00:00
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
// Rules are applied rules. If Rules are not empty, each rule is not nil.
|
2020-12-17 10:32:46 +00:00
|
|
|
Rules []*ResultRule `json:",omitempty"`
|
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
// IPList is the lookup rewrite result. It is empty unless Reason is set to
|
2021-11-23 15:01:48 +00:00
|
|
|
// Rewritten.
|
2020-12-17 10:32:46 +00:00
|
|
|
IPList []net.IP `json:",omitempty"`
|
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
// CanonName is the CNAME value from the lookup rewrite result. It is empty
|
|
|
|
// unless Reason is set to Rewritten or RewrittenRule.
|
2020-12-17 10:32:46 +00:00
|
|
|
CanonName string `json:",omitempty"`
|
2019-07-23 09:21:37 +00:00
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
// ServiceName is the name of the blocked service. It is empty unless
|
|
|
|
// Reason is set to FilteredBlockedService.
|
2020-12-17 10:32:46 +00:00
|
|
|
ServiceName string `json:",omitempty"`
|
2020-12-21 14:48:07 +00:00
|
|
|
|
|
|
|
// DNSRewriteResult is the $dnsrewrite filter rule result.
|
|
|
|
DNSRewriteResult *DNSRewriteResult `json:",omitempty"`
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// Matched returns true if any match at all was found regardless of
|
|
|
|
// whether it was filtered or not.
|
2018-08-30 14:25:33 +00:00
|
|
|
func (r Reason) Matched() bool {
|
|
|
|
return r != NotFilteredNotFound
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// CheckHostRules tries to match the host against filtering rules only.
|
2021-05-21 13:15:47 +00:00
|
|
|
func (d *DNSFilter) CheckHostRules(host string, qtype uint16, setts *Settings) (Result, error) {
|
2019-12-23 12:59:49 +00:00
|
|
|
if !setts.FilteringEnabled {
|
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-12 10:10:47 +00:00
|
|
|
host = strings.ToLower(host)
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
return d.matchHost(host, qtype, setts)
|
2019-12-23 12:59:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
// CheckHost tries to match the host against filtering rules, then safebrowsing
|
|
|
|
// and parental control rules, if they are enabled.
|
|
|
|
func (d *DNSFilter) CheckHost(
|
|
|
|
host string,
|
|
|
|
qtype uint16,
|
2021-05-21 13:15:47 +00:00
|
|
|
setts *Settings,
|
2021-03-25 17:30:30 +00:00
|
|
|
) (res Result, err error) {
|
|
|
|
// Sometimes clients try to resolve ".", which is a request to get root
|
|
|
|
// servers.
|
2018-08-30 14:25:33 +00:00
|
|
|
if host == "" {
|
2021-10-20 16:52:13 +00:00
|
|
|
return Result{}, nil
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
host = strings.ToLower(host)
|
2019-02-22 13:34:36 +00:00
|
|
|
|
2021-10-20 16:52:13 +00:00
|
|
|
if setts.FilteringEnabled {
|
|
|
|
res = d.processRewrites(host, qtype)
|
|
|
|
if res.Reason == Rewritten {
|
|
|
|
return res, nil
|
|
|
|
}
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
for _, hc := range d.hostCheckers {
|
|
|
|
res, err = hc.check(host, qtype, setts)
|
2018-08-30 14:25:33 +00:00
|
|
|
if err != nil {
|
2021-03-25 17:30:30 +00:00
|
|
|
return Result{}, fmt.Errorf("%s: %w", hc.name, err)
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
2020-09-11 12:52:46 +00:00
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
if res.Reason.Matched() {
|
|
|
|
return res, nil
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
// matchSysHosts tries to match the host against the operating system's hosts
|
2021-11-16 13:16:38 +00:00
|
|
|
// database. err is always nil.
|
2021-11-11 13:19:33 +00:00
|
|
|
func (d *DNSFilter) matchSysHosts(
|
|
|
|
host string,
|
|
|
|
qtype uint16,
|
|
|
|
setts *Settings,
|
|
|
|
) (res Result, err error) {
|
2021-10-20 16:52:13 +00:00
|
|
|
if !setts.FilteringEnabled || d.EtcHosts == nil {
|
2021-11-16 13:16:38 +00:00
|
|
|
return res, nil
|
2021-03-25 17:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
return d.matchSysHostsIntl(&urlfilter.DNSRequest{
|
2021-10-14 16:39:21 +00:00
|
|
|
Hostname: host,
|
|
|
|
SortedClientTags: setts.ClientTags,
|
2021-11-16 13:16:38 +00:00
|
|
|
// TODO(e.burkov): Wait for urlfilter update to pass net.IP.
|
2021-10-14 16:39:21 +00:00
|
|
|
ClientIP: setts.ClientIP.String(),
|
|
|
|
ClientName: setts.ClientName,
|
|
|
|
DNSType: qtype,
|
|
|
|
})
|
2021-11-23 15:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// matchSysHostsIntl actually matches the request. It's separated to avoid
|
|
|
|
// perfoming checks twice.
|
|
|
|
func (d *DNSFilter) matchSysHostsIntl(
|
|
|
|
req *urlfilter.DNSRequest,
|
|
|
|
) (res Result, err error) {
|
|
|
|
dnsres, _ := d.EtcHosts.MatchRequest(*req)
|
2021-11-11 13:19:33 +00:00
|
|
|
if dnsres == nil {
|
2021-11-16 13:16:38 +00:00
|
|
|
return res, nil
|
2020-11-06 14:34:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
dnsr := dnsres.DNSRewrites()
|
|
|
|
if len(dnsr) == 0 {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res = d.processDNSRewrites(dnsr)
|
|
|
|
if cn := res.CanonName; cn != "" {
|
|
|
|
// Probably an alias.
|
|
|
|
req.Hostname = cn
|
|
|
|
|
|
|
|
return d.matchSysHostsIntl(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Reason = RewrittenAutoHosts
|
|
|
|
for _, r := range res.Rules {
|
|
|
|
r.Text = stringutil.Coalesce(d.EtcHosts.Translate(r.Text), r.Text)
|
2020-11-06 14:34:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 13:16:38 +00:00
|
|
|
return res, nil
|
2020-11-06 14:34:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 08:37:16 +00:00
|
|
|
// Process rewrites table
|
2020-01-16 09:51:35 +00:00
|
|
|
// . Find CNAME for a domain name (exact match or by wildcard)
|
2020-05-26 08:42:42 +00:00
|
|
|
// . if found and CNAME equals to domain name - this is an exception; exit
|
2019-07-29 08:37:16 +00:00
|
|
|
// . if found, set domain name to canonical name
|
2020-01-16 09:51:35 +00:00
|
|
|
// . repeat for the new domain name (Note: we return only the last CNAME)
|
|
|
|
// . Find A or AAAA record for a domain name (exact match or by wildcard)
|
2020-06-03 09:04:23 +00:00
|
|
|
// . if found, set IP addresses (IPv4 or IPv6 depending on qtype) in Result.IPList array
|
2020-12-21 14:48:07 +00:00
|
|
|
func (d *DNSFilter) processRewrites(host string, qtype uint16) (res Result) {
|
2019-10-09 16:51:26 +00:00
|
|
|
d.confLock.RLock()
|
|
|
|
defer d.confLock.RUnlock()
|
|
|
|
|
2021-07-12 18:10:39 +00:00
|
|
|
rr := findRewrites(d.Rewrites, host, qtype)
|
2020-01-16 09:51:35 +00:00
|
|
|
if len(rr) != 0 {
|
2020-12-28 15:41:50 +00:00
|
|
|
res.Reason = Rewritten
|
2019-07-29 08:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 14:40:31 +00:00
|
|
|
cnames := stringutil.NewSet()
|
2020-01-16 09:51:35 +00:00
|
|
|
origHost := host
|
|
|
|
for len(rr) != 0 && rr[0].Type == dns.TypeCNAME {
|
2021-04-20 13:26:19 +00:00
|
|
|
log.Debug("rewrite: CNAME for %s is %s", host, rr[0].Answer)
|
2020-05-26 08:42:42 +00:00
|
|
|
|
|
|
|
if host == rr[0].Answer { // "host == CNAME" is an exception
|
2020-12-21 14:48:07 +00:00
|
|
|
res.Reason = NotFilteredNotFound
|
|
|
|
|
2020-05-26 08:42:42 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-01-16 09:51:35 +00:00
|
|
|
host = rr[0].Answer
|
2021-04-20 13:26:19 +00:00
|
|
|
if cnames.Has(host) {
|
|
|
|
log.Info("rewrite: breaking CNAME redirection loop: %s. Question: %s", host, origHost)
|
|
|
|
|
2020-01-16 09:51:35 +00:00
|
|
|
return res
|
2019-07-29 08:37:16 +00:00
|
|
|
}
|
2021-04-20 13:26:19 +00:00
|
|
|
|
|
|
|
cnames.Add(host)
|
2020-01-16 09:51:35 +00:00
|
|
|
res.CanonName = rr[0].Answer
|
2021-07-12 18:10:39 +00:00
|
|
|
rr = findRewrites(d.Rewrites, host, qtype)
|
2019-07-29 08:37:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 09:51:35 +00:00
|
|
|
for _, r := range rr {
|
2021-07-12 18:10:39 +00:00
|
|
|
if r.Type == qtype && (qtype == dns.TypeA || qtype == dns.TypeAAAA) {
|
2020-06-03 09:04:23 +00:00
|
|
|
if r.IP == nil { // IP exception
|
2021-07-12 18:10:39 +00:00
|
|
|
res.Reason = NotFilteredNotFound
|
|
|
|
|
2020-06-03 09:04:23 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-01-16 09:51:35 +00:00
|
|
|
res.IPList = append(res.IPList, r.IP)
|
2021-04-20 13:26:19 +00:00
|
|
|
log.Debug("rewrite: A/AAAA for %s is %s", host, r.IP)
|
2020-01-16 09:51:35 +00:00
|
|
|
}
|
2019-07-29 08:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
// matchBlockedServicesRules checks the host against the blocked services rules
|
|
|
|
// in settings, if any. The err is always nil, it is only there to make this
|
|
|
|
// a valid hostChecker function.
|
|
|
|
func matchBlockedServicesRules(
|
|
|
|
host string,
|
|
|
|
_ uint16,
|
2021-05-21 13:15:47 +00:00
|
|
|
setts *Settings,
|
2021-03-25 17:30:30 +00:00
|
|
|
) (res Result, err error) {
|
2021-10-20 16:52:13 +00:00
|
|
|
if !setts.ProtectionEnabled {
|
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
svcs := setts.ServicesRules
|
|
|
|
if len(svcs) == 0 {
|
|
|
|
return Result{}, nil
|
|
|
|
}
|
2019-07-23 09:21:37 +00:00
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
req := rules.NewRequestForHostname(host)
|
2019-07-23 09:21:37 +00:00
|
|
|
for _, s := range svcs {
|
|
|
|
for _, rule := range s.Rules {
|
|
|
|
if rule.Match(req) {
|
|
|
|
res.Reason = FilteredBlockedService
|
|
|
|
res.IsFiltered = true
|
|
|
|
res.ServiceName = s.Name
|
2020-12-17 10:32:46 +00:00
|
|
|
|
|
|
|
ruleText := rule.Text()
|
|
|
|
res.Rules = []*ResultRule{{
|
|
|
|
FilterListID: int64(rule.GetFilterListID()),
|
|
|
|
Text: ruleText,
|
|
|
|
}}
|
|
|
|
|
|
|
|
log.Debug("blocked services: matched rule: %s host: %s service: %s",
|
|
|
|
ruleText, host, s.Name)
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
return res, nil
|
2019-07-23 09:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 17:30:30 +00:00
|
|
|
|
|
|
|
return res, nil
|
2019-07-23 09:21:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 14:25:33 +00:00
|
|
|
//
|
|
|
|
// Adding rule and matching against the rules
|
|
|
|
//
|
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
func newRuleStorage(filters []Filter) (rs *filterlist.RuleStorage, err error) {
|
|
|
|
lists := make([]filterlist.RuleList, 0, len(filters))
|
2020-02-26 16:58:25 +00:00
|
|
|
for _, f := range filters {
|
2021-11-26 15:25:43 +00:00
|
|
|
switch id := int(f.ID); {
|
|
|
|
case len(f.Data) != 0:
|
|
|
|
lists = append(lists, &filterlist.StringRuleList{
|
|
|
|
ID: id,
|
2020-02-26 16:58:25 +00:00
|
|
|
RulesText: string(f.Data),
|
2019-10-22 11:58:20 +00:00
|
|
|
IgnoreCosmetic: true,
|
2021-11-26 15:25:43 +00:00
|
|
|
})
|
|
|
|
case f.FilePath == "":
|
|
|
|
continue
|
|
|
|
case runtime.GOOS == "windows":
|
|
|
|
// On Windows we don't pass a file to urlfilter because it's
|
|
|
|
// difficult to update this file while it's being used.
|
|
|
|
var data []byte
|
|
|
|
data, err = os.ReadFile(f.FilePath)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading filter content: %w", err)
|
2019-10-22 11:58:20 +00:00
|
|
|
}
|
2021-05-21 11:55:42 +00:00
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
lists = append(lists, &filterlist.StringRuleList{
|
|
|
|
ID: id,
|
2019-10-22 11:58:20 +00:00
|
|
|
RulesText: string(data),
|
|
|
|
IgnoreCosmetic: true,
|
2021-11-26 15:25:43 +00:00
|
|
|
})
|
|
|
|
default:
|
|
|
|
var list *filterlist.FileRuleList
|
|
|
|
list, err = filterlist.NewFileRuleList(id, f.FilePath, true)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating file rule list with %q: %w", f.FilePath, err)
|
2019-07-05 14:35:40 +00:00
|
|
|
}
|
2021-11-26 15:25:43 +00:00
|
|
|
|
|
|
|
lists = append(lists, list)
|
2019-07-04 11:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
rs, err = filterlist.NewRuleStorage(lists)
|
2019-05-15 13:46:11 +00:00
|
|
|
if err != nil {
|
2021-12-10 14:20:15 +00:00
|
|
|
return nil, fmt.Errorf("creating rule storage: %w", err)
|
2019-05-15 13:46:11 +00:00
|
|
|
}
|
2021-11-26 15:25:43 +00:00
|
|
|
|
|
|
|
return rs, nil
|
2020-02-26 16:58:25 +00:00
|
|
|
}
|
2019-10-09 16:51:26 +00:00
|
|
|
|
2020-11-06 09:15:08 +00:00
|
|
|
// Initialize urlfilter objects.
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) initFiltering(allowFilters, blockFilters []Filter) error {
|
2021-11-26 15:25:43 +00:00
|
|
|
rulesStorage, err := newRuleStorage(blockFilters)
|
2020-02-26 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-26 15:25:43 +00:00
|
|
|
|
|
|
|
rulesStorageAllow, err := newRuleStorage(allowFilters)
|
2020-02-26 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-09 16:51:26 +00:00
|
|
|
}
|
2020-08-26 15:58:21 +00:00
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
filteringEngine := urlfilter.NewDNSEngine(rulesStorage)
|
|
|
|
filteringEngineAllow := urlfilter.NewDNSEngine(rulesStorageAllow)
|
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
func() {
|
|
|
|
d.engineLock.Lock()
|
|
|
|
defer d.engineLock.Unlock()
|
|
|
|
|
|
|
|
d.reset()
|
|
|
|
d.rulesStorage = rulesStorage
|
|
|
|
d.filteringEngine = filteringEngine
|
|
|
|
d.rulesStorageAllow = rulesStorageAllow
|
|
|
|
d.filteringEngineAllow = filteringEngineAllow
|
|
|
|
}()
|
2020-05-12 21:46:35 +00:00
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
// Make sure that the OS reclaims memory as soon as possible.
|
2020-05-12 21:46:35 +00:00
|
|
|
debug.FreeOSMemory()
|
2019-10-09 16:51:26 +00:00
|
|
|
log.Debug("initialized filtering engine")
|
|
|
|
|
2018-11-30 10:48:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-17 11:09:16 +00:00
|
|
|
// hostRules is a helper that converts a slice of host rules into a slice of the
|
|
|
|
// rules.Rule interface values.
|
|
|
|
func hostRulesToRules(netRules []*rules.HostRule) (res []rules.Rule) {
|
|
|
|
if netRules == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res = make([]rules.Rule, len(netRules))
|
|
|
|
for i, nr := range netRules {
|
|
|
|
res[i] = nr
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-21 14:48:07 +00:00
|
|
|
// matchHostProcessAllowList processes the allowlist logic of host
|
|
|
|
// matching.
|
2021-06-17 11:09:16 +00:00
|
|
|
func (d *DNSFilter) matchHostProcessAllowList(
|
|
|
|
host string,
|
2021-11-11 13:19:33 +00:00
|
|
|
dnsres *urlfilter.DNSResult,
|
2021-06-17 11:09:16 +00:00
|
|
|
) (res Result, err error) {
|
|
|
|
var matchedRules []rules.Rule
|
2020-12-21 14:48:07 +00:00
|
|
|
if dnsres.NetworkRule != nil {
|
2021-06-17 11:09:16 +00:00
|
|
|
matchedRules = []rules.Rule{dnsres.NetworkRule}
|
2020-12-21 14:48:07 +00:00
|
|
|
} else if len(dnsres.HostRulesV4) > 0 {
|
2021-06-17 11:09:16 +00:00
|
|
|
matchedRules = hostRulesToRules(dnsres.HostRulesV4)
|
2020-12-21 14:48:07 +00:00
|
|
|
} else if len(dnsres.HostRulesV6) > 0 {
|
2021-06-17 11:09:16 +00:00
|
|
|
matchedRules = hostRulesToRules(dnsres.HostRulesV6)
|
2020-12-21 14:48:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 11:09:16 +00:00
|
|
|
if len(matchedRules) == 0 {
|
2020-12-21 14:48:07 +00:00
|
|
|
return Result{}, fmt.Errorf("invalid dns result: rules are empty")
|
|
|
|
}
|
|
|
|
|
2021-06-17 11:09:16 +00:00
|
|
|
log.Debug("filtering: allowlist rules for host %q: %+v", host, matchedRules)
|
2020-12-21 14:48:07 +00:00
|
|
|
|
2021-06-17 11:09:16 +00:00
|
|
|
return makeResult(matchedRules, NotFilteredAllowList), nil
|
2020-12-21 14:48:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 10:03:36 +00:00
|
|
|
// matchHostProcessDNSResult processes the matched DNS filtering result.
|
|
|
|
func (d *DNSFilter) matchHostProcessDNSResult(
|
|
|
|
qtype uint16,
|
2021-11-11 13:19:33 +00:00
|
|
|
dnsres *urlfilter.DNSResult,
|
2021-03-26 10:03:36 +00:00
|
|
|
) (res Result) {
|
|
|
|
if dnsres.NetworkRule != nil {
|
|
|
|
reason := FilteredBlockList
|
|
|
|
if dnsres.NetworkRule.Whitelist {
|
|
|
|
reason = NotFilteredAllowList
|
|
|
|
}
|
|
|
|
|
2021-06-17 11:09:16 +00:00
|
|
|
return makeResult([]rules.Rule{dnsres.NetworkRule}, reason)
|
2021-03-26 10:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if qtype == dns.TypeA && dnsres.HostRulesV4 != nil {
|
2021-06-17 11:09:16 +00:00
|
|
|
res = makeResult(hostRulesToRules(dnsres.HostRulesV4), FilteredBlockList)
|
|
|
|
for i, hr := range dnsres.HostRulesV4 {
|
|
|
|
res.Rules[i].IP = hr.IP.To4()
|
|
|
|
}
|
2021-03-26 10:03:36 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
if qtype == dns.TypeAAAA && dnsres.HostRulesV6 != nil {
|
2021-06-17 11:09:16 +00:00
|
|
|
res = makeResult(hostRulesToRules(dnsres.HostRulesV6), FilteredBlockList)
|
|
|
|
for i, hr := range dnsres.HostRulesV6 {
|
|
|
|
res.Rules[i].IP = hr.IP.To16()
|
|
|
|
}
|
2021-03-26 10:03:36 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
if dnsres.HostRulesV4 != nil || dnsres.HostRulesV6 != nil {
|
2021-10-14 16:39:21 +00:00
|
|
|
// Question type doesn't match the host rules. Return the first matched
|
|
|
|
// host rule, but without an IP address.
|
2021-06-17 11:09:16 +00:00
|
|
|
var matchedRules []rules.Rule
|
2021-03-26 10:03:36 +00:00
|
|
|
if dnsres.HostRulesV4 != nil {
|
2021-06-17 11:09:16 +00:00
|
|
|
matchedRules = []rules.Rule{dnsres.HostRulesV4[0]}
|
2021-03-26 10:03:36 +00:00
|
|
|
} else if dnsres.HostRulesV6 != nil {
|
2021-06-17 11:09:16 +00:00
|
|
|
matchedRules = []rules.Rule{dnsres.HostRulesV6[0]}
|
2021-03-26 10:03:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 11:09:16 +00:00
|
|
|
return makeResult(matchedRules, FilteredBlockList)
|
2021-03-26 10:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result{}
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:15:08 +00:00
|
|
|
// matchHost is a low-level way to check only if hostname is filtered by rules,
|
|
|
|
// skipping expensive safebrowsing and parental lookups.
|
2021-03-25 17:30:30 +00:00
|
|
|
func (d *DNSFilter) matchHost(
|
|
|
|
host string,
|
|
|
|
qtype uint16,
|
2021-05-21 13:15:47 +00:00
|
|
|
setts *Settings,
|
2021-03-25 17:30:30 +00:00
|
|
|
) (res Result, err error) {
|
|
|
|
if !setts.FilteringEnabled {
|
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:55:05 +00:00
|
|
|
ureq := urlfilter.DNSRequest{
|
|
|
|
Hostname: host,
|
|
|
|
SortedClientTags: setts.ClientTags,
|
2021-01-20 14:27:53 +00:00
|
|
|
// TODO(e.burkov): Wait for urlfilter update to pass net.IP.
|
|
|
|
ClientIP: setts.ClientIP.String(),
|
|
|
|
ClientName: setts.ClientName,
|
|
|
|
DNSType: qtype,
|
2020-11-24 16:55:05 +00:00
|
|
|
}
|
2020-06-23 11:36:26 +00:00
|
|
|
|
2021-10-14 16:39:21 +00:00
|
|
|
d.engineLock.RLock()
|
|
|
|
// Keep in mind that this lock must be held no just when calling Match() but
|
|
|
|
// also while using the rules returned by it.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Inspect if the above is true.
|
|
|
|
defer d.engineLock.RUnlock()
|
|
|
|
|
2021-10-20 16:52:13 +00:00
|
|
|
if setts.ProtectionEnabled && d.filteringEngineAllow != nil {
|
2020-12-21 14:48:07 +00:00
|
|
|
dnsres, ok := d.filteringEngineAllow.MatchRequest(ureq)
|
2020-02-26 16:58:25 +00:00
|
|
|
if ok {
|
2020-12-21 14:48:07 +00:00
|
|
|
return d.matchHostProcessAllowList(host, dnsres)
|
2020-02-26 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 13:46:11 +00:00
|
|
|
if d.filteringEngine == nil {
|
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-21 14:48:07 +00:00
|
|
|
dnsres, ok := d.filteringEngine.MatchRequest(ureq)
|
2021-03-26 10:03:36 +00:00
|
|
|
// Check DNS rewrites first, because the API there is a bit awkward.
|
2020-12-21 14:48:07 +00:00
|
|
|
if dnsr := dnsres.DNSRewrites(); len(dnsr) > 0 {
|
|
|
|
res = d.processDNSRewrites(dnsr)
|
2020-12-28 15:41:50 +00:00
|
|
|
if res.Reason == RewrittenRule && res.CanonName == host {
|
2021-10-14 16:39:21 +00:00
|
|
|
// A rewrite of a host to itself. Go on and try matching other
|
|
|
|
// things.
|
2020-12-21 14:48:07 +00:00
|
|
|
} else {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
} else if !ok {
|
2019-05-15 13:46:11 +00:00
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-20 16:52:13 +00:00
|
|
|
if !setts.ProtectionEnabled {
|
|
|
|
// Don't check non-dnsrewrite filtering results.
|
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-26 10:03:36 +00:00
|
|
|
res = d.matchHostProcessDNSResult(qtype, dnsres)
|
2021-06-17 11:09:16 +00:00
|
|
|
for _, r := range res.Rules {
|
2021-03-26 10:03:36 +00:00
|
|
|
log.Debug(
|
|
|
|
"filtering: found rule %q for host %q, filter list id: %d",
|
|
|
|
r.Text,
|
|
|
|
host,
|
|
|
|
r.FilterListID,
|
|
|
|
)
|
2020-01-30 16:06:09 +00:00
|
|
|
}
|
2019-05-15 13:46:11 +00:00
|
|
|
|
2021-03-26 10:03:36 +00:00
|
|
|
return res, nil
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// makeResult returns a properly constructed Result.
|
2021-06-17 11:09:16 +00:00
|
|
|
func makeResult(matchedRules []rules.Rule, reason Reason) (res Result) {
|
|
|
|
resRules := make([]*ResultRule, len(matchedRules))
|
|
|
|
for i, mr := range matchedRules {
|
|
|
|
resRules[i] = &ResultRule{
|
|
|
|
FilterListID: int64(mr.GetFilterListID()),
|
|
|
|
Text: mr.Text(),
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 11:09:16 +00:00
|
|
|
return Result{
|
|
|
|
IsFiltered: reason == FilteredBlockList,
|
|
|
|
Reason: reason,
|
|
|
|
Rules: resRules,
|
2020-02-26 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
// InitModule manually initializes blocked services map.
|
2020-04-27 10:21:16 +00:00
|
|
|
func InitModule() {
|
|
|
|
initBlockedServices()
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:15:08 +00:00
|
|
|
// New creates properly initialized DNS Filter that is ready to be used.
|
2021-11-26 15:25:43 +00:00
|
|
|
func New(c *Config, blockFilters []Filter) (d *DNSFilter) {
|
|
|
|
d = &DNSFilter{
|
|
|
|
resolver: net.DefaultResolver,
|
|
|
|
}
|
2019-06-24 16:00:03 +00:00
|
|
|
if c != nil {
|
2019-08-22 12:09:43 +00:00
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
d.safebrowsingCache = cache.New(cache.Config{
|
|
|
|
EnableLRU: true,
|
|
|
|
MaxSize: c.SafeBrowsingCacheSize,
|
|
|
|
})
|
|
|
|
d.safeSearchCache = cache.New(cache.Config{
|
|
|
|
EnableLRU: true,
|
|
|
|
MaxSize: c.SafeSearchCacheSize,
|
|
|
|
})
|
|
|
|
d.parentalCache = cache.New(cache.Config{
|
|
|
|
EnableLRU: true,
|
|
|
|
MaxSize: c.ParentalCacheSize,
|
|
|
|
})
|
2021-02-04 17:35:13 +00:00
|
|
|
|
|
|
|
if c.CustomResolver != nil {
|
2021-11-26 15:25:43 +00:00
|
|
|
d.resolver = c.CustomResolver
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
2019-06-24 16:00:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
d.hostCheckers = []hostChecker{{
|
2021-10-14 16:39:21 +00:00
|
|
|
check: d.matchSysHosts,
|
|
|
|
name: "hosts container",
|
2021-03-25 17:30:30 +00:00
|
|
|
}, {
|
|
|
|
check: d.matchHost,
|
|
|
|
name: "filtering",
|
|
|
|
}, {
|
|
|
|
check: matchBlockedServicesRules,
|
|
|
|
name: "blocked services",
|
|
|
|
}, {
|
|
|
|
check: d.checkSafeBrowsing,
|
|
|
|
name: "safe browsing",
|
|
|
|
}, {
|
|
|
|
check: d.checkParental,
|
|
|
|
name: "parental",
|
|
|
|
}, {
|
|
|
|
check: d.checkSafeSearch,
|
|
|
|
name: "safe search",
|
|
|
|
}}
|
|
|
|
|
2019-10-16 09:57:49 +00:00
|
|
|
err := d.initSecurityServices()
|
|
|
|
if err != nil {
|
2021-05-21 13:15:47 +00:00
|
|
|
log.Error("filtering: initialize services: %s", err)
|
2019-10-16 09:57:49 +00:00
|
|
|
return nil
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
2019-10-16 09:57:49 +00:00
|
|
|
|
2018-11-30 10:47:26 +00:00
|
|
|
if c != nil {
|
|
|
|
d.Config = *c
|
2020-01-16 09:51:35 +00:00
|
|
|
d.prepareRewrites()
|
2018-11-30 10:47:26 +00:00
|
|
|
}
|
2018-09-10 17:34:42 +00:00
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
bsvcs := []string{}
|
|
|
|
for _, s := range d.BlockedServices {
|
|
|
|
if !BlockedSvcKnown(s) {
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Debug("skipping unknown blocked-service %q", s)
|
2020-02-18 17:17:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
bsvcs = append(bsvcs, s)
|
|
|
|
}
|
|
|
|
d.BlockedServices = bsvcs
|
|
|
|
|
2020-02-26 16:58:25 +00:00
|
|
|
if blockFilters != nil {
|
2021-03-11 17:36:54 +00:00
|
|
|
err = d.initFiltering(nil, blockFilters)
|
2019-05-15 13:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't initialize filtering subsystem: %s", err)
|
2019-10-09 16:51:26 +00:00
|
|
|
d.Close()
|
2019-05-15 13:46:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 11:25:40 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:06:09 +00:00
|
|
|
// Start - start the module:
|
|
|
|
// . start async filtering initializer goroutine
|
|
|
|
// . register web handlers
|
2020-12-17 10:32:46 +00:00
|
|
|
func (d *DNSFilter) Start() {
|
2019-10-09 16:51:26 +00:00
|
|
|
d.filtersInitializerChan = make(chan filtersInitializerParams, 1)
|
|
|
|
go d.filtersInitializer()
|
2018-08-30 14:25:33 +00:00
|
|
|
|
2019-10-09 16:51:26 +00:00
|
|
|
if d.Config.HTTPRegister != nil { // for tests
|
|
|
|
d.registerSecurityHandlers()
|
|
|
|
d.registerRewritesHandlers()
|
2020-02-18 17:17:35 +00:00
|
|
|
d.registerBlockedServicesHandlers()
|
2019-05-15 13:46:11 +00:00
|
|
|
}
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|