Skip to content

simlock tunnel: reverse a leased device's port to the caller's machine #1114

Closed

Description

@leonardoxr

Part of #1110.

Scope

An agent on another machine runs simlock tunnel reverse <device-port>[:<caller-port>] … against a lone worker or a gateway, and its leased Android device reaches the caller's loopback ports while the command runs. This adds the tunnel CLI command, a WebSocket client port built on ws and loaded with import() only when simlock tunnel first opens a socket, an HTTP lookup port (GET /v1/identity, GET /v1/leases), the fake driver's device-tcp use in a fast end-to-end test against a lone worker, and the slow-lane emulator test for reverse against a lone worker. It corrects the comment in src/ports/uplink-websocket.ts that says Node 22's client cannot set headers (it can; ws is chosen for the refusal body and pings). The CLI reference describes the command, SIMLOCK_URL and SIMLOCK_TOKEN, and the global --token's new wording.

In short

After this PR, on the caller's machine:

$ export SIMLOCK_URL=https://worker.example:7300 SIMLOCK_TOKEN=sk_agent_…
$ simlock tunnel reverse 5173 reverse 8081:3000
reverse device:5173 -> localhost:5173 (lease 3f9c)
reverse device:8081 -> localhost:3000 (lease 3f9c)
reverse 5173: nothing listening on localhost:5173        # stderr, at most once per 10 s per tunnel
^C                                                       # close sent (waited at most 2 s), exit 0

--url overrides SIMLOCK_URL; the global --token overrides SIMLOCK_TOKEN and is sent as Authorization: Bearer <token>. --lease <id> names the lease; without it an agent token's only lease is used (its role from GET /v1/identity, its leases from GET /v1/leases), and an operator token must name one. Every failure is one JSON line on stderr, {"error":{"code":"<code>","message":"<message>"}}, with the codes and exits of #1110's "CLI exits and bounds" row: a usage error exits 2; a refusal exits 1 with its code and Lease <id>: <message>; a lost connection (including 45 s with no message or ping, or 90 s with no READY) exits 1 with TUNNEL_DISCONNECTED; the lease ending exits 14 with TUNNEL_LEASE_ENDED and Lease <id> ended: <reason>.

flowchart LR
  U[simlock tunnel - new] -->|GET /v1/leases when no --lease - new port| H[daemon HTTP]
  U -->|ws, loaded on first use, Authorization: Bearer - new port| T[/v1/leases/id/tunnel]
  T --> M[worker tunnel module]
  U -->|per OPEN| L[caller localhost:port]
Loading
Input Today After
simlock tunnel reverse 5173 unknown command tunnel open
reverse 8081:3000 unknown command device 8081 → caller 3000
reverse 80 unknown command the worker refuses: exit 1 TUNNEL_BAD_PORT
reverse 70000, reverse abc, reverse 5173:, reverse 5173:0, no tunnel unknown command usage error, exit 2
reverse 5173 reverse 6000:5173 (same caller port) unknown command usage error, exit 2
--url http://10.0.0.5:7300 unknown command usage error: http:// only on loopback
no --url, or no token unknown command usage error naming the flag and the variable
no --lease; an agent token with 0 or 2 leases; an operator token unknown command usage error naming them, exit 2

If this goes wrong, the command keeps running after its tunnels are gone, or a large page stalls halfway.

Technical spec

Facts

Commit 31ff66e.

