Skip to content

iOS simulator tunnels: forward only to the lease's simulator, reverse on the exact port #1288

Closed

Description

@V3RON

Part of #1282.

Scope

After this PR a holder of an iOS simulator lease can run simlock tunnel forward <p> and simlock tunnel reverse <p> against it, on a lone worker or through a gateway, as Android holders can today. A forward connection reaches the port only when a process of the lease's own simulator listens there. A reverse listens on exactly the device port on the worker's loopback and is refused when anything there already listens on it. An iPhone lease stays refused, with the new message.

In short

An iOS simulator has no network of its own: an app in it listening on 9100 listens on the worker Mac's 127.0.0.1:9100 or [::1]:9100, and an app opening localhost:8081 reaches the worker's port 8081 (F10, F11). So the iOS driver gets the same deviceTcp member the Android driver has (F1, F2), working on the worker's loopback instead of through adb.

  • owner check: before each forward connection, the worker lists every process listening on the port and the address each listens on (F18). It dials 127.0.0.1:<p> when a lease process holds 127.0.0.1:<p>, or holds *:<p> while no other process holds 127.0.0.1:<p>; else [::1]:<p> by the same rule; and never an address another process holds more specifically, because a loopback connect reaches the most specific bind (F13). A lease process is one whose environment has a word exactly SIMULATOR_UDID=<the lease's UDID> (F12), not a substring of its arguments.
  • exact-port reverse: the worker listens on the device port itself, on 127.0.0.1 and ::1, because an app tries ::1 first (F11).
flowchart LR
  C[simlock tunnel] -->|WS| W[tunnel module]
  W --> K{lease}
  K -->|Android| A[Android deviceTcp: adb]
  K -->|iOS simulator| I["iOS deviceTcp (new): loopback, owner check"]
  K -->|iPhone| R[refused TUNNEL_UNSUPPORTED, new text]
Loading

Before and after, on an iOS simulator lease:

$ simlock tunnel reverse 8081 forward 9100
before: {"code":"TUNNEL_UNSUPPORTED","message":"Lease 3f2a: the lease is on an iOS simulator; tunnels need an Android emulator or phone"}
after:  reverse device:8081 -> localhost:8081 (lease 3f2a)
        forward localhost:9100 -> device:9100 (lease 3f2a)
Input, iOS simulator lease Today After
forward 9100, the lease's app listens on 127.0.0.1:9100 TUNNEL_UNSUPPORTED bytes flow
forward 9100, the lease's app listens on [::1]:9100 only TUNNEL_UNSUPPORTED bytes flow
forward 9100, another simulator's app listens TUNNEL_UNSUPPORTED READY; each connection reset nothing on the lease's simulator listens on port 9100
forward 5038, the worker's adb server listens TUNNEL_UNSUPPORTED READY; each connection reset, same message for 5038
forward 9100, nothing listens TUNNEL_UNSUPPORTED READY; each connection reset, same message
reverse 8081, nothing listens on 8081 TUNNEL_UNSUPPORTED READY
reverse 8081, a process holds 127.0.0.1:8081, [::1]:8081, 0.0.0.0:8081 or [::]:8081 TUNNEL_UNSUPPORTED TUNNEL_PORT_IN_USE reverse 8081: port 8081 is already in use on the worker; pick another port
any tunnel, iPhone lease TUNNEL_UNSUPPORTED …on an iPhone; tunnels need an Android emulator or phone TUNNEL_UNSUPPORTED the lease is on an iPhone; tunnels need an Android emulator or phone, or an iOS simulator

The worker resets a forward connection whose owner check fails within 5 s of the caller's OPEN, counted from the OPEN and shared with the connect (F7).

What a user would notice if this goes wrong: a forward to their own app's port is closed with "nothing on the lease's simulator listens", or a reverse takes a port another process on the worker serves.

Technical spec

Facts

Commit ec46e1c (origin/main).

