// handleStartup handles the initial protocol exchange between the Postgres
// client (e.g. psql) and this proxy.
//
// Returns the startup message that contains initial connect parameters and
// the upgraded TLS connection.
handleStartup handles the initial protocol exchange between the Postgres
client  and this proxy.

Returns the startup message that contains initial connect parameters and
the upgraded TLS connection.
// handleStartup handles the initial protocol exchange between the Postgres // client ( e . g . psql ) and this proxy . // // Returns the startup message that contains initial connect parameters and // the upgraded TLS connection .
func (p *Proxy) handleStartup(ctx context.Context, clientConn net.Conn) (*pgproto3.StartupMessage, *tls.Conn, *pgproto3.Backend, error) {
	backend := pgproto3.NewBackend(pgproto3.NewChunkReader(clientConn), clientConn)
	startupMessage, err := backend.ReceiveStartupMessage()
	if err != nil {
		return nil, nil, nil, trace.Wrap(err)
	}
	p.Log.Debugf("Received startup message: %#v.", startupMessage)
	 Once the server has indicated the support (by sending 'S' in reply),
	 it will send a StartupMessage with the connection parameters such as
	 user name, database name, etc.
	 https:www.postgresql.org/docs/13/protocol-flow.html#id-1.10.5.7.11
	switch m := startupMessage.(type) {
	case *pgproto3.SSLRequest:
		 Send 'S' back to indicate TLS support to the client.
		_, err := clientConn.Write([]byte("S"))
		if err != nil {
			return nil, nil, nil, trace.Wrap(err)
		}
		 Upgrade the connection to TLS and wait for the next message
		 which should be of the StartupMessage type.
		clientConn = tls.Server(clientConn, p.TLSConfig)
		return p.handleStartup(ctx, clientConn)
	case *pgproto3.StartupMessage:
		 TLS connection between the client and this proxy has been
		 established, just return the startup message.
		tlsConn, ok := clientConn.(*tls.Conn)
		if !ok {
			return nil, nil, nil, trace.BadParameter(
				"expected tls connection, got %T", clientConn)
		}
		return m, tlsConn, backend, nil
	}
	return nil, nil, nil, trace.BadParameter(
		"unsupported startup message: %#v", startupMessage)
}
func ( p * Proxy ) handleStartup ( ctx context . Context , clientConn net . Conn ) ( * pgproto3 . StartupMessage , * tls . Conn , * pgproto3 . Backend , error ) { backend := pgproto3 . NewBackend ( pgproto3 . NewChunkReader ( clientConn ) , clientConn ) startupMessage , err := backend . ReceiveStartupMessage ( ) if err != nil { return nil , nil , nil , trace . Wrap ( err ) } p . Log . Debugf ( " " , startupMessage ) switch m := startupMessage . ( type ) { case * pgproto3 . SSLRequest : _ , err := clientConn . Write ( [ ] byte ( " " ) ) if err != nil { return nil , nil , nil , trace . Wrap ( err ) } clientConn = tls . Server ( clientConn , p . TLSConfig ) return p . handleStartup ( ctx , clientConn ) case * pgproto3 . StartupMessage : tlsConn , ok := clientConn . ( * tls . Conn ) if ! ok { return nil , nil , nil , trace . BadParameter ( " " , clientConn ) } return m , tlsConn , backend , nil } return nil , nil , nil , trace . BadParameter ( " " , startupMessage ) }
handleStartup handles the initial protocol exchange between the Postgres
client  and this proxy.
handleStartup handles the initial protocol exchange between the Postgres client and this proxy .
// Backend acts as a server for the Postgres wire protocol. // When initiating an encrypted connection, psql will first check with // the server whether it supports TLS by sending an SSLRequest message. // // Once the server has indicated the support (by sending 'S' in reply), // it will send a StartupMessage with the connection parameters such as // user name, database name, etc. // // https://www.postgresql.org/docs/13/protocol-flow.html#id-1.10.5.7.11 // Send 'S' back to indicate TLS support to the client. // Upgrade the connection to TLS and wait for the next message // which should be of the StartupMessage type. // TLS connection between the client and this proxy has been // established, just return the startup message.