strip default ports from Host header in http proxy

Previously the Host header used the destination address verbatim, including
the port even for default ports (80 for HTTPS, 443 for HTTPS). This violates
HTTP specifications which require omitting default ports in Host headers.

This ensures compliant Host headers like "example.com" instead of "example.com:80"

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
Jianhui Zhao
2025-08-10 21:30:50 +08:00
parent 1470e04c4d
commit 2f04a373c1
+11 -6
View File
@@ -139,9 +139,7 @@ func doHttpProxy(srv *RttyServer, c net.Conn) {
return
}
hostHeaderRewrite := ses.destaddr
destAddr := genDestAddr(hostHeaderRewrite, ses.https)
destAddr, hostHeaderRewrite := genDestAddrAndHost(ses.destaddr, ses.https)
hpw := &HttpProxyWriter{
destAddr: destAddr,
@@ -318,10 +316,10 @@ func sendHttpReq(dev *Device, https bool, srcAddr []byte, destAddr []byte, data
dev.WriteMsg(proto.MsgTypeHttp, bb)
}
func genDestAddr(addr string, https bool) []byte {
func genDestAddrAndHost(addr string, https bool) ([]byte, string) {
destIP, destPort, err := httpProxyVaildAddr(addr, https)
if err != nil {
return nil
return nil, ""
}
b := make([]byte, 6)
@@ -329,7 +327,14 @@ func genDestAddr(addr string, https bool) []byte {
binary.BigEndian.PutUint16(b[4:], destPort)
return b
host := addr
ips, ports, _ := net.SplitHostPort(addr)
if ports == "80" || ports == "443" {
host = ips
}
return b, host
}
func tcpAddr2Bytes(addr *net.TCPAddr, b []byte) {