# Fact Source
F1 Driver has an optional deviceTcp?: DeviceTcp beside input and screenCapture src/core/driver.ts:339-340
F2 DeviceTcp is supports, sweep, reverse(device, devicePort, signal) → DeviceReverse {hostPort, onConnection, close}, connect(device, devicePort, signal) → TcpStream; a driver without it is refused TUNNEL_UNSUPPORTED by the catalog src/core/device-tcp.ts:24-53, src/core/driver-catalog.ts:106-125
F3 The iOS driver declares screenCapture and input and builds them in its constructor; it has no deviceTcp src/drivers/ios/index.ts:357-367
F4 The daemon passes tcpListener and tcpStreamConnector to the Android driver only; the iOS create gets neither src/daemon/main.ts:1538-1540, src/daemon/main.ts:1435-1445
F5 NodeTcpListenerFactory.listen(port) binds 127.0.0.1 only and answers undefined on EADDRINUSE; NodeTcpStreamConnector.connect(port, timeoutMs) dials 127.0.0.1 only src/ports/tcp-listener.ts:22-37, src/ports/tcp-stream.ts:65-70
F6 unsupportedTunnelMessage(platform, physical) gives the lease is on <an iOS simulator|an iPhone|a <platform> device>; tunnels need an Android emulator or phone; the tunnel module sends it as TUNNEL_UNSUPPORTED when supports is false src/core/device-tcp.ts:16-19, src/tunnel/sessions.ts:385-390
F7 A forward OPEN calls deviceTcp.connect; a DeviceTcpError becomes RESET {reason} with its message and the tunnel stays; the Android connect bounds itself at 5 s src/tunnel/sessions.ts:305-309, src/tunnel/AGENTS.md (3a), src/drivers/android/device-tcp.ts:23
F8 A virtual iOS device's driverData carries its udid src/drivers/ios/index.ts:174
F9 The iOS driver has no loopback, socket or port code today git grep -nE "127\.0\.0\.1|localhost|listen\(" origin/main -- src/drivers/ios (no non-test hit)
F10 A listener in a simulator on 127.0.0.1:9100 answers a host nc 127.0.0.1 9100; one on ::1 only answers on ::1 only; two simulators cannot bind one port (errno 48), and a host process cannot either spike 2026-11-04, iOS 26.4.1 runtime, Xcode 27.0 (ADR 0064 Context)
F11 In a simulator getaddrinfo("localhost") gives ::1 then 127.0.0.1; with a host listener on 127.0.0.1 only, the ::1 attempt is refused in 0.000 s and 127.0.0.1 connects same spike
F12 A process a simulator runs, spawned, orphaned (parent /sbin/launchd) or SpringBoard, shows SIMULATOR_UDID=<udid> in ps eww -p <pid>, without root, in 0.014 s; lsof -nP -iTCP:<p> -sTCP:LISTEN names the listening pid in 0.094 s without root; the parent chain breaks after a double fork same spike
F13 With a process holding [::]:46811, Node bound 127.0.0.1:46811 and ::1:46811 round-0 spec check on this Mac, 2026-11-05
F14 A lone worker's local agent profile points at http://127.0.0.1:<http.port> when http.host is 0.0.0.0 or :: src/modules/host.ts:21-23
F15 src/drivers/ios/index.ts (2671) and src/daemon/main.ts (1916) are on the size test's shrink-only list; a code file's limit is 1,400 lines; a PR that grows a listed file raises its number src/source-hygiene.test.ts:26-27 ("A PR that has to grow one raises its number here"), src/source-hygiene.test.ts:39, src/source-hygiene.test.ts:43, src/source-hygiene.test.ts:53
F16 e2e/slow-android-tunnel.test.ts is the existing slow-lane tunnel test; iOS slow-lane tests live beside it as e2e/slow-ios-*.test.ts git ls-tree origin/main e2e/
F17 Android's deviceTcp is tested through the driver in src/drivers/android/index-lifecycle.test.ts; NodeTcpProbe.isListening(port) connects to 127.0.0.1 with a 250 ms default git grep -l deviceTcp origin/main -- '*.test.ts', src/ports/tcp-probe.ts:29-37
F18 lsof -nP -iTCP:<p> -sTCP:LISTEN -Fpn prints p<pid> then one f<fd> and n<address> pair per socket, the address 127.0.0.1:<p>, [::1]:<p> or *:<p> (all interfaces); with nothing listening it prints nothing and exits 1; two processes can hold 127.0.0.1:<p> and [::1]:<p> apart this Mac, 2026-11-05: a host process on 127.0.0.1:47917 and [::1]:47917 gave p90218, n127.0.0.1:47917, n[::1]:47917; on [::]:47918 gave n*:47918; port 47999 gave exit 1, no output; F13
F19 On a lease end whose signal is aborted (nuke, a stopping daemon), the tunnel module skips closing a non-phone device's reverses and leaves them to reclaim; an iOS simulator is never reclaimed, it is destroyed src/tunnel/sessions.ts:151-156, src/tunnel/sessions.ts:168-171, src/tunnel/sessions.ts:555-557, src/drivers/ios/AGENTS.md (Reclaim)
F20 No iOS runtime's usr/bin has nc or python; e2e/fixtures holds only the appduct app; the spike built its listener and client with xcrun --sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator and ran them with simctl spawn spec check on this Mac, 2026-11-05; git ls-tree origin/main e2e/fixtures; spike 2026-11-04
F21 The catalog still refuses a driver with no deviceTcp using unsupportedTunnelMessage; tests assert today's simulator text src/core/driver-catalog.ts:116-121, src/tunnel/worker.test.ts:267-277, src/core/driver-catalog.test.ts:66-73
F22 The CLI closes a connection the worker resets and drops its reason; the reverse "nothing listening" warning is printed at most once per 10 s per tunnel src/cli/commands/tunnel.ts:247-249, src/cli/AGENTS.md (5b)

