From 57c510631e44d8a36a7fb086b23143b36110a956 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Mon, 15 Jul 2019 12:10:43 +0300 Subject: [PATCH 1/2] - dnsfilter: fix crash when global setting 'SafeSearch' is off but per-client setting is on --- dnsfilter/dnsfilter.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dnsfilter/dnsfilter.go b/dnsfilter/dnsfilter.go index 738ee039..3716139c 100644 --- a/dnsfilter/dnsfilter.go +++ b/dnsfilter/dnsfilter.go @@ -731,13 +731,13 @@ func New(c *Config, filters map[int]string) *Dnsfilter { if c != nil { // initialize objects only once - if c.SafeBrowsingEnabled && gctx.safebrowsingCache == nil { + if gctx.safebrowsingCache == nil { gctx.safebrowsingCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() } - if c.SafeSearchEnabled && gctx.safeSearchCache == nil { + if gctx.safeSearchCache == nil { gctx.safeSearchCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() } - if c.ParentalEnabled && gctx.parentalCache == nil { + if gctx.parentalCache == nil { gctx.parentalCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() } if len(c.ResolverAddress) != 0 && gctx.dialCache == nil { From a79643f23e4bb45a912a71b4a973a027431a8720 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Mon, 15 Jul 2019 14:03:22 +0300 Subject: [PATCH 2/2] + dnsfilter-test: override global safe-browsing setting with a per-client setting --- dnsfilter/dnsfilter_test.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/dnsfilter/dnsfilter_test.go b/dnsfilter/dnsfilter_test.go index 75b5cb47..bf1f3df4 100644 --- a/dnsfilter/dnsfilter_test.go +++ b/dnsfilter/dnsfilter_test.go @@ -455,13 +455,16 @@ func TestMatching(t *testing.T) { func applyClientSettings(clientAddr string, setts *RequestFilteringSettings) { setts.FilteringEnabled = false setts.ParentalEnabled = false + setts.SafeBrowsingEnabled = true } +// Check behaviour without any per-client settings, +// then apply per-client settings and check behaviour once again func TestClientSettings(t *testing.T) { var r Result filters := make(map[int]string) filters[0] = "||example.org^\n" - d := NewForTest(&Config{ParentalEnabled: true}, filters) + d := NewForTest(&Config{ParentalEnabled: true, SafeBrowsingEnabled: false}, filters) defer d.Destroy() d.ParentalSensitivity = 3 @@ -479,6 +482,12 @@ func TestClientSettings(t *testing.T) { t.Fatalf("CheckHost FilteredParental") } + // safesearch is disabled + r, _ = d.CheckHost("wmconvirus.narod.ru", dns.TypeA, "1.1.1.1") + if r.IsFiltered { + t.Fatalf("CheckHost safesearch") + } + // override client settings: d.FilterHandler = applyClientSettings @@ -488,11 +497,17 @@ func TestClientSettings(t *testing.T) { t.Fatalf("CheckHost") } - // override parental settings + // override parental settings (force disable parental) r, _ = d.CheckHost("pornhub.com", dns.TypeA, "1.1.1.1") if r.IsFiltered { t.Fatalf("CheckHost") } + + // override safesearch settings (force enable safesearch) + r, _ = d.CheckHost("wmconvirus.narod.ru", dns.TypeA, "1.1.1.1") + if !r.IsFiltered || r.Reason != FilteredSafeBrowsing { + t.Fatalf("CheckHost FilteredSafeBrowsing") + } } // BENCHMARKS