From 62606db1af73a88b277043a0f141b99bad047403 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Mon, 24 Dec 2018 23:06:36 +0300 Subject: [PATCH] fix client IP address --- dnsforward/dnsforward.go | 2 +- dnsforward/querylog.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/dnsforward/dnsforward.go b/dnsforward/dnsforward.go index fb0f8012..54bf659a 100644 --- a/dnsforward/dnsforward.go +++ b/dnsforward/dnsforward.go @@ -252,7 +252,7 @@ func (s *Server) handleDNSRequest(p *proxy.Proxy, d *proxy.DNSContext) error { if d.Upstream != nil { upstreamAddr = d.Upstream.Address() } - logRequest(msg, d.Res, res, elapsed, d.Addr.String(), upstreamAddr) + logRequest(msg, d.Res, res, elapsed, d.Addr, upstreamAddr) } return nil diff --git a/dnsforward/querylog.go b/dnsforward/querylog.go index 7058a3b9..51ca3575 100644 --- a/dnsforward/querylog.go +++ b/dnsforward/querylog.go @@ -3,6 +3,7 @@ package dnsforward import ( "encoding/json" "fmt" + "net" "net/http" "strconv" "strings" @@ -41,10 +42,11 @@ type logEntry struct { Upstream string `json:",omitempty"` // if empty, means it was cached } -func logRequest(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Result, elapsed time.Duration, ip string, upstream string) { +func logRequest(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Result, elapsed time.Duration, addr net.Addr, upstream string) { var q []byte var a []byte var err error + ip := getIPString(addr) if question != nil { q, err = question.Pack() @@ -227,3 +229,14 @@ func HandleQueryLog(w http.ResponseWriter, r *http.Request) { http.Error(w, errorText, http.StatusInternalServerError) } } + +// getIPString is a helper function that extracts IP address from net.Addr +func getIPString(addr net.Addr) string { + switch addr := addr.(type) { + case *net.UDPAddr: + return addr.IP.String() + case *net.TCPAddr: + return addr.IP.String() + } + return "" +}