Skip to content

simlock tunnel forward: reach a leased device's port from the caller's machine #1115

Closed

Description

@leonardoxr

Part of #1110.

Scope

An agent runs simlock tunnel forward <device-port>[:<caller-port>] … and its loopback port reaches the leased Android device's port, alone or beside reverse tunnels. The worker opens each forwarded connection through adb's host protocol, with no adb forward rule (ctx.deviceTcp.connect, Android driver and fake driver); the CLI listens on loopback only. The worker validates caller-opened connections. The slow-lane test gains a gateway run of both directions. The CLI reference adds forward.

In short

After this PR:

$ simlock tunnel reverse 5173 forward 9229 forward 8080:18080
reverse device:5173 -> localhost:5173 (lease w1.3f9c)
forward localhost:9229 -> device:9229 (lease w1.3f9c)
forward localhost:18080 -> device:8080 (lease w1.3f9c)

The CLI binds each forward's caller port before HELLO; a busy one exits 1 with TUNNEL_CALLER_PORT_IN_USE and forward <d>: localhost:<c> is already in use before anything is set up on the device. Connections accepted before READY wait, and are closed on REFUSED. A connection to 127.0.0.1:9229 is announced as OPEN {kind:"forward", devicePort:9229} with an even id; the worker opens the device port and bytes flow under the same window; END half-closes the device stream. A device port with nothing listening resets the caller's connection. Every listener is closed when the command ends, however it ends.

flowchart LR
  A[caller app] -->|127.0.0.1:9229| U[simlock tunnel listener - new]
  U -->|OPEN, DATA, END| M[worker tunnel module - accepts forward - new]
  M -->|deviceTcp.connect - new| D[device tcp:9229 via adb host protocol]