Modules touched

Searches: deviceTcp (12 non-test files), unsupportedTunnelMessage (3), TcpListenerFactory (6), TcpStreamConnector (5), IosSimctlDriverOptions (2).

flowchart LR
  D["daemon/main.ts (changed: wiring)"] --> I["drivers/ios (changed: device-tcp.ts new, index.ts)"]
  I --> P["ports (changed: listen and connect on ::1)"]
  I --> C["core/device-tcp.ts (changed: message)"]
  T[tunnel module] --> CAT[core driver-catalog]
  CAT --> I
Loading
sequenceDiagram
  participant C as simlock tunnel
  participant W as tunnel module (worker)
  participant I as iOS deviceTcp
  participant L as worker loopback
  C->>W: OPEN forward 9100
  W->>I: connect(device, 9100)
  I->>L: who listens on 9100? SIMULATOR_UDID?
  alt lease's UDID
    I->>L: connect 127.0.0.1, else ::1
    I-->>W: stream
  else other, none, or lookup failed
    I-->>W: DeviceTcpError(reason)
    W-->>C: RESET {reason}
  end
Loading
  • src/drivers/ios/device-tcp.ts (new): createIosDeviceTcp(options).
  • e2e/slow-ios-tunnel.test.ts (new): the slow-lane test, modelled on e2e/slow-android-tunnel.test.ts (F16).
  • src/drivers/ios/index.ts: a deviceTcp member built in the constructor when the listener and connector options are given (as F4 does for Android); new options tcpListener, tcpStreamConnector, tcpProbe.
  • src/ports/tcp-listener.ts, src/ports/tcp-stream.ts: an optional host, 127.0.0.1 (default) or ::1; src/ports/tcp-probe.ts the same if used for the listening check.
  • src/cli/commands/tunnel.ts, src/cli/AGENTS.md: a forward connection the worker resets prints warning: forward <d>: <reason> on stderr, at most once per 10 s per tunnel (F22), for every platform.
  • src/core/device-tcp.ts: the message (Shared values); a simulator on a driver with no deviceTcp (F21) reads the lease is on an iOS simulator; this worker's iOS driver takes no tunnels, and the tests of F21 change to it. DeviceReverse or DeviceTcp says whether its rules need reclaim, read by src/tunnel/sessions.ts (F19).
  • The slow-lane test builds its simulator listener and client with xcrun clang for the simulator and runs them with simctl spawn (F20).
  • src/daemon/main.ts: pass the three ports to IosSimctlDriver.create.
  • Manuals: src/drivers/ios/AGENTS.md, src/ports/AGENTS.md, src/tunnel/AGENTS.md (iOS line). Docs: remote-agents.mdx Tunnels, errors.mdx Tunnels rows, cli.mdx simlock tunnel.
  • Size list (F15): the PR raises the listed lengths of src/drivers/ios/index.ts and src/daemon/main.ts by exactly the lines it adds there, and says so in its body; the iOS logic lives in device-tcp.ts.

