Skip to content

Support adb reverse through the TcpUsb bridge - #550

Merged
koral-- merged 1 commit into
DeviceFarmer:masterfrom
jozenstar:tcpusb-reverse
Oct 5, 2026
Merged

koral-- merged 1 commit into
DeviceFarmer:masterfrom
jozenstar:tcpusb-reverse

Conversation

@jozenstar

Copy link
Copy Markdown

adb reverse doesn't work through the TcpUsb bridge (createTcpUsbBridge, used by STF for remote debugging). The bridge only relays streams the client opens. A reverse stream is opened by the device's adbd towards the adb server it's attached to, which is the bridge's host, so it never reaches the client. Android Studio's Device Mirroring and scrcpy's default tunnel both depend on it: Studio's mirroring agent exits with code 0, and scrcpy only works with --force-adb-forward.

Change

  • Rewrite reverse:forward: reverse:forward:[norebind:]<remote>;<local> now points <remote> at a listener on the bridge host (127.0.0.1:<ephemeral>).
  • Relay each connection: each connection to that listener goes to the client as a device-initiated A_OPEN <local>, the same packet a USB-attached device's adbd sends. The new ReverseStream carries the A_OKAY/A_WRTE/A_CLSE exchange.
  • Half-closed connections: a device side that closes before the client's first A_OKAY is flushed first, then closed.
  • Cleanup: reverse:killforward and reverse:killforward-all close the matching listeners, and all listeners close when the bridge socket ends.
  • Remote adb server: if the adb server isn't on the bridge's host, the rewrite is skipped and the service passes through unchanged, because that server couldn't reach the listener.

Known limitation: adb reverse --list shows the bridge's listener port as the local side, not the original <local>.

Testing

  • Unit tests: added test/adb/tcpusb/socket.ts (rewrite, pass-through, killforward, relay with a half-close). npm test: 203 passing.
  • Lint: npm run lint fails on master before this change. .eslintrc extends prettier/@typescript-eslint, which eslint-config-prettier 8 removed. Prettier passes on the changed files.
  • Real devices: deployed on two STF 3.8.0 providers, patching the published 3.3.9 build.
    • adb reverse tcp:9123 tcp:9123 plus nc on the device reaches a listener on the client host and gets its reply.
    • scrcpy 4.1 works without --force-adb-forward.

The bridge only relays client-initiated streams. With `adb reverse`, the
device's adbd connects the reverse stream to the adb server it is attached
to, i.e. the bridge's host, so the client never sees it. Android Studio's
Device Mirroring and scrcpy's default tunnel rely on adb reverse, and fail
through the bridge (Studio's agent exits with code 0; scrcpy needs
--force-adb-forward).

reverse:forward:<remote>;<local> is now rewritten to point <remote> at a
listener on the bridge host. Each connection to it is relayed to the client
as a device-initiated A_OPEN <local>, which is what a USB-attached device's
adbd sends. reverse:killforward and killforward-all close the listeners,
and they close with the socket.

The rewrite is skipped when the adb server is not on the bridge's host,
since that server has to be able to reach the listener.

Signed-off-by: Arthur Yazhevich <[email protected]>
@koral--
koral-- merged commit 68899b2 into DeviceFarmer:master Oct 5, 2026
2 checks passed
@jozenstar
jozenstar deleted the tcpusb-reverse branch October 5, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants