Skip to content

Commit 8d99af1

Browse files
committed
fix corrupted hostname from partial connection read
1 parent dc3acbd commit 8d99af1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pkg/tcpproxy/tcp.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ func (p *TCPProxy) Handle(conn net.Conn) {
6262
// See: https://www.ibm.com/docs/en/ztpf/1.1.0.15?topic=sessions-ssl-record-format
6363
data := make([]byte, 16384)
6464

65-
length, err := conn.Read(data)
65+
// read the tls header first
66+
_, err := io.ReadFull(conn, data[:parser.TLSHeaderLength])
67+
if err != nil {
68+
klog.V(4).ErrorS(err, "Error reading TLS header from the connection")
69+
return
70+
}
71+
// get the total data length then read the rest
72+
length := int(data[3])<<8 + int(data[4]) + parser.TLSHeaderLength
73+
_, err = io.ReadFull(conn, data[parser.TLSHeaderLength:length])
6674
if err != nil {
6775
klog.V(4).ErrorS(err, "Error reading data from the connection")
6876
return
@@ -115,7 +123,7 @@ func (p *TCPProxy) Handle(conn net.Conn) {
115123
} else {
116124
_, err = clientConn.Write(data[:length])
117125
if err != nil {
118-
klog.Errorf("Error writing the first 4k of proxy data: %v", err)
126+
klog.Errorf("Error writing the first %d bytes of proxy data: %v", length, err)
119127
clientConn.Close()
120128
}
121129
}

0 commit comments

Comments
 (0)