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"
|
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"
|
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
defer func(oldURL, oldName string, oldEnabled bool, oldUpdated time.Time) {
|
|
|
|
|
if err != nil {
|
|
|
|
|
filt.URL = oldURL
|
|
|
|
|
filt.Name = oldName
|
|
|
|
|
filt.Enabled = oldEnabled
|
|
|
|
|
filt.LastUpdated = oldUpdated
|
|
|
|
|
}
|
|
|
|
|
}(filt.URL, filt.Name, filt.Enabled, filt.LastUpdated)
|
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
|
|
|
|
|
// kick in when the filter is enabled. Consider making changing this
|
|
|
|
|
// behavior to be stricter.
|
|
|
|
|
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
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) filterAdd(flt FilterYAML) bool {
|
|
|
|
|
d.filtersMu.Lock()
|
|
|
|
|
defer d.filtersMu.Unlock()
|
2019-03-18 14:41:38 +03:00
|
|
|
|
|
|
|
|
|
// Check for duplicates
|
2022-10-21 20:14:43 +03:00
|
|
|
|
if d.filterExistsLocked(flt.URL) {
|
2020-02-26 19:58:25 +03:00
|
|
|
|
return false
|
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
|
|
|
|
}
|
2019-03-18 14:41:38 +03:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
// sync.Mutex.TryLock.
|
|
|
|
|
func (d *DNSFilter) tryRefreshFilters(block, allow, force bool) (updated int, isNetworkErr, ok bool) {
|
|
|
|
|
if ok = d.refreshLock.TryLock(); !ok {
|
|
|
|
|
return 0, false, ok
|
|
|
|
|
}
|
|
|
|
|
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
|
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
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
d.filtersMu.Lock()
|
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
|
|
|
|
|
}
|
|
|
|
|
f.LastUpdated = uf.LastUpdated
|
|
|
|
|
if !updated {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Info("Updated filter #%d. Rules: %d -> %d",
|
|
|
|
|
f.ID, f.RulesCount, uf.RulesCount)
|
|
|
|
|
f.Name = uf.Name
|
|
|
|
|
f.RulesCount = uf.RulesCount
|
|
|
|
|
f.checksum = uf.checksum
|
|
|
|
|
updateCount++
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
2022-09-23 13:23:35 +03:00
|
|
|
|
d.filtersMu.Unlock()
|
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
|
|
|
|
|
}
|
2022-09-23 13:23:35 +03:00
|
|
|
|
_ = os.Remove(uf.Path(d.DataDir) + ".old")
|
2019-01-24 20:11:01 +03:00
|
|
|
|
}
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
2019-10-09 19:51:26 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
log.Debug("filtering: update finished")
|
|
|
|
|
|
|
|
|
|
return updNum, false
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 19:37:27 +03:00
|
|
|
|
// Allows printable UTF-8 text with CR, LF, TAB characters
|
2020-04-07 23:29:03 +03:00
|
|
|
|
func isPrintableText(data []byte, len int) bool {
|
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
|
c := data[i]
|
2019-09-04 19:37:27 +03:00
|
|
|
|
if (c >= ' ' && c != 0x7f) || c == '\n' || c == '\r' || c == '\t' {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 20:14:54 +03:00
|
|
|
|
// A helper function that parses filter contents and returns a number of rules and a filter name (if there's any)
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) parseFilterContents(file io.Reader) (int, uint32, string) {
|
2018-11-28 20:14:54 +03:00
|
|
|
|
rulesCount := 0
|
|
|
|
|
name := ""
|
|
|
|
|
seenTitle := false
|
2020-03-17 15:00:40 +03:00
|
|
|
|
r := bufio.NewReader(file)
|
|
|
|
|
checksum := uint32(0)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
line, err := r.ReadString('\n')
|
|
|
|
|
checksum = crc32.Update(checksum, crc32.IEEETable, []byte(line))
|
|
|
|
|
|
|
|
|
|
line = strings.TrimSpace(line)
|
2019-03-15 16:02:48 +03:00
|
|
|
|
if len(line) == 0 {
|
2020-05-18 10:53:28 +03:00
|
|
|
|
//
|
|
|
|
|
} else if line[0] == '!' {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
m := d.filterTitleRegexp.FindAllStringSubmatch(line, -1)
|
2019-03-15 16:02:48 +03:00
|
|
|
|
if len(m) > 0 && len(m[0]) >= 2 && !seenTitle {
|
2018-11-28 20:14:54 +03:00
|
|
|
|
name = m[0][1]
|
|
|
|
|
seenTitle = true
|
|
|
|
|
}
|
2020-05-18 10:53:28 +03:00
|
|
|
|
|
2020-04-30 17:19:10 +03:00
|
|
|
|
} else if line[0] == '#' {
|
2020-05-18 10:53:28 +03:00
|
|
|
|
//
|
2019-03-15 16:02:48 +03:00
|
|
|
|
} else {
|
2018-11-28 20:14:54 +03:00
|
|
|
|
rulesCount++
|
|
|
|
|
}
|
2020-05-18 10:53:28 +03:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 15:00:40 +03:00
|
|
|
|
return rulesCount, checksum, name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Perform upgrade on a filter and update LastUpdated value
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) update(filter *FilterYAML) (bool, error) {
|
|
|
|
|
b, err := d.updateIntl(filter)
|
2020-03-17 15:00:40 +03:00
|
|
|
|
filter.LastUpdated = time.Now()
|
|
|
|
|
if !b {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
e := os.Chtimes(filter.Path(d.DataDir), filter.LastUpdated, filter.LastUpdated)
|
2020-03-17 15:00:40 +03:00
|
|
|
|
if e != nil {
|
|
|
|
|
log.Error("os.Chtimes(): %v", e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return b, err
|
2018-11-28 20:14:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) read(reader io.Reader, tmpFile *os.File, filter *FilterYAML) (int, error) {
|
2020-03-17 15:00:40 +03:00
|
|
|
|
htmlTest := true
|
|
|
|
|
firstChunk := make([]byte, 4*1024)
|
|
|
|
|
firstChunkLen := 0
|
|
|
|
|
buf := make([]byte, 64*1024)
|
|
|
|
|
total := 0
|
|
|
|
|
for {
|
2020-03-05 14:37:43 +03:00
|
|
|
|
n, err := reader.Read(buf)
|
2020-03-17 15:00:40 +03:00
|
|
|
|
total += n
|
|
|
|
|
|
|
|
|
|
if htmlTest {
|
2020-11-06 12:15:08 +03:00
|
|
|
|
num := len(firstChunk) - firstChunkLen
|
|
|
|
|
if n < num {
|
|
|
|
|
num = n
|
|
|
|
|
}
|
2020-03-17 15:00:40 +03:00
|
|
|
|
copied := copy(firstChunk[firstChunkLen:], buf[:num])
|
|
|
|
|
firstChunkLen += copied
|
|
|
|
|
|
|
|
|
|
if firstChunkLen == len(firstChunk) || err == io.EOF {
|
2020-04-07 23:29:03 +03:00
|
|
|
|
if !isPrintableText(firstChunk, firstChunkLen) {
|
2020-11-20 17:32:41 +03:00
|
|
|
|
return total, fmt.Errorf("data contains non-printable characters")
|
2020-03-17 15:00:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s := strings.ToLower(string(firstChunk))
|
2020-11-06 12:15:08 +03:00
|
|
|
|
if strings.Contains(s, "<html") || strings.Contains(s, "<!doctype") {
|
2020-11-20 17:32:41 +03:00
|
|
|
|
return total, fmt.Errorf("data is HTML, not plain text")
|
2020-03-17 15:00:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
htmlTest = false
|
|
|
|
|
firstChunk = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 13:02:28 +03:00
|
|
|
|
_, err2 := tmpFile.Write(buf[:n])
|
2020-03-17 15:00:40 +03:00
|
|
|
|
if err2 != nil {
|
2020-11-20 17:32:41 +03:00
|
|
|
|
return total, err2
|
2020-03-17 15:00:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err == io.EOF {
|
2020-11-20 17:32:41 +03:00
|
|
|
|
return total, nil
|
2020-03-17 15:00:40 +03:00
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Couldn't fetch filter contents from URL %s, skipping: %s", filter.URL, err)
|
2020-11-20 17:32:41 +03:00
|
|
|
|
return total, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-09-23 13:23:35 +03:00
|
|
|
|
if err = file.Close(); 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)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
log.Printf("saving filter %d contents to: %s", flt.ID, flt.Path(d.DataDir))
|
2021-09-15 20:09:32 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if err = os.Rename(tmpFileName, flt.Path(d.DataDir)); err != nil {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return errors.WithDeferred(err, os.Remove(tmpFileName))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flt.Name = stringutil.Coalesce(flt.Name, name)
|
|
|
|
|
flt.checksum = cs
|
|
|
|
|
flt.RulesCount = rnum
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// processUpdate copies filter's content from src to dst and returns the name,
|
|
|
|
|
// rules number, and checksum for it. It also returns the number of bytes read
|
|
|
|
|
// from src.
|
2022-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) processUpdate(
|
2021-09-15 20:09:32 +03:00
|
|
|
|
src io.Reader,
|
|
|
|
|
dst *os.File,
|
2022-09-23 13:23:35 +03:00
|
|
|
|
flt *FilterYAML,
|
2021-09-15 20:09:32 +03:00
|
|
|
|
) (name string, rnum int, cs uint32, n int, err error) {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
if n, err = d.read(src, dst, flt); err != nil {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return "", 0, 0, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err = dst.Seek(0, io.SeekStart); err != nil {
|
|
|
|
|
return "", 0, 0, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
rnum, cs, name = d.parseFilterContents(dst)
|
2021-09-15 20:09:32 +03:00
|
|
|
|
|
|
|
|
|
return name, rnum, cs, n, nil
|
|
|
|
|
}
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
2021-09-15 20:09:32 +03:00
|
|
|
|
// 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() {
|
2022-09-23 13:23:35 +03:00
|
|
|
|
err = errors.WithDeferred(err, d.finalizeUpdate(tmpFile, flt, ok, name, rnum, cs))
|
2021-09-15 20:09:32 +03:00
|
|
|
|
ok = ok && err == nil
|
|
|
|
|
if ok {
|
|
|
|
|
log.Printf("updated filter %d: %d bytes, %d rules", flt.ID, n, rnum)
|
2020-11-20 17:32:41 +03:00
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2021-05-28 18:45:11 +03:00
|
|
|
|
// Change the default 0o600 permission to something more acceptable by
|
|
|
|
|
// end users.
|
|
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 20:09:32 +03:00
|
|
|
|
var r io.Reader
|
|
|
|
|
if filepath.IsAbs(flt.URL) {
|
|
|
|
|
var file io.ReadCloser
|
|
|
|
|
file, err = os.Open(flt.URL)
|
2020-11-20 17:32:41 +03:00
|
|
|
|
if err != nil {
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return false, fmt.Errorf("open file: %w", err)
|
2020-03-17 15:00:40 +03:00
|
|
|
|
}
|
2021-09-15 20:09:32 +03:00
|
|
|
|
defer func() { err = errors.WithDeferred(err, file.Close()) }()
|
2021-03-12 14:32:08 +03:00
|
|
|
|
|
2021-09-15 20:09:32 +03:00
|
|
|
|
r = file
|
2020-11-20 17:32:41 +03:00
|
|
|
|
} else {
|
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
|
|
|
|
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return false, fmt.Errorf("got status code != 200: %d", resp.StatusCode)
|
|
|
|
|
}
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2021-09-15 20:09:32 +03:00
|
|
|
|
r = resp.Body
|
2019-03-15 16:49:10 +03:00
|
|
|
|
}
|
2020-04-07 13:02:28 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
name, rnum, cs, n, err = d.processUpdate(r, tmpFile, flt)
|
2020-03-17 15:00:40 +03:00
|
|
|
|
|
2021-09-15 20:09:32 +03:00
|
|
|
|
return cs != flt.checksum, 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-09-23 13:23:35 +03:00
|
|
|
|
func (d *DNSFilter) load(filter *FilterYAML) (err error) {
|
|
|
|
|
filterFilePath := filter.Path(d.DataDir)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
log.Tracef("filtering: loading filter %d from %s", filter.ID, filterFilePath)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
2020-03-17 15:00:40 +03:00
|
|
|
|
file, err := os.Open(filterFilePath)
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Tracef("filtering: File %s, id %d, length %d", filterFilePath, filter.ID, st.Size())
|
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
|
rulesCount, checksum, _ := d.parseFilterContents(file)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
|
|
|
|
filter.RulesCount = rulesCount
|
2020-03-17 15:00:40 +03:00
|
|
|
|
filter.checksum = checksum
|
2021-05-14 19:41:45 +03:00
|
|
|
|
filter.LastUpdated = 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) {
|
|
|
|
|
filters := []Filter{{
|
|
|
|
|
ID: CustomListID,
|
|
|
|
|
Data: []byte(strings.Join(d.UserRules, "\n")),
|
2021-05-12 20:04:50 +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
|
|
|
|
}
|