fix: sniffing handled HTTP host header incorrectly

This commit is contained in:
Toby 2024-08-16 15:46:30 -07:00
parent 442ee3898c
commit 48bf9b964a

View file

@ -112,11 +112,21 @@ func (h *Sniffer) TCP(stream quic.Stream, reqAddr *string) ([]byte, error) {
tr := &teeReader{Stream: stream, Pre: pre} tr := &teeReader{Stream: stream, Pre: pre}
req, _ := http.ReadRequest(bufio.NewReader(tr)) req, _ := http.ReadRequest(bufio.NewReader(tr))
if req != nil && req.Host != "" { if req != nil && req.Host != "" {
_, port, err := net.SplitHostPort(*reqAddr) // req.Host may already contain the port.
// If it does, just overwrite the whole address with req.Host.
// Otherwise, use the port in reqAddr.
_, _, err := net.SplitHostPort(req.Host)
if err != nil { if err != nil {
return nil, err // Not host:port format, append the port from reqAddr
_, port, err := net.SplitHostPort(*reqAddr)
if err != nil {
return nil, err
}
*reqAddr = net.JoinHostPort(req.Host, port)
} else {
// Already host:port format
*reqAddr = req.Host
} }
*reqAddr = net.JoinHostPort(req.Host, port)
} }
return tr.Buffer(), nil return tr.Buffer(), nil
} else if h.isTLS(pre) { } else if h.isTLS(pre) {