AdGuardHome/internal/filtering/filter.go

630 lines
15 KiB
Go
Raw Normal View History

2022-09-29 15:36:01 +01:00
package filtering
import (
"fmt"
"io"
Pull request: 2271 handle nolint Merge in DNS/adguard-home from 2271-handle-nolint to master Closes #2271. Squashed commit of the following: commit fde5c8795ac79e1f7d02ba8c8e369b5a724a000e Merge: fc2acd898 642dcd647 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 20 17:12:28 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit fc2acd89871de08c39e80ace9e5bb8a7acb7afba Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 17 11:55:29 2020 +0300 dnsforward: fix test output strings commit c4ebae6ea9c293bad239519c44ca5a6c576bb921 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 22:43:20 2020 +0300 dnsfilter: make package pass tests commit f2d98c6acabd8977f3b1b361987eaa31eb6eb9ad Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 20:05:00 2020 +0300 querylog: make decoding pass tests commit ab5850d24c50d53b8393f2de448cc340241351d7 Merge: 6ed2066bf 8a9c6e8a0 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:48:31 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit 6ed2066bf567e13dd14cfa16fc7b109b59fa39ef Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:13:45 2020 +0300 home: fix tests naming commit af691081fb02b7500a746b16492f01f7f9befe9a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:15:49 2020 +0300 home: impove code quality commit 2914cd3cd23ef2a1964116baab9187d89b377f86 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 15:46:39 2020 +0300 * querylog: remove useless check commit 9996840650e784ccc76d1f29964560435ba27dc7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 13:18:34 2020 +0300 * all: fix noticed defects commit 2b15293e59337f70302fbc0db81ebb26bee0bed2 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 20:15:53 2020 +0300 * stats: remove last nolint directive commit b2e1ddf7b58196a2fdbf879f084edb41ca1aa1eb Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:35:41 2020 +0300 * all: remove another nolint directive commit c6fc5cfcc9c95ab9e570a95ab41c3e5c0125e62e Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:11:28 2020 +0300 * querylog: remove nolint directive commit 226ddbf2c92f737f085b44a4ddf6daec7b602153 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:35:26 2020 +0300 * home: remove nolint directive commit 2ea3086ad41e9003282add7e996ae722d72d878b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:13:57 2020 +0300 * home: reduce cyclomatic complexity of run function commit f479b480c48e0bb832ddef8f57586f56b8a55bab Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 15:35:46 2020 +0300 * home: use crypto/rand instead of math/rand commit a28d4a53e3b930136b036606fc7e78404f1d208b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 14:11:07 2020 +0300 * dnsforward: remove gocyclo nolint directive commit 64a0a324cc2b20614ceec3ccc6505e960fe526e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 11:45:49 2020 +0300 all *: remove some nolint directives Updates #2271.
2020-11-20 14:32:41 +00:00
"net/http"
"os"
"path/filepath"
2024-03-12 14:45:11 +00:00
"slices"
"strconv"
"strings"
"time"
2023-07-26 11:18:44 +01:00
"github.com/AdguardTeam/AdGuardHome/internal/aghrenameio"
2023-07-12 13:13:31 +01:00
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
2024-04-02 18:22:19 +01:00
"github.com/AdguardTeam/golibs/container"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
)
2022-09-29 15:36:01 +01:00
// filterDir is the subdirectory of a data directory to store downloaded
// filters.
const filterDir = "filters"
2023-07-12 13:13:31 +01:00
// FilterYAML represents a filter list in the configuration file.
2022-09-29 15:36:01 +01:00
//
2023-07-12 13:13:31 +01:00
// TODO(e.burkov): Investigate if the field ordering is important.
2022-09-29 15:36:01 +01:00
type FilterYAML struct {
Enabled bool
URL string // URL or a file path
Name string `yaml:"name"`
RulesCount int `yaml:"-"`
LastUpdated time.Time `yaml:"-"`
checksum uint32 // checksum of the file data
white bool
2022-09-29 15:36:01 +01: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 {
2024-04-02 18:22:19 +01:00
return filepath.Join(
dataDir,
filterDir,
strconv.FormatInt(int64(filter.ID), 10)+".txt")
}
2023-09-07 15:13:48 +01:00
// ensureName sets provided title or default name for the filter if it doesn't
// have name already.
func (filter *FilterYAML) ensureName(title string) {
if filter.Name != "" {
return
}
if title != "" {
filter.Name = title
return
}
filter.Name = fmt.Sprintf("List %d", filter.ID)
}
const (
2022-11-02 13:18:02 +00: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"
)
2022-11-02 13:18:02 +00: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) {
2023-09-07 15:13:48 +01:00
d.conf.filtersMu.Lock()
defer d.conf.filtersMu.Unlock()
2023-09-07 15:13:48 +01:00
filters := d.conf.Filters
2022-11-02 13:18:02 +00:00
if isAllowlist {
2023-09-07 15:13:48 +01:00
filters = d.conf.WhitelistFilters
}
2023-07-26 11:18:44 +01:00
i := slices.IndexFunc(filters, func(flt FilterYAML) bool { return flt.URL == listURL })
2022-09-29 15:36:01 +01:00
if i == -1 {
2022-11-02 13:18:02 +00:00
return false, errFilterNotExist
2022-09-29 15:36:01 +01:00
}
2023-07-26 11:18:44 +01:00
flt := &filters[i]
2022-11-02 13:18:02 +00:00
log.Debug(
"filtering: set name to %q, url to %s, enabled to %t for filter %s",
newList.Name,
newList.URL,
newList.Enabled,
2023-07-26 11:18:44 +01:00
flt.URL,
2022-11-02 13:18:02 +00:00
)
2023-01-19 12:04:46 +00:00
defer func(oldURL, oldName string, oldEnabled bool, oldUpdated time.Time, oldRulesCount int) {
2022-11-02 13:18:02 +00:00
if err != nil {
2023-07-26 11:18:44 +01:00
flt.URL = oldURL
flt.Name = oldName
flt.Enabled = oldEnabled
flt.LastUpdated = oldUpdated
flt.RulesCount = oldRulesCount
2022-11-02 13:18:02 +00:00
}
2023-07-26 11:18:44 +01:00
}(flt.URL, flt.Name, flt.Enabled, flt.LastUpdated, flt.RulesCount)
2023-07-26 11:18:44 +01:00
flt.Name = newList.Name
2022-09-29 15:36:01 +01:00
2023-07-26 11:18:44 +01:00
if flt.URL != newList.URL {
2022-11-02 13:18:02 +00:00
if d.filterExistsLocked(newList.URL) {
return false, errFilterExists
}
2022-11-02 13:18:02 +00:00
shouldRestart = true
2023-07-26 11:18:44 +01:00
flt.URL = newList.URL
flt.LastUpdated = time.Time{}
flt.unload()
2022-09-29 15:36:01 +01:00
}
2023-07-26 11:18:44 +01:00
if flt.Enabled != newList.Enabled {
flt.Enabled = newList.Enabled
2022-11-02 13:18:02 +00:00
shouldRestart = true
}
2023-07-26 11:18:44 +01:00
if flt.Enabled {
2022-11-02 13:18:02 +00:00
if shouldRestart {
// Download the filter contents.
2023-07-26 11:18:44 +01:00
shouldRestart, err = d.update(flt)
}
2022-11-02 13:18:02 +00: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
2023-01-19 12:04:46 +00:00
// kick in when the filter is enabled. Consider changing this behavior
// to be stricter.
2023-07-26 11:18:44 +01:00
flt.unload()
}
2022-09-29 15:36:01 +01:00
2022-11-02 13:18:02 +00:00
return shouldRestart, err
}
2022-11-02 13:18:02 +00: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) {
2023-09-07 15:13:48 +01:00
d.conf.filtersMu.RLock()
defer d.conf.filtersMu.RUnlock()
2022-09-29 15:36:01 +01:00
2022-11-02 13:18:02 +00:00
r := d.filterExistsLocked(url)
2022-09-29 15:36:01 +01:00
return r
}
2022-11-02 13:18:02 +00: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) {
2023-09-07 15:13:48 +01:00
for _, f := range d.conf.Filters {
if f.URL == url {
return true
}
}
2022-11-02 13:18:02 +00:00
2023-09-07 15:13:48 +01:00
for _, f := range d.conf.WhitelistFilters {
if f.URL == url {
return true
}
}
2022-11-02 13:18:02 +00:00
return false
}
// Add a filter
// Return FALSE if a filter with this URL exists
2023-04-12 12:48:42 +01:00
func (d *DNSFilter) filterAdd(flt FilterYAML) (err error) {
// Defer annotating to unlock sooner.
defer func() { err = errors.Annotate(err, "adding filter: %w") }()
2023-09-07 15:13:48 +01:00
d.conf.filtersMu.Lock()
defer d.conf.filtersMu.Unlock()
2023-04-12 12:48:42 +01:00
// Check for duplicates.
2022-11-02 13:18:02 +00:00
if d.filterExistsLocked(flt.URL) {
2023-04-12 12:48:42 +01:00
return errFilterExists
}
2022-09-29 15:36:01 +01:00
if flt.white {
2023-09-07 15:13:48 +01:00
d.conf.WhitelistFilters = append(d.conf.WhitelistFilters, flt)
} else {
2023-09-07 15:13:48 +01:00
d.conf.Filters = append(d.conf.Filters, flt)
}
2023-04-12 12:48:42 +01:00
return nil
}
2019-03-15 16:41:45 +00:00
// Load filters from the disk
// And if any filter has zero ID, assign a new one
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) loadFilters(array []FilterYAML) {
for i := range array {
filter := &array[i] // otherwise we're operating on a copy
2019-03-15 16:41:45 +00:00
if filter.ID == 0 {
2024-04-02 18:22:19 +01:00
newID := d.idGen.next()
log.Info("filtering: warning: filter at index %d has no id; assigning to %d", i, newID)
filter.ID = newID
2019-03-15 16:41:45 +00:00
}
if !filter.Enabled {
// No need to load a filter that is not enabled
continue
}
2022-09-29 15:36:01 +01:00
err := d.load(filter)
2019-03-15 16:41:45 +00:00
if err != nil {
2023-07-12 13:13:31 +01:00
log.Error("filtering: loading filter %d: %s", filter.ID, err)
2019-03-15 16:41:45 +00:00
}
}
}
2022-09-29 15:36:01 +01:00
func deduplicateFilters(filters []FilterYAML) (deduplicated []FilterYAML) {
2024-04-02 18:22:19 +01:00
urls := container.NewMapSet[string]()
2022-09-29 15:36:01 +01:00
lastIdx := 0
for _, filter := range filters {
if !urls.Has(filter.URL) {
urls.Add(filter.URL)
filters[lastIdx] = filter
lastIdx++
}
}
2022-09-29 15:36:01 +01:00
return filters[:lastIdx]
}
2022-09-29 15:36:01 +01:00
// tryRefreshFilters is like [refreshFilters], but backs down if the update is
// already going on.
2022-09-07 16:03:18 +01:00
//
2022-09-29 15:36:01 +01:00
// TODO(e.burkov): Get rid of the concurrency pattern which requires the
2023-01-19 12:04:46 +00:00
// [sync.Mutex.TryLock].
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) tryRefreshFilters(block, allow, force bool) (updated int, isNetworkErr, ok bool) {
if ok = d.refreshLock.TryLock(); !ok {
2023-01-19 12:04:46 +00:00
return 0, false, false
2022-09-29 15:36:01 +01:00
}
defer d.refreshLock.Unlock()
updated, isNetworkErr = d.refreshFiltersIntl(block, allow, force)
return updated, isNetworkErr, ok
}
2022-09-29 15:36:01 +01:00
// listsToUpdate returns the slice of filter lists that could be updated.
func (d *DNSFilter) listsToUpdate(filters *[]FilterYAML, force bool) (toUpd []FilterYAML) {
now := time.Now()
2022-09-29 15:36:01 +01:00
2023-09-07 15:13:48 +01:00
d.conf.filtersMu.RLock()
defer d.conf.filtersMu.RUnlock()
2022-09-29 15:36:01 +01:00
for i := range *filters {
2022-09-29 15:36:01 +01:00
flt := &(*filters)[i] // otherwise we will be operating on a copy
2022-09-29 15:36:01 +01:00
if !flt.Enabled {
continue
}
2022-09-29 15:36:01 +01:00
if !force {
2023-09-07 15:13:48 +01:00
exp := flt.LastUpdated.Add(time.Duration(d.conf.FiltersUpdateIntervalHours) * time.Hour)
2022-09-29 15:36:01 +01:00
if now.Before(exp) {
continue
}
}
2022-09-29 15:36:01 +01:00
toUpd = append(toUpd, FilterYAML{
Filter: Filter{
ID: flt.ID,
},
URL: flt.URL,
Name: flt.Name,
checksum: flt.checksum,
})
}
2022-09-29 15:36:01 +01: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 11:07:11 +00:00
if len(updateFilters) == 0 {
return 0, nil, nil, false
2020-01-28 11:07:11 +00:00
}
2023-07-26 11:18:44 +01:00
failNum := 0
for i := range updateFilters {
uf := &updateFilters[i]
2022-09-29 15:36:01 +01:00
updated, err := d.update(uf)
2019-07-16 13:29:36 +01:00
updateFlags = append(updateFlags, updated)
if err != nil {
2023-07-26 11:18:44 +01:00
failNum++
log.Error("filtering: updating filter from url %q: %s\n", uf.URL, err)
2023-07-12 13:13:31 +01:00
continue
}
}
2023-07-26 11:18:44 +01:00
if failNum == len(updateFilters) {
return 0, nil, nil, true
}
updateCount := 0
2023-04-12 12:48:42 +01:00
2023-09-07 15:13:48 +01:00
d.conf.filtersMu.Lock()
defer d.conf.filtersMu.Unlock()
2023-04-12 12:48:42 +01:00
for i := range updateFilters {
uf := &updateFilters[i]
updated := updateFlags[i]
for k := range *filters {
f := &(*filters)[k]
if f.ID != uf.ID || f.URL != uf.URL {
continue
}
2023-04-12 12:48:42 +01:00
f.LastUpdated = uf.LastUpdated
if !updated {
continue
}
2023-07-12 13:13:31 +01:00
log.Info(
"filtering: updated filter %d; rule count: %d (was %d)",
f.ID,
uf.RulesCount,
f.RulesCount,
)
f.Name = uf.Name
f.RulesCount = uf.RulesCount
f.checksum = uf.checksum
updateCount++
}
}
return updateCount, updateFilters, updateFlags, false
}
2022-09-29 15:36:01 +01:00
// refreshFiltersIntl checks filters and updates them if necessary. If force is
// true, it ignores the filter.LastUpdated field value.
//
// Algorithm:
//
2022-09-07 16:03:18 +01: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-29 15:36:01 +01:00
// refreshFiltersIntl returns the number of updated filters. It also returns
// true if there was a network error and nothing could be updated.
2022-09-07 16:03:18 +01:00
//
// TODO(a.garipov, e.burkov): What the hell?
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) refreshFiltersIntl(block, allow, force bool) (int, bool) {
updNum := 0
2023-07-12 13:13:31 +01:00
log.Debug("filtering: starting updating")
defer func() { log.Debug("filtering: finished updating, %d updated", updNum) }()
2022-09-29 15:36:01 +01:00
var lists []FilterYAML
var toUpd []bool
isNetErr := false
if block {
2023-09-07 15:13:48 +01:00
updNum, lists, toUpd, isNetErr = d.refreshFiltersArray(&d.conf.Filters, force)
2022-09-29 15:36:01 +01:00
}
if allow {
2023-09-07 15:13:48 +01:00
updNumAl, listsAl, toUpdAl, isNetErrAl := d.refreshFiltersArray(&d.conf.WhitelistFilters, force)
2022-09-29 15:36:01 +01:00
updNum += updNumAl
lists = append(lists, listsAl...)
toUpd = append(toUpd, toUpdAl...)
isNetErr = isNetErr || isNetErrAl
}
if isNetErr {
return 0, true
}
2022-09-29 15:36:01 +01:00
if updNum != 0 {
d.EnableFilters(false)
2022-09-29 15:36:01 +01:00
for i := range lists {
uf := &lists[i]
updated := toUpd[i]
if !updated {
continue
}
2023-04-12 12:48:42 +01:00
2023-09-07 15:13:48 +01:00
p := uf.Path(d.conf.DataDir)
2023-04-12 12:48:42 +01:00
err := os.Remove(p + ".old")
if err != nil {
log.Debug("filtering: removing old filter file %q: %s", p, err)
}
2019-01-24 17:11:01 +00:00
}
}
2022-09-29 15:36:01 +01:00
return updNum, false
}
2023-04-12 12:48:42 +01: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)
filter.LastUpdated = time.Now()
if !b {
2023-04-12 12:48:42 +01:00
chErr := os.Chtimes(
2023-09-07 15:13:48 +01:00
filter.Path(d.conf.DataDir),
2023-04-12 12:48:42 +01:00
filter.LastUpdated,
filter.LastUpdated,
)
if chErr != nil {
2023-07-12 13:13:31 +01:00
log.Error("filtering: os.Chtimes(): %s", chErr)
}
}
2023-01-19 12:04:46 +00:00
return b, err
Pull request: 2271 handle nolint Merge in DNS/adguard-home from 2271-handle-nolint to master Closes #2271. Squashed commit of the following: commit fde5c8795ac79e1f7d02ba8c8e369b5a724a000e Merge: fc2acd898 642dcd647 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 20 17:12:28 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit fc2acd89871de08c39e80ace9e5bb8a7acb7afba Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 17 11:55:29 2020 +0300 dnsforward: fix test output strings commit c4ebae6ea9c293bad239519c44ca5a6c576bb921 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 22:43:20 2020 +0300 dnsfilter: make package pass tests commit f2d98c6acabd8977f3b1b361987eaa31eb6eb9ad Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 20:05:00 2020 +0300 querylog: make decoding pass tests commit ab5850d24c50d53b8393f2de448cc340241351d7 Merge: 6ed2066bf 8a9c6e8a0 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:48:31 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit 6ed2066bf567e13dd14cfa16fc7b109b59fa39ef Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:13:45 2020 +0300 home: fix tests naming commit af691081fb02b7500a746b16492f01f7f9befe9a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:15:49 2020 +0300 home: impove code quality commit 2914cd3cd23ef2a1964116baab9187d89b377f86 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 15:46:39 2020 +0300 * querylog: remove useless check commit 9996840650e784ccc76d1f29964560435ba27dc7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 13:18:34 2020 +0300 * all: fix noticed defects commit 2b15293e59337f70302fbc0db81ebb26bee0bed2 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 20:15:53 2020 +0300 * stats: remove last nolint directive commit b2e1ddf7b58196a2fdbf879f084edb41ca1aa1eb Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:35:41 2020 +0300 * all: remove another nolint directive commit c6fc5cfcc9c95ab9e570a95ab41c3e5c0125e62e Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:11:28 2020 +0300 * querylog: remove nolint directive commit 226ddbf2c92f737f085b44a4ddf6daec7b602153 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:35:26 2020 +0300 * home: remove nolint directive commit 2ea3086ad41e9003282add7e996ae722d72d878b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:13:57 2020 +0300 * home: reduce cyclomatic complexity of run function commit f479b480c48e0bb832ddef8f57586f56b8a55bab Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 15:35:46 2020 +0300 * home: use crypto/rand instead of math/rand commit a28d4a53e3b930136b036606fc7e78404f1d208b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 14:11:07 2020 +0300 * dnsforward: remove gocyclo nolint directive commit 64a0a324cc2b20614ceec3ccc6505e960fe526e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 11:45:49 2020 +0300 all *: remove some nolint directives Updates #2271.
2020-11-20 14:32:41 +00:00
}
2023-07-26 11:18:44 +01:00
// updateIntl updates the flt rewriting it's actual file. It returns true if
// the actual update has been performed.
func (d *DNSFilter) updateIntl(flt *FilterYAML) (ok bool, err error) {
log.Debug("filtering: downloading update for filter %d from %q", flt.ID, flt.URL)
var res *rulelist.ParseResult
// Change the default 0o600 permission to something more acceptable by end
// users.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/3198.
2023-09-07 15:13:48 +01:00
tmpFile, err := aghrenameio.NewPendingFile(flt.Path(d.conf.DataDir), 0o644)
2023-07-26 11:18:44 +01:00
if err != nil {
return false, err
}
defer func() { err = d.finalizeUpdate(tmpFile, flt, res, err, ok) }()
r, err := d.reader(flt.URL)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return false, err
}
defer func() { err = errors.WithDeferred(err, r.Close()) }()
2023-10-11 15:31:41 +01:00
bufPtr := d.bufPool.Get()
2023-07-26 11:18:44 +01:00
defer d.bufPool.Put(bufPtr)
p := rulelist.NewParser()
res, err = p.Parse(tmpFile, r, *bufPtr)
return res.Checksum != flt.checksum && err == nil, err
}
// 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
2023-07-12 13:13:31 +01:00
// and checksum if succeeded.
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) finalizeUpdate(
2023-07-26 11:18:44 +01:00
file aghrenameio.PendingFile,
2022-09-29 15:36:01 +01:00
flt *FilterYAML,
2023-07-12 13:13:31 +01:00
res *rulelist.ParseResult,
2023-07-26 11:18:44 +01:00
returned error,
updated bool,
) (err error) {
2023-07-26 11:18:44 +01:00
id := flt.ID
if !updated {
2023-07-26 11:18:44 +01:00
if returned == nil {
log.Debug("filtering: filter %d from url %q has no changes, skipping", id, flt.URL)
}
2023-07-26 11:18:44 +01:00
return errors.WithDeferred(returned, file.Cleanup())
}
2023-09-07 15:13:48 +01:00
log.Info("filtering: saving contents of filter %d into %q", id, flt.Path(d.conf.DataDir))
2023-04-12 12:48:42 +01:00
2023-07-26 11:18:44 +01:00
err = file.CloseReplace()
2023-01-19 12:04:46 +00:00
if err != nil {
2023-07-26 11:18:44 +01:00
return fmt.Errorf("finalizing update: %w", err)
}
2023-07-26 11:18:44 +01:00
rulesCount := res.RulesCount
log.Info("filtering: updated filter %d: %d bytes, %d rules", id, res.BytesWritten, rulesCount)
2023-09-07 15:13:48 +01:00
flt.ensureName(res.Title)
2023-07-26 11:18:44 +01:00
flt.checksum = res.Checksum
flt.RulesCount = rulesCount
return nil
}
2023-07-26 11:18:44 +01:00
// reader returns an io.ReadCloser reading filtering-rule list data form either
// a file on the filesystem or the filter's HTTP URL.
func (d *DNSFilter) reader(fltURL string) (r io.ReadCloser, err error) {
if !filepath.IsAbs(fltURL) {
r, err = d.readerFromURL(fltURL)
if err != nil {
return nil, fmt.Errorf("reading from url: %w", err)
}
2023-07-26 11:18:44 +01:00
return r, nil
}
2023-07-26 11:18:44 +01:00
r, err = os.Open(fltURL)
Pull request: 2271 handle nolint Merge in DNS/adguard-home from 2271-handle-nolint to master Closes #2271. Squashed commit of the following: commit fde5c8795ac79e1f7d02ba8c8e369b5a724a000e Merge: fc2acd898 642dcd647 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 20 17:12:28 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit fc2acd89871de08c39e80ace9e5bb8a7acb7afba Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 17 11:55:29 2020 +0300 dnsforward: fix test output strings commit c4ebae6ea9c293bad239519c44ca5a6c576bb921 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 22:43:20 2020 +0300 dnsfilter: make package pass tests commit f2d98c6acabd8977f3b1b361987eaa31eb6eb9ad Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 20:05:00 2020 +0300 querylog: make decoding pass tests commit ab5850d24c50d53b8393f2de448cc340241351d7 Merge: 6ed2066bf 8a9c6e8a0 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:48:31 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit 6ed2066bf567e13dd14cfa16fc7b109b59fa39ef Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:13:45 2020 +0300 home: fix tests naming commit af691081fb02b7500a746b16492f01f7f9befe9a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:15:49 2020 +0300 home: impove code quality commit 2914cd3cd23ef2a1964116baab9187d89b377f86 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 15:46:39 2020 +0300 * querylog: remove useless check commit 9996840650e784ccc76d1f29964560435ba27dc7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 13:18:34 2020 +0300 * all: fix noticed defects commit 2b15293e59337f70302fbc0db81ebb26bee0bed2 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 20:15:53 2020 +0300 * stats: remove last nolint directive commit b2e1ddf7b58196a2fdbf879f084edb41ca1aa1eb Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:35:41 2020 +0300 * all: remove another nolint directive commit c6fc5cfcc9c95ab9e570a95ab41c3e5c0125e62e Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:11:28 2020 +0300 * querylog: remove nolint directive commit 226ddbf2c92f737f085b44a4ddf6daec7b602153 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:35:26 2020 +0300 * home: remove nolint directive commit 2ea3086ad41e9003282add7e996ae722d72d878b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:13:57 2020 +0300 * home: reduce cyclomatic complexity of run function commit f479b480c48e0bb832ddef8f57586f56b8a55bab Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 15:35:46 2020 +0300 * home: use crypto/rand instead of math/rand commit a28d4a53e3b930136b036606fc7e78404f1d208b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 14:11:07 2020 +0300 * dnsforward: remove gocyclo nolint directive commit 64a0a324cc2b20614ceec3ccc6505e960fe526e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 11:45:49 2020 +0300 all *: remove some nolint directives Updates #2271.
2020-11-20 14:32:41 +00:00
if err != nil {
2023-07-26 11:18:44 +01:00
return nil, fmt.Errorf("opening file: %w", err)
Pull request: 2271 handle nolint Merge in DNS/adguard-home from 2271-handle-nolint to master Closes #2271. Squashed commit of the following: commit fde5c8795ac79e1f7d02ba8c8e369b5a724a000e Merge: fc2acd898 642dcd647 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 20 17:12:28 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit fc2acd89871de08c39e80ace9e5bb8a7acb7afba Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 17 11:55:29 2020 +0300 dnsforward: fix test output strings commit c4ebae6ea9c293bad239519c44ca5a6c576bb921 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 22:43:20 2020 +0300 dnsfilter: make package pass tests commit f2d98c6acabd8977f3b1b361987eaa31eb6eb9ad Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 20:05:00 2020 +0300 querylog: make decoding pass tests commit ab5850d24c50d53b8393f2de448cc340241351d7 Merge: 6ed2066bf 8a9c6e8a0 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:48:31 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit 6ed2066bf567e13dd14cfa16fc7b109b59fa39ef Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:13:45 2020 +0300 home: fix tests naming commit af691081fb02b7500a746b16492f01f7f9befe9a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:15:49 2020 +0300 home: impove code quality commit 2914cd3cd23ef2a1964116baab9187d89b377f86 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 15:46:39 2020 +0300 * querylog: remove useless check commit 9996840650e784ccc76d1f29964560435ba27dc7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 13:18:34 2020 +0300 * all: fix noticed defects commit 2b15293e59337f70302fbc0db81ebb26bee0bed2 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 20:15:53 2020 +0300 * stats: remove last nolint directive commit b2e1ddf7b58196a2fdbf879f084edb41ca1aa1eb Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:35:41 2020 +0300 * all: remove another nolint directive commit c6fc5cfcc9c95ab9e570a95ab41c3e5c0125e62e Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:11:28 2020 +0300 * querylog: remove nolint directive commit 226ddbf2c92f737f085b44a4ddf6daec7b602153 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:35:26 2020 +0300 * home: remove nolint directive commit 2ea3086ad41e9003282add7e996ae722d72d878b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:13:57 2020 +0300 * home: reduce cyclomatic complexity of run function commit f479b480c48e0bb832ddef8f57586f56b8a55bab Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 15:35:46 2020 +0300 * home: use crypto/rand instead of math/rand commit a28d4a53e3b930136b036606fc7e78404f1d208b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 14:11:07 2020 +0300 * dnsforward: remove gocyclo nolint directive commit 64a0a324cc2b20614ceec3ccc6505e960fe526e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 11:45:49 2020 +0300 all *: remove some nolint directives Updates #2271.
2020-11-20 14:32:41 +00:00
}
2023-04-12 12:48:42 +01:00
2023-07-26 11:18:44 +01:00
return r, nil
}
Pull request: 2271 handle nolint Merge in DNS/adguard-home from 2271-handle-nolint to master Closes #2271. Squashed commit of the following: commit fde5c8795ac79e1f7d02ba8c8e369b5a724a000e Merge: fc2acd898 642dcd647 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 20 17:12:28 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit fc2acd89871de08c39e80ace9e5bb8a7acb7afba Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 17 11:55:29 2020 +0300 dnsforward: fix test output strings commit c4ebae6ea9c293bad239519c44ca5a6c576bb921 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 22:43:20 2020 +0300 dnsfilter: make package pass tests commit f2d98c6acabd8977f3b1b361987eaa31eb6eb9ad Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 20:05:00 2020 +0300 querylog: make decoding pass tests commit ab5850d24c50d53b8393f2de448cc340241351d7 Merge: 6ed2066bf 8a9c6e8a0 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:48:31 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit 6ed2066bf567e13dd14cfa16fc7b109b59fa39ef Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:13:45 2020 +0300 home: fix tests naming commit af691081fb02b7500a746b16492f01f7f9befe9a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:15:49 2020 +0300 home: impove code quality commit 2914cd3cd23ef2a1964116baab9187d89b377f86 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 15:46:39 2020 +0300 * querylog: remove useless check commit 9996840650e784ccc76d1f29964560435ba27dc7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 13:18:34 2020 +0300 * all: fix noticed defects commit 2b15293e59337f70302fbc0db81ebb26bee0bed2 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 20:15:53 2020 +0300 * stats: remove last nolint directive commit b2e1ddf7b58196a2fdbf879f084edb41ca1aa1eb Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:35:41 2020 +0300 * all: remove another nolint directive commit c6fc5cfcc9c95ab9e570a95ab41c3e5c0125e62e Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:11:28 2020 +0300 * querylog: remove nolint directive commit 226ddbf2c92f737f085b44a4ddf6daec7b602153 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:35:26 2020 +0300 * home: remove nolint directive commit 2ea3086ad41e9003282add7e996ae722d72d878b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:13:57 2020 +0300 * home: reduce cyclomatic complexity of run function commit f479b480c48e0bb832ddef8f57586f56b8a55bab Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 15:35:46 2020 +0300 * home: use crypto/rand instead of math/rand commit a28d4a53e3b930136b036606fc7e78404f1d208b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 14:11:07 2020 +0300 * dnsforward: remove gocyclo nolint directive commit 64a0a324cc2b20614ceec3ccc6505e960fe526e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 11:45:49 2020 +0300 all *: remove some nolint directives Updates #2271.
2020-11-20 14:32:41 +00:00
2023-07-26 11:18:44 +01:00
// readerFromURL returns an io.ReadCloser reading filtering-rule list data form
// the filter's URL.
func (d *DNSFilter) readerFromURL(fltURL string) (r io.ReadCloser, err error) {
2023-09-07 15:13:48 +01:00
resp, err := d.conf.HTTPClient.Get(fltURL)
2023-07-26 11:18:44 +01:00
if err != nil {
// Don't wrap the error since it's informative enough as is.
return nil, err
}
2023-07-26 11:18:44 +01:00
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got status code %d, want %d", resp.StatusCode, http.StatusOK)
}
2023-07-26 11:18:44 +01:00
return resp.Body, nil
}
// loads filter contents from the file in dataDir
2023-01-19 12:04:46 +00:00
func (d *DNSFilter) load(flt *FilterYAML) (err error) {
2023-09-07 15:13:48 +01:00
fileName := flt.Path(d.conf.DataDir)
2023-07-12 13:13:31 +01:00
log.Debug("filtering: loading filter %d from %q", flt.ID, fileName)
2023-01-19 12:04:46 +00:00
file, err := os.Open(fileName)
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)
}
defer func() { err = errors.WithDeferred(err, file.Close()) }()
st, err := file.Stat()
if err != nil {
return fmt.Errorf("getting filter file stat: %w", err)
}
2023-07-12 13:13:31 +01:00
log.Debug("filtering: file %q, id %d, length %d", fileName, flt.ID, st.Size())
2023-10-11 15:31:41 +01:00
bufPtr := d.bufPool.Get()
2023-07-12 13:13:31 +01:00
defer d.bufPool.Put(bufPtr)
2023-07-12 13:13:31 +01:00
p := rulelist.NewParser()
res, err := p.Parse(io.Discard, file, *bufPtr)
2023-01-19 12:04:46 +00:00
if err != nil {
return fmt.Errorf("parsing filter file: %w", err)
}
2023-09-07 15:13:48 +01:00
flt.ensureName(res.Title)
2023-07-12 13:13:31 +01:00
flt.RulesCount, flt.checksum, flt.LastUpdated = res.RulesCount, res.Checksum, st.ModTime()
return nil
}
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) EnableFilters(async bool) {
2023-09-07 15:13:48 +01:00
d.conf.filtersMu.RLock()
defer d.conf.filtersMu.RUnlock()
2022-09-29 15:36:01 +01:00
d.enableFiltersLocked(async)
}
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) enableFiltersLocked(async bool) {
2023-09-07 15:13:48 +01:00
filters := make([]Filter, 1, len(d.conf.Filters)+len(d.conf.WhitelistFilters)+1)
2023-04-12 12:48:42 +01:00
filters[0] = Filter{
2024-04-02 18:22:19 +01:00
ID: rulelist.URLFilterIDCustom,
2023-09-07 15:13:48 +01:00
Data: []byte(strings.Join(d.conf.UserRules, "\n")),
2023-04-12 12:48:42 +01:00
}
2023-09-07 15:13:48 +01:00
for _, filter := range d.conf.Filters {
if !filter.Enabled {
continue
}
2022-09-29 15:36:01 +01:00
filters = append(filters, Filter{
ID: filter.ID,
2023-09-07 15:13:48 +01:00
FilePath: filter.Path(d.conf.DataDir),
})
}
Pull request: 2499 merge rewrites vol.1 Merge in DNS/adguard-home from 2499-merge-rewrites-vol.1 to master Updates #2499. Squashed commit of the following: commit 6b308bc2b360cee8c22e506f31d62bacb4bf8fb3 Merge: f49e9186 2b635bf6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 14 19:23:07 2021 +0300 Merge branch 'master' into 2499-merge-rewrites-vol.1 commit f49e9186ffc8b7074d03c6721ee56cdb09243684 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 14 18:50:49 2021 +0300 aghos: fix fs events filtering commit 567dd646556606212af5dab60e3ecbb8fff22c25 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 14 16:50:37 2021 +0300 all: imp code, docs, fix windows commit 140c8bf519345eb54d0e7500a996fcf465353d71 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Oct 13 19:41:53 2021 +0300 aghnet: use const commit bebf3f76bd394a498ccad812c57d4507c69529ba Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Oct 13 19:32:37 2021 +0300 all: imp tests, docs commit 9bfdbb6eb454833135d616e208e82699f98e2562 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Oct 13 18:42:20 2021 +0300 all: imp path more, imp docs commit ee9ea4c132a6b17787d150bf2bee703abaa57be3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Oct 13 16:09:46 2021 +0300 all: fix windows, imp paths commit 6fac8338a81e9ecfebfc23a1adcb964e89f6aee6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 11 19:53:35 2021 +0300 all: imp code, docs commit da1ce1a2a3dd2be3fdff2412a6dbd596859dc249 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 11 18:22:50 2021 +0300 aghnet: fix windows tests commit d29de359ed68118d71efb226a8433fac15ff5c66 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 8 21:02:14 2021 +0300 all: repl & imp commit 1356c08944cdbb85ce5532d90fe5b077219ce5ff Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 8 01:41:19 2021 +0300 all: add tests, mv logic, added tmpfs commit f4b11adf8998bc8d9d955c5ac9f386f671bd5213 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 7 14:26:30 2021 +0300 all: imp filewalker, refactor hosts container
2021-10-14 17:39:21 +01:00
2022-09-29 15:36:01 +01:00
var allowFilters []Filter
2023-09-07 15:13:48 +01:00
for _, filter := range d.conf.WhitelistFilters {
if !filter.Enabled {
continue
}
2022-09-29 15:36:01 +01:00
allowFilters = append(allowFilters, Filter{
ID: filter.ID,
2023-09-07 15:13:48 +01:00
FilePath: filter.Path(d.conf.DataDir),
})
}
2023-07-12 13:13:31 +01:00
err := d.setFilters(filters, allowFilters, async)
if err != nil {
log.Error("filtering: enabling filters: %s", err)
}
2023-09-07 15:13:48 +01:00
d.SetEnabled(d.conf.FilteringEnabled)
}