format import { CertificateExchangeMedium , SecureServerConfig , } from '../utils/CertificateProvider' ; import { ClientConnection } from './ClientConnection' ; import { transformCertificateExchangeMediumToType } from './Utilities' ; import { ClientDescription , ClientQuery , SignCertificateMessage , } from 'flipper-common' ; /**
 * ClientCsrQuery defines a client query with CSR
 * information.
 */ export type ClientCsrQuery = { csr ? : string | undefined ; csr_path ? : string | undefined ; } ; /**
 * SecureClientQuery combines a ClientQuery with
 * ClientCsrQuery. It also adds medium information.
 */ export type SecureClientQuery = ClientQuery & ClientCsrQuery & { medium : 1 | 2 | 3 | undefined ; } ; /**
 * Defines an interface for events triggered by a running server interacting
 * with a client.
 */ export interface ServerEventsListener { param port onListening ( port ) : void ; param clientQuery onConnectionAttempt ( clientQuery ) : void ; param clientQuery onSecureConnectionAttempt ( clientQuery ) : void ; param unsanitizedCSR param clientQuery param appDirectory param medium onProcessCSR ( unsanitizedCSR , clientQuery , appDirectory , medium ) : Promise < { deviceId : string ; } > ; param clientQuery param clientConnection onConnectionCreated ( clientQuery , clientConnection , downgrade ? ) : Promise < ClientDescription > ; param id onConnectionClosed ( id ) : void ; param error onError ( error ) : void ; /**
     * A message was received for a specif client
     * // TODO: payload should become JSON
     */ onClientMessage ( clientId , payload ) : void ; } /**
 * Defines the base class to be used by any server implementation e.g.
 * RSocket, WebSocket, etc.
 */ abstract class ServerAdapter { constructor ( protected listener ) { } param port param sslConfig returns abstract start ( port , sslConfig ? ) ; /**
     * Stop the server.
     */ abstract stop ( ) ; param clientQuery param rawData returns protected async _onHandleUntrustedMessage ( clientQuery , rawData ) { const message = rawData ; console . info ( `[conn] Connection attempt: ${ clientQuery . app } on ${ clientQuery . device }, medium: ${ message . medium }, cert: ${ message . destination }` , clientQuery ) ; if ( message . method === 'signCertificate' ) { console . debug ( 'CSR received from device' , 'server' ) ; const { csr , destination , medium } = message ; console . info ( `[conn] Starting certificate exchange: ${ clientQuery . app } on ${ clientQuery . device }` ) ; try { const result = await this . listener . onProcessCSR ( csr , clientQuery , destination , transformCertificateExchangeMediumToType ( medium ) ) ; console . info ( `[conn] Exchanged certificate: ${ clientQuery . app } on ${ result . deviceId }` ) ; const response = JSON . stringify ( { deviceId : result . deviceId , } ) ; return response ; } catch ( e ) { console . warn ( `[conn] Failed to exchange certificate with ${ clientQuery . app } on ${ clientQuery . device || clientQuery . device_id }` , e ) ; } } return undefined ; } } export default ServerAdapter ;