Files
flipper/iOS/FlipperKit/FKPortForwarding
Dominik Pich 9703a68423 surpress unused error for retainedData as it is deliberate to delay its deallocation
Summary:
this diff ignores an used variable error as it is deliberate
xcode 13.3 doesnt like that

see:
https://www.internalfb.com/intern/testinfra/diagnostics/5066549669072759.844424981821815.1644949594/

Note: feel free to commandeer for better fix!

Reviewed By: d16r

Differential Revision: D34259729

fbshipit-source-id: 7c6ba4129a83a6f82981e927744c3a36046d88df
2022-02-16 15:48:54 -08:00
..

FKPortForwarding

FKPortForwarding lets you expose your Mac's port to iOS device via lightning cable. The typical usecase is connecting to a TCP server that runs on OS X from an iPhone app without common WiFi network.

Benefits:

  1. No need to be on the same WiFi, worry about firewalls (fbguest) or VPN
  2. iOS app doesn't have to know your Mac's IP address
  3. Secure - communication is possible only when connected via USB

How it works

iOS provides a way to connect to device's TCP server from Mac via USBHub, but there is no API to connect from iOS to TCP server running on Mac. FKPortForwarding uses Peertalk to establish communication channel from Mac to iOS, creates a TCP server on iOS and multiplexes all connections to that server via the peertalk channel. Helper app running on Mac listens for commands on the peertalk channel and initializes TCP connections to local port and forwards all communication back via the same peertalk channel.



                                                       |
                                          iOS Device   |   Mac
                                                       |
                                 +----------------+            +----------------+
                                 |Peertalk Server |  connect   |Peertalk Client |
                                 |                <------------+                |
                                 |                |            |                |
                                 |       Port 8025|            |                |
                                 +----+-----------+            +---------^------+
                                      |                                  |
                                      |                                  |
    incoming     +----------------+   |                                  |                 +--------------+
    connections  |Proxy Server    |   |                                  |                 |Real Server   |
   ------------->>                |   |         +-------------+ commands |                 |              |
                 |       Port 8081|   | create  |             |  stream  |                 |     Port 8081|
                 +-+--------------+   +---------> Peertalk    <----------+                 +-^------------+
                   |                            | Channel     |                              ^
                   |   +--------+               |             |               +--------+     | outgoing
                   |   |        | onConnect     |             | connect       |        |     | connections
                   +---> Client +---------------> OpenPipe    +---------------> Client +-----+
                       | #[tag] | onRead        |             | write         | #[tag] |
                       |        +---------------> WriteToPipe +--------------->        |
                       |        | onDisconnect  |             | disconnect    |        |
                       |        +---------------> ClosePipe   +--------------->        |
                       |        |               |             |               |        |
                       |        | write         |             | onRead        |        |
                       |        <---------------+ WriteToPipe <---------------+        |
                       |        | close         |             | onDisconnect  |        |
                       |        <---------------+ ClosePipe   <---------------+        |
                       |        |               |             |               |        |
                       +--------+               |             |               +--------+
                                                +-------------+

First, the library on iOS device creates a TCP server on the port we want to forward (let's say 8081) and a special Peertalk server on port 8025. Mac helper app looks for connected iOS devices, and once it finds one it connects to its peertalk server. Only one channel is created that's going to be used for multiplexing data.

When a socket connects to local proxy server, FKPortForwarding is going to assign a tag to the connection and use peertalk channel to tell Mac helper app to connect to TCP port 8081 on Mac. Now events and data on both sides of the wire are going to be multiplexed and transferred via the peertalk channel.