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 { if d.Upstream != nil {
upstreamAddr = d.Upstream.Address() 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 return nil

View File

@ -3,6 +3,7 @@ package dnsforward
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -41,10 +42,11 @@ type logEntry struct {
Upstream string `json:",omitempty"` // if empty, means it was cached 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 q []byte
var a []byte var a []byte
var err error var err error
ip := getIPString(addr)
if question != nil { if question != nil {
q, err = question.Pack() q, err = question.Pack()
@ -227,3 +229,14 @@ func HandleQueryLog(w http.ResponseWriter, r *http.Request) {
http.Error(w, errorText, http.StatusInternalServerError) 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 ""
}