Skip to content

simlock tunnel emulator adb: the caller's own adb sees the remote emulator #1289

Closed

Description

@V3RON

Part of #1282.

Scope

After this PR a holder of an Android emulator lease runs simlock tunnel emulator adb on their own machine, and their own adb, and so Android Studio, lists the remote emulator as a device and drives it: shell, install, Logcat, the debugger, adb reverse. The command runs adb connect itself and adb disconnect when it exits. A phone or iOS lease is refused.

In short

adb does not reach an emulator through any port inside Android. It connects to a TCP port the emulator program opens on the worker, the console port + 1 (F6, F7). A tunnel can now name that port: an emulator port, chosen by name, never by number. The only name is adb.

  • emulator port: a port the emulator program opens on the worker for one emulator, e.g. adb, which is 5587 for emulator-5586.
flowchart LR
  A[caller's adb] -->|127.0.0.1:c| C[simlock tunnel emulator adb]
  C -->|WS, via gateway| W[tunnel module]
  W -->|"connectEmulatorPort (new)"| D[Android deviceTcp]
  D -->|"127.0.0.1:console+1"| E[emulator program]
Loading

Before and after:

$ simlock tunnel emulator adb
before: usage error, exit 2: Unknown tunnel kind "emulator": use reverse or forward
after:  emulator localhost:61544 -> emulator:adb (lease 9c1d)
        adb: connected to 127.0.0.1:61544
$ adb devices
127.0.0.1:61544	device
Input Today After
emulator adb, emulator lease usage error READY on a free caller port, adb connected
emulator adb:6000, emulator lease usage error READY on caller port 6000
emulator adb, phone lease usage error exit 1 TUNNEL_UNSUPPORTED emulator adb: the lease is on an Android phone; emulator ports need an Android emulator
emulator adb, iOS simulator / iPhone lease usage error exit 1 TUNNEL_UNSUPPORTED, …on an iOS simulator… / …on an iPhone…
emulator console, emulator grpc, emulator 5555, emulator usage error usage error
emulator adb:80, emulator adb:70000, emulator adb: usage error usage error
emulator adb emulator adb usage error usage error
forward 6000 emulator adb:6000 usage error usage error
reverse 8081 emulator adb usage error both tunnels, two lines

Usage errors (exit 2), word for word: an unknown kind Unknown tunnel kind "<word>": use reverse, forward or emulator; Unknown emulator port "<word>": use adb (console, grpc, 5555, nothing); emulator caller port "<c>" is below 1024 and the existing caller-port message for a bad number; Name emulator adb once; the existing Caller port <c> is named by two tunnels. A free caller port is picked after parsing, so it never clashes with a named one.

adb connect is waited at most 10 s from READY; adb disconnect at most 2 s from the start of the exit. Messages, stderr: warning: emulator adb: adb was not found; run: adb connect 127.0.0.1:<c>, warning: emulator adb: adb answered: <adb's first output line>; run: adb connect 127.0.0.1:<c>, warning: emulator adb: adb did not answer in 10 s; run: adb connect 127.0.0.1:<c>.

What a user would notice if this goes wrong: adb devices shows the emulator offline or nothing, or Simlock's own screen stream or input on the lease stops.

Technical spec

Facts

Commit ec46e1c (origin/main).

# Fact Source
F1 HELLO is {tunnels:[{kind,devicePort}]}; a kind other than reverse/forward or a non-integer port is BAD_REQUEST HELLO tunnels must each be {kind, devicePort}; other keys are ignored src/tunnel/sessions.ts:74-87
F2 HELLO order: parse, then the 8-per-lease limit and the 1024–65535 port range, then supports → TUNNEL_UNSUPPORTED src/tunnel/sessions.ts:98-112, src/tunnel/sessions.ts:371-391
F3 A caller OPEN needs an even id above its last and must name one of the socket's forward tunnels, else BAD_REQUEST, close 4003; it calls deviceTcp.connect(platform, device, devicePort, signal), and a DeviceTcpError becomes RESET {reason} src/tunnel/sessions.ts:285-309
F4 ModuleDeviceTcp is supports, reverse, connect, each taking platform first, so it has no by-shape check against core's DeviceTcp; the daemon builds it at src/daemon/main.ts:570 and fakeWorkerContext at src/modules/testing.ts:194; the catalog delegates to driver.deviceTcp, and its refusals are tested in src/core/driver-catalog.test.ts:49 src/modules/types.ts:208-222, src/core/driver-catalog.ts:106-143, git grep -n "deviceTcp: {" origin/main -- src
F5 FakeDeviceTcp has supports, sweep, reverse, connect, openFromDevice; connect refuses unless the port is in listening src/core/fake-device-tcp.ts:40-105
F6 An emulator's serial is emulator-<console port>, console ports even 5586–5682 src/drivers/android/adb-tools.ts:145, src/drivers/android/console-ports.ts:15-16
F7 The emulator serves adb on console port + 1 src/drivers/android/adb-registrar.ts:29-31
F8 The Android driver gets a tcpStreamConnector (127.0.0.1 only) from the daemon and builds deviceTcp in its constructor with target, isOwn, runAdb; its connect bounds itself at 5 s src/daemon/main.ts:1540, src/ports/tcp-stream.ts:65-70, src/drivers/android/index.ts:345-357, src/drivers/android/device-tcp.ts:23
F9 CLI parse: kind word, then device[:caller], caller defaults to device; forward caller port 1024 and up; at most 8; a caller port twice is a usage error; help Usage: simlock tunnel reverse|forward <device-port>[:<caller-port>] src/cli/commands/tunnel.ts:48-81, src/cli/commands/tunnel.ts:539
F10 CLI prints forward localhost:<c> -> device:<d> (lease <id>) on READY; a forward binds its caller port through tcpListeners first (busy → exit 1 TUNNEL_CALLER_PORT_IN_USE); Ctrl-C/SIGTERM close, wait at most 2 s, exit 0 src/cli/commands/tunnel.ts:497-502, src/cli/commands/tunnel.ts:552-558, src/cli/AGENTS.md (5b)
F11 The CLI never imports drivers; every port and process handle comes through CliEnvironment; ProcessRunner (run a command with a time limit) is a port src/cli/AGENTS.md:8, src/cli/types.ts:57, src/ports/process-runner.ts:166
F12 The driver's SDK folders: ANDROID_HOME, ANDROID_SDK_ROOT, then ~/Library/Android/sdk, on Windows %LOCALAPPDATA%/Android/Sdk (fallback ~/AppData/Local/Android/Sdk); adb is platform-tools/adb, adb.exe on Windows src/drivers/android/sdk-paths.ts:28-50, src/drivers/android/sdk-paths.ts:99
F13 Through a TCP relay to 127.0.0.1:5587, a second adb server's adb connect printed connected to 127.0.0.1:6000 and listed device; shell, install and logcat worked on both servers at once; Simlock's server kept emulator-5586 device during and after; the caller's emu kill failed with the emulator alive; the caller's adb reverse tcp:8081 tcp:8081 reached the caller and showed in Simlock's reverse --list spike 2026-11-04, emulator 36.5.11, adb 37.0.0 (ADR 0064 Context)
F14 A Play Store image (ro.adb.secure 1) answered failed to authenticate to 127.0.0.1:6001 and listed unauthorized; a google_apis image has ro.adb.secure 0 same spike
F15 Gateway tunnel code reads no HELLO; it relays frames up to 65541 bytes git grep -n -i hello origin/main -- src/tunnel/gateway.ts src/tunnel/gateway-link.ts (no hit), src/tunnel/AGENTS.md (Through a gateway)
F16 A virtual Android device is never reclaimed; it is destroyed at lease end, and its rules go with its folder src/drivers/android/index.ts:638, src/drivers/android/AGENTS.md (Tunnels: "an emulator's rules die with its folder at destroy")
F17 e2e/slow-android-tunnel.test.ts exists, with a gateway case git ls-tree origin/main e2e/, #1115 Behaviours
F18 Today an unknown kind is the usage error Unknown tunnel kind "<word>": use reverse or forward; a repeated caller port is Caller port <c> is named by two tunnels; the help line is Usage: simlock tunnel reverse|forward <device-port>[:<caller-port>] [reverse|forward ...] [--lease <id>] [--url <url>] src/cli/commands/tunnel.ts:48-81, src/cli/commands/tunnel.ts:539
F19 NodeTcpListenerFactory.listen(port) answers the port it was given, not the bound one, and the fake builds its listener with that port; the published route text says "each {kind, devicePort}"; the real runner throws ProcessSpawnError when a command cannot start, while ScriptedProcessRunner throws a plain error for an unscripted call; the CLI only sets process.exitCode src/ports/tcp-listener.ts:37-50, src/ports/tcp-listener.ts:86-91, src/tunnel/definition.ts:18, src/ports/process-runner.ts:259-265, src/ports/process-runner.ts:762-768, src/cli/main.ts:4

Modules touched

Searches: devicePort (31 non-test hits in src/tunnel, src/cli/commands/tunnel.ts, src/contract/tunnel-frames.ts, src/core), ModuleDeviceTcp (3), deviceTcp (12 non-test files).

flowchart LR
  CLI["cli/commands/tunnel.ts (changed)"] --> ENV["cli environment (changed: runs caller adb)"]
  T["tunnel/sessions.ts (changed)"] --> MT["modules/types.ts (changed)"]
  MT --> CAT["core/driver-catalog.ts (changed)"]
  CAT --> DT["core/device-tcp.ts (changed: interface, message)"]
  CAT --> A["drivers/android/device-tcp.ts (changed)"]
  D["daemon/main.ts (changed: wiring)"] --> CAT
Loading
sequenceDiagram
  participant A as caller's adb
  participant C as simlock tunnel
  participant W as tunnel module
  participant E as emulator :console+1
  C->>W: HELLO [{kind:"emulator", emulatorPort:"adb"}]
  alt phone or iOS lease
    W-->>C: REFUSED TUNNEL_UNSUPPORTED, 4003
  else emulator
    W-->>C: READY
    C->>A: adb connect 127.0.0.1:c
    A->>C: TCP connection
    C->>W: OPEN {kind:"emulator", emulatorPort:"adb"}
    W->>E: connect
    alt nothing listens or 5 s pass
      W-->>C: RESET {reason}
    else ok
      W-->>C: bytes both ways
    end
  end
Loading
  • src/tunnel/sessions.ts: parse and check the emulator entry (Shared values), count it, route its OPEN; src/tunnel/AGENTS.md.
  • src/core/device-tcp.ts: DeviceTcp gains optional supportsEmulatorPort(device) and connectEmulatorPort(device, name, signal); the emulator-port message. src/core/driver-catalog.ts (a driver without the members answers unsupported), src/core/fake-device-tcp.ts, src/modules/types.ts (ModuleDeviceTcp), src/modules/testing.ts (fakeWorkerContext), src/daemon/main.ts:570 (the module context), src/tunnel/definition.ts (the route text names the emulator entry; pnpm openapi).
  • src/ports/tcp-listener.ts: listen(0) answers the port the OS bound, and the fake picks one (F19), so a free caller port is printed and connected as itself.
  • src/drivers/android/device-tcp.ts: the two methods, a phone → unsupported; src/drivers/android/index.ts #deviceTcpFor passes tcpStreamConnector into createAndroidDeviceTcp; src/drivers/android/AGENTS.md.
  • Size list: src/daemon/main.ts is on the size test's shrink-only list (src/source-hygiene.test.ts:39); the PR raises its number by exactly the lines it adds there and says so in its body.
  • src/cli/commands/tunnel.ts, src/cli/environment.ts, src/cli/types.ts: the emulator word, the caller adb run through a ProcessRunner (F11), its own SDK folder list (F12); src/cli/AGENTS.md. adb is "not found" when starting it throws ProcessSpawnError for adb and for every SDK candidate (F19); the CLI test's fake runner throws that error.
  • adb output rule: stdout and stderr together, lines starting * dropped (a fresh server's * daemon not running…); success is a line starting connected to or already connected to; "adb's first line" is the first line left.
  • Exit while adb connect still runs: it is killed, nothing more is printed for it, and adb disconnect still runs.
  • e2e/slow-android-tunnel.test.ts: the emulator case (F17).
  • Docs: cli.mdx (simlock tunnel emulator adb), remote-agents.mdx (Android Studio on a remote emulator; Play Store images answer unauthorized, lease a google_apis image), errors.mdx (the new TUNNEL_UNSUPPORTED text).

Surface and extension points

DeviceTcp's emulator port is one more optional pair of members; a later name (console) is one more accepted emulatorPort value and one more case in the Android driver, not a new interface.

Contract and event changes

None. The frames helper is outside the protocol-bump check and no error code is added (ADR 0064 §7). Protocol-bump: none. New wire field, read by the worker: emulatorPort, a string, exactly adb; anything else, missing, or another type is BAD_REQUEST (Shared values).

Other code on the same state

Searches: adb devices (6 non-test hits), reverse --list (3), emu kill (4), disconnect (2) in src/drivers/android.

  • Simlock's adb server and the caller's server share the emulator's adb port at once; each keeps its own connection (F13). Neither wins; both work.
  • listManaged and the port allocator read adb devices on Simlock's server only; the caller's connection is not there and changes nothing.
  • The caller's adb reverse rules land in the emulator's own list (F13); they go when the emulator is destroyed at lease end (F16); the start sweep removes only ones into 47000–47999; a caller rule on the same device port as a later simlock tunnel reverse makes that tunnel TUNNEL_PORT_IN_USE, as an exec rule does today.
  • The caller's reboot: the device drops off both servers; the existing lost-device handling runs, as for a crash.
  • simlock adb's refusals: unchanged; they guard Simlock's server and the console, which this tunnel does not reach (F13).

Failure modes

Call Outcome What happens Limit, counted from
worker connect to 127.0.0.1:<console+1> ok bytes flow 5 s, from the caller's OPEN
same refused RESET <the connector's message> same
same no answer RESET timeout same
same device record has no emulator-<n> serial RESET the emulator has no adb port yet —
caller adb connect prints connected to stdout adb: connected to 127.0.0.1:<c> 10 s, from READY
same adb not found warning, tunnel stays —
same other output (failed to authenticate, cannot connect) warning quoting the first line, tunnel stays same
same no answer warning did not answer in 10 s, the adb process killed, tunnel stays same
caller adb disconnect any outcome ignored; exit code as without it 2 s, from the start of the exit
caller adb disconnect still running at 2 s killed; the command exits with its own code same
caller adb connect still running when the exit starts killed, no warning; the disconnect runs —

The CLI prints a reset emulator connection as stderr warning: emulator adb: <reason>, at most once per 10 s per tunnel, the same rule #1288 gives forward tunnels (#1282 Shared values, CLI forward reset warning).

State: nothing new is stored. A daemon restart closes the socket; the caller's adb then shows the device offline until it disconnects. Two emulator adb tunnels on one lease from two sockets: both work, each counted toward 8.

Rules in play

  • ADR 0064 §1, §5–7: the adb name only, full adb for the holder, the CLI's connect and disconnect, no protocol bump.
  • ADR 0001 §4, as narrowed by 0064: no adb server on the worker sees a Simlock emulator; this task starts none there.
  • ADR 0054 §5 ("reaches adb only through a port the daemon gives it"): the emulator port goes through deviceTcp, the connector from the daemon.
  • Safety rule 1 (never act on a phone not enrolled; ADR 0022 §3): a phone is refused, nothing runs on it.
  • Safety rule 10: emulatorPort from the wire is checked against the one allowed value.

Tests

Seam: the worker module through src/tunnel/worker.test.ts with FakeDeviceTcp; the real listener through src/ports/tcp-listener.test.ts; the driver through the deviceTcp cases of src/drivers/android/index-lifecycle.test.ts; the CLI through src/cli/tunnel.test.ts with fake ports and a fake process runner; the slow lane in e2e/slow-android-tunnel.test.ts.

  • A holder's HELLO with an emulator entry is checked and answered: {kind:"emulator", emulatorPort:"adb"} on an emulator → READY echoing it; with two device-port tunnels → 3 counted toward 8; emulatorPort console or missing → BAD_REQUEST; on a phone, an iOS simulator, an iPhone → TUNNEL_UNSUPPORTED with the emulator-port message.
  • A holder's emulator connection reaches the emulator's adb port: caller OPEN emulator → bytes both ways; OPEN emulator when HELLO named none → BAD_REQUEST, 4003; the port refuses → RESET {reason}, tunnel stays.
  • The Android driver connects an emulator port to the right worker port: emulator-5586 → 127.0.0.1:5587; emulator-5682 → 5683; a phone → unsupported; nothing listening → TUNNEL_FAILED with the reason; no answer in 5 s → TUNNEL_FAILED timeout; a driver built without a tcpStreamConnector → TUNNEL_FAILED "the emulator has no adb port yet" (decided in delivery, pending the maintainer). The daemon wiring of supportsEmulatorPort and connectEmulatorPort gets no fast test: the slow-lane case below proves it (decided in delivery, pending the maintainer).
  • The real loopback listener asked for any free port reports the port it bound: listen(0) → a port above 0 that a client can connect to; listen(<a taken port>) → undefined, as today.
  • The catalog says which leases take an emulator port: a driver whose deviceTcp has the emulator members → delegated; one without them (iOS) → unsupported; no deviceTcp → unsupported.
  • A user runs simlock tunnel emulator adb… and sees the tunnel line, and adb connected or a warning: emulator adb (free port → the bound port, not 0, in the line and in adb connect); reverse 8081 emulator adb → one HELLO with both, two lines, a reverse OPEN still reaches localhost:8081; * daemon not running then connected to → info line; already connected to → info line; emulator adb:6000 → READY on 6000; adb missing on PATH and in every SDK folder → warning; found only under $ANDROID_HOME → used; found only in the default SDK folder → used; adb answers failed to authenticate → warning with that line; adb hangs past 10 s → warning; the worker resets an emulator connection → warning: emulator adb: <reason>, once per 10 s; on Ctrl-C, a refusal after READY, lease end and a lost connection → adb disconnect run; disconnect fails → exit code unchanged; disconnect hangs → exit at 2 s, code unchanged; Ctrl-C while connect still runs → connect killed, no warning, disconnect run.
  • A user's simlock tunnel emulator … that cannot run exits 2 with a usage error, and the help line names emulator adb[:<caller-port>]: tunnel emulate adb (unknown kind); emulator console; emulator grpc; emulator 5555; emulator; emulator adb:80; emulator adb:70000; emulator adb:; emulator adb emulator adb; forward 6000 emulator adb:6000.
  • On a real emulator through a gateway, the caller's own adb sees and drives the emulator: a separate adb server (its own port and HOME) lists device; shell echo ok → ok; after exit Simlock's server still lists the emulator device.

Done when

  • pnpm check passes.
  • Slow lane, on a machine with the Android SDK: scripts/slow-e2e.sh e2e/slow-android-tunnel.test.ts passes, its emulator case included (needs a real emulator).
  • cli.mdx, remote-agents.mdx and errors.mdx describe simlock tunnel emulator adb, the automatic adb connect, the Play Store unauthorized case and the refusals.

Out of scope

  • The console and grpc names, phones, iOS, filtering the caller's adb, adding the caller's key on a Play Store image.

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. 4 remaining items

  5. added a commit that references this issue on Nov 5, 2026
    e3a4972
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