From b9f17b3c1968af63cc06654268b79aeb5d520f80 Mon Sep 17 00:00:00 2001 From: bloved Date: Sun, 24 Jan 2021 10:51:01 +0100 Subject: [PATCH 1/4] - network down is ignored if the selected configuration is local responder --- 01.conf.go | 7 +++++-- dns_client.go | 3 ++- dns_handler.go | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/01.conf.go b/01.conf.go index c0d788e..e3060d2 100644 --- a/01.conf.go +++ b/01.conf.go @@ -13,10 +13,13 @@ import ( "github.com/miekg/dns" ) +var localresponderConfigName string + type stringarray []string type urlsMap map[string]stringarray func init() { + localresponderConfigName = "__localresponder__" var MyConfRaw interface{} file, err := ioutil.ReadFile("config.json") @@ -248,7 +251,7 @@ func init() { } //************************ - // Local responser section + // Local responder section //************************ if MyConf["localresponder"] != nil { localresponder := MyConf["localresponder"].(map[string]interface{}) @@ -258,7 +261,7 @@ func init() { ZabovLocalResponder = localresponder["responder"].(string) if len(ZabovLocalResponder) > 0 { local := ZabovConfig{ZabovDNSArray: []string{ZabovLocalResponder}, references: 1} - ZabovConfigs["__localresponder__"] = &local + ZabovConfigs[localresponderConfigName] = &local fmt.Println("ZabovLocalResponder:", ZabovLocalResponder) } } diff --git a/dns_client.go b/dns_client.go index a6c7849..a9707f4 100644 --- a/dns_client.go +++ b/dns_client.go @@ -42,7 +42,8 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg { for { // round robin with retry - if !NetworkUp { + // local responder should always be available also if no internet connection + if !NetworkUp && localresponderConfigName != config { time.Sleep(10 * time.Second) go incrementStats("Network Problems ", 1) continue diff --git a/dns_handler.go b/dns_handler.go index 12c6801..c7e0353 100644 --- a/dns_handler.go +++ b/dns_handler.go @@ -269,7 +269,7 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { if len(ZabovLocalResponder) > 0 { if !strings.Contains(fqdn, ".") || (len(ZabovLocalDomain) > 0 && strings.HasSuffix(fqdn, ZabovLocalDomain)) { - config = "__localresponder__" + config = localresponderConfigName ret := ForwardQuery(r, config, true) w.WriteMsg(ret) go logQuery(remIP, fqdn, QType, config, timetable, "localresponder") @@ -297,7 +297,7 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { if len(ZabovLocalResponder) > 0 { // if set use local responder for reverse lookup (suffix ".in-addr.arpa.") - config = "__localresponder__" + config = localresponderConfigName } ret := ForwardQuery(r, config, true) w.WriteMsg(ret) -- 2.40.1 From 9b4e4fbcb970ce2da476e6b92eab07875bc01459 Mon Sep 17 00:00:00 2001 From: bloved Date: Mon, 25 Jan 2021 17:00:34 +0100 Subject: [PATCH 2/4] - optimization: blackholeip is parsed only once at startup --- 01.conf.go | 2 +- dns_handler.go | 2 +- main.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/01.conf.go b/01.conf.go index e3060d2..35b3cb3 100644 --- a/01.conf.go +++ b/01.conf.go @@ -110,7 +110,7 @@ func init() { conf.ZabovUpDNS = confRaw["upstream"].(string) conf.ZabovSingleBL = confRaw["singlefilters"].(string) conf.ZabovDoubleBL = confRaw["doublefilters"].(string) - conf.ZabovAddBL = confRaw["blackholeip"].(string) + conf.ZabovAddBL = net.ParseIP(confRaw["blackholeip"].(string)) conf.ZabovHostsFile = confRaw["hostsfile"].(string) conf.ZabovDNSArray = fileByLines(conf.ZabovUpDNS) diff --git a/dns_handler.go b/dns_handler.go index c7e0353..8a66673 100644 --- a/dns_handler.go +++ b/dns_handler.go @@ -282,7 +282,7 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { msg.Answer = append(msg.Answer, &dns.A{ Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60}, - A: net.ParseIP(ZabovConfig.ZabovAddBL), + A: ZabovConfig.ZabovAddBL, }) go logQuery(remIP, fqdn, QType, config, timetable, "killed") } else { diff --git a/main.go b/main.go index 3f778ce..f1de1ab 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ type handler struct{} type ZabovConfig struct { ZabovSingleBL string // json:singlefilters -> ZabovSingleBL list of urls returning a file with just names of domains ZabovDoubleBL string // json:doublefilters -> ZabovDoubleBL list of urls returning a file with IPdomain - ZabovAddBL string // json:blackholeip -> ZabovAddBL is the IP we want to send all the clients to. Usually is 127.0.0.1 + ZabovAddBL net.IP // json:blackholeip -> ZabovAddBL is the IP we want to send all the clients to. Usually is 127.0.0.1 ZabovHostsFile string // json:hostsfile -> ZabovHostsFile is the file we use to keep our hosts ZabovUpDNS string // json:upstream -> ZabovUpDNS keeps the name of upstream DNSs ZabovDNSArray []string // contains all the DNS we mention, parsed from ZabovUpDNS file -- 2.40.1 From c56789e19396837c00865ec020edf4a8af39ad48 Mon Sep 17 00:00:00 2001 From: bloved Date: Fri, 29 Jan 2021 13:01:18 +0100 Subject: [PATCH 3/4] - BUG FIX: IP Groups: blank timetable was not allowed; timetable & cfg con now be blank or undefined - FIX: enable response compression by default to be compatible with bogus DNS clients not understanding responses split to multiple UDP packets (FIX failure of Amazon PrimeVideo on LG WebOS) - new conf-specific setting "cache": allows disabling cache on configuration basis (default: cache is enabled) - updated documentation - added more debug logs --- 01.conf.go | 32 ++++++++++++++++++++++++-------- README.md | 19 +++++++++++++++++-- dns_client.go | 14 +++++++++++++- dns_handler.go | 4 ++-- main.go | 1 + 5 files changed, 57 insertions(+), 13 deletions(-) diff --git a/01.conf.go b/01.conf.go index 35b3cb3..937c334 100644 --- a/01.conf.go +++ b/01.conf.go @@ -113,6 +113,11 @@ func init() { conf.ZabovAddBL = net.ParseIP(confRaw["blackholeip"].(string)) conf.ZabovHostsFile = confRaw["hostsfile"].(string) + if confRaw["cache"] != nil { + conf.ZabovCache = confRaw["cache"].(bool) + } else { + conf.ZabovCache = true + } conf.ZabovDNSArray = fileByLines(conf.ZabovUpDNS) ZabovConfigs[name] = &conf @@ -219,8 +224,16 @@ func init() { } groupStruct.ips = append(groupStruct.ips, ip) } - groupStruct.cfg = groupMap["cfg"].(string) - groupStruct.timetable = groupMap["timetable"].(string) + if groupMap["cfg"] != nil { + groupStruct.cfg = groupMap["cfg"].(string) + } + if groupMap["timetable"] != nil { + groupStruct.timetable = groupMap["timetable"].(string) + } + if len(groupStruct.cfg) == 0 && len(groupStruct.timetable) == 0 { + log.Println("ip group error: specify cfg or timetable") + os.Exit(1) + } if len(groupStruct.cfg) > 0 { refConfig, ok := ZabovConfigs[groupStruct.cfg] if !ok { @@ -229,14 +242,17 @@ func init() { } else { refConfig.references++ } + fmt.Println("cfg:", groupStruct.cfg) } - fmt.Println("cfg:", groupStruct.cfg) - fmt.Println("timetable:", groupStruct.timetable) - _, ok := ZabovTimetables[groupStruct.timetable] - if !ok { - log.Println("inexistent timetable:", groupStruct.timetable) - os.Exit(1) + if len(groupStruct.timetable) > 0 { + fmt.Println("timetable:", groupStruct.timetable) + _, ok := ZabovTimetables[groupStruct.timetable] + if !ok { + log.Println("inexistent timetable:", groupStruct.timetable) + os.Exit(1) + } } + ZabovIPGroups = append(ZabovIPGroups, groupStruct) } } diff --git a/README.md b/README.md index a1db98f..13aee9a 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,8 @@ Minimal config file should look like: "singlefilters":"./urls-domains.txt", "doublefilters":"./urls-hosts.txt", "blackholeip":"127.0.0.1", - "hostsfile":"./urls-local.txt" + "hostsfile":"./urls-local.txt", + "cache":true }, } } @@ -84,7 +85,7 @@ configs: - doublefilters: name of the file, for blacklists following the "doublefilter" schema.(one URL per line) - blackholeip: IP address to return when the IP is banned. This is because you may want to avoid MX issues, mail loops on localhost, or you have a web server running on localhost - hostsfile: path where you keep your local blacklistfile : this is in the format "singlefilter", meaning one domain per line, unlike hosts file. - +- cache: if set to false disable the cache for this configuration. Boolean, defaults true Advanced configuration includes support for multiple configurations based on IP Source and timetables:
@@ -104,6 +105,7 @@ Advanced configuration includes support for multiple configurations based on IP
     },
     "ipaliases":{
         "pc8":"192.168.178.29",
+        "lg-tv":"192.168.178.10",
         "localhost":"127.0.0.1"
     },
     "ipgroups":[
@@ -111,6 +113,11 @@ Advanced configuration includes support for multiple configurations based on IP
             "ips":["localhost", "::1", "192.168.178.30", "192.168.178.31", "pc8"],
             "cfg":"",
             "timetable":"tt_children"
+        },
+        {
+            "ips":["lg-tv"],
+            "cfg":"tv",
+            "timetable":""
         }
     ],
     "timetables":{
@@ -146,6 +153,14 @@ Advanced configuration includes support for multiple configurations based on IP
             "doublefilters":"./urls-hosts-restricted.txt", 
             "blackholeip":"127.0.0.1",
             "hostsfile":"./urls-local.txt"
+        },
+        "tv":{
+            "upstream":"./dns-upstream.txt",
+            "singlefilters":"",
+            "doublefilters":"", 
+            "blackholeip":"127.0.0.1",
+            "hostsfile":"",
+            "cache":false
         }
     }
 }
