coredns plugin -- forgot to pass client's IP to querylog, fix that.

Closes #311
This commit is contained in:
Eugene Bujak 2018-08-31 19:59:04 +03:00
parent 2ca1a0e586
commit 379e14c28b
2 changed files with 7 additions and 3 deletions

View file

@ -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) { func (d *Plugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
start := time.Now() start := time.Now()
requests.Inc() requests.Inc()
state := request.Request{W: w, Req: r}
ip := state.IP()
// capture the written answer // capture the written answer
rrw := dnstest.NewRecorder(w) rrw := dnstest.NewRecorder(w)
rcode, err, result := d.serveDNSInternal(ctx, rrw, r) rcode, err, result := d.serveDNSInternal(ctx, rrw, r)
if rcode > 0 { if rcode > 0 {
// actually send the answer if we have one // actually send the answer if we have one
state := request.Request{W: w, Req: r}
answer := new(dns.Msg) answer := new(dns.Msg)
answer.SetRcode(r, rcode) answer.SetRcode(r, rcode)
state.SizeAndDo(answer) state.SizeAndDo(answer)
@ -497,7 +498,7 @@ func (d *Plugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// log // log
if d.QueryLogEnabled { if d.QueryLogEnabled {
logRequest(rrw.Msg, result, time.Since(start)) logRequest(rrw.Msg, result, time.Since(start), ip)
} }
return rcode, err return rcode, err
} }

View file

@ -23,18 +23,20 @@ type logEntry struct {
Result dnsfilter.Result Result dnsfilter.Result
Time time.Time Time time.Time
Elapsed time.Duration Elapsed time.Duration
IP string
} }
func init() { func init() {
logBuffer.SetCapacity(1000) 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{ entry := logEntry{
R: r, R: r,
Result: result, Result: result,
Time: time.Now(), Time: time.Now(),
Elapsed: elapsed, Elapsed: elapsed,
IP: ip,
} }
logBuffer.Enqueue(entry) logBuffer.Enqueue(entry)
} }
@ -52,6 +54,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
"reason": entry.Result.Reason.String(), "reason": entry.Result.Reason.String(),
"elapsed_ms": strconv.FormatFloat(entry.Elapsed.Seconds()*1000, 'f', -1, 64), "elapsed_ms": strconv.FormatFloat(entry.Elapsed.Seconds()*1000, 'f', -1, 64),
"time": entry.Time.Format(time.RFC3339), "time": entry.Time.Format(time.RFC3339),
"client": entry.IP,
} }
question := map[string]interface{}{ question := map[string]interface{}{
"host": strings.ToLower(strings.TrimSuffix(entry.R.Question[0].Name, ".")), "host": strings.ToLower(strings.TrimSuffix(entry.R.Question[0].Name, ".")),