# Fact Source
F1 Top-level commands are registered in the CLI tree; #1108 adds rotate there and to the root usage src/cli/tree.ts:131-132, #1108 Modules touched
F2 The CLI may import ports and contract, never modules, daemon, drivers, gateway, http src/cli/AGENTS.md:5-8
F3 Every CLI failure is one JSON line on stderr; its code and exit come from the contract's error table; a usage error exits 2 src/cli/errors.ts:45, src/cli/errors.ts:56, src/cli/AGENTS.md:51-52
F4 Exit 14 is LEASE_LOST_EXIT_CODE, no row in the error table; simlock lease returns it when its lease is lost src/contract/errors.ts:216-217, src/cli/commands/lease.ts:223
F5 --token is a global flag, stripped before any command runs, handed to the command as context.token; its help says "connect as admin" src/cli/index.ts:45, src/cli/index.ts:60, src/cli/index.ts:223
F6 Node 22.13's bundled undici WebSocket sends custom headers, so the comment in uplink-websocket.ts is wrong; but it turns any non-101 answer into one generic error with no status or body, and answers pings without telling the caller v6.21.1:lib/web/websocket/connection.js:59-63, v6.21.1:lib/web/websocket/connection.js:108-111, v6.21.1:lib/web/websocket/receiver.js:385-399 (undici); src/ports/uplink-websocket.ts:6-8
F7 GET /v1/leases lists the caller's leases, role agent; an operator sees every lease src/http/lease-declarations.ts:285-292
F8 resolveRemoteLeaseId (moved to a shared CLI file by #1108) picks the only lease and errors on none or several, filtering by requester for an admin session; its messages name requesterId and SIMLOCK_AGENT_ID src/cli/commands/passthrough.ts:106-126
F9 The upgrade router answers a path it does not route with a bare 404 Not Found; lease ids match [^/]+ src/ports/upgrade-router.ts:24-31
F10 TcpStreamConnector connects to a loopback port; TcpStream gains end() in #1112 src/ports/tcp-stream.ts:6-14, #1110 Shared values "byte streams"
F11 The codec, codes, limits and close codes are #1110's Shared values; the worker side is #1112, the relay #1113 #1110 Shared values
F12 ADR 0005 accepts wss://, or ws:// only on loopback, for a gateway URL; WHATWG URL.hostname gives [::1] for IPv6 loopback docs/internal/adr/0005-gateway-and-worker-modes.md:116-121
F13 The slow Android lane runs e2e/slow-android-*.test.ts e2e/slow-android-smoke.test.ts:1
F14 adb reverse reaches a host loopback listener (adb 37.0.0) run 2026-10-30: host listener received "reverse-ok\n"
F15 Only uplink-websocket.ts and upgrade-router.ts import ws, so the CLI and MCP never load it; the CLI may import a ports file only through index, testing or definition src/ports/AGENTS.md Rules, .dependency-cruiser.mjs:29-44
F16 GET /v1/identity answers who the token in the request is, with its role src/http/route-declarations.ts:78-85
F17 src/cli/lazy-loading.test.ts checks which modules each command loads src/cli/lazy-loading.test.ts:1

Modules touched

Searches: name: "adb" in src/cli/tree.ts, resolveRemoteLeaseId, SIMLOCK_ in src/cli/environment.ts, connect as admin.

  • src/cli/ — commands/tunnel.ts (new), tree.ts, index.ts (usage; the global --token help: "the credential a command sends"), environment.ts (SIMLOCK_URL, SIMLOCK_TOKEN); the shared resolver takes a lease list and the caller's role instead of an admin client, and words its errors for the caller (--lease, the token) when called by tunnel; lazy-loading.test.ts gains tunnel and shows no other command loads ws.
  • src/ports/ — websocket-client.ts (new, in the barrel: the port, its fake, and a Node adapter that loads ws with import() on first connect, exposing a refused upgrade's status and body and every ping); http-json.ts (new: one GET with a bearer token, 10 s, body at most 1 MiB, with its fake); uplink-websocket.ts comment corrected; the manual's ws rule names the lazy adapter.
  • src/core/ — FakeDriver's device-tcp opens fake device connections on request (test support).
  • src/daemon/ — tunnel-cli.e2e.test.ts (new).
  • e2e/slow-android-tunnel.test.ts (new).
  • website/ — CLI reference.
flowchart LR
  cli[cli tunnel*] --> ws[ports websocket-client*]
  cli --> http[ports http-json*]
  cli --> tcp[ports tcp-stream]
  cli --> codec[contract tunnel-frames]
  cli --> resolver[cli shared lease resolver*]
Loading

Contract and event changes

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

New inputs: --url an absolute https:// URL, or http:// with hostname 127.0.0.1, [::1] or localhost, at most 2048 characters; the token 1 to 4096 characters; --lease 1 to 256 characters, percent-encoded into the path; tunnel arguments reverse <d>[:<c>] with d and c integers 1 to 65535 (the worker checks d's 1024 floor), at most 8, no caller port twice.

Other code on the same state

Searches: SIMLOCK_TOKEN, SIMLOCK_URL (0 hits), context.token (each command's admin connection).

  • Other commands' use of --token: unchanged (the admin credential for the local daemon); only the help wording widens.
  • simlock simctl/adb: keep using the shared resolver through their admin client's lease list; their tests stay as they are.

Failure modes

Call Outcome What happens Limit, counted from
GET /v1/identity, GET /v1/leases 401 or 403 exit 1 with the body's code 10 s, from the request
GET /v1/identity, GET /v1/leases no answer exit 1 TUNNEL_DISCONNECTED 10 s
upgrade 401, 403, 404, 503 exit 1 with the body's code (from ws's unexpected-response); a bare 404 is UNKNOWN_LEASE 10 s handshake
HELLO no READY or REFUSED exit 1 TUNNEL_DISCONNECTED 90 s, from HELLO
socket REFUSED exit 1 with its code —
socket close 4001 exit 14 TUNNEL_LEASE_ENDED —
socket close 1011, 1001, dropped, or silent exit 1 TUNNEL_DISCONNECTED 45 s with no message or ping
loopback connect on OPEN refused RESET to the worker; the warning line 5 s, from the OPEN
Ctrl-C, SIGTERM — close sent, exit 0 2 s for the close

The command writes no state.

Rules in play

Tests

Seam: the CLI command with fake ports, modelled on src/cli/passthrough.test.ts; the join on a daemon end-to-end with real HTTP, modelled on src/daemon/input-lease.e2e.test.ts.

  • A user runs simlock tunnel reverse … on another machine and sees one line per tunnel, then the device reaches its loopback port; cases: 5173; 8081:3000; two reverses; flags vs env; --lease vs an agent token's only lease vs none vs several; operator token without --lease → usage error; nothing listening → one warning per 10 s, tunnel stays; a 1 MiB response arrives whole (credit granted).
  • A user's simlock tunnel that cannot run exits with its code and one JSON error line; cases: usage errors (70000, abc, 5173:, no tunnel, same caller port twice); http:// to a non-loopback host; 401 on the upgrade; refusal → exit 1 with its code (reverse 80 → TUNNEL_BAD_PORT); no message for 45 s → TUNNEL_DISCONNECTED; no READY in 90 s → TUNNEL_DISCONNECTED; an idle tunnel with server pings stays open past 60 s; a refused upgrade shows its code (401, 404 UNKNOWN_LEASE, 503 TUNNEL_UNAVAILABLE); lease ends → TUNNEL_LEASE_ENDED, exit 14; Ctrl-C and SIGTERM → close sent, exit 0.
  • The CLI against a lone worker with fake devices opens a reverse tunnel, carries bytes, and its rules are gone after Ctrl-C; cases: reverse ok; refusal reaches the CLI as its code; Ctrl-C removes the rule.
  • On a real emulator against a lone worker, a page served on the caller loads on the device through reverse; cases: reverse.

Done when

  • The Tests lines pass.
  • e2e/slow-android-tunnel.test.ts passes in the slow lane on a machine with the Android SDK: an emulator lease on a lone worker with tunnels on; simlock tunnel reverse <p> with a loopback HTTP server on the caller; on the device, (printf 'GET / HTTP/1.0\r\n\r\n'; sleep 2) | nc 127.0.0.1 <p> prints the server's body (the nc form the Facts' check used).
  • lazy-loading.test.ts shows ws loaded by simlock tunnel and by no other command or the MCP entry.
  • The CLI reference describes simlock tunnel, lists SIMLOCK_URL and SIMLOCK_TOKEN, and words the global --token as the credential a command sends.

Out of scope

Depends on

Written by an agent.

Activity

  1. 3 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:

    Not done: the review cap is reached. The fix-check raised R4: the 90 s and 45 s timeout tests never asserted that the socket is terminated. R4 is fixed in 50c1f69, two test assertions only. Both were probed: removing , true from each timer's end call fails the matching assertion. No lens has reviewed the fix.

    Findings: R1, R2 and R4 are fixed. R3 (--url is origin-only) is nonblocking.

    Blocked on: the maintainer, who either accepts 50c1f69 and merges, or asks for one more check. #1115 (#1127) waits on this PR.

    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
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