From 00a82aca87548acd0a3194773b5368b401da8e95 Mon Sep 17 00:00:00 2001 From: Niklas Date: Mon, 15 Feb 2021 12:02:12 +0100 Subject: [PATCH 1/5] added debug messages --- internal/authoritative.go | 2 ++ internal/cooldns.go | 12 ++++++++++++ internal/debug.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 internal/debug.go diff --git a/internal/authoritative.go b/internal/authoritative.go index cb6fcc8..f9f84eb 100644 --- a/internal/authoritative.go +++ b/internal/authoritative.go @@ -124,5 +124,7 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg, zone zoneView) { m.SetRcode(m, dns.RcodeNameError) } + debugResponse(m) + w.WriteMsg(m) } diff --git a/internal/cooldns.go b/internal/cooldns.go index af7d896..812582f 100644 --- a/internal/cooldns.go +++ b/internal/cooldns.go @@ -18,6 +18,13 @@ type zoneMap map[string][]zoneView // Start starts cooldns func Start(configPath string) { + + debug := os.Getenv("COOLDNS_DEBUG") + if len(debug) != 0 { + debugFlag = true + debugLog("Enabled debug") + } + config, err := loadConfig(configPath) if err != nil { log.Fatalf("Failed to load config: %s\n", err.Error()) @@ -80,6 +87,8 @@ func createServer(zones zoneMap, config config, aclList map[string]*net.IPNet, b return } + debugRequest(r, ip) + // Check if it is a ACME DNS-01 challange if config.Lego.Enable && handleACMERequest(w, r, acmeList) { return @@ -96,6 +105,7 @@ func createServer(zones zoneMap, config config, aclList map[string]*net.IPNet, b handleBlockedDomain(w, r) } else { // Forward request + debugLog("Forwarded request") in, _, err := c.Exchange(r, config.Forward.Server) if err != nil { @@ -167,6 +177,8 @@ func createHandler(zones []zoneView, config config, aclList map[string]*net.IPNe return } + debugRequest(r, ip) + // Check if it is a ACME DNS-01 challange if config.Lego.Enable && handleACMERequest(w, r, acmeList) { return diff --git a/internal/debug.go b/internal/debug.go new file mode 100644 index 0000000..338736d --- /dev/null +++ b/internal/debug.go @@ -0,0 +1,35 @@ +package cooldns + +import ( + "fmt" + "log" + "net" + + "github.com/miekg/dns" +) + +var debugFlag = false + +func debugLog(msg string) { + if !debugFlag { + return + } + + log.Printf("DEBUG: %s", msg) +} + +func debugRequest(r *dns.Msg, ip net.IP) { + if !debugFlag { + return + } + + debugLog(fmt.Sprintf("Request from %s: %s", ip, r.String())) +} + +func debugResponse(m *dns.Msg) { + if !debugFlag { + return + } + + debugLog(fmt.Sprintf("Response send: %s", m.String())) +} From 7255fc02c8f2a80a178e8bb547d0d4b3e3e2f6e1 Mon Sep 17 00:00:00 2001 From: Niklas Date: Wed, 24 Feb 2021 13:50:13 +0100 Subject: [PATCH 2/5] Revert "added debug messages" This reverts commit 00a82aca87548acd0a3194773b5368b401da8e95. --- internal/authoritative.go | 2 -- internal/cooldns.go | 12 ------------ internal/debug.go | 35 ----------------------------------- 3 files changed, 49 deletions(-) delete mode 100644 internal/debug.go diff --git a/internal/authoritative.go b/internal/authoritative.go index f9f84eb..cb6fcc8 100644 --- a/internal/authoritative.go +++ b/internal/authoritative.go @@ -124,7 +124,5 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg, zone zoneView) { m.SetRcode(m, dns.RcodeNameError) } - debugResponse(m) - w.WriteMsg(m) } diff --git a/internal/cooldns.go b/internal/cooldns.go index 812582f..af7d896 100644 --- a/internal/cooldns.go +++ b/internal/cooldns.go @@ -18,13 +18,6 @@ type zoneMap map[string][]zoneView // Start starts cooldns func Start(configPath string) { - - debug := os.Getenv("COOLDNS_DEBUG") - if len(debug) != 0 { - debugFlag = true - debugLog("Enabled debug") - } - config, err := loadConfig(configPath) if err != nil { log.Fatalf("Failed to load config: %s\n", err.Error()) @@ -87,8 +80,6 @@ func createServer(zones zoneMap, config config, aclList map[string]*net.IPNet, b return } - debugRequest(r, ip) - // Check if it is a ACME DNS-01 challange if config.Lego.Enable && handleACMERequest(w, r, acmeList) { return @@ -105,7 +96,6 @@ func createServer(zones zoneMap, config config, aclList map[string]*net.IPNet, b handleBlockedDomain(w, r) } else { // Forward request - debugLog("Forwarded request") in, _, err := c.Exchange(r, config.Forward.Server) if err != nil { @@ -177,8 +167,6 @@ func createHandler(zones []zoneView, config config, aclList map[string]*net.IPNe return } - debugRequest(r, ip) - // Check if it is a ACME DNS-01 challange if config.Lego.Enable && handleACMERequest(w, r, acmeList) { return diff --git a/internal/debug.go b/internal/debug.go deleted file mode 100644 index 338736d..0000000 --- a/internal/debug.go +++ /dev/null @@ -1,35 +0,0 @@ -package cooldns - -import ( - "fmt" - "log" - "net" - - "github.com/miekg/dns" -) - -var debugFlag = false - -func debugLog(msg string) { - if !debugFlag { - return - } - - log.Printf("DEBUG: %s", msg) -} - -func debugRequest(r *dns.Msg, ip net.IP) { - if !debugFlag { - return - } - - debugLog(fmt.Sprintf("Request from %s: %s", ip, r.String())) -} - -func debugResponse(m *dns.Msg) { - if !debugFlag { - return - } - - debugLog(fmt.Sprintf("Response send: %s", m.String())) -} From ebce6058631f2c3e81c237f29b03e815f35aeda1 Mon Sep 17 00:00:00 2001 From: Niklas Date: Wed, 24 Feb 2021 14:09:28 +0100 Subject: [PATCH 3/5] dont handle missing rr with nxdomain --- internal/authoritative.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/authoritative.go b/internal/authoritative.go index cb6fcc8..be08e48 100644 --- a/internal/authoritative.go +++ b/internal/authoritative.go @@ -121,7 +121,15 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg, zone zoneView) { } if len(m.Answer) == 0 { - m.SetRcode(m, dns.RcodeNameError) + var soa dns.RR + for _, v := range zone.rr[dns.TypeSOA] { + if len(v) == 1 { + soa = v[0] + } + } + if soa != nil { + m.Extra = append(m.Extra, soa) + } } w.WriteMsg(m) From 4394d6078dcc277424913a3c1162dae4d3d67fe5 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 25 Feb 2021 13:24:57 +0100 Subject: [PATCH 4/5] changed regex for parsing host format --- internal/blacklist.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/blacklist.go b/internal/blacklist.go index 49ae789..446fb1c 100644 --- a/internal/blacklist.go +++ b/internal/blacklist.go @@ -86,12 +86,10 @@ func parseRawBlacklist(blacklist configBlacklist, raw string) []string { // parseHostFormat parse the string in the format of a hostfile func parseHostFormat(raw string) []string { finalList := make([]string, 0) - reg := regexp.MustCompile(`(?mi)^\s*(#*)\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([a-zA-Z0-9\.\- ]+)$`) + reg := regexp.MustCompile(`(?m)^\s*(0\.0\.0\.0) ([a-zA-Z0-9-.]*)`) matches := reg.FindAllStringSubmatch(raw, -1) for _, match := range matches { - if match[1] != "#" { - finalList = append(finalList, dns.Fqdn(match[3])) - } + finalList = append(finalList, dns.Fqdn(match[2])) } return finalList From afdea7e64f2d3a165ae95fd022085381e5d88a76 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 25 Feb 2021 13:25:19 +0100 Subject: [PATCH 5/5] up block TTL --- internal/blacklist.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/blacklist.go b/internal/blacklist.go index 446fb1c..2634383 100644 --- a/internal/blacklist.go +++ b/internal/blacklist.go @@ -12,7 +12,7 @@ import ( "github.com/miekg/dns" ) -const blockTTL uint32 = 300 +const blockTTL uint32 = 604800 var nullIPv4 = net.IPv4(0, 0, 0, 0) var nullIPv6 = net.ParseIP("::/0")