Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

docs: a loopback relay for the stock adb macOS keeps off the LAN (#3336)

xbsheng20c72458f1f3
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index 7447915fe..fef8c0a1e 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -24,6 +24,10 @@ Things that bit us or contributors, so they don't bite you. Commands use `tools/
network?` followed by the Local Network note, exit 2, so a machine without the permission can neither
confirm nor deny that the port is closed. Grant it to the terminal, reopen it, and check again: only a
"closed" from a granted terminal counts.
+ If `tools/tv.py` reaches the TV from that terminal but the stock `adb` still answers `No route to host
+ (65)`, the terminal holds the permission and the server `adb` leaves behind does not, so granting it
+ again cannot help: [tools/native-adb.md](../tools/native-adb.md#macos-a-stock-adb-that-cannot-reach-the-lan)
+ has a loopback relay for that case ([#3336](https://github.com/Gathin23/cleanyourtv/issues/3336)).
- **No "wireless debugging" toggle** on some TVs, Xiaomi among them. USB debugging alone opens
network ADB on port 5555.
- **USB debugging is greyed out and there is no Wireless debugging entry either**, seen on a carrier
diff --git a/tools/native-adb.md b/tools/native-adb.md
index f3ea2e019..d1772c59f 100644
--- a/tools/native-adb.md
+++ b/tools/native-adb.md
@@ -44,3 +44,76 @@ When finished, switch Wireless debugging and any other enabled debugging modes o
TV and revoke the authorizations. Check the noted connection port with
`python3 tools/adb-closed.py <tv-ip> <connection-port>` before rebooting, then check that
debugging remains off after the reboot. Pairing codes, keys and screenshots stay private.
+
+## macOS: a stock `adb` that cannot reach the LAN
+
+On macOS 15 (Sequoia) the `adb` command starts its own server daemon (`adb fork-server server`) and
+leaves the work to it. Local Network privacy grants the permission to the app that ran the command -
+Terminal, iTerm2, VS Code - and that daemon outlives the command, which the report reads as the
+grant not reaching it: `adb connect <tv-ip>:5555` answers `failed to connect to '<tv-ip>:5555': No
+route to host (65)` while `tools/tv.py` in the same window reaches the TV. Reported on macOS Sequoia
+in [#3336](https://github.com/Gathin23/cleanyourtv/issues/3336), which names no TV model.
+
+The Python client needs nothing here, since it opens its own socket from the window that holds the
+permission. The stock binary is what `TV_ADB_BINARY` runs for a Wireless debugging pairing, and what
+`adb install` and `adb push` use. The relay below runs in that window instead: it listens on
+loopback, which the Local Network check does not cover, and forwards to the TV from the process that
+does hold the permission. Save it as `relay.py`:
+
+```python
+#!/usr/bin/env python3
+"""Forward 127.0.0.1:<local port> to <tv ip>:<tv port>, for a stock adb blocked on macOS."""
+import socket
+import sys
+import threading
+
+
+def pipe(src, dst):
+ try:
+ while data := src.recv(65536):
+ dst.sendall(data)
+ except OSError:
+ pass
+ finally:
+ for sock in (src, dst):
+ sock.close()
+
+
+def main():
+ tv_host, tv_port, local_port = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
+ server = socket.socket()
+ server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ server.bind(("127.0.0.1", local_port))
+ server.listen(8)
+ print(f"relaying 127.0.0.1:{local_port} -> {tv_host}:{tv_port}", flush=True)
+ while True:
+ client, _ = server.accept()
+ upstream = socket.create_connection((tv_host, tv_port), timeout=10)
+ upstream.settimeout(None) # the connect timeout must not cut an idle session off later
+ threading.Thread(target=pipe, args=(client, upstream), daemon=True).start()
+ threading.Thread(target=pipe, args=(upstream, client), daemon=True).start()
+
+
+main()
+```
+
+Start it for the port `adb` has to reach, and give both `adb` and the tools the loopback address:
+
+```bash
+python3 relay.py <tv-ip> 5555 15555
+adb connect 127.0.0.1:15555
+export TV_ADB_BINARY=adb
+export TV_HOST=127.0.0.1:15555
+tools/venv/bin/python -I tools/tv.py --probe
+```
+
+A pairing goes through a second copy on the pairing port while the first one carries the connection,
+for example `python3 relay.py <tv-ip> 37123 37123` and then `adb pair 127.0.0.1:37123`, since the two
+ports are different. Two things to keep in mind, as it bridges rather than fixes the denial:
+
+- It has to stay up for as long as `adb` is used. Stop it and the connection it carries reads as a
+ TV that went away; `adb kill-server` reconnects through it only while it listens.
+- What it forwards is the plaintext port, though only from `127.0.0.1`, so nothing else on the network
+ reaches the TV through it. The Python client needs no relay at all, and a window that is itself
+ denied by Local Network privacy still fails: that is the macOS bullet in
+ [docs/TROUBLESHOOTING.md](../docs/TROUBLESHOOTING.md#connecting), not this one.