2022-09-23 13:23:35 +03:00
|
|
|
|
package filtering
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
|
|
|
|
import (
|
2020-03-17 15:00:40 +03:00
|
|
|
|
"bufio"
|
2022-12-20 16:40:42 +03:00
|
|
|
|
"bytes"
|
2018-11-28 20:14:54 +03:00
|
|
|
|
"fmt"
|
2019-03-15 16:49:10 +03:00
|
|
|
|
"hash/crc32"
|
2020-03-17 15:00:40 +03:00
|
|
|
|
"io"
|
2020-11-20 17:32:41 +03:00
|
|
|
|
"net/http"
|
2018-11-28 20:14:54 +03:00
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-05-24 17:28:11 +03:00
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 16:44:22 +03:00
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-09-15 20:09:32 +03:00
|
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2022-09-23 13:23:35 +03:00
|
|
|
|
"golang.org/x/exp/slices"
|
2018-11-28 20:14:54 +03:00
|
|
|
|
)
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
// filterDir is the subdirectory of a data directory to store downloaded
|
|
|
|
|
// filters.
|
|
|
|
|
const filterDir = "filters"
|
2020-03-17 15:00:40 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
// nextFilterID is a way to seed a unique ID generation.
|
|
|
|
|
//
|
|
|
|
|
// TODO(e.burkov): Use more deterministic approach.
|
|
|
|
|
var nextFilterID = time.Now().Unix()
|
2019-09-04 14:12:00 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
// FilterYAML respresents a filter list in the configuration file.
|
|
|
|
|
//
|
|
|
|
|
// TODO(e.burkov): Investigate if the field oredering is important.
|
|
|
|
|
type FilterYAML struct {
|
2019-09-04 14:12:00 +03:00
|
|
|
|
Enabled bool
|
2020-03-05 14:37:43 +03:00
|
|
|
|
URL string // URL or a file path
|
2019-09-04 14:12:00 +03:00
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
|
RulesCount int `yaml:"-"`
|
|
|
|
|
LastUpdated time.Time `yaml:"-"`
|
2019-03-15 16:49:10 +03:00
|
|
|
|
checksum uint32 // checksum of the file data
|
2020-02-26 19:58:25 +03:00
|
|
|
|
white bool
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
Filter `yaml:",inline"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear filter rules
|
|
|
|
|
func (filter *FilterYAML) unload() {
|
|
|
|
|
filter.RulesCount = 0
|
|
|
|
|
filter.checksum = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Path to the filter contents
|
|
|
|
|
func (filter *FilterYAML) Path(dataDir string) string {
|
|
|
|
|
return filepath.Join(dataDir, filterDir, strconv.FormatInt(filter.ID, 10)+".txt")
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 15:56:29 +03:00
|
|
|
|
const (
|
2022-10-21 20:14:43 +03:00
|
|
|
|
// errFilterNotExist is returned from [filterSetProperties] when there are
|
|
|
|
|
// no lists with the desired URL to update.
|
|
|
|
|
//
|
|
|
|
|
// TODO(e.burkov): Use wherever the same error is needed.
|
|
|
|
|
errFilterNotExist errors.Error = "url doesn't exist"
|
|
|
|
|
|
|
|
|
|
// errFilterExists is returned from [filterSetProperties] when there is
|
|
|
|
|
// another filter having the same URL as the one updated.
|
|
|
|
|
//
|
|
|
|
|
// TODO(e.burkov): Use wherever the same error is needed.
|
|
|
|
|
errFilterExists errors.Error = "url already exists"
|
2019-11-06 15:56:29 +03:00
|
|
|
|
)
|
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
// filterSetProperties searches for the particular filter list by url and sets
|
|
|
|
|
// the values of newList to it, updating afterwards if needed. It returns true
|
|
|
|
|
// if the update was performed and the filtering engine restart is required.
|
|
|
|
|
func (d *DNSFilter) filterSetProperties(
|
|
|
|
|
listURL string,
|
|
|
|
|
newList FilterYAML,
|
|
|
|
|
isAllowlist bool,
|
|
|
|
|
) (shouldRestart bool, err error) {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
d.filtersMu.Lock()
|
|
|
|
|
defer d.filtersMu.Unlock()
|
2019-11-06 15:56:29 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
filters := d.Filters
|
2022-10-21 20:14:43 +03:00
|
|
|
|
if isAllowlist {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
filters = d.WhitelistFilters
|
2020-02-26 19:58:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
i := slices.IndexFunc(filters, func(filt FilterYAML) bool { return filt.URL == listURL })
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if i == -1 {
|
2022-10-21 20:14:43 +03:00
|
|
|
|
return false, errFilterNotExist
|
2022-09-23 13:23:35 +03:00
|
|
|
|
}
|
2019-11-06 15:56:29 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
filt := &filters[i]
|
2022-10-21 20:14:43 +03:00
|
|
|
|
log.Debug(
|
|
|
|
|
"filtering: set name to %q, url to %s, enabled to %t for filter %s",
|
|
|
|
|
newList.Name,
|
|
|
|
|
newList.URL,
|
|
|
|
|
newList.Enabled,
|
|
|
|
|
filt.URL,
|
|
|
|
|
)
|
|
|
|
|
|
2022-12-23 17:11:11 +03:00
|
|
|
|
defer func(oldURL, oldName string, oldEnabled bool, oldUpdated time.Time, oldRulesCount int) {
|
2022-10-21 20:14:43 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
filt.URL = oldURL
|
|
|
|
|
filt.Name = oldName
|
|
|
|
|
filt.Enabled = oldEnabled
|
|
|
|
|
filt.LastUpdated = oldUpdated
|
2022-12-23 17:11:11 +03:00
|
|
|
|
filt.RulesCount = oldRulesCount
|
2022-10-21 20:14:43 +03:00
|
|
|
|
}
|
2022-12-23 17:11:11 +03:00
|
|
|
|
}(filt.URL, filt.Name, filt.Enabled, filt.LastUpdated, filt.RulesCount)
|
2019-11-06 15:56:29 +03:00
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
filt.Name = newList.Name
|
2022-09-23 13:23:35 +03:00
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
if filt.URL != newList.URL {
|
|
|
|
|
if d.filterExistsLocked(newList.URL) {
|
|
|
|
|
return false, errFilterExists
|
2019-11-06 15:56:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
shouldRestart = true
|
|
|
|
|
|
|
|
|
|
filt.URL = newList.URL
|
2022-09-23 13:23:35 +03:00
|
|
|
|
filt.LastUpdated = time.Time{}
|
2022-10-21 20:14:43 +03:00
|
|
|
|
filt.unload()
|
2022-09-23 13:23:35 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
if filt.Enabled != newList.Enabled {
|
|
|
|
|
filt.Enabled = newList.Enabled
|
|
|
|
|
shouldRestart = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if filt.Enabled {
|
|
|
|
|
if shouldRestart {
|
|
|
|
|
// Download the filter contents.
|
|
|
|
|
shouldRestart, err = d.update(filt)
|
2019-03-18 14:12:04 +03:00
|
|
|
|
}
|
2022-10-21 20:14:43 +03:00
|
|
|
|
} else {
|
|
|
|
|
// TODO(e.burkov): The validation of the contents of the new URL is
|
|
|
|
|
// currently skipped if the rule list is disabled. This makes it
|
|
|
|
|
// possible to set a bad rules source, but the validation should still
|
2022-12-20 16:40:42 +03:00
|
|
|
|
// kick in when the filter is enabled. Consider changing this behavior
|
|
|
|
|
// to be stricter.
|
2022-10-21 20:14:43 +03:00
|
|
|
|
filt.unload()
|
2019-03-18 14:12:04 +03:00
|
|
|
|
}
|
2022-09-23 13:23:35 +03:00
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
return shouldRestart, err
|
2019-03-18 14:12:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
// filterExists returns true if a filter with the same url exists in d. It's
|
|
|
|
|
// safe for concurrent use.
|
|
|
|
|
func (d *DNSFilter) filterExists(url string) (ok bool) {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
d.filtersMu.RLock()
|
|
|
|
|
defer d.filtersMu.RUnlock()
|
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
r := d.filterExistsLocked(url)
|
2022-09-23 13:23:35 +03:00
|
|
|
|
|
2019-11-06 15:56:29 +03:00
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
|
// filterExistsLocked returns true if d contains the filter with the same url.
|
|
|
|
|
// d.filtersMu is expected to be locked.
|
|
|
|
|
func (d *DNSFilter) filterExistsLocked(url string) (ok bool) {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
for _, f := range d.Filters {
|
2020-02-26 19:58:25 +03:00
|
|
|
|
if f.URL == url {
|
|
|
|
|
return true
|
2019-03-18 14:41:38 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-21 20:14:43 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
for _, f := range d.WhitelistFilters {
|
2020-02-26 19:58:25 +03:00
|
|
|
|
if f.URL == url {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-21 20:14:43 +03:00
|
|
|
|
|
2020-02-26 19:58:25 +03:00
|
|
|
|
return false
|
2019-03-18 14:41:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a filter
|
|
|
|
|
// Return FALSE if a filter with this URL exists
|
2023-03-29 19:09:54 +03:00
|
|
|
|
func (d *DNSFilter) filterAdd(flt FilterYAML) (err error) {
|
|
|
|
|
// Defer annotating to unlock sooner.
|
|
|
|
|
defer func() { err = errors.Annotate(err, "adding filter: %w") }()
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
d.filtersMu.Lock()
|
|
|
|
|
defer d.filtersMu.Unlock()
|
2019-03-18 14:41:38 +03:00
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
// Check for duplicates.
|
2022-10-21 20:14:43 +03:00
|
|
|
|
if d.filterExistsLocked(flt.URL) {
|
2023-03-29 19:09:54 +03:00
|
|
|
|
return errFilterExists
|
2019-03-18 14:41:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if flt.white {
|
|
|
|
|
d.WhitelistFilters = append(d.WhitelistFilters, flt)
|
2020-02-26 19:58:25 +03:00
|
|
|
|
} else {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
d.Filters = append(d.Filters, flt)
|
2020-02-26 19:58:25 +03:00
|
|
|
|
}
|
2023-03-29 19:09:54 +03:00
|
|
|
|
|
|
|
|
|
return nil
|
2019-03-18 14:41:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 19:41:45 +03:00
|
|
|
|
// Load filters from the disk
|
|
|
|
|
// And if any filter has zero ID, assign a new one
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) loadFilters(array []FilterYAML) {
|
2020-02-26 19:58:25 +03:00
|
|
|
|
for i := range array {
|
|
|
|
|
filter := &array[i] // otherwise we're operating on a copy
|
2019-03-15 19:41:45 +03:00
|
|
|
|
if filter.ID == 0 {
|
|
|
|
|
filter.ID = assignUniqueFilterID()
|
|
|
|
|
}
|
2019-03-18 12:52:34 +03:00
|
|
|
|
|
|
|
|
|
if !filter.Enabled {
|
|
|
|
|
// No need to load a filter that is not enabled
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
err := d.load(filter)
|
2019-03-15 19:41:45 +03:00
|
|
|
|
if err != nil {
|
2019-09-04 14:12:00 +03:00
|
|
|
|
log.Error("Couldn't load filter %d contents due to %s", filter.ID, err)
|
2019-03-15 19:41:45 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func deduplicateFilters(filters []FilterYAML) (deduplicated []FilterYAML) {
|
|
|
|
|
urls := stringutil.NewSet()
|
|
|
|
|
lastIdx := 0
|
|
|
|
|
|
|
|
|
|
for _, filter := range filters {
|
|
|
|
|
if !urls.Has(filter.URL) {
|
|
|
|
|
urls.Add(filter.URL)
|
|
|
|
|
filters[lastIdx] = filter
|
|
|
|
|
lastIdx++
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
return filters[:lastIdx]
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the next filter ID to max(filter.ID) + 1
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func updateUniqueFilterID(filters []FilterYAML) {
|
2018-11-28 20:14:54 +03:00
|
|
|
|
for _, filter := range filters {
|
|
|
|
|
if nextFilterID < filter.ID {
|
|
|
|
|
nextFilterID = filter.ID + 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
// TODO(e.burkov): Improve this inexhaustible source of races.
|
2018-11-28 20:14:54 +03:00
|
|
|
|
func assignUniqueFilterID() int64 {
|
|
|
|
|
value := nextFilterID
|
2019-01-24 20:11:01 +03:00
|
|
|
|
nextFilterID++
|
2018-11-28 20:14:54 +03:00
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sets up a timer that will be checking for filters updates periodically
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) periodicallyRefreshFilters() {
|
2020-01-15 18:41:27 +03:00
|
|
|
|
const maxInterval = 1 * 60 * 60
|
|
|
|
|
intval := 5 // use a dynamically increasing time interval
|
2019-09-04 14:12:00 +03:00
|
|
|
|
for {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
isNetErr, ok := false, false
|
|
|
|
|
if d.FiltersUpdateIntervalHours != 0 {
|
|
|
|
|
_, isNetErr, ok = d.tryRefreshFilters(true, true, false)
|
|
|
|
|
if ok && !isNetErr {
|
2020-01-15 18:41:27 +03:00
|
|
|
|
intval = maxInterval
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if isNetErr {
|
2020-01-15 18:41:27 +03:00
|
|
|
|
intval *= 2
|
|
|
|
|
if intval > maxInterval {
|
|
|
|
|
intval = maxInterval
|
|
|
|
|
}
|
2019-10-09 19:51:26 +03:00
|
|
|
|
}
|
2020-01-15 18:41:27 +03:00
|
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(intval) * time.Second)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
// tryRefreshFilters is like [refreshFilters], but backs down if the update is
|
|
|
|
|
// already going on.
|
2022-08-31 18:57:02 +03:00
|
|
|
|
//
|
2022-09-23 13:23:35 +03:00
|
|
|
|
// TODO(e.burkov): Get rid of the concurrency pattern which requires the
|
2022-12-20 16:40:42 +03:00
|
|
|
|
// [sync.Mutex.TryLock].
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) tryRefreshFilters(block, allow, force bool) (updated int, isNetworkErr, ok bool) {
|
|
|
|
|
if ok = d.refreshLock.TryLock(); !ok {
|
2022-12-20 16:40:42 +03:00
|
|
|
|
return 0, false, false
|
2022-09-23 13:23:35 +03:00
|
|
|
|
}
|
|
|
|
|
defer d.refreshLock.Unlock()
|
|
|
|
|
|
|
|
|
|
updated, isNetworkErr = d.refreshFiltersIntl(block, allow, force)
|
|
|
|
|
|
|
|
|
|
return updated, isNetworkErr, ok
|
2019-10-09 19:51:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
// listsToUpdate returns the slice of filter lists that could be updated.
|
|
|
|
|
func (d *DNSFilter) listsToUpdate(filters *[]FilterYAML, force bool) (toUpd []FilterYAML) {
|
2019-09-04 14:12:00 +03:00
|
|
|
|
now := time.Now()
|
2022-09-23 13:23:35 +03:00
|
|
|
|
|
|
|
|
|
d.filtersMu.RLock()
|
|
|
|
|
defer d.filtersMu.RUnlock()
|
|
|
|
|
|
2020-02-28 12:40:16 +03:00
|
|
|
|
for i := range *filters {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
flt := &(*filters)[i] // otherwise we will be operating on a copy
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if !flt.Enabled {
|
2019-03-18 12:52:34 +03:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if !force {
|
|
|
|
|
exp := flt.LastUpdated.Add(time.Duration(d.FiltersUpdateIntervalHours) * time.Hour)
|
|
|
|
|
if now.Before(exp) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2019-03-15 16:09:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
toUpd = append(toUpd, FilterYAML{
|
|
|
|
|
Filter: Filter{
|
|
|
|
|
ID: flt.ID,
|
|
|
|
|
},
|
|
|
|
|
URL: flt.URL,
|
|
|
|
|
Name: flt.Name,
|
|
|
|
|
checksum: flt.checksum,
|
|
|
|
|
})
|
2019-03-18 17:23:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
return toUpd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DNSFilter) refreshFiltersArray(filters *[]FilterYAML, force bool) (int, []FilterYAML, []bool, bool) {
|
|
|
|
|
var updateFlags []bool // 'true' if filter data has changed
|
|
|
|
|
|
|
|
|
|
updateFilters := d.listsToUpdate(filters, force)
|
2020-01-28 14:07:11 +03:00
|
|
|
|
if len(updateFilters) == 0 {
|
2020-02-28 12:40:16 +03:00
|
|
|
|
return 0, nil, nil, false
|
2020-01-28 14:07:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 18:41:27 +03:00
|
|
|
|
nfail := 0
|
2019-03-18 17:23:02 +03:00
|
|
|
|
for i := range updateFilters {
|
|
|
|
|
uf := &updateFilters[i]
|
2022-09-23 13:23:35 +03:00
|
|
|
|
updated, err := d.update(uf)
|
2019-07-16 15:29:36 +03:00
|
|
|
|
updateFlags = append(updateFlags, updated)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
if err != nil {
|
2020-01-15 18:41:27 +03:00
|
|
|
|
nfail++
|
2019-03-18 17:23:02 +03:00
|
|
|
|
log.Printf("Failed to update filter %s: %s\n", uf.URL, err)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
continue
|
|
|
|
|
}
|
2019-07-16 14:32:58 +03:00
|
|
|
|
}
|
2019-07-16 12:55:18 +03:00
|
|
|
|
|
2020-01-15 18:41:27 +03:00
|
|
|
|
if nfail == len(updateFilters) {
|
2020-02-28 12:40:16 +03:00
|
|
|
|
return 0, nil, nil, true
|
2020-01-15 18:41:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 19:51:26 +03:00
|
|
|
|
updateCount := 0
|
2023-03-29 19:09:54 +03:00
|
|
|
|
|
|
|
|
|
d.filtersMu.Lock()
|
|
|
|
|
defer d.filtersMu.Unlock()
|
|
|
|
|
|
2019-07-16 12:55:18 +03:00
|
|
|
|
for i := range updateFilters {
|
|
|
|
|
uf := &updateFilters[i]
|
|
|
|
|
updated := updateFlags[i]
|
2019-03-18 17:23:02 +03:00
|
|
|
|
|
2020-02-28 12:40:16 +03:00
|
|
|
|
for k := range *filters {
|
|
|
|
|
f := &(*filters)[k]
|
2019-03-18 17:23:02 +03:00
|
|
|
|
if f.ID != uf.ID || f.URL != uf.URL {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-03-29 19:09:54 +03:00
|
|
|
|
|
2019-03-18 17:23:02 +03:00
|
|
|
|
f.LastUpdated = uf.LastUpdated
|
|
|
|
|
if !updated {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
log.Info("Updated filter #%d. Rules: %d -> %d", f.ID, f.RulesCount, uf.RulesCount)
|
2019-03-18 17:23:02 +03:00
|
|
|
|
f.Name = uf.Name
|
|
|
|
|
f.RulesCount = uf.RulesCount
|
|
|
|
|
f.checksum = uf.checksum
|
|
|
|
|
updateCount++
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 12:40:16 +03:00
|
|
|
|
return updateCount, updateFilters, updateFlags, false
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
// refreshFiltersIntl checks filters and updates them if necessary. If force is
|
|
|
|
|
// true, it ignores the filter.LastUpdated field value.
|
2020-02-28 12:40:16 +03:00
|
|
|
|
//
|
|
|
|
|
// Algorithm:
|
|
|
|
|
//
|
2022-08-31 18:57:02 +03:00
|
|
|
|
// 1. Get the list of filters to be updated. For each filter, run the download
|
|
|
|
|
// and checksum check operation. Store downloaded data in a temporary file
|
|
|
|
|
// inside data/filters directory
|
|
|
|
|
//
|
|
|
|
|
// 2. For each filter, if filter data hasn't changed, just set new update time
|
|
|
|
|
// on file. Otherwise, rename the temporary file (<temp> -> 1.txt). Note
|
|
|
|
|
// that this method works only on Unix systems. On Windows, don't pass
|
|
|
|
|
// files to filtering, pass the whole data.
|
|
|
|
|
//
|
2022-09-23 13:23:35 +03:00
|
|
|
|
// refreshFiltersIntl returns the number of updated filters. It also returns
|
|
|
|
|
// true if there was a network error and nothing could be updated.
|
2022-08-31 18:57:02 +03:00
|
|
|
|
//
|
|
|
|
|
// TODO(a.garipov, e.burkov): What the hell?
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) refreshFiltersIntl(block, allow, force bool) (int, bool) {
|
|
|
|
|
log.Debug("filtering: updating...")
|
2020-02-28 12:40:16 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
updNum := 0
|
|
|
|
|
var lists []FilterYAML
|
|
|
|
|
var toUpd []bool
|
|
|
|
|
isNetErr := false
|
|
|
|
|
|
|
|
|
|
if block {
|
|
|
|
|
updNum, lists, toUpd, isNetErr = d.refreshFiltersArray(&d.Filters, force)
|
|
|
|
|
}
|
|
|
|
|
if allow {
|
|
|
|
|
updNumAl, listsAl, toUpdAl, isNetErrAl := d.refreshFiltersArray(&d.WhitelistFilters, force)
|
|
|
|
|
|
|
|
|
|
updNum += updNumAl
|
|
|
|
|
lists = append(lists, listsAl...)
|
|
|
|
|
toUpd = append(toUpd, toUpdAl...)
|
|
|
|
|
isNetErr = isNetErr || isNetErrAl
|
|
|
|
|
}
|
|
|
|
|
if isNetErr {
|
2020-02-28 12:40:16 +03:00
|
|
|
|
return 0, true
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if updNum != 0 {
|
|
|
|
|
d.EnableFilters(false)
|
2019-10-09 19:51:26 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
for i := range lists {
|
|
|
|
|
uf := &lists[i]
|
|
|
|
|
updated := toUpd[i]
|
2019-10-09 19:51:26 +03:00
|
|
|
|
if !updated {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-03-29 19:09:54 +03:00
|
|
|
|
|
|
|
|
|
p := uf.Path(d.DataDir)
|
|
|
|
|
err := os.Remove(p + ".old")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Debug("filtering: removing old filter file %q: %s", p, err)
|
|
|
|
|
}
|
2019-01-24 20:11:01 +03:00
|
|
|
|
}
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
2019-10-09 19:51:26 +03:00
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
log.Debug("filtering: update finished: %d lists updated", updNum)
|
2022-09-23 13:23:35 +03:00
|
|
|
|
|
|
|
|
|
return updNum, false
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
// isPrintableText returns true if data is printable UTF-8 text with CR, LF, TAB
|
|
|
|
|
// characters.
|
|
|
|
|
//
|
|
|
|
|
// TODO(e.burkov): Investigate the purpose of this and improve the
|
|
|
|
|
// implementation. Perhaps, use something from the unicode package.
|
|
|
|
|
func isPrintableText(data string) (ok bool) {
|
|
|
|
|
for _, c := range []byte(data) {
|
2019-09-04 19:37:27 +03:00
|
|
|
|
if (c >= ' ' && c != 0x7f) || c == '\n' || c == '\r' || c == '\t' {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2022-12-20 16:40:42 +03:00
|
|
|
|
|
2019-09-04 19:37:27 +03:00
|
|
|
|
return false
|
|
|
|
|
}
|
2022-12-20 16:40:42 +03:00
|
|
|
|
|
2019-09-04 19:37:27 +03:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
// scanLinesWithBreak is essentially a [bufio.ScanLines] which keeps trailing
|
|
|
|
|
// line breaks.
|
|
|
|
|
func scanLinesWithBreak(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
|
|
|
if atEOF && len(data) == 0 {
|
|
|
|
|
return 0, nil, nil
|
|
|
|
|
}
|
2020-03-17 15:00:40 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
if i := bytes.IndexByte(data, '\n'); i >= 0 {
|
|
|
|
|
return i + 1, data[0 : i+1], nil
|
|
|
|
|
}
|
2020-03-17 15:00:40 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
if atEOF {
|
|
|
|
|
return len(data), data, nil
|
|
|
|
|
}
|
2020-05-18 10:53:28 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
// Request more data.
|
|
|
|
|
return 0, nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parseFilter copies filter's content from src to dst and returns the number of
|
2023-03-29 19:09:54 +03:00
|
|
|
|
// rules, number of bytes written, checksum, and title of the parsed list. dst
|
|
|
|
|
// must not be nil.
|
2022-12-20 16:40:42 +03:00
|
|
|
|
func (d *DNSFilter) parseFilter(
|
|
|
|
|
src io.Reader,
|
|
|
|
|
dst io.Writer,
|
|
|
|
|
) (rulesNum, written int, checksum uint32, title string, err error) {
|
|
|
|
|
scanner := bufio.NewScanner(src)
|
|
|
|
|
scanner.Split(scanLinesWithBreak)
|
|
|
|
|
|
|
|
|
|
titleFound := false
|
|
|
|
|
for n := 0; scanner.Scan(); written += n {
|
|
|
|
|
line := scanner.Text()
|
|
|
|
|
var isRule bool
|
|
|
|
|
var likelyTitle string
|
|
|
|
|
isRule, likelyTitle, err = d.parseFilterLine(line, !titleFound, written == 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, written, 0, "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isRule {
|
|
|
|
|
rulesNum++
|
|
|
|
|
} else if likelyTitle != "" {
|
|
|
|
|
title, titleFound = likelyTitle, true
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
2020-05-18 10:53:28 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
checksum = crc32.Update(checksum, crc32.IEEETable, []byte(line))
|
|
|
|
|
|
|
|
|
|
n, err = dst.Write([]byte(line))
|
2020-05-18 10:53:28 +03:00
|
|
|
|
if err != nil {
|
2022-12-20 16:40:42 +03:00
|
|
|
|
return 0, written, 0, "", fmt.Errorf("writing filter line: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = scanner.Err(); err != nil {
|
|
|
|
|
return 0, written, 0, "", fmt.Errorf("scanning filter contents: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rulesNum, written, checksum, title, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parseFilterLine returns true if the passed line is a rule. line is
|
|
|
|
|
// considered a rule if it's not a comment and contains no title.
|
|
|
|
|
func (d *DNSFilter) parseFilterLine(
|
|
|
|
|
line string,
|
|
|
|
|
lookForTitle bool,
|
|
|
|
|
testHTML bool,
|
|
|
|
|
) (isRule bool, title string, err error) {
|
|
|
|
|
if !isPrintableText(line) {
|
|
|
|
|
return false, "", errors.Error("filter contains non-printable characters")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
if line == "" || line[0] == '#' {
|
|
|
|
|
return false, "", nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if testHTML && isHTML(line) {
|
|
|
|
|
return false, "", errors.Error("data is HTML, not plain text")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if line[0] == '!' && lookForTitle {
|
|
|
|
|
match := d.filterTitleRegexp.FindStringSubmatch(line)
|
|
|
|
|
if len(match) > 1 {
|
|
|
|
|
title = match[1]
|
2020-05-18 10:53:28 +03:00
|
|
|
|
}
|
2022-12-20 16:40:42 +03:00
|
|
|
|
|
|
|
|
|
return false, title, nil
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
return true, "", nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// isHTML returns true if the line contains HTML tags instead of plain text.
|
|
|
|
|
// line shouldn have no leading space symbols.
|
|
|
|
|
//
|
|
|
|
|
// TODO(ameshkov): It actually gives too much false-positives. Perhaps, just
|
|
|
|
|
// check if trimmed string begins with angle bracket.
|
|
|
|
|
func isHTML(line string) (ok bool) {
|
|
|
|
|
line = strings.ToLower(line)
|
|
|
|
|
|
|
|
|
|
return strings.HasPrefix(line, "<html") || strings.HasPrefix(line, "<!doctype")
|
2020-03-17 15:00:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
// update refreshes filter's content and a/mtimes of it's file.
|
|
|
|
|
func (d *DNSFilter) update(filter *FilterYAML) (b bool, err error) {
|
|
|
|
|
b, err = d.updateIntl(filter)
|
2020-03-17 15:00:40 +03:00
|
|
|
|
filter.LastUpdated = time.Now()
|
|
|
|
|
if !b {
|
2023-03-29 19:09:54 +03:00
|
|
|
|
chErr := os.Chtimes(
|
|
|
|
|
filter.Path(d.DataDir),
|
|
|
|
|
filter.LastUpdated,
|
|
|
|
|
filter.LastUpdated,
|
|
|
|
|
)
|
|
|
|
|
if chErr != nil {
|
|
|
|
|
log.Error("os.Chtimes(): %v", chErr)
|
2020-03-17 15:00:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
return b, err
|
2020-11-20 17:32:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 20:09:32 +03:00
|
|
|
|
// finalizeUpdate closes and gets rid of temporary file f with filter's content
|
|
|
|
|
// according to updated. It also saves new values of flt's name, rules number
|
|
|
|
|
// and checksum if sucсeeded.
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) finalizeUpdate(
|
|
|
|
|
file *os.File,
|
|
|
|
|
flt *FilterYAML,
|
2021-09-15 20:09:32 +03:00
|
|
|
|
updated bool,
|
|
|
|
|
name string,
|
|
|
|
|
rnum int,
|
|
|
|
|
cs uint32,
|
|
|
|
|
) (err error) {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
tmpFileName := file.Name()
|
2021-09-15 20:09:32 +03:00
|
|
|
|
|
|
|
|
|
// Close the file before renaming it because it's required on Windows.
|
|
|
|
|
//
|
|
|
|
|
// See https://github.com/adguardTeam/adGuardHome/issues/1553.
|
2022-12-20 16:40:42 +03:00
|
|
|
|
err = file.Close()
|
|
|
|
|
if err != nil {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return fmt.Errorf("closing temporary file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !updated {
|
|
|
|
|
log.Tracef("filter #%d from %s has no changes, skip", flt.ID, flt.URL)
|
|
|
|
|
|
|
|
|
|
return os.Remove(tmpFileName)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
fltPath := flt.Path(d.DataDir)
|
|
|
|
|
|
|
|
|
|
log.Printf("saving contents of filter #%d into %s", flt.ID, fltPath)
|
2021-09-15 20:09:32 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
// Don't use renamio or maybe packages, since those will require loading the
|
|
|
|
|
// whole filter content to the memory on Windows.
|
2023-03-29 19:09:54 +03:00
|
|
|
|
err = os.Rename(tmpFileName, fltPath)
|
2022-12-20 16:40:42 +03:00
|
|
|
|
if err != nil {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return errors.WithDeferred(err, os.Remove(tmpFileName))
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
flt.Name, flt.checksum, flt.RulesCount = aghalg.Coalesce(flt.Name, name), cs, rnum
|
2021-09-15 20:09:32 +03:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// updateIntl updates the flt rewriting it's actual file. It returns true if
|
|
|
|
|
// the actual update has been performed.
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) updateIntl(flt *FilterYAML) (ok bool, err error) {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
log.Tracef("downloading update for filter %d from %s", flt.ID, flt.URL)
|
|
|
|
|
|
|
|
|
|
var name string
|
|
|
|
|
var rnum, n int
|
|
|
|
|
var cs uint32
|
|
|
|
|
|
|
|
|
|
var tmpFile *os.File
|
2022-09-23 13:23:35 +03:00
|
|
|
|
tmpFile, err = os.CreateTemp(filepath.Join(d.DataDir, filterDir), "")
|
2020-11-20 17:32:41 +03:00
|
|
|
|
if err != nil {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return false, err
|
2020-11-20 17:32:41 +03:00
|
|
|
|
}
|
|
|
|
|
defer func() {
|
2023-03-29 19:09:54 +03:00
|
|
|
|
finErr := d.finalizeUpdate(tmpFile, flt, ok, name, rnum, cs)
|
|
|
|
|
if ok && finErr == nil {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
log.Printf("updated filter %d: %d bytes, %d rules", flt.ID, n, rnum)
|
2023-03-29 19:09:54 +03:00
|
|
|
|
|
|
|
|
|
return
|
2020-11-20 17:32:41 +03:00
|
|
|
|
}
|
2023-03-29 19:09:54 +03:00
|
|
|
|
|
|
|
|
|
err = errors.WithDeferred(err, finErr)
|
2020-11-20 17:32:41 +03:00
|
|
|
|
}()
|
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
// Change the default 0o600 permission to something more acceptable by end
|
|
|
|
|
// users.
|
2021-05-28 18:45:11 +03:00
|
|
|
|
//
|
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3198.
|
2021-09-15 20:09:32 +03:00
|
|
|
|
if err = tmpFile.Chmod(0o644); err != nil {
|
|
|
|
|
return false, fmt.Errorf("changing file mode: %w", err)
|
2021-05-28 18:45:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
var r io.Reader
|
2022-12-20 16:40:42 +03:00
|
|
|
|
if !filepath.IsAbs(flt.URL) {
|
2021-03-12 14:32:08 +03:00
|
|
|
|
var resp *http.Response
|
2022-09-23 13:23:35 +03:00
|
|
|
|
resp, err = d.HTTPClient.Get(flt.URL)
|
2020-11-20 17:32:41 +03:00
|
|
|
|
if err != nil {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
log.Printf("requesting filter from %s, skip: %s", flt.URL, err)
|
2021-05-24 17:28:11 +03:00
|
|
|
|
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return false, err
|
2020-11-20 17:32:41 +03:00
|
|
|
|
}
|
2021-05-24 17:28:11 +03:00
|
|
|
|
defer func() { err = errors.WithDeferred(err, resp.Body.Close()) }()
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
log.Printf("got status code %d from %s, skip", resp.StatusCode, flt.URL)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
return false, fmt.Errorf("got status code %d, want %d", resp.StatusCode, http.StatusOK)
|
2021-09-15 20:09:32 +03:00
|
|
|
|
}
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
r = resp.Body
|
2022-12-20 16:40:42 +03:00
|
|
|
|
} else {
|
2023-03-29 19:09:54 +03:00
|
|
|
|
var f *os.File
|
|
|
|
|
f, err = os.Open(flt.URL)
|
2022-12-20 16:40:42 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("open file: %w", err)
|
|
|
|
|
}
|
2023-03-29 19:09:54 +03:00
|
|
|
|
defer func() { err = errors.WithDeferred(err, f.Close()) }()
|
|
|
|
|
|
|
|
|
|
r = f
|
2019-03-15 16:49:10 +03:00
|
|
|
|
}
|
2020-04-07 13:02:28 +03:00
|
|
|
|
|
2023-03-29 19:09:54 +03:00
|
|
|
|
rnum, n, cs, name, err = d.parseFilter(r, tmpFile)
|
2020-03-17 15:00:40 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
return cs != flt.checksum && err == nil, err
|
2019-10-09 19:51:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 20:14:54 +03:00
|
|
|
|
// loads filter contents from the file in dataDir
|
2022-12-20 16:40:42 +03:00
|
|
|
|
func (d *DNSFilter) load(flt *FilterYAML) (err error) {
|
|
|
|
|
fileName := flt.Path(d.DataDir)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
log.Debug("filtering: loading filter %d from %s", flt.ID, fileName)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
file, err := os.Open(fileName)
|
2021-05-14 19:41:45 +03:00
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
// Do nothing, file doesn't exist.
|
|
|
|
|
return nil
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return fmt.Errorf("opening filter file: %w", err)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
2021-05-24 17:28:11 +03:00
|
|
|
|
defer func() { err = errors.WithDeferred(err, file.Close()) }()
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2021-05-14 19:41:45 +03:00
|
|
|
|
st, err := file.Stat()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("getting filter file stat: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
log.Debug("filtering: file %s, id %d, length %d", fileName, flt.ID, st.Size())
|
2021-05-14 19:41:45 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
rulesCount, _, checksum, _, err := d.parseFilter(file, io.Discard)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("parsing filter file: %w", err)
|
|
|
|
|
}
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2022-12-20 16:40:42 +03:00
|
|
|
|
flt.RulesCount, flt.checksum, flt.LastUpdated = rulesCount, checksum, st.ModTime()
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) EnableFilters(async bool) {
|
|
|
|
|
d.filtersMu.RLock()
|
|
|
|
|
defer d.filtersMu.RUnlock()
|
2021-05-24 14:48:42 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
d.enableFiltersLocked(async)
|
2021-05-24 14:48:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) enableFiltersLocked(async bool) {
|
2023-03-29 19:09:54 +03:00
|
|
|
|
filters := make([]Filter, 1, len(d.Filters)+len(d.WhitelistFilters)+1)
|
|
|
|
|
filters[0] = Filter{
|
2022-09-23 13:23:35 +03:00
|
|
|
|
ID: CustomListID,
|
|
|
|
|
Data: []byte(strings.Join(d.UserRules, "\n")),
|
2023-03-29 19:09:54 +03:00
|
|
|
|
}
|
2019-10-09 19:51:26 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
for _, filter := range d.Filters {
|
2021-05-12 20:04:50 +03:00
|
|
|
|
if !filter.Enabled {
|
|
|
|
|
continue
|
2020-02-26 19:58:25 +03:00
|
|
|
|
}
|
2019-10-09 19:51:26 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
filters = append(filters, Filter{
|
2021-05-12 20:04:50 +03:00
|
|
|
|
ID: filter.ID,
|
2022-09-23 13:23:35 +03:00
|
|
|
|
FilePath: filter.Path(d.DataDir),
|
2021-05-12 20:04:50 +03:00
|
|
|
|
})
|
|
|
|
|
}
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
var allowFilters []Filter
|
|
|
|
|
for _, filter := range d.WhitelistFilters {
|
2021-05-12 20:04:50 +03:00
|
|
|
|
if !filter.Enabled {
|
|
|
|
|
continue
|
2020-02-26 19:58:25 +03:00
|
|
|
|
}
|
2021-03-12 14:32:08 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
allowFilters = append(allowFilters, Filter{
|
2021-05-12 20:04:50 +03:00
|
|
|
|
ID: filter.ID,
|
2022-09-23 13:23:35 +03:00
|
|
|
|
FilePath: filter.Path(d.DataDir),
|
2021-05-12 20:04:50 +03:00
|
|
|
|
})
|
2019-10-09 19:51:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if err := d.SetFilters(filters, allowFilters, async); err != nil {
|
2021-05-12 20:04:50 +03:00
|
|
|
|
log.Debug("enabling filters: %s", err)
|
|
|
|
|
}
|
2021-05-24 14:48:42 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
d.SetEnabled(d.FilteringEnabled)
|
2019-10-09 19:51:26 +03:00
|
|
|
|
}
|