From 945709f24e07301d2edff681b324620334523327 Mon Sep 17 00:00:00 2001 From: bloved Date: Wed, 13 Jan 2021 23:38:33 +0100 Subject: [PATCH] - config: - more sanity checks - added localresponder: if set use specified DNS server for local domains - ipaliases is now used in DNS responses (works same as /etc/hosts file) - ForwardQuery(): accept param to avoid cache --- 01.conf.go | 26 ++++++++++++++++++++++++-- adlist_hosts.go | 4 +++- adlist_single.go | 4 ++++ config.json | 4 ++++ dns_client.go | 14 ++++++++------ dns_handler.go | 28 +++++++++++++++++++++++----- hostfile.go | 3 +++ main.go | 6 ++++++ 8 files changed, 75 insertions(+), 14 deletions(-) diff --git a/01.conf.go b/01.conf.go index 90ae23a..21fe76d 100644 --- a/01.conf.go +++ b/01.conf.go @@ -46,15 +46,20 @@ func init() { ZabovCacheTTL = int(zabov["cachettl"].(float64)) ZabovKillTTL = int(zabov["killfilettl"].(float64)) + if MyConf["configs"] == nil { + log.Println("configs not set: you shall set at least 'default' config") + os.Exit(1) + } + configs := MyConf["configs"].(map[string]interface{}) if len(configs) == 0 { - log.Println("you shall set at least default config") + log.Println("you shall set at least 'default' config") os.Exit(1) } if configs["default"] == nil { - log.Println("default config is required") + log.Println("'default' config is required") os.Exit(1) } @@ -189,6 +194,23 @@ func init() { } ZabovIPGroups = append(ZabovIPGroups, groupStruct) } + + localresponder := MyConf["localresponder"].(map[string]interface{}) + + if localresponder != nil { + if localresponder["responder"] != nil { + ZabovLocalResponder = localresponder["responder"].(string) + if len(ZabovLocalResponder) > 0 { + local := ZabovConfig{} + local.ZabovDNSArray = []string{ZabovLocalResponder} + ZabovConfigs["__localresponder__"] = local + fmt.Println("ZabovLocalResponder:", ZabovLocalResponder) + } + } + if localresponder["localdomain"] != nil { + ZabovLocalDomain = localresponder["localdomain"].(string) + } + } //fmt.Println("ZabovConfigs:", ZabovConfigs) //fmt.Println("ZabovTimetables:", ZabovTimetables) //fmt.Println("ZabovIPAliases:", ZabovIPAliases) diff --git a/adlist_hosts.go b/adlist_hosts.go index daac544..eda7b21 100644 --- a/adlist_hosts.go +++ b/adlist_hosts.go @@ -102,7 +102,9 @@ func downloadDoubleThread() { fmt.Println("downloadDoubleThread: collecting urls from all configs...") for config := range ZabovConfigs { ZabovDoubleBL := ZabovConfigs[config].ZabovDoubleBL - + if len(ZabovDoubleBL) == 0 { + continue + } s := fileByLines(ZabovDoubleBL) for _, v := range s { configs := _urls[v] diff --git a/adlist_single.go b/adlist_single.go index e08af54..a7c1e7a 100644 --- a/adlist_single.go +++ b/adlist_single.go @@ -100,6 +100,10 @@ func downloadThread() { for config := range ZabovConfigs { ZabovSingleBL := ZabovConfigs[config].ZabovSingleBL + if len(ZabovSingleBL) == 0 { + continue + } + s := fileByLines(ZabovSingleBL) for _, v := range s { configs := _urls[v] diff --git a/config.json b/config.json index cec4562..e320ef3 100644 --- a/config.json +++ b/config.json @@ -6,6 +6,10 @@ "cachettl": 1, "killfilettl": 12 }, + "localresponder":{ + "responder":"192.168.178.1:53", + "localdomain":"fritz.box" + }, "ipaliases":{ "pc8":"192.168.178.29", "localhost":"127.0.0.1" diff --git a/dns_client.go b/dns_client.go index 866ab32..65bb214 100644 --- a/dns_client.go +++ b/dns_client.go @@ -13,7 +13,7 @@ import ( //ForwardQuery forwards the query to the upstream server //first server to answer wins //accepts config name to select the UP DNS source list -func ForwardQuery(query *dns.Msg, config string) *dns.Msg { +func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg { go incrementStats("ForwardQueries", 1) @@ -24,12 +24,14 @@ func ForwardQuery(query *dns.Msg, config string) *dns.Msg { fqdn := strings.TrimRight(query.Question[0].Name, ".") lfqdn := fmt.Sprintf("%d", query.Question[0].Qtype) + "." + fqdn - if cached := GetDomainFromCache(lfqdn); cached != nil { - go incrementStats("CacheHit", 1) - cached.SetReply(query) - cached.Authoritative = true - return cached + if !nocache { + if cached := GetDomainFromCache(lfqdn); cached != nil { + go incrementStats("CacheHit", 1) + cached.SetReply(query) + cached.Authoritative = true + return cached + } } c := new(dns.Client) diff --git a/dns_handler.go b/dns_handler.go index 7cfb769..ed88805 100644 --- a/dns_handler.go +++ b/dns_handler.go @@ -35,14 +35,14 @@ func confFromTimeTable(timetable string) string { if (nowHour > t.start.hour || (nowHour == t.start.hour && nowMinute >= t.start.minute)) && (nowHour < t.stop.hour || (nowHour == t.stop.hour && nowMinute <= t.stop.minute)) { - incrementStats("TIMETABLE IN: "+timetable, 1) + go incrementStats("TIMETABLE IN: "+timetable, 1) fmt.Println("confFromTimeTable: return IN", tt.cfgin) return tt.cfgin } } } } - incrementStats("TIMETABLE OUT: "+timetable, 1) + go incrementStats("TIMETABLE OUT: "+timetable, 1) fmt.Println("confFromTimeTable: return OUT", tt.cfgout) return tt.cfgout } @@ -77,7 +77,6 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { msg.SetReply(r) config := confFromIP(net.ParseIP(remIP)) - incrementStats("CONFIG: "+config, 1) ZabovConfig := ZabovConfigs[config] switch r.Question[0].Qtype { @@ -86,6 +85,24 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { domain := msg.Question[0].Name fqdn := strings.TrimRight(domain, ".") + if len(ZabovIPAliases[fqdn]) > 0 { + config = "__aliases__" + msg.Answer = append(msg.Answer, &dns.A{ + Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60}, + A: net.ParseIP(ZabovIPAliases[fqdn]), + }) + break + } + if len(ZabovLocalResponder) > 0 { + if !strings.Contains(fqdn, ".") || + (len(ZabovLocalDomain) > 0 && strings.HasSuffix(fqdn, ZabovLocalDomain)) { + config = "__localresponder__" + ret := ForwardQuery(r, config, true) + w.WriteMsg(ret) + break + } + + } if domainInKillfile(fqdn, config) { go incrementStats("Killed", 1) @@ -94,13 +111,14 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { A: net.ParseIP(ZabovConfig.ZabovAddBL), }) } else { - ret := ForwardQuery(r, config) + ret := ForwardQuery(r, config, false) w.WriteMsg(ret) } default: - ret := ForwardQuery(r, config) + ret := ForwardQuery(r, config, false) w.WriteMsg(ret) } + go incrementStats("CONFIG: "+config, 1) w.WriteMsg(&msg) } diff --git a/hostfile.go b/hostfile.go index a2946ff..c64cb98 100644 --- a/hostfile.go +++ b/hostfile.go @@ -19,6 +19,9 @@ func ingestLocalBlacklists() { _files := urlsMap{} for config := range ZabovConfigs { ZabovHostsFile := ZabovConfigs[config].ZabovHostsFile + if len(ZabovHostsFile) == 0 { + continue + } configs := _files[ZabovHostsFile] if configs == nil { configs = stringarray{} diff --git a/main.go b/main.go index e79b1de..41546c6 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,12 @@ var ZabovCacheTTL int //ZabovKillTTL is the amount of hours we cache the killfile (global) var ZabovKillTTL int +//ZabovLocalResponder is the default DNS server for loca domains +var ZabovLocalResponder string + +//ZabovLocalDomain is the default local domain +var ZabovLocalDomain string + type handler struct{} // ZabovConfig contains all Zabov configs