AdGuardDNS/internal/cmd/filter.go

123 lines
4.0 KiB
Go
Raw Permalink Normal View History

2022-08-26 14:18:35 +03:00
package cmd
import (
2023-03-18 17:11:10 +03:00
"fmt"
2024-12-05 14:19:25 +03:00
2024-10-14 17:44:24 +03:00
"github.com/AdguardTeam/golibs/errors"
2022-08-26 14:18:35 +03:00
"github.com/AdguardTeam/golibs/timeutil"
2023-08-08 18:31:48 +03:00
"github.com/c2h5oh/datasize"
2022-08-26 14:18:35 +03:00
)
// filtersConfig contains the configuration for the filter lists and filtering
// storage to be used.
2024-06-07 14:27:46 +03:00
//
// TODO(a.garipov): Add the timeout for the blocked-service index refresh. It
// is currently hardcoded to 3 minutes.
2022-08-26 14:18:35 +03:00
type filtersConfig struct {
2023-08-08 18:31:48 +03:00
// RuleListCache is the cache settings for the filtering rule-list.
RuleListCache *fltRuleListCache `yaml:"rule_list_cache"`
2022-08-26 14:18:35 +03:00
// CustomFilterCacheSize is the size of the LRU cache of compiled filtering
// engines for profiles with custom filtering rules.
2024-12-05 14:19:25 +03:00
//
// TODO(a.garipov): Rename to "custom_filter_cache_count"?
2022-08-26 14:18:35 +03:00
CustomFilterCacheSize int `yaml:"custom_filter_cache_size"`
2022-12-29 15:36:26 +03:00
// SafeSearchCacheSize is the size of the LRU cache of safe-search results.
2024-12-05 14:19:25 +03:00
//
// TODO(a.garipov): Rename to "safe_search_cache_count"?
2022-12-29 15:36:26 +03:00
SafeSearchCacheSize int `yaml:"safe_search_cache_size"`
2022-08-26 14:18:35 +03:00
// ResponseTTL is the TTL to set for DNS responses to requests for filtered
// domains.
ResponseTTL timeutil.Duration `yaml:"response_ttl"`
// RefreshIvl defines how often AdGuard DNS refreshes the rule-based filters
// from filter index.
RefreshIvl timeutil.Duration `yaml:"refresh_interval"`
// RefreshTimeout is the timeout for the entire filter update operation.
2024-06-07 14:27:46 +03:00
// Note that filter rule-list index and each filter rule-list update
// operations have their own timeouts, see IndexRefreshTimeout and
// RuleListRefreshTimeout.
2022-08-26 14:18:35 +03:00
RefreshTimeout timeutil.Duration `yaml:"refresh_timeout"`
2022-12-29 15:36:26 +03:00
2024-06-07 14:27:46 +03:00
// IndexRefreshTimeout is the timeout for the filter rule-list index update
// operation. See also RefreshTimeout for the entire filter update
// operation.
IndexRefreshTimeout timeutil.Duration `yaml:"index_refresh_timeout"`
// RuleListRefreshTimeout is the timeout for the filter update operation of
// each rule-list, which includes safe-search filters. See also
// RefreshTimeout for the entire filter update operation.
RuleListRefreshTimeout timeutil.Duration `yaml:"rule_list_refresh_timeout"`
2023-08-08 18:31:48 +03:00
// MaxSize is the maximum size of the downloadable filtering rule-list.
MaxSize datasize.ByteSize `yaml:"max_size"`
2024-11-08 16:26:22 +03:00
// EDEEnabled enables the Extended DNS Errors feature.
EDEEnabled bool `yaml:"ede_enabled"`
// SDEEnabled enables the experimental Structured DNS Errors feature.
SDEEnabled bool `yaml:"sde_enabled"`
2022-08-26 14:18:35 +03:00
}
2024-10-14 17:44:24 +03:00
// type check
var _ validator = (*filtersConfig)(nil)
// validate implements the [validator] interface for *filtersConfig.
2022-08-26 14:18:35 +03:00
func (c *filtersConfig) validate() (err error) {
2024-11-08 16:26:22 +03:00
if c == nil {
2024-10-14 17:44:24 +03:00
return errors.ErrNoValue
2024-11-08 16:26:22 +03:00
}
errs := []error{
validatePositive("custom_filter_cache_size", c.CustomFilterCacheSize),
validatePositive("safe_search_cache_size", c.SafeSearchCacheSize),
validatePositive("response_ttl", c.ResponseTTL),
validatePositive("refresh_interval", c.RefreshIvl),
validatePositive("refresh_timeout", c.RefreshTimeout),
validatePositive("index_refresh_timeout", c.IndexRefreshTimeout),
validatePositive("rule_list_refresh_timeout", c.RuleListRefreshTimeout),
validatePositive("max_size", c.MaxSize),
}
if !c.EDEEnabled && c.SDEEnabled {
errs = append(errs, errors.Error("ede must be enabled to enable sde"))
2023-08-08 18:31:48 +03:00
}
err = c.RuleListCache.validate()
if err != nil {
2024-11-08 16:26:22 +03:00
errs = append(errs, fmt.Errorf("rule_list_cache: %w", err))
2023-08-08 18:31:48 +03:00
}
2024-11-08 16:26:22 +03:00
return errors.Join(errs...)
2023-08-08 18:31:48 +03:00
}
// fltRuleListCache contains filtering rule-list cache configuration.
type fltRuleListCache struct {
// Size defines the size of the LRU cache of rule-list filtering results.
2024-12-05 14:19:25 +03:00
//
// TODO(a.garipov): Rename to "count"?
2023-08-08 18:31:48 +03:00
Size int `yaml:"size"`
// Enabled shows if the rule-list cache is enabled. If it is false, the
// rest of the settings are ignored.
Enabled bool `yaml:"enabled"`
}
2024-10-14 17:44:24 +03:00
// type check
var _ validator = (*fltRuleListCache)(nil)
// validate implements the [validator] interface for *fltRuleListCache.
2023-08-08 18:31:48 +03:00
func (c *fltRuleListCache) validate() (err error) {
switch {
case c == nil:
2024-10-14 17:44:24 +03:00
return errors.ErrNoValue
2023-08-08 18:31:48 +03:00
case c.Size <= 0:
2024-10-14 17:44:24 +03:00
return newNotPositiveError("size", c.Size)
2022-08-26 14:18:35 +03:00
default:
return nil
}
}