Repository navigation
Description
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]
| 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.connectfor emulators and phones, throughadbHostRequestandtcp:<p>.src/core/—connectonDeviceTcpand inFakeDriver;src/modules/,src/daemon/—ctx.deviceTcp.connect.src/tunnel/— accept caller-opened connections forforwardtunnels and validate them; refuse a forward-onlyHELLOon an unsupported lease; drop theforwardrefusal.src/cli/—forwardarguments, a loopback listener per tunnel.src/daemon/—tunnel-cli.e2e.test.tsgains 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
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).
- The screen stream's device socket on the same device: a separate host-protocol connection per stream; no shared state.
adb forwardrules an agent made through exec: untouched here; reclaim removes them (The tunnel module: reverse a leased Android device's port over a lease WebSocket on a worker #1112).
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
- ADR 0054 §4 (PR docs(adr): 0054 a lease holder tunnels device ports to its own machine #1111):
forwardthrough the host protocol, the caller side on loopback only. - Modules manual: the module reaches adb only through
ctx.deviceTcp.
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 beforeHELLO; 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; callerENDhalf-closes the device stream; connections accepted beforeREADYwait, and close onREFUSED; listeners closed after Ctrl-C,REFUSED, 4001 and a lost connection; forward-onlyHELLOon an iOS lease →TUNNEL_UNSUPPORTED. - The worker validates caller-opened connections; cases:
OPENnaming a port that is not one of the socket's forward tunnels, an odd or reused id, orkindreverse →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 →
RESETwith its message);tcp:<p>answersFAIL→RESET; no answer in 5 s →RESET {reason:"timeout"}. - On a real emulator through a gateway,
reverseandforwardboth 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.tspasses 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 answersdevice-okto a client on the caller'slocalhost:<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
- The tunnel module: reverse a leased Android device's port over a lease WebSocket on a worker #1112
- The gateway relays the tunnel socket to the lease's worker #1113
- simlock tunnel: reverse a leased device's port to the caller's machine #1114
Written by an agent.
Activity
test: forward caller port below 1024 and a caller port named across d…
feat(cli): a forward caller port below 1024 is a usage error (callsta…
test: forward binds before HELLO, holds connections until READY and c…
feat(cli): forward connections accepted before READY wait and close o…
test: forward device port closed resets the caller's connection and c…
feat(tunnel): a device port with nothing listening resets the forward…
test: the worker validates caller-opened connections and counts them …
feat(tunnel): the worker validates caller-opened connections and sets…
test: the android driver opens a device port through adb's host proto…
feat(android): open a device port through adb's host protocol for for…
test: reverse and forward both work on a real emulator through a gate…
docs(tunnel): forward in the CLI reference, the wire description and …
chore(tunnel): Fallow findings of the forward cycles (callstackincuba…
test: audit boundary cases for forward tunnels, and one wording for a…
test: the closed-gate case uses a server that differs from Simlock's …
2 remaining items
test(tunnel): a forward connection made before the socket exists is c…
fix(tunnel): a forward OPEN after the session ends opens nothing, and…
test(tunnel): a device's END half-closes the caller's forwarded conne…
V3RON commentedon Nov 1, 2026
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.
feat(tunnel): simlock tunnel forward reaches a leased device's port f…