diff --git a/dns_client.go b/dns_client.go
index a9707f4..241a678 100644
--- a/dns_client.go
+++ b/dns_client.go
@@ -2,6 +2,7 @@ package main
 
 import (
 	"fmt"
+	"log"
 	"time"
 
 	"math/rand"
@@ -14,7 +15,9 @@ import (
 //first server to answer wins
 //accepts config name to select the UP DNS source list
 func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
-
+	if ZabovDebug {
+		log.Println("ForwardQuery: nocache", nocache)
+	}
 	go incrementStats("ForwardQueries", 1)
 
 	r := new(dns.Msg)
@@ -29,6 +32,10 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
 			go incrementStats("CacheHit", 1)
 			cached.SetReply(query)
 			cached.Authoritative = true
+			if ZabovDebug {
+				log.Println("ForwardQuery: CacheHit")
+			}
+			cached.Compress = true
 			return cached
 
 		}
@@ -60,7 +67,12 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
 			go incrementStats(d, 1)
 			in.SetReply(query)
 			in.Authoritative = true
+			in.Compress = true
 			go DomainCache(lfqdn, in)
+			if ZabovDebug {
+				log.Println("ForwardQuery: OK!")
+			}
+
 			return in
 
 		}
diff --git a/dns_handler.go b/dns_handler.go
index 8a66673..9f06219 100644
--- a/dns_handler.go
+++ b/dns_handler.go
@@ -287,7 +287,7 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
 			go logQuery(remIP, fqdn, QType, config, timetable, "killed")
 		} else {
 			go logQuery(remIP, fqdn, QType, config, timetable, "forwarded")
-			ret := ForwardQuery(r, config, false)
+			ret := ForwardQuery(r, config, !ZabovConfig.ZabovCache)
 			w.WriteMsg(ret)
 		}
 	case dns.TypePTR:
