From c16dc6cf626428eeeaeb7f19262b08dee9333ea5 Mon Sep 17 00:00:00 2001
From: Simon Zolin <s.zolin@adguard.com>
Date: Tue, 24 Mar 2020 11:30:22 +0300
Subject: [PATCH 1/2] - DNS: filtering didn't work

---
 dnsforward/dnsforward.go | 8 ++++++--
 util/auto_hosts.go       | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/dnsforward/dnsforward.go b/dnsforward/dnsforward.go
index 59b3e516..9c9121c5 100644
--- a/dnsforward/dnsforward.go
+++ b/dnsforward/dnsforward.go
@@ -686,12 +686,12 @@ func processFilteringAfterResponse(ctx *dnsContext) int {
 			d.Res.Answer = answer
 		}
 
-	case dnsfilter.RewriteEtcHosts:
 	case dnsfilter.NotFilteredWhiteList:
 		// nothing
 
 	default:
-		if !ctx.protectionEnabled {
+		if !ctx.protectionEnabled || // filters are disabled: there's nothing to check for
+			!ctx.responseFromUpstream { // only check response if it's from an upstream server
 			break
 		}
 		origResp2 := d.Res
@@ -817,6 +817,10 @@ func (s *Server) updateStats(d *proxy.DNSContext, elapsed time.Duration, res dns
 	case dnsfilter.NotFilteredWhiteList:
 		fallthrough
 	case dnsfilter.NotFilteredError:
+		fallthrough
+	case dnsfilter.ReasonRewrite:
+		fallthrough
+	case dnsfilter.RewriteEtcHosts:
 		e.Result = stats.RNotFiltered
 
 	case dnsfilter.FilteredSafeBrowsing:
diff --git a/util/auto_hosts.go b/util/auto_hosts.go
index a2ea56ae..0af5bf08 100644
--- a/util/auto_hosts.go
+++ b/util/auto_hosts.go
@@ -225,11 +225,15 @@ func (a *AutoHosts) update() {
 }
 
 // Process - get the list of IP addresses for the hostname
+// Return nil if not found
 func (a *AutoHosts) Process(host string) []net.IP {
+	var ipsCopy []net.IP
 	a.lock.Lock()
 	ips, _ := a.table[host]
-	ipsCopy := make([]net.IP, len(ips))
-	copy(ipsCopy, ips)
+	if len(ips) != 0 {
+		ipsCopy = make([]net.IP, len(ips))
+		copy(ipsCopy, ips)
+	}
 	a.lock.Unlock()
 	return ipsCopy
 }

From 7c192212410ef877aea5c0d018cc0f9ae8b41879 Mon Sep 17 00:00:00 2001
From: Simon Zolin <s.zolin@adguard.com>
Date: Tue, 24 Mar 2020 11:49:42 +0300
Subject: [PATCH 2/2] minor

---
 util/auto_hosts_test.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/util/auto_hosts_test.go b/util/auto_hosts_test.go
index 6243b5ca..ce06e361 100644
--- a/util/auto_hosts_test.go
+++ b/util/auto_hosts_test.go
@@ -37,7 +37,7 @@ func TestAutoHosts(t *testing.T) {
 	ips := ah.Process("localhost")
 	assert.True(t, ips[0].Equal(net.ParseIP("127.0.0.1")))
 	ips = ah.Process("newhost")
-	assert.True(t, len(ips) == 0)
+	assert.True(t, ips == nil)
 
 	table := ah.List()
 	ips, _ = table["host"]