Contract and event changes

None. No error code is added (TUNNEL_PORT_IN_USE, TUNNEL_FAILED, TUNNEL_UNSUPPORTED exist), the frames do not change. Protocol-bump: none.

Other code on the same state

Searches: listen( and createServer in src/ (4 hits: the HTTP server, the tunnel listener factory, the IPC listener, the uplink listener); 47_000 (1, Android only).

  • The worker's HTTP server: on 0.0.0.0/:: it holds the port on every interface (F14); the reverse's listening check sees it and refuses (Shared values). Either order: the one listening first wins.
  • Android tunnel listeners in 47000–47999: an iOS reverse of such a port while Android holds it is refused by the check; an Android tunnel that later wants it skips it as busy (F5 answers undefined).
  • Another lease's iOS reverse or an app of any simulator on the port: refused by the check, or, if the tunnel was first, the app's bind fails (F10).
  • Sweep and reclaim: nothing on the device to remove; sweep does nothing on iOS. The tunnel module's lease-end skip (F19) does not apply to iOS (Failure modes).

Failure modes

Call Outcome What happens Limit, counted from
listener lookup a lease process holds an address dial that address only (owner check above) 5 s in all, from the caller's OPEN
listener lookup listeners, none a lease process RESET nothing on the lease's simulator listens on port <p> same
listener lookup exit 1 with no output (nothing listens, F18) same RESET same
listener lookup any other exit, no start, or unparsable output RESET could not check the port's owner: <first line of stderr> same
lookup or connect 5 s pass RESET timeout, the lookup process killed same
loopback connect refused on both addresses RESET with the same "nothing … listens" message same
reverse listening check something answers a connect to 127.0.0.1:<p> or [::1]:<p> (a wildcard listener included) TUNNEL_PORT_IN_USE, nothing bound 250 ms per address (F17), from HELLO
reverse bind in use on one address TUNNEL_PORT_IN_USE, the other address closed —
reverse bind other error TUNNEL_FAILED reverse <p>: <error>, the other address closed —

Lease end with an aborted signal (nuke, a stopping daemon): an iOS reverse's close runs anyway, because it only closes two local listeners and no reclaim will (F19); sessions.ts treats a device whose driver reverse sets no device rule like a phone here.

State: listeners live in memory only; a daemon restart closes the socket and the listeners with it, and nothing stays on the simulator. Two callers reversing one port at once: the first bind wins, the second is TUNNEL_PORT_IN_USE.

Rules in play

  • ADR 0064 §2–4: owner check per connection, exact port on both loopbacks, refused when anything listens, iPhone refused.
  • ADR 0054 §5 ("reaches adb only through a port the daemon gives it"): the iOS driver gets its ports from the daemon (F4 pattern).
  • Safety rule 10 ("anything that arrives over the wire is a claim"): the device port is checked 1024–65535 by the tunnel module as today; the owner is read from the machine, never the caller.
  • Architecture: application code takes ports, never child_process (src/ports/AGENTS.md Rules): the lookup runs through the driver's ProcessRunner.

Retired words

  • tunnels need an Android emulator or phone: the message ends …, or an iOS simulator.

Tests

Seam: createIosDeviceTcp with fake processes, listeners, probe and connector, in a new src/drivers/ios/device-tcp.test.ts modelled on the deviceTcp cases of src/drivers/android/index-lifecycle.test.ts (F17); the iPhone message and the aborted lease end through src/tunnel/worker.test.ts; the slow lane in a new e2e/slow-ios-tunnel.test.ts modelled on e2e/slow-android-tunnel.test.ts.

  • The iOS driver says which leases take device-port tunnels: simulator → yes; iPhone → no, with the iPhone message through the tunnel module.
  • A holder's iOS forward connection reaches the port only when the lease's simulator listens there, lookup output taken from F18's real lines: lease's simulator on 127.0.0.1 → bytes both ways; on ::1 only → bytes; on * → 127.0.0.1 dialled; lease's app on * and another process on 127.0.0.1 → ::1 dialled; lease's app on [::1] and another simulator's on 127.0.0.1 → ::1 dialled, never 127.0.0.1; another simulator's process → reset with the reason; a worker process with no SIMULATOR_UDID, or with SIMULATOR_UDID=<udid> only inside an argument → reset; lookup exit 1, no output → reset "nothing … listens"; lookup exit 2 → reset "could not check the port's owner"; lookup or connect still pending at 5 s → reset timeout, at 4.9 s still waiting; a connect that resolves after that reset → its stream closed (decided in delivery, pending the maintainer); a second connection after the app moved to another simulator's process → reset (checked per connection).
  • A holder's iOS reverse listens on exactly the device port, on both loopbacks: both free → connections from 127.0.0.1 and ::1 arrive; 127.0.0.1 taken → TUNNEL_PORT_IN_USE, ::1 not left bound; ::1 taken → same, 127.0.0.1 not left bound; another process holds 0.0.0.0:<p> or [::]:<p> → TUNNEL_PORT_IN_USE, nothing bound; close frees both ports.
  • A lease end with an aborted signal (nuke, a stopping daemon) still closes reverses whose rules need no reclaim: a fake device-tcp whose reverse needs no reclaim → close runs and its ports are freed; an emulator-like fake → close skipped, as today.
  • A user's forward connection that the worker resets shows the reason: first reset → warning: forward <d>: <reason> on stderr; a second within 10 s → nothing; one 10 s later → printed again; the tunnel stays.
  • On a real simulator against a lone worker, iOS forward and reverse work and the owner check holds: forward to a listener in the simulator; forward to a worker-only listener → connection closed and the warning names the reason; reverse from the simulator to the caller's localhost; a second reverse of the same port → TUNNEL_PORT_IN_USE.
  • The Node listener, probe and connector work on ::1: the existing src/ports tests gain a ::1 case each, on real loopback sockets, because the slow lane's simulator listener is IPv4 only and never reaches the connector or probe on ::1 (decided in delivery, pending the maintainer).

Done when

  • pnpm check passes, with the size list raised only for the two files and lines named above.
  • Slow lane, on a Mac with Xcode and an iOS runtime: scripts/slow-e2e.sh e2e/slow-ios-tunnel.test.ts passes (needs a real simulator).
  • remote-agents.mdx, errors.mdx and cli.mdx say iOS simulators take tunnels, iPhones do not, and that two leases on one worker cannot reverse the same port.

Out of scope

Depends on

Written by an agent.

Activity

  1. added
    task:draft
    on Nov 5, 2026
  2. added
    task:ready
    and removed
    task:draft
    on Nov 5, 2026
  3. self-assigned this
    on Nov 5, 2026
  4. added 20 commits that reference this issue on Nov 5, 2026
  5. added a commit that references this issue on Nov 5, 2026
    00fb9f0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

task:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions