+ dnsfilter: use callback function for applying per-client settings

This commit is contained in:
Simon Zolin
2019-05-28 14:14:12 +03:00
parent 4bb7b654ab
commit 8bf76c331d
3 changed files with 98 additions and 23 deletions

View File

@@ -18,6 +18,7 @@ import (
// SAFE SEARCH
// PARENTAL
// FILTERING
// CLIENTS SETTINGS
// BENCHMARKS
// HELPERS
@@ -52,7 +53,7 @@ func NewForTestFilters(filters map[int]string) *Dnsfilter {
func (d *Dnsfilter) checkMatch(t *testing.T, hostname string) {
t.Helper()
ret, err := d.CheckHost(hostname, dns.TypeA)
ret, err := d.CheckHost(hostname, dns.TypeA, "")
if err != nil {
t.Errorf("Error while matching host %s: %s", hostname, err)
}
@@ -63,7 +64,7 @@ func (d *Dnsfilter) checkMatch(t *testing.T, hostname string) {
func (d *Dnsfilter) checkMatchIP(t *testing.T, hostname string, ip string, qtype uint16) {
t.Helper()
ret, err := d.CheckHost(hostname, qtype)
ret, err := d.CheckHost(hostname, qtype, "")
if err != nil {
t.Errorf("Error while matching host %s: %s", hostname, err)
}
@@ -77,7 +78,7 @@ func (d *Dnsfilter) checkMatchIP(t *testing.T, hostname string, ip string, qtype
func (d *Dnsfilter) checkMatchEmpty(t *testing.T, hostname string) {
t.Helper()
ret, err := d.CheckHost(hostname, dns.TypeA)
ret, err := d.CheckHost(hostname, dns.TypeA, "")
if err != nil {
t.Errorf("Error while matching host %s: %s", hostname, err)
}
@@ -212,7 +213,7 @@ func TestCheckHostSafeSearchYandex(t *testing.T) {
// Check host for each domain
for _, host := range yandex {
result, err := d.CheckHost(host, dns.TypeA)
result, err := d.CheckHost(host, dns.TypeA, "")
if err != nil {
t.Errorf("SafeSearch doesn't work for yandex domain `%s` cause %s", host, err)
}
@@ -235,7 +236,7 @@ func TestCheckHostSafeSearchGoogle(t *testing.T) {
// Check host for each domain
for _, host := range googleDomains {
result, err := d.CheckHost(host, dns.TypeA)
result, err := d.CheckHost(host, dns.TypeA, "")
if err != nil {
t.Errorf("SafeSearch doesn't work for %s cause %s", host, err)
}
@@ -255,7 +256,7 @@ func TestSafeSearchCacheYandex(t *testing.T) {
var err error
// Check host with disabled safesearch
result, err = d.CheckHost(domain, dns.TypeA)
result, err = d.CheckHost(domain, dns.TypeA, "")
if err != nil {
t.Fatalf("Cannot check host due to %s", err)
}
@@ -265,7 +266,7 @@ func TestSafeSearchCacheYandex(t *testing.T) {
// Enable safesearch
d.SafeSearchEnabled = true
result, err = d.CheckHost(domain, dns.TypeA)
result, err = d.CheckHost(domain, dns.TypeA, "")
if err != nil {
t.Fatalf("CheckHost for safesearh domain %s failed cause %s", domain, err)
}
@@ -295,7 +296,7 @@ func TestSafeSearchCacheGoogle(t *testing.T) {
d := NewForTest()
defer d.Destroy()
domain := "www.google.ru"
result, err := d.CheckHost(domain, dns.TypeA)
result, err := d.CheckHost(domain, dns.TypeA, "")
if err != nil {
t.Fatalf("Cannot check host due to %s", err)
}
@@ -324,7 +325,7 @@ func TestSafeSearchCacheGoogle(t *testing.T) {
}
}
result, err = d.CheckHost(domain, dns.TypeA)
result, err = d.CheckHost(domain, dns.TypeA, "")
if err != nil {
t.Fatalf("CheckHost for safesearh domain %s failed cause %s", domain, err)
}
@@ -441,7 +442,7 @@ func TestMatching(t *testing.T) {
d := NewForTestFilters(filters)
defer d.Destroy()
ret, err := d.CheckHost(test.hostname, dns.TypeA)
ret, err := d.CheckHost(test.hostname, dns.TypeA, "")
if err != nil {
t.Errorf("Error while matching host %s: %s", test.hostname, err)
}
@@ -455,6 +456,52 @@ func TestMatching(t *testing.T) {
}
}
// CLIENT SETTINGS
func applyClientSettings(clientAddr string, setts *RequestFilteringSettings) {
setts.FilteringEnabled = false
setts.ParentalEnabled = false
}
func TestClientSettings(t *testing.T) {
var r Result
filters := make(map[int]string)
filters[0] = "||example.org^\n"
d := NewForTestFilters(filters)
defer d.Destroy()
d.ParentalEnabled = true
d.ParentalSensitivity = 3
// no client settings:
// blocked by filters
r, _ = d.CheckHost("example.org", dns.TypeA, "1.1.1.1")
if !r.IsFiltered || r.Reason != FilteredBlackList {
t.Fatalf("CheckHost FilteredBlackList")
}
// blocked by parental
r, _ = d.CheckHost("pornhub.com", dns.TypeA, "1.1.1.1")
if !r.IsFiltered || r.Reason != FilteredParental {
t.Fatalf("CheckHost FilteredParental")
}
// override client settings:
d.FilterHandler = applyClientSettings
// override filtering settings
r, _ = d.CheckHost("example.org", dns.TypeA, "1.1.1.1")
if r.IsFiltered {
t.Fatalf("CheckHost")
}
// override parental settings
r, _ = d.CheckHost("pornhub.com", dns.TypeA, "1.1.1.1")
if r.IsFiltered {
t.Fatalf("CheckHost")
}
}
// BENCHMARKS
func BenchmarkSafeBrowsing(b *testing.B) {
@@ -463,7 +510,7 @@ func BenchmarkSafeBrowsing(b *testing.B) {
d.SafeBrowsingEnabled = true
for n := 0; n < b.N; n++ {
hostname := "wmconvirus.narod.ru"
ret, err := d.CheckHost(hostname, dns.TypeA)
ret, err := d.CheckHost(hostname, dns.TypeA, "")
if err != nil {
b.Errorf("Error while matching host %s: %s", hostname, err)
}
@@ -480,7 +527,7 @@ func BenchmarkSafeBrowsingParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
hostname := "wmconvirus.narod.ru"
ret, err := d.CheckHost(hostname, dns.TypeA)
ret, err := d.CheckHost(hostname, dns.TypeA, "")
if err != nil {
b.Errorf("Error while matching host %s: %s", hostname, err)
}