dnsfilter -- Add a convinience function to add all rules from all filters.

This commit is contained in:
Eugene Bujak 2018-11-30 13:48:53 +03:00
parent e26837d9e8
commit 7d2e39ed52
2 changed files with 22 additions and 12 deletions

View File

@ -737,6 +737,24 @@ func (d *Dnsfilter) lookupCommon(host string, lookupstats *LookupStats, cache gc
// Adding rule and matching against the rules // 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 // 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 { func (d *Dnsfilter) AddRule(input string, filterListID int64) error {
input = strings.TrimSpace(input) input = strings.TrimSpace(input)

View File

@ -323,18 +323,10 @@ func (s *Server) reconfigureFilters(new ServerConfig) {
} }
dnsFilter := dnsfilter.New(&new.Config) // sets safebrowsing, safesearch and parental dnsFilter := dnsfilter.New(&new.Config) // sets safebrowsing, safesearch and parental
for _, f := range newFilters {
for _, rule := range f.Rules { // add rules only if they are enabled
err := dnsFilter.AddRule(rule, f.ID) if new.FilteringEnabled {
if err == dnsfilter.ErrAlreadyExists || err == dnsfilter.ErrInvalidSyntax { dnsFilter.AddRules(newFilters)
continue
}
if err != nil {
log.Printf("Cannot add rule %s: %s", rule, err)
// Just ignore invalid rules
continue
}
}
} }
s.Lock() s.Lock()