Merge: - dnsfilter: fix crash when global setting 'SafeSearch' is off

Close #880

* commit 'a79643f23e4bb45a912a71b4a973a027431a8720':
  + dnsfilter-test: override global safe-browsing setting with a per-client setting
  - dnsfilter: fix crash when global setting 'SafeSearch' is off
This commit is contained in:
Simon Zolin 2019-07-15 18:22:58 +03:00
commit cc8633ed7d
2 changed files with 20 additions and 5 deletions

View file

@ -731,13 +731,13 @@ func New(c *Config, filters map[int]string) *Dnsfilter {
if c != nil { if c != nil {
// initialize objects only once // initialize objects only once
if c.SafeBrowsingEnabled && gctx.safebrowsingCache == nil { if gctx.safebrowsingCache == nil {
gctx.safebrowsingCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build() 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() 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() gctx.parentalCache = gcache.New(defaultCacheSize).LRU().Expiration(defaultCacheTime).Build()
} }
if len(c.ResolverAddress) != 0 && gctx.dialCache == nil { if len(c.ResolverAddress) != 0 && gctx.dialCache == nil {

View file

@ -455,13 +455,16 @@ func TestMatching(t *testing.T) {
func applyClientSettings(clientAddr string, setts *RequestFilteringSettings) { func applyClientSettings(clientAddr string, setts *RequestFilteringSettings) {
setts.FilteringEnabled = false setts.FilteringEnabled = false
setts.ParentalEnabled = 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) { func TestClientSettings(t *testing.T) {
var r Result var r Result
filters := make(map[int]string) filters := make(map[int]string)
filters[0] = "||example.org^\n" filters[0] = "||example.org^\n"
d := NewForTest(&Config{ParentalEnabled: true}, filters) d := NewForTest(&Config{ParentalEnabled: true, SafeBrowsingEnabled: false}, filters)
defer d.Destroy() defer d.Destroy()
d.ParentalSensitivity = 3 d.ParentalSensitivity = 3
@ -479,6 +482,12 @@ func TestClientSettings(t *testing.T) {
t.Fatalf("CheckHost FilteredParental") 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: // override client settings:
d.FilterHandler = applyClientSettings d.FilterHandler = applyClientSettings
@ -488,11 +497,17 @@ func TestClientSettings(t *testing.T) {
t.Fatalf("CheckHost") t.Fatalf("CheckHost")
} }
// override parental settings // override parental settings (force disable parental)
r, _ = d.CheckHost("pornhub.com", dns.TypeA, "1.1.1.1") r, _ = d.CheckHost("pornhub.com", dns.TypeA, "1.1.1.1")
if r.IsFiltered { if r.IsFiltered {
t.Fatalf("CheckHost") 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 // BENCHMARKS