2022-09-23 13:23:35 +03:00
|
|
|
package filtering
|
2018-11-28 20:14:54 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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"
|
2024-02-08 20:39:18 +03:00
|
|
|
"slices"
|
2018-11-28 20:14:54 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghrenameio"
|
2023-07-07 18:27:33 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
|
2024-04-03 13:44:51 +03:00
|
|
|
"github.com/AdguardTeam/golibs/container"
|
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"
|
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
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// FilterYAML represents a filter list in the configuration file.
|
2022-09-23 13:23:35 +03:00
|
|
|
//
|
2023-07-07 18:27:33 +03:00
|
|
|
// TODO(e.burkov): Investigate if the field ordering is important.
|
2022-09-23 13:23:35 +03:00
|
|
|
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 {
|
2024-03-21 18:45:34 +03:00
|
|
|
return filepath.Join(
|
|
|
|
dataDir,
|
|
|
|
filterDir,
|
|
|
|
strconv.FormatInt(int64(filter.ID), 10)+".txt")
|
2018-11-28 20:14:54 +03:00
|
|
|
}
|
|
|
|
|
2023-08-11 13:55:49 +03: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)
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.filtersMu.Lock()
|
|
|
|
defer d.conf.filtersMu.Unlock()
|
2019-11-06 15:56:29 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
filters := d.conf.Filters
|
2022-10-21 20:14:43 +03:00
|
|
|
if isAllowlist {
|
2023-09-04 17:18:43 +03:00
|
|
|
filters = d.conf.WhitelistFilters
|
2020-02-26 19:58:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
i := slices.IndexFunc(filters, func(flt FilterYAML) bool { return flt.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
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
flt := &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,
|
2023-07-25 17:47:24 +03:00
|
|
|
flt.URL,
|
2022-10-21 20:14:43 +03:00
|
|
|
)
|
|
|
|
|
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 {
|
2023-07-25 17:47:24 +03:00
|
|
|
flt.URL = oldURL
|
|
|
|
flt.Name = oldName
|
|
|
|
flt.Enabled = oldEnabled
|
|
|
|
flt.LastUpdated = oldUpdated
|
|
|
|
flt.RulesCount = oldRulesCount
|
2022-10-21 20:14:43 +03:00
|
|
|
}
|
2023-07-25 17:47:24 +03:00
|
|
|
}(flt.URL, flt.Name, flt.Enabled, flt.LastUpdated, flt.RulesCount)
|
2019-11-06 15:56:29 +03:00
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
flt.Name = newList.Name
|
2022-09-23 13:23:35 +03:00
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
if flt.URL != newList.URL {
|
2022-10-21 20:14:43 +03:00
|
|
|
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
|
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
flt.URL = newList.URL
|
|
|
|
flt.LastUpdated = time.Time{}
|
|
|
|
flt.unload()
|
2022-09-23 13:23:35 +03:00
|
|
|
}
|
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
if flt.Enabled != newList.Enabled {
|
|
|
|
flt.Enabled = newList.Enabled
|
2022-10-21 20:14:43 +03:00
|
|
|
shouldRestart = true
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
if flt.Enabled {
|
2022-10-21 20:14:43 +03:00
|
|
|
if shouldRestart {
|
|
|
|
// Download the filter contents.
|
2023-07-25 17:47:24 +03:00
|
|
|
shouldRestart, err = d.update(flt)
|
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.
|
2023-07-25 17:47:24 +03:00
|
|
|
flt.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) {
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.filtersMu.RLock()
|
|
|
|
defer d.conf.filtersMu.RUnlock()
|
2022-09-23 13:23:35 +03:00
|
|
|
|
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) {
|
2023-09-04 17:18:43 +03:00
|
|
|
for _, f := range d.conf.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
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
for _, f := range d.conf.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") }()
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.filtersMu.Lock()
|
|
|
|
defer d.conf.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 {
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.WhitelistFilters = append(d.conf.WhitelistFilters, flt)
|
2020-02-26 19:58:25 +03:00
|
|
|
} else {
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.Filters = append(d.conf.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 {
|
2024-03-21 18:45:34 +03: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 19:41:45 +03:00
|
|
|
}
|
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 {
|
2023-07-07 18:27:33 +03:00
|
|
|
log.Error("filtering: loading filter %d: %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) {
|
2024-04-03 13:44:51 +03:00
|
|
|
urls := container.NewMapSet[string]()
|
2022-09-23 13:23:35 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.filtersMu.RLock()
|
|
|
|
defer d.conf.filtersMu.RUnlock()
|
2022-09-23 13:23:35 +03:00
|
|
|
|
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 {
|
2023-09-04 17:18:43 +03:00
|
|
|
exp := flt.LastUpdated.Add(time.Duration(d.conf.FiltersUpdateIntervalHours) * time.Hour)
|
2022-09-23 13:23:35 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
failNum := 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 {
|
2023-07-25 17:47:24 +03:00
|
|
|
failNum++
|
|
|
|
log.Error("filtering: updating filter from url %q: %s\n", uf.URL, err)
|
2023-07-07 18:27:33 +03:00
|
|
|
|
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
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
if failNum == 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
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.filtersMu.Lock()
|
|
|
|
defer d.conf.filtersMu.Unlock()
|
2023-03-29 19:09:54 +03:00
|
|
|
|
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-07-07 18:27:33 +03:00
|
|
|
log.Info(
|
|
|
|
"filtering: updated filter %d; rule count: %d (was %d)",
|
|
|
|
f.ID,
|
|
|
|
uf.RulesCount,
|
|
|
|
f.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) {
|
|
|
|
updNum := 0
|
2023-07-07 18:27:33 +03:00
|
|
|
log.Debug("filtering: starting updating")
|
|
|
|
defer func() { log.Debug("filtering: finished updating, %d updated", updNum) }()
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
var lists []FilterYAML
|
|
|
|
var toUpd []bool
|
|
|
|
isNetErr := false
|
|
|
|
|
|
|
|
if block {
|
2023-09-04 17:18:43 +03:00
|
|
|
updNum, lists, toUpd, isNetErr = d.refreshFiltersArray(&d.conf.Filters, force)
|
2022-09-23 13:23:35 +03:00
|
|
|
}
|
|
|
|
if allow {
|
2023-09-04 17:18:43 +03:00
|
|
|
updNumAl, listsAl, toUpdAl, isNetErrAl := d.refreshFiltersArray(&d.conf.WhitelistFilters, force)
|
2022-09-23 13:23:35 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
p := uf.Path(d.conf.DataDir)
|
2023-03-29 19:09:54 +03:00
|
|
|
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
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
return updNum, false
|
2018-11-28 20:14:54 +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(
|
2023-09-04 17:18:43 +03:00
|
|
|
filter.Path(d.conf.DataDir),
|
2023-03-29 19:09:54 +03:00
|
|
|
filter.LastUpdated,
|
|
|
|
filter.LastUpdated,
|
|
|
|
)
|
|
|
|
if chErr != nil {
|
2023-07-07 18:27:33 +03:00
|
|
|
log.Error("filtering: os.Chtimes(): %s", 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
|
|
|
}
|
|
|
|
|
2023-07-25 17:47:24 +03: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-04 17:18:43 +03:00
|
|
|
tmpFile, err := aghrenameio.NewPendingFile(flt.Path(d.conf.DataDir), 0o644)
|
2023-07-25 17:47:24 +03: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-06 16:45:48 +03:00
|
|
|
bufPtr := d.bufPool.Get()
|
2023-07-25 17:47:24 +03:00
|
|
|
defer d.bufPool.Put(bufPtr)
|
|
|
|
|
|
|
|
p := rulelist.NewParser()
|
|
|
|
res, err = p.Parse(tmpFile, r, *bufPtr)
|
|
|
|
|
|
|
|
return res.Checksum != flt.checksum && err == nil, 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
|
2023-07-07 18:27:33 +03:00
|
|
|
// and checksum if succeeded.
|
2022-09-23 13:23:35 +03:00
|
|
|
func (d *DNSFilter) finalizeUpdate(
|
2023-07-25 17:47:24 +03:00
|
|
|
file aghrenameio.PendingFile,
|
2022-09-23 13:23:35 +03:00
|
|
|
flt *FilterYAML,
|
2023-07-07 18:27:33 +03:00
|
|
|
res *rulelist.ParseResult,
|
2023-07-25 17:47:24 +03:00
|
|
|
returned error,
|
|
|
|
updated bool,
|
2021-09-15 20:09:32 +03:00
|
|
|
) (err error) {
|
2023-07-25 17:47:24 +03:00
|
|
|
id := flt.ID
|
2021-09-15 20:09:32 +03:00
|
|
|
if !updated {
|
2023-07-25 17:47:24 +03:00
|
|
|
if returned == nil {
|
|
|
|
log.Debug("filtering: filter %d from url %q has no changes, skipping", id, flt.URL)
|
|
|
|
}
|
2021-09-15 20:09:32 +03:00
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
return errors.WithDeferred(returned, file.Cleanup())
|
2021-09-15 20:09:32 +03:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
log.Info("filtering: saving contents of filter %d into %q", id, flt.Path(d.conf.DataDir))
|
2023-03-29 19:09:54 +03:00
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
err = file.CloseReplace()
|
2022-12-20 16:40:42 +03:00
|
|
|
if err != nil {
|
2023-07-25 17:47:24 +03:00
|
|
|
return fmt.Errorf("finalizing update: %w", err)
|
2021-09-15 20:09:32 +03:00
|
|
|
}
|
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
rulesCount := res.RulesCount
|
|
|
|
log.Info("filtering: updated filter %d: %d bytes, %d rules", id, res.BytesWritten, rulesCount)
|
|
|
|
|
2023-08-11 13:55:49 +03:00
|
|
|
flt.ensureName(res.Title)
|
2023-07-25 17:47:24 +03:00
|
|
|
flt.checksum = res.Checksum
|
|
|
|
flt.RulesCount = rulesCount
|
2021-09-15 20:09:32 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:47:24 +03: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)
|
|
|
|
}
|
2021-09-15 20:09:32 +03:00
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
return r, nil
|
|
|
|
}
|
2021-09-15 20:09:32 +03:00
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
r, err = os.Open(fltURL)
|
2020-11-20 17:32:41 +03:00
|
|
|
if err != nil {
|
2023-07-25 17:47:24 +03:00
|
|
|
return nil, fmt.Errorf("opening file: %w", err)
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
2023-03-29 19:09:54 +03:00
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
return r, nil
|
|
|
|
}
|
2020-11-20 17:32:41 +03:00
|
|
|
|
2023-07-25 17:47:24 +03: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-04 17:18:43 +03:00
|
|
|
resp, err := d.conf.HTTPClient.Get(fltURL)
|
2023-07-25 17:47:24 +03:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
2021-05-28 18:45:11 +03:00
|
|
|
}
|
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("got status code %d, want %d", resp.StatusCode, http.StatusOK)
|
2019-03-15 16:49:10 +03:00
|
|
|
}
|
2020-04-07 13:02:28 +03:00
|
|
|
|
2023-07-25 17:47:24 +03:00
|
|
|
return resp.Body, nil
|
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) {
|
2023-09-04 17:18:43 +03:00
|
|
|
fileName := flt.Path(d.conf.DataDir)
|
2018-11-28 20:14:54 +03:00
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
log.Debug("filtering: loading filter %d from %q", 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)
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
log.Debug("filtering: file %q, id %d, length %d", fileName, flt.ID, st.Size())
|
|
|
|
|
2023-10-06 16:45:48 +03:00
|
|
|
bufPtr := d.bufPool.Get()
|
2023-07-07 18:27:33 +03:00
|
|
|
defer d.bufPool.Put(bufPtr)
|
2021-05-14 19:41:45 +03:00
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
p := rulelist.NewParser()
|
|
|
|
res, err := p.Parse(io.Discard, file, *bufPtr)
|
2022-12-20 16:40:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing filter file: %w", err)
|
|
|
|
}
|
2018-11-28 20:14:54 +03:00
|
|
|
|
2023-08-11 13:55:49 +03:00
|
|
|
flt.ensureName(res.Title)
|
2023-07-07 18:27:33 +03:00
|
|
|
flt.RulesCount, flt.checksum, flt.LastUpdated = res.RulesCount, res.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) {
|
2023-09-04 17:18:43 +03:00
|
|
|
d.conf.filtersMu.RLock()
|
|
|
|
defer d.conf.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-09-04 17:18:43 +03:00
|
|
|
filters := make([]Filter, 1, len(d.conf.Filters)+len(d.conf.WhitelistFilters)+1)
|
2023-03-29 19:09:54 +03:00
|
|
|
filters[0] = Filter{
|
2024-03-15 17:23:36 +03:00
|
|
|
ID: rulelist.URLFilterIDCustom,
|
2023-09-04 17:18:43 +03:00
|
|
|
Data: []byte(strings.Join(d.conf.UserRules, "\n")),
|
2023-03-29 19:09:54 +03:00
|
|
|
}
|
2019-10-09 19:51:26 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
for _, filter := range d.conf.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,
|
2023-09-04 17:18:43 +03:00
|
|
|
FilePath: filter.Path(d.conf.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
|
2023-09-04 17:18:43 +03:00
|
|
|
for _, filter := range d.conf.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,
|
2023-09-04 17:18:43 +03:00
|
|
|
FilePath: filter.Path(d.conf.DataDir),
|
2021-05-12 20:04:50 +03:00
|
|
|
})
|
2019-10-09 19:51:26 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
err := d.setFilters(filters, allowFilters, async)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("filtering: enabling filters: %s", err)
|
2021-05-12 20:04:50 +03:00
|
|
|
}
|
2021-05-24 14:48:42 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
d.SetEnabled(d.conf.FilteringEnabled)
|
2019-10-09 19:51:26 +03:00
|
|
|
}
|