@@ -303,7 +303,7 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
 		w.WriteMsg(ret)
 		go logQuery(remIP, msg.Question[0].Name, QType, config, timetable, "localresponder")
 	default:
-		ret := ForwardQuery(r, config, false)
+		ret := ForwardQuery(r, config, !ZabovConfig.ZabovCache)
 		w.WriteMsg(ret)
 		if len(ZabovDebugDBPath) > 0 {
 			go logQuery(remIP, msg.Question[0].Name, QType, config, timetable, "forwarded")
diff --git a/main.go b/main.go
index f1de1ab..b57fb60 100644
--- a/main.go
+++ b/main.go
@@ -41,6 +41,7 @@ type ZabovConfig struct {
 	ZabovHostsFile string   // json:hostsfile -> ZabovHostsFile is the file we use to keep our hosts
 	ZabovUpDNS     string   // json:upstream -> ZabovUpDNS keeps the name of upstream DNSs
 	ZabovDNSArray  []string // contains all the DNS we mention, parsed from ZabovUpDNS file
+	ZabovCache     bool     // allows to disable cache
 	references     int      // contains references to this config; if zero, config shall be removed
 }
 
-- 
2.40.1


From d74f2a973ece1e4d3090e5900da6115a4b390407 Mon Sep 17 00:00:00 2001
From: bloved 
Date: Fri, 29 Jan 2021 19:47:01 +0100
Subject: [PATCH 4/4] - BUG FIX: preserving DNS RCode in Zabov responses
 (SetReply(query) resets it to RcodeSuccess)   Some DNS client (such as MS
 Windows) misbehave in case of always positive responses

---
 dns_client.go | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/dns_client.go b/dns_client.go
index 241a678..861f503 100644
--- a/dns_client.go
+++ b/dns_client.go
@@ -30,7 +30,9 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
 	if !nocache {
 		if cached := GetDomainFromCache(lfqdn); cached != nil {
 			go incrementStats("CacheHit", 1)
+			Rcode := cached.MsgHdr.Rcode
 			cached.SetReply(query)
+			cached.MsgHdr.Rcode = Rcode
 			cached.Authoritative = true
 			if ZabovDebug {
 				log.Println("ForwardQuery: CacheHit")
@@ -65,7 +67,9 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
 			continue
 		} else {
 			go incrementStats(d, 1)
+			Rcode := in.MsgHdr.Rcode
 			in.SetReply(query)
+			in.MsgHdr.Rcode = Rcode
 			in.Authoritative = true
 			in.Compress = true
 			go DomainCache(lfqdn, in)
-- 
2.40.1