func (p *Proxy) handleConn(ctx context.Context, clientConn net.Conn) error {
	hello, conn, err := p.readHelloMessageWithoutTLSTermination(clientConn)
	if err != nil {
		return trace.Wrap(err)
	}

	handlerDesc, err := p.getHandlerDescBaseOnClientHelloMsg(hello)
	if err != nil {
		return trace.Wrap(err)
	}

	connInfo := ConnectionInfo{
		SNI:  hello.ServerName,
		ALPN: hello.SupportedProtos,
	}

	if handlerDesc.ForwardTLS {
		return trace.Wrap(handlerDesc.handle(ctx, conn, connInfo))
	}

	tlsConn := tls.Server(conn, p.getTLSConfig(handlerDesc))
	if err := tlsConn.SetReadDeadline(p.cfg.Clock.Now().Add(p.cfg.ReadDeadline)); err != nil {
		return trace.Wrap(err)
	}
	if err := tlsConn.Handshake(); err != nil {
		return trace.Wrap(err)
	}
	if err := tlsConn.SetReadDeadline(time.Time{}); err != nil {
		return trace.Wrap(err)
	}

	isDatabaseConnection, err := dbcommon.IsDatabaseConnection(tlsConn.ConnectionState())
	if err != nil {
		p.log.WithError(err).Debug("Failed to check if connection is database connection.")
	}
	if isDatabaseConnection {
		return trace.Wrap(p.handleDatabaseConnection(ctx, tlsConn, connInfo))
	}
	return trace.Wrap(handlerDesc.handle(ctx, tlsConn, connInfo))
}
// handleConn routes incoming connection based on SNI TLS information to the proper Handler by following steps:
// 1) Read TLS hello message without TLS termination and returns conn that will be used for further operations.
// 2) Get routing rules for p.Router.Router based on SNI and ALPN fields read in step 1.
// 3) If the selected handler was configured with the ForwardTLS
//    forwards the connection to the handler without TLS termination.
// 4) Trigger TLS handshake and terminates the TLS connection.
// 5) For backward compatibility check RouteToDatabase identity field
//    was set if yes forward to the generic TLS DB handler.
// 6) Forward connection to the handler obtained in step 2.
handleConn routes incoming connection based on SNI TLS information to the proper Handler by following steps:
1) Read TLS hello message without TLS termination and returns conn that will be used for further operations.
2) Get routing rules for p.Router.Router based on SNI and ALPN fields read in step 1.
3) If the selected handler was configured with the ForwardTLS
forwards the connection to the handler without TLS termination.
4) Trigger TLS handshake and terminates the TLS connection.
5) For backward compatibility check RouteToDatabase identity field
was set if yes forward to the generic TLS DB handler.
6) Forward connection to the handler obtained in step 2.
// handleConn routes incoming connection based on SNI TLS information to the proper Handler by following steps : // 1 ) Read TLS hello message without TLS termination and returns conn that will be used for further operations . // 2 ) Get routing rules for p . Router . Router based on SNI and ALPN fields read in step 1 . // 3 ) If the selected handler was configured with the ForwardTLS // forwards the connection to the handler without TLS termination . // 4 ) Trigger TLS handshake and terminates the TLS connection . // 5 ) For backward compatibility check RouteToDatabase identity field // was set if yes forward to the generic TLS DB handler . // 6 ) Forward connection to the handler obtained in step 2 .
func (p *Proxy) handleConn(ctx context.Context, clientConn net.Conn) error {
	hello, conn, err := p.readHelloMessageWithoutTLSTermination(clientConn)
	if err != nil {
		return trace.Wrap(err)
	}
	handlerDesc, err := p.getHandlerDescBaseOnClientHelloMsg(hello)
	if err != nil {
		return trace.Wrap(err)
	}
	connInfo := ConnectionInfo{
		SNI:  hello.ServerName,
		ALPN: hello.SupportedProtos,
	}
	if handlerDesc.ForwardTLS {
		return trace.Wrap(handlerDesc.handle(ctx, conn, connInfo))
	}
	tlsConn := tls.Server(conn, p.getTLSConfig(handlerDesc))
	if err := tlsConn.SetReadDeadline(p.cfg.Clock.Now().Add(p.cfg.ReadDeadline)); err != nil {
		return trace.Wrap(err)
	}
	if err := tlsConn.Handshake(); err != nil {
		return trace.Wrap(err)
	}
	if err := tlsConn.SetReadDeadline(time.Time{}); err != nil {
		return trace.Wrap(err)
	}
	isDatabaseConnection, err := dbcommon.IsDatabaseConnection(tlsConn.ConnectionState())
	if err != nil {
		p.log.WithError(err).Debug("Failed to check if connection is database connection.")
	}
	if isDatabaseConnection {
		return trace.Wrap(p.handleDatabaseConnection(ctx, tlsConn, connInfo))
	}
	return trace.Wrap(handlerDesc.handle(ctx, tlsConn, connInfo))
}
func ( p * Proxy ) handleConn ( ctx context . Context , clientConn net . Conn ) error { hello , conn , err := p . readHelloMessageWithoutTLSTermination ( clientConn ) if err != nil { return trace . Wrap ( err ) } handlerDesc , err := p . getHandlerDescBaseOnClientHelloMsg ( hello ) if err != nil { return trace . Wrap ( err ) } connInfo := ConnectionInfo { SNI : hello . ServerName , ALPN : hello . SupportedProtos , } if handlerDesc . ForwardTLS { return trace . Wrap ( handlerDesc . handle ( ctx , conn , connInfo ) ) } tlsConn := tls . Server ( conn , p . getTLSConfig ( handlerDesc ) ) if err := tlsConn . SetReadDeadline ( p . cfg . Clock . Now ( ) . Add ( p . cfg . ReadDeadline ) ) ; err != nil { return trace . Wrap ( err ) } if err := tlsConn . Handshake ( ) ; err != nil { return trace . Wrap ( err ) } if err := tlsConn . SetReadDeadline ( time . Time { } ) ; err != nil { return trace . Wrap ( err ) } isDatabaseConnection , err := dbcommon . IsDatabaseConnection ( tlsConn . ConnectionState ( ) ) if err != nil { p . log . WithError ( err ) . Debug ( " " ) } if isDatabaseConnection { return trace . Wrap ( p . handleDatabaseConnection ( ctx , tlsConn , connInfo ) ) } return trace . Wrap ( handlerDesc . handle ( ctx , tlsConn , connInfo ) ) }
handleConn routes incoming connection based on SNI TLS information to the proper Handler by following steps:
1) Read TLS hello message without TLS termination and returns conn that will be used for further operations.
handleConn routes incoming connection based on SNI TLS information to the proper Handler by following steps : 1 ) Read TLS hello message without TLS termination and returns conn that will be used for further operations .