docs/extending/establishing-a-connection.mdx
import useBaseUrl from '@docusaurus/useBaseUrl'; import Link from '@docusaurus/Link';
This page provides an outline of how a connection is established between an app, with the Flipper SDK integrated, and the desktop app. This connection occurs behind the scenes inside the mobile SDK, so users shouldn't need to worry about it.
To prevent Flipper clients on mobile apps from connecting to any server that happens to be running on localhost and potentially leaking information from your app, the connection process is a little more involved than you may expect.
Flipper uses WebSocket to communicate between the desktop and mobile apps. WebSocket enables bi-directional communication.
When the desktop app starts up, it opens a secure socket on port 9088. The Flipper client will continually attempt to connect to this port on localhost to establish a connection with the desktop app.
To avoid mobile apps from connecting to untrusted ports on localhost, a Flipper client should only connect to servers that have a valid, trusted TLS certificate. In order for the mobile app to know which certificates it can trust, it conducts a certificate exchange with the desktop app before it can make its first secure connection.
This is achieved through the following steps:
FS_ACCESS - the desktop uses ADB (for Android) or the mounted file system (for iOS simulators) to write the following files to the mobile app's private data partition:
WWW - the desktop app will use your implementation of Certificate Uploader to upload the certificates:
<OssOnly>Currently, Flipper does not support WWW mode but there is work underway to start support.</OssOnly>
<FbInternalOnly>To learn about WWW mode and how to use it, refer to the <Link to={useBaseUrl('/docs/fb/www-certificate-exchange')}>WWW Certificate Exchange</Link> page.</FbInternalOnly>
Now that the mobile app knows which server certificate it can trust, you can connect to the secure server.
:::note This enables the mobile app to trust a certificate if and only if it is stored inside its internal data partition. Typically, it's only possible to write there with physical access to the device (that is, through ADB or a mounted simulator). :::
To get the desktop app to generate a client certificate for your client and then deploy it, go through the following steps:
Use a WebSocket client to connect (insecurely) to the following URL (Parameters are defined in Implementing a Flipper Client):
localhost:9089/sonar?os={OS}
&device={DEVICE}
&app={APP}
&sdk_version={SDK_VERSION}
&medium={CERTIFICATE_EXCHANGE_MEDIUM}
On that connection, send the following payload:
Request = {
"method": "signCertificate",
"csr": string,
"destination": string,
"medium": int
}
csr - a Certificate Signing Request the client has generated, and destination identifies a location accessible to both the client and Flipper desktop, where the certificate should be placed.
CertificateProvider implementation in Flipper may use this in combination with the destination to determine where to put the certificate. This will ask Flipper desktop to generate a client certificate, using the CSR provided, and put it into the specified destination.destination - depending on the client, destination can have a different meaning:
destination argument and deploy certificates to it.medium - the destination field may not be relevant if your medium value is more than 1:
medium=1(default) - Flipper should do certificate exchange by directly putting certificates at destination in the sandbox of the app.medium=2 - Flipper will use the Certificate Uploader and Provider to upload certificates and download it on the client side respectively.You can see the current implementations in CertificateProvider.tsx.