fix client IP address

This commit is contained in:
Andrey Meshkov 2018-12-24 23:06:36 +03:00
parent 8227970d39
commit 62606db1af
2 changed files with 15 additions and 2 deletions

View file

@ -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

View file

@ -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 ""
}