added debug messages

This commit is contained in:
Niklas 2021-02-15 12:02:12 +01:00
parent 8d366a9833
commit 00a82aca87
3 changed files with 49 additions and 0 deletions

View File

@ -124,5 +124,7 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg, zone zoneView) {
m.SetRcode(m, dns.RcodeNameError) m.SetRcode(m, dns.RcodeNameError)
} }
debugResponse(m)
w.WriteMsg(m) w.WriteMsg(m)
} }

View File

@ -18,6 +18,13 @@ type zoneMap map[string][]zoneView
// Start starts cooldns // Start starts cooldns
func Start(configPath string) { func Start(configPath string) {
debug := os.Getenv("COOLDNS_DEBUG")
if len(debug) != 0 {
debugFlag = true
debugLog("Enabled debug")
}
config, err := loadConfig(configPath) config, err := loadConfig(configPath)
if err != nil { if err != nil {
log.Fatalf("Failed to load config: %s\n", err.Error()) 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 return
} }
debugRequest(r, ip)
// Check if it is a ACME DNS-01 challange // Check if it is a ACME DNS-01 challange
if config.Lego.Enable && handleACMERequest(w, r, acmeList) { if config.Lego.Enable && handleACMERequest(w, r, acmeList) {
return return
@ -96,6 +105,7 @@ func createServer(zones zoneMap, config config, aclList map[string]*net.IPNet, b
handleBlockedDomain(w, r) handleBlockedDomain(w, r)
} else { } else {
// Forward request // Forward request
debugLog("Forwarded request")
in, _, err := c.Exchange(r, config.Forward.Server) in, _, err := c.Exchange(r, config.Forward.Server)
if err != nil { if err != nil {
@ -167,6 +177,8 @@ func createHandler(zones []zoneView, config config, aclList map[string]*net.IPNe
return return
} }
debugRequest(r, ip)
// Check if it is a ACME DNS-01 challange // Check if it is a ACME DNS-01 challange
if config.Lego.Enable && handleACMERequest(w, r, acmeList) { if config.Lego.Enable && handleACMERequest(w, r, acmeList) {
return return

35
internal/debug.go Normal file
View File

@ -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()))
}