Loading
Input Today (after #1114) After
forward 9229 usage error (only reverse parsed) caller 9229 → device 9229
forward 9229:19229 usage error caller 19229 → device 9229
forward 9229:80 usage error usage error: a forward caller port is 1024 to 65535
reverse 5173 forward 5173 usage error usage error: caller port 5173 twice
forward 9229, caller 9229 busy usage error exit 1 TUNNEL_CALLER_PORT_IN_USE
forward 9229 on an iOS lease usage error exit 1 TUNNEL_UNSUPPORTED
a connection, nothing on the device port — caller connection closed

If this goes wrong, a caller port stays listening after the command ends, or a forwarded connection hangs instead of closing.

Technical spec

Facts

Commit 31ff66e.

# Fact Source
F1 The driver opens a device socket through the host protocol: host:transport:<serial>, then a service, reading OKAY or FAIL src/drivers/android/screen/adb-socket.ts:68, src/drivers/android/screen/adb-socket.ts:18-24
F2 adbHostRequest frames a host request: 4 hex digits of length, then the service src/drivers/android/adb-host-protocol.ts:11
F3 host:transport:<serial> then tcp:<p> answers OKAY, OKAY and carries bytes to a device listener on <p>, with no adb forward rule run 2026-10-30, adb 37.0.0, a physical phone: transport=OKAY tcp=OKAY, the device file held forward-ok
F4 tcp:<p> with nothing listening on the device answers FAIL with closed run 2026-10-31, adb 37.0.0: "OKAYFAIL0006closed"
F5 Emulators use the driver's configured adb server port (as the screen target does); phones 5037, every call after the default-server gate src/drivers/android/index.ts:608-627, src/drivers/android/physical-handler.ts:101-111
F6 TcpStream gains end() and the loopback TcpListener (named port, in-use error) lands in #1112 #1110 Shared values "byte streams"
F7 Before this task the worker refuses forward with TUNNEL_UNSUPPORTED, and #1114's CLI parses only reverse #1112 Tests line 2; #1114 Contract and event changes
F8 simlock tunnel, its exits and the fast end-to-end test come from #1114; the gateway relay from #1113 #1110 Tasks

Modules touched

Searches: openDeviceSocket, adbHostRequest, deviceTcp, "forward" in src/tunnel.

  • src/drivers/android/ — deviceTcp.connect for emulators and phones, through adbHostRequest and tcp:<p>.
  • src/core/ — connect on DeviceTcp and in FakeDriver; src/modules/, src/daemon/ — ctx.deviceTcp.connect.
  • src/tunnel/ — accept caller-opened connections for forward tunnels and validate them; refuse a forward-only HELLO on an unsupported lease; drop the forward refusal.
  • src/cli/ — forward arguments, a loopback listener per tunnel.
  • src/daemon/ — tunnel-cli.e2e.test.ts gains the forward cases.
  • e2e/slow-android-tunnel.test.ts — a gateway run of reverse and forward.
  • website/ — CLI reference.
sequenceDiagram
  participant A as Caller app
  participant U as simlock tunnel
  participant M as Worker tunnel
  participant D as Device
  A->>U: connect 127.0.0.1:9229
  U->>M: OPEN conn 2 {forward, 9229}
  M->>D: host:transport:serial, tcp:9229
  alt FAIL closed
    M-->>U: RESET conn 2
    U-->>A: close
  else OKAY
    A->>U: bytes, then FIN
    U->>M: DATA conn 2, END conn 2
    M->>D: bytes, half-close
  end
Loading

Contract and event changes

none: the codec and codes are #1112's; searched src/contract: no change.

New input: forward <d>[:<c>], d 1 to 65535 (the worker checks the 1024 floor), c 1024 to 65535; no caller port twice across all tunnels of a command.

Wire input from the caller: an OPEN must carry an even id above the caller's last, kind forward, and a devicePort that is one of this socket's forward tunnels; anything else is REFUSED BAD_REQUEST and close 4003. Caller-opened and device-opened connections count together toward 256.

Other code on the same state

Searches: tcp: services in src/drivers/android (0 before this task), openDeviceSocket (screen only).

Failure modes

Call Outcome What happens Limit, counted from
deviceTcp.connect OKAY bytes flow 5 s for both answers, from the connect
deviceTcp.connect FAIL RESET {reason}; the caller's connection closes —
deviceTcp.connect no answer RESET {reason:"timeout"} 5 s
deviceTcp.connect phone gate closed RESET {reason: <gate message>} —
caller listen port in use exit 1 TUNNEL_CALLER_PORT_IN_USE, before HELLO —
caller connections a 257th, both directions counted closed at once —
command ends (Ctrl-C, REFUSED, 4001, lost) — every listener closed —

Rules in play

Tests

Seam: the daemon end-to-end with real HTTP, the CLI command and the fake driver's device-tcp (src/daemon/tunnel-cli.e2e.test.ts from #1114); the worker module on src/input/worker.test.ts's model; the driver on src/drivers/android/index.test.ts.

  • A user runs simlock tunnel forward … and the caller's loopback port reaches the device port; cases: 9229; 9229:19229; the line printed per tunnel; caller port already in use → exit 1 before HELLO; caller port below 1024 → usage error; reverse 5173 forward 5173 → usage error (same caller port across directions); device port closed → the caller's connection closed; caller END half-closes the device stream; connections accepted before READY wait, and close on REFUSED; listeners closed after Ctrl-C, REFUSED, 4001 and a lost connection; forward-only HELLO on an iOS lease → TUNNEL_UNSUPPORTED.
  • The worker validates caller-opened connections; cases: OPEN naming a port that is not one of the socket's forward tunnels, an odd or reused id, or kind reverse → BAD_REQUEST, 4003; caller and device connections share the 256.
  • The worker opens a device port through adb's host protocol for each forwarded connection; cases: configured emulator server port; phone behind the gate (gate closed → RESET with its message); tcp:<p> answers FAIL → RESET; no answer in 5 s → RESET {reason:"timeout"}.
  • On a real emulator through a gateway, reverse and forward both work, both ways; cases: reverse through a gateway; forward through a gateway with a device server that answers.

Done when

  • The Tests lines pass.
  • e2e/slow-android-tunnel.test.ts passes in the slow lane with a gateway and a worker on one machine: simlock tunnel reverse <p> forward <q>; the device fetches the caller's page on <p>; a device-side server started with (printf 'device-ok'; sleep 30) | nc -l -p <q> after the tunnel prints its lines answers device-ok to a client on the caller's localhost:<q> that retries its connect until it succeeds, for at most 5 s, and receives the bytes that client sends.
  • The CLI reference describes forward.

Out of scope

  • iOS; any address but loopback on the caller.

Depends on

Written by an agent.

Activity

  1. 2 remaining items

  2. added
    task:ready
    on Nov 1, 2026
  3. self-assigned this
    on Nov 1, 2026
  4. V3RON commented on Nov 1, 2026

    @V3RON
    Contributor

    Handoff

    Done: main is merged into PR #1127, and the #1110 input-plus-tunnel e2e tests are restored. Review round 1, round 2 and the fix-check passed. The slow lane passed 4/4 at f38b02c. At 4ec57d6 it passed 3/4: the one failure was the test reading rules before reclaim finished, now fixed.

    Not done: the slow lane at head ac517aa. The emulator timed out booting four times, with no orphan process left and the disk not full.

    Findings: five fixed (R1, R2, R3, R4, R10). R5 (listener factory on macOS) is a note.

    Blocked on: a machine whose emulator boots. Re-run scripts/slow-e2e.sh e2e/slow-android-tunnel.test.ts, tick the line, then gate. ADR 0054 (#1111) flips to Accepted and merges after that.

    Written by an agent.

  5. added
    task:blocked
    and removed
    task:ready
    on Nov 1, 2026
  6. removed their assignment
    on Nov 1, 2026
  7. added
    task:ready
    and removed
    task:blocked
    on Nov 1, 2026
  8. self-assigned this
    on Nov 1, 2026
  9. added a commit that references this issue on Nov 1, 2026
    9c3f869
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