Patterns are now fully supported in cloaking rules

Fixes #306
This commit is contained in:
Frank Denis 2018-04-09 13:16:30 +02:00
parent 7d10628a5f
commit 44880f9b2c
2 changed files with 8 additions and 8 deletions

View File

@ -9,8 +9,7 @@
# configuration file # configuration file
www.google.com forcesafesearch.google.com www.google.* forcesafesearch.google.com
www.google.fr forcesafesearch.google.com
www.bing.com strict.bing.com www.bing.com strict.bing.com

View File

@ -22,7 +22,7 @@ type CloakedName struct {
type PluginCloak struct { type PluginCloak struct {
sync.RWMutex sync.RWMutex
cloakedNames map[string]*CloakedName patternMatcher *PatternMatcher
ttl uint32 ttl uint32
} }
@ -41,7 +41,7 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
return err return err
} }
plugin.ttl = proxy.cacheMinTTL plugin.ttl = proxy.cacheMinTTL
plugin.cloakedNames = make(map[string]*CloakedName) plugin.patternMatcher = NewPatternPatcher()
for lineNo, line := range strings.Split(string(bin), "\n") { for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace) line = strings.TrimFunc(line, unicode.IsSpace)
if len(line) == 0 || strings.HasPrefix(line, "#") { if len(line) == 0 || strings.HasPrefix(line, "#") {
@ -75,7 +75,7 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
} else { } else {
cloakedName.target = target cloakedName.target = target
} }
plugin.cloakedNames[line] = &cloakedName plugin.patternMatcher.Add(line, &cloakedName, lineNo+1)
} }
return nil return nil
} }
@ -103,11 +103,12 @@ func (plugin *PluginCloak) Eval(pluginsState *PluginsState, msg *dns.Msg) error
} }
now := time.Now() now := time.Now()
plugin.RLock() plugin.RLock()
cloakedName := plugin.cloakedNames[qName] _, _, xcloakedName := plugin.patternMatcher.Eval(qName)
if cloakedName == nil { if xcloakedName == nil {
plugin.RUnlock() plugin.RUnlock()
return nil return nil
} }
cloakedName := xcloakedName.(*CloakedName)
ttl, expired := plugin.ttl, false ttl, expired := plugin.ttl, false
if cloakedName.lastUpdate != nil { if cloakedName.lastUpdate != nil {
if elapsed := uint32(now.Sub(*cloakedName.lastUpdate).Seconds()); elapsed < ttl { if elapsed := uint32(now.Sub(*cloakedName.lastUpdate).Seconds()); elapsed < ttl {