Skip to content

Commit

Permalink
Merge pull request #1183 from apernet/fix-http-sniff
Browse files Browse the repository at this point in the history
fix: sniffing handled HTTP host header incorrectly
  • Loading branch information
tobyxdd authored Aug 17, 2024
2 parents 442ee38 + 55c3a06 commit f2712aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion extras/sniff/sniff.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,17 @@ func (h *Sniffer) TCP(stream quic.Stream, reqAddr *string) ([]byte, error) {
tr := &teeReader{Stream: stream, Pre: pre}
req, _ := http.ReadRequest(bufio.NewReader(tr))
if req != nil && req.Host != "" {
// req.Host can be host:port, in which case we need to extract the host part
host, _, err := net.SplitHostPort(req.Host)
if err != nil {
// No port, just use the whole string
host = req.Host
}
_, port, err := net.SplitHostPort(*reqAddr)
if err != nil {
return nil, err
}
*reqAddr = net.JoinHostPort(req.Host, port)
*reqAddr = net.JoinHostPort(host, port)
}
return tr.Buffer(), nil
} else if h.isTLS(pre) {
Expand Down
12 changes: 12 additions & 0 deletions extras/sniff/sniff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ func TestSnifferTCP(t *testing.T) {
assert.Equal(t, *buf, putback)
assert.Equal(t, "example.com:80", reqAddr)

// Test HTTP with Host as host:port
*buf = []byte("GET / HTTP/1.1\r\n" +
"Host: example.com:8080\r\n" +
"User-Agent: test-agent\r\n" +
"Accept: */*\r\n\r\n")
index = 0
reqAddr = "222.222.222.222:10086"
putback, err = sniffer.TCP(stream, &reqAddr)
assert.NoError(t, err)
assert.Equal(t, *buf, putback)
assert.Equal(t, "example.com:10086", reqAddr)

// Test TLS
*buf, err = base64.StdEncoding.DecodeString("FgMBARcBAAETAwPJL2jlt1OAo+Rslkjv/aqKiTthKMaCKg2Gvd+uALDbDCDdY+UIk8ouadEB9fC3j52Y1i7SJZqGIgBRIS6kKieYrAAoEwITAcAswCvAMMAvwCTAI8AowCfACsAJwBTAEwCdAJwAPQA8ADUALwEAAKIAAAAOAAwAAAlpcGluZm8uaW8ABQAFAQAAAAAAKwAJCAMEAwMDAgMBAA0AGgAYCAQIBQgGBAEFAQIBBAMFAwIDAgIGAQYDACMAAAAKAAgABgAdABcAGAAQAAsACQhodHRwLzEuMQAzACYAJAAdACBguQbqNJNyamYxYcrBFpBP7pWv5TgZsP9gwGtMYNKVBQAxAAAAFwAA/wEAAQAALQACAQE=")
assert.NoError(t, err)
Expand Down

0 comments on commit f2712aa

Please sign in to comment.