WebDriverAgent icon indicating copy to clipboard operation
WebDriverAgent copied to clipboard

feat: optional reverse TCP tunnel for WDA in NAT-restricted environments

Open dankefox opened this issue 7 months ago • 7 comments

Summary

Add an optional reverse TCP tunnel mode that allows WDA to actively connect outbound to an external relay server. This enables remote control of iOS devices in network environments where inbound connections to port 8100 are not feasible.

Problem

WDA defaults to listening on port 8100 for inbound HTTP connections. However, in many real-world environments, inbound connections to the iOS device are blocked or unreachable:

  • Symmetric NAT — common in corporate and campus networks
  • Multi-layer firewalls — enterprise security policies blocking inbound traffic to mobile devices
  • VPN tunnels — overlay networks where iOS restricts inbound TCP on the tunnel interface
  • Remote test labs — devices behind NAT gateways with no port forwarding

In these scenarios, the standard http://<device-ip>:8100 approach simply does not work.

Solution

Instead of requiring the client to connect in to WDA, this PR lets WDA connect out to a relay server. The relay bridges HTTP clients on one side and the WDA reverse connection on the other.

How it works

  1. Set WDA_RELAY_HOST and optionally WDA_RELAY_PORT (default: 8201) as environment variables when launching WDA
  2. On startup, WDA opens an outbound TCP connection to the relay
  3. The relay forwards HTTP requests through this connection and returns responses
  4. If the connection drops, WDA automatically reconnects after 5 seconds

When not configured

Zero impact. If WDA_RELAY_HOST is not set, the feature is completely inactive. No code paths are touched, no connections are made, existing behavior is identical.

Changes

File Change
FBConfiguration.h/m Add relayHost and relayPort accessors reading from WDA_RELAY_HOST / WDA_RELAY_PORT env vars
FBWebServer.m Add reverse tunnel client using Apple's Network.framework (nw_connection), with auto-reconnect on failure
Scripts/wda-relay-server.js Example Node.js relay server (~100 lines) that bridges HTTP ↔ reverse tunnel

Usage

# 1. Start the relay server on a reachable host
node Scripts/wda-relay-server.js  # listens on 8201 (relay) and 8100 (HTTP proxy)

# 2. Launch WDA with relay configured
WDA_RELAY_HOST=192.168.1.100 WDA_RELAY_PORT=8201 xcodebuild test-without-building \
  -project WebDriverAgent.xcodeproj \
  -scheme WebDriverAgentRunner \
  -destination 'id=<device-udid>'

# 3. Use WDA as usual — requests go through the relay
curl http://localhost:8100/status

Design decisions

  • Opt-in via environment variables — consistent with existing USE_PORT, USE_IP, and MJPEG_SERVER_PORT patterns in FBConfiguration
  • Network.framework — uses Apple's modern networking API (nw_connection) for reliable connection management
  • 4-byte length-prefixed framing — simple binary protocol for multiplexing HTTP over a single TCP connection
  • Auto-reconnect — 5-second backoff on connection failure, no manual intervention needed
  • Relay server as example script — included in Scripts/ for easy adoption, not a required component

Testing

Tested on:

  • iPhone 16 Pro Max, iOS 26.3.1
  • Xcode 26.4
  • macOS Tahoe 26.4
  • Relay server running on a separate host behind NAT

Verified: /status, /session, tap, swipe, screenshot, and other WDA endpoints all work correctly through the reverse tunnel.

dankefox avatar Apr 25 '26 19:04 dankefox

CLA Signed

The committers listed above are authorized under a signed CLA.

  • :white_check_mark: login: dankefox / name: dankefox (24f73f18e7a45b67d01a4177178b6d17358db52d, abafbc5a76fce71b7a7821b6fba1bb2329111966, d909419c4bb1f0a0fa29312b50d13bc0c08d4f72, e462c4127f0d808e32355931d0d0e9a10fd18e88)

I'm not sure why to reinvent a custom protocol if the project already uses CocoaHTTPServer

mykola-mokhnach avatar Apr 25 '26 21:04 mykola-mokhnach

Thank you for the review @mykola-mokhnach!

Regarding CocoaHTTPServer — it serves as an inbound HTTP server (listening on a port for incoming connections). The core problem this PR addresses is precisely that inbound connections to the iOS device are blocked in NAT-restricted environments.

The reverse tunnel requires WDA to initiate an outbound TCP connection to an external relay. During development, I tested three approaches for the outbound connection:

  1. NSStream — failed to establish reliable connections over VPN/tunnel interfaces
  2. POSIX sockets — also unreliable on iOS for outbound TCP over non-standard network paths
  3. nw_connection (Network.framework) — worked reliably, and is Apple's recommended API for modern network connections

The 4-byte length-prefixed framing is intentionally minimal — just enough to multiplex HTTP request/response pairs over a single persistent TCP connection. I considered using HTTP itself as the transport (e.g., long-polling or WebSocket), but a raw TCP connection with simple framing has significantly lower overhead and latency for this use case.

That said, I'm absolutely open to alternative approaches if you have a preferred pattern in mind. Would it make sense to, for example, use CocoaHTTPServer as an HTTP client (reverse proxy) that forwards to a relay via HTTP? I'd be happy to explore that if it fits better with the project's architecture.

dankefox avatar Apr 26 '26 05:04 dankefox

Updated the PR based on all review feedback:

  • Extracted to separate module: All reverse tunnel logic is now in FBReverseTunnel.h/m. FBWebServer.m has a single one-line call: [FBReverseTunnel startIfConfiguredWithLocalPort:]
  • Split complex methods: FBReverseTunnel.m is organized into focused methods — parseHTTPRequest:, buildLocalRequestWithMethod:path:body:, buildFramedResponse:statusCode:, sendData:throughConnection:, etc.
  • Named constants: Extracted FBReverseTunnelMaxPayloadSize (10 MB) and FBReverseTunnelReconnectDelay (5s) with documenting comments
  • Relay script: Kept in Scripts/ as it is a required counterpart (users need a relay server on the other end). Happy to move to a different directory if preferred.

Regarding the CLA — I'll get that signed. The commit email needs to be linked to my GitHub account.

Ready for another round of review!

dankefox avatar Apr 26 '26 05:04 dankefox

So, this is when a client wants to connect to the WDA over ip:port (which is not though local proxy forward appium xcuitest driver generally does,) correct? For example, this PR + appium:webDriverAgentUrl (to the relay), configuration? - just to understand the usage properly

KazuCocoa avatar Apr 26 '26 07:04 KazuCocoa

Yes, exactly! The primary use case is:

  1. WDA runs on an iOS device behind NAT/firewall (cellular network, corporate firewall, etc.) where inbound TCP is blocked
  2. A relay server runs on a publicly accessible host
  3. WDA initiates an outbound connection to the relay and establishes a reverse tunnel
  4. The Appium client connects to the relay using appium:webDriverAgentUrl pointed at the relay address

So the flow is: Appium Client → Relay Server ← WDA (reverse tunnel)

This is particularly useful for remote device farms, CI/CD pipelines with devices on cellular networks, or any environment where direct port access to the iOS device isn't possible.

dankefox avatar Apr 26 '26 07:04 dankefox

These might have issues in CI:

  • npm run lint
  • unit test in WDA project

KazuCocoa avatar Apr 26 '26 19:04 KazuCocoa