Skip to content

Commit

Permalink
added parsing for dwnReqHost for logging purposes. fetches the idna a…
Browse files Browse the repository at this point in the history
… label of a host header.
  • Loading branch information
simonmittag committed May 4, 2023
1 parent 938ddb0 commit a1d5684
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -229,6 +230,7 @@ type Down struct {
Req *http.Request
Resp Resp
Method string
Host string
Path string
URI string
UserAgent string
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
24 changes: 24 additions & 0 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions proxyhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit a1d5684

Please sign in to comment.