diff --git a/logger.go b/logger.go index 9252d56..bf4e9fc 100644 --- a/logger.go +++ b/logger.go @@ -12,6 +12,7 @@ import ( const dwnReqRemoteAddr = "dwnReqRemoteAddr" const dwnReqPort = "dwnReqPort" const dwnReqPath = "dwnReqPath" +const dwnReqHost = "dwnReqHost" const dwnReqMethod = "dwnReqMethod" const dwnReqUserAgent = "dwnReqUserAgent" const dwnReqHttpVer = "dwnReqHttpVer" diff --git a/proxy.go b/proxy.go index d1e966b..e008c50 100644 --- a/proxy.go +++ b/proxy.go @@ -14,6 +14,7 @@ import ( "github.com/lestrrat-go/jwx/jws" "github.com/lestrrat-go/jwx/jwt" "github.com/rs/zerolog" + "golang.org/x/net/idna" "io" "io/ioutil" "net/http" @@ -229,6 +230,7 @@ type Down struct { Req *http.Request Resp Resp Method string + Host string Path string URI string UserAgent string @@ -366,6 +368,7 @@ func (proxy *Proxy) parseIncoming(request *http.Request) *Proxy { if !Runner.DisableXRequestInfo { proxy.XRequestInfo = parseXRequestInfo(request) } + proxy.Dwn.Host = parseHost(request) proxy.Dwn.Path = request.URL.EscapedPath() proxy.Dwn.URI = request.URL.RequestURI() proxy.Dwn.AcceptEncoding = parseAcceptEncoding(request) @@ -518,6 +521,15 @@ func infoOrDebugEv(proxy *Proxy) *zerolog.Event { return ev } +const colon = ":" + +func parseHost(request *http.Request) string { + //ignore conversion errors + al := strings.Split(request.Host, colon)[0] + al2, _ := idna.ToASCII(al) + return al2 +} + func parseMethod(request *http.Request) string { return strings.ToUpper(request.Method) } diff --git a/proxy_test.go b/proxy_test.go index 2c95fc1..7d38fd9 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -463,6 +463,30 @@ func TestParseRequestBody(t *testing.T) { } } +func TestParseHost(t *testing.T) { + var tests = []struct { + name string + url string + host string + }{ + {name: "simple", url: "http://host/path", host: "host"}, + {name: "simple with port", url: "http://host:8080/path", host: "host"}, + {name: "fqdn with port", url: "http://sub.host.com:8080/path", host: "sub.host.com"}, + {name: "idna simple", url: "http://aaa😀😀😀:8080/path", host: "xn--aaa-th33baa"}, + {name: "idna fqdn", url: "http://aaa😀😀😀.com:8080/path", host: "xn--aaa-th33baa.com"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req, _ := http.NewRequest("GET", tt.url, nil) + got := parseHost(req) + if got != tt.host { + t.Errorf("url %v, want host %v, got %v", tt.url, tt.host, got) + } + }) + } +} + func TestParseRequestBodyTooLarge(t *testing.T) { Runner = mockRuntime() Runner.Connection.Downstream.MaxBodyBytes = 65535 diff --git a/proxyhandler.go b/proxyhandler.go index 2f91d23..cc56757 100644 --- a/proxyhandler.go +++ b/proxyhandler.go @@ -460,6 +460,7 @@ func logHandledDownstreamRoundtrip(proxy *Proxy) { ev = ev.Str(dwnReqListnr, proxy.Dwn.Listener). Str(dwnReqPort, fmt.Sprintf(pdS, proxy.Dwn.Port)). + Str(dwnReqHost, proxy.Dwn.Host). Str(dwnReqPath, proxy.Dwn.Path). Str(dwnReqRemoteAddr, ipr.extractAddr(proxy.Dwn.Req.RemoteAddr)). Str(dwnReqMethod, proxy.Dwn.Method).