From 815ca66828c283032087fa046ddba189f61bc892 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Mon, 18 Sep 2023 14:27:36 -0400 Subject: [PATCH 1/2] feat: query logging Signed-off-by: Chris Gianelloni --- internal/config/config.go | 2 ++ internal/dns/dns.go | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index f19bf1f..cd27cc2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -38,6 +38,7 @@ type Config struct { type LoggingConfig struct { Healthchecks bool `yaml:"healthchecks" envconfig:"LOGGING_HEALTHCHECKS"` Level string `yaml:"level" envconfig:"LOGGING_LEVEL"` + QueryLog bool `yaml:"queryLog" envconfig:"LOGGING_QUERY_LOG"` } type DnsConfig struct { @@ -76,6 +77,7 @@ var globalConfig = &Config{ Logging: LoggingConfig{ Level: "info", Healthchecks: false, + QueryLog: true, }, Dns: DnsConfig{ ListenAddress: "", diff --git a/internal/dns/dns.go b/internal/dns/dns.go index e0fab65..0a6151d 100644 --- a/internal/dns/dns.go +++ b/internal/dns/dns.go @@ -38,6 +38,16 @@ func handleQuery(w dns.ResponseWriter, r *dns.Msg) { cfg := config.GetConfig() m := new(dns.Msg) + if cfg.Logging.QueryLog { + for _, q := range r.Question { + logger.Infof("query: name: %s, type: %s, class: %s", + fmt.Sprintf("%s", q.Name), + fmt.Sprintf("%s", dns.Type(q.Qtype).String()), + fmt.Sprintf("%s", dns.Class(q.Qclass).String()), + ) + } + } + // Split query name into labels and lookup each domain and parent until we get a hit queryLabels := dns.SplitDomainName(r.Question[0].Name) for startLabelIdx := 0; startLabelIdx < len(queryLabels); startLabelIdx++ { From 0eb121a5e2e7a25846c930bf1bb4c773085291c5 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Mon, 18 Sep 2023 16:38:18 -0400 Subject: [PATCH 2/2] fix: remove fmt to appease golangci-lint Signed-off-by: Chris Gianelloni --- internal/dns/dns.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/dns/dns.go b/internal/dns/dns.go index 0a6151d..0cef299 100644 --- a/internal/dns/dns.go +++ b/internal/dns/dns.go @@ -41,9 +41,9 @@ func handleQuery(w dns.ResponseWriter, r *dns.Msg) { if cfg.Logging.QueryLog { for _, q := range r.Question { logger.Infof("query: name: %s, type: %s, class: %s", - fmt.Sprintf("%s", q.Name), - fmt.Sprintf("%s", dns.Type(q.Qtype).String()), - fmt.Sprintf("%s", dns.Class(q.Qclass).String()), + q.Name, + dns.Type(q.Qtype).String(), + dns.Class(q.Qclass).String(), ) } }