add comments to explain prefixConn

Also change a few variable names, for clarification.
This commit is contained in:
nervuri 2023-03-11 15:16:39 +00:00
parent 72b3259428
commit 2eedb5537f
1 changed files with 10 additions and 5 deletions

View File

@ -26,6 +26,9 @@ type tlsConnectionInfo struct {
NegotiatedProtocol string `json:"alpn_negotiated_protocol"` // ALPN
}
// Connection wrapper that enables exposing the Client Hello to the
// request handler.
// See https://github.com/FiloSottile/mostly-harmless/tree/main/talks/asyncnet
type prefixConn struct {
net.Conn
io.Reader
@ -73,21 +76,24 @@ func peek(conn net.Conn, tlsConfig *tls.Config) {
}
rawClientHello := buf.Bytes()
// "Put back" the Client Hello bytes we just read, so that they can be
// used in the TLS handshake. Concatenate the read bytes with the
// unread bytes using a MultiReader, inside a connection wrapper.
pConn := prefixConn{
Conn: conn,
Reader: io.MultiReader(&buf, conn),
}
server := tls.Server(pConn, tlsConfig)
err = server.Handshake()
tlsConnection := tls.Server(pConn, tlsConfig)
err = tlsConnection.Handshake()
if err != nil {
log.Println(err)
return
}
tlsHandler(server, rawClientHello)
requestHandler(tlsConnection, rawClientHello)
}
func tlsHandler(conn *tls.Conn, rawClientHello []byte) {
func requestHandler(conn *tls.Conn, rawClientHello []byte) {
defer conn.Close()
scanner := bufio.NewScanner(conn)
@ -98,7 +104,6 @@ func tlsHandler(conn *tls.Conn, rawClientHello []byte) {
log.Println(err)
return
}
//log.Println(line)
var protocol string
var path string // requested page