From 7d2e39ed52c4813b811fbc9117a238d813f96694 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Fri, 30 Nov 2018 13:48:53 +0300 Subject: [PATCH] dnsfilter -- Add a convinience function to add all rules from all filters. --- dnsfilter/dnsfilter.go | 18 ++++++++++++++++++ dnsforward/dnsforward.go | 16 ++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/dnsfilter/dnsfilter.go b/dnsfilter/dnsfilter.go index 7fd01be3..2c78a92b 100644 --- a/dnsfilter/dnsfilter.go +++ b/dnsfilter/dnsfilter.go @@ -737,6 +737,24 @@ func (d *Dnsfilter) lookupCommon(host string, lookupstats *LookupStats, cache gc // Adding rule and matching against the rules // +// AddRules is a convinience function to add an array of filters in one call +func (d *Dnsfilter) AddRules(filters []Filter) error { + for _, f := range filters { + for _, rule := range f.Rules { + err := d.AddRule(rule, f.ID) + if err == ErrAlreadyExists || err == ErrInvalidSyntax { + continue + } + if err != nil { + log.Printf("Cannot add rule %s: %s", rule, err) + // Just ignore invalid rules + continue + } + } + } + return nil +} + // AddRule adds a rule, checking if it is a valid rule first and if it wasn't added already func (d *Dnsfilter) AddRule(input string, filterListID int64) error { input = strings.TrimSpace(input) diff --git a/dnsforward/dnsforward.go b/dnsforward/dnsforward.go index 3a2de123..7d15d44a 100644 --- a/dnsforward/dnsforward.go +++ b/dnsforward/dnsforward.go @@ -323,18 +323,10 @@ func (s *Server) reconfigureFilters(new ServerConfig) { } dnsFilter := dnsfilter.New(&new.Config) // sets safebrowsing, safesearch and parental - for _, f := range newFilters { - for _, rule := range f.Rules { - err := dnsFilter.AddRule(rule, f.ID) - if err == dnsfilter.ErrAlreadyExists || err == dnsfilter.ErrInvalidSyntax { - continue - } - if err != nil { - log.Printf("Cannot add rule %s: %s", rule, err) - // Just ignore invalid rules - continue - } - } + + // add rules only if they are enabled + if new.FilteringEnabled { + dnsFilter.AddRules(newFilters) } s.Lock()