From 379e14c28b18c0632e42760aca94aa7b72b05885 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Fri, 31 Aug 2018 19:59:04 +0300 Subject: [PATCH] coredns plugin -- forgot to pass client's IP to querylog, fix that. Closes #311 --- coredns_plugin/coredns_plugin.go | 5 +++-- coredns_plugin/querylog.go | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/coredns_plugin/coredns_plugin.go b/coredns_plugin/coredns_plugin.go index 8d999d4c..a2347cb7 100644 --- a/coredns_plugin/coredns_plugin.go +++ b/coredns_plugin/coredns_plugin.go @@ -452,13 +452,14 @@ func (d *Plugin) serveDNSInternal(ctx context.Context, w dns.ResponseWriter, r * func (d *Plugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { start := time.Now() requests.Inc() + state := request.Request{W: w, Req: r} + ip := state.IP() // capture the written answer rrw := dnstest.NewRecorder(w) rcode, err, result := d.serveDNSInternal(ctx, rrw, r) if rcode > 0 { // actually send the answer if we have one - state := request.Request{W: w, Req: r} answer := new(dns.Msg) answer.SetRcode(r, rcode) state.SizeAndDo(answer) @@ -497,7 +498,7 @@ func (d *Plugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) // log if d.QueryLogEnabled { - logRequest(rrw.Msg, result, time.Since(start)) + logRequest(rrw.Msg, result, time.Since(start), ip) } return rcode, err } diff --git a/coredns_plugin/querylog.go b/coredns_plugin/querylog.go index ae32b9cd..4fdcd345 100644 --- a/coredns_plugin/querylog.go +++ b/coredns_plugin/querylog.go @@ -23,18 +23,20 @@ type logEntry struct { Result dnsfilter.Result Time time.Time Elapsed time.Duration + IP string } func init() { logBuffer.SetCapacity(1000) } -func logRequest(r *dns.Msg, result dnsfilter.Result, elapsed time.Duration) { +func logRequest(r *dns.Msg, result dnsfilter.Result, elapsed time.Duration, ip string) { entry := logEntry{ R: r, Result: result, Time: time.Now(), Elapsed: elapsed, + IP: ip, } logBuffer.Enqueue(entry) } @@ -52,6 +54,7 @@ func handler(w http.ResponseWriter, r *http.Request) { "reason": entry.Result.Reason.String(), "elapsed_ms": strconv.FormatFloat(entry.Elapsed.Seconds()*1000, 'f', -1, 64), "time": entry.Time.Format(time.RFC3339), + "client": entry.IP, } question := map[string]interface{}{ "host": strings.ToLower(strings.TrimSuffix(entry.R.Question[0].Name, ".")),