Skip to content

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

tools/adb-closed.py: check that port 5555 is closed without nc

James Coates350695a80f80
diff --git a/README.md b/README.md
index f322f078b..5d175c9bf 100644
--- a/README.md
+++ b/README.md
@@ -343,6 +343,7 @@ same as developing an Android app. Network ADB is plaintext on port 5555, and on
1. On the TV: Developer options → turn **USB debugging** and **Network/ADB debugging** off, and
revoke the USB debugging authorisations.
2. Reboot the TV, then confirm the port is gone from your computer: `nc -vz <tv ip> 5555` must fail.
+ Without `nc` (Windows): `python tools/adb-closed.py <tv ip>` must say the port is closed.
The launcher keeps working without debugging.
diff --git a/tests/test_adb_closed.py b/tests/test_adb_closed.py
new file mode 100644
index 000000000..4b22a7d38
--- /dev/null
+++ b/tests/test_adb_closed.py
@@ -0,0 +1,45 @@
+"""tools/adb-closed.py tells an open port from a closed one, on 127.0.0.1.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+from pathlib import Path
+import socket
+import subprocess
+import sys
+import unittest
+
+
+ROOT = Path(__file__).resolve().parents[1]
+SCRIPT = ROOT / "tools" / "adb-closed.py"
+
+
+def check(*args):
+ return subprocess.run([sys.executable, "-I", str(SCRIPT), *args],
+ capture_output=True, text=True, timeout=30)
+
+
+class AdbClosedTest(unittest.TestCase):
+ def test_open_port(self):
+ with socket.socket() as server:
+ server.bind(("127.0.0.1", 0))
+ server.listen(1)
+ result = check("127.0.0.1", str(server.getsockname()[1]))
+ self.assertEqual(result.returncode, 1, result.stderr)
+ self.assertIn("still open", result.stderr)
+
+ def test_closed_port(self):
+ # Take a free port and let it go again: nothing listens there, so the connection is refused.
+ with socket.socket() as probe:
+ probe.bind(("127.0.0.1", 0))
+ port = probe.getsockname()[1]
+ result = check("127.0.0.1", str(port))
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertIn("is closed: the TV refused", result.stdout)
+
+ def test_bad_arguments(self):
+ self.assertEqual(check("127.0.0.1", "not-a-port").returncode, 2)
+ self.assertEqual(check("a", "b", "c").returncode, 2)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/adb-closed.py b/tools/adb-closed.py
new file mode 100644
index 000000000..5b26114ba
--- /dev/null
+++ b/tools/adb-closed.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+"""Check that the TV's network ADB port is closed, after turning debugging off (no nc needed).
+
+ adb-closed.py <tv ip> [port] port defaults to TV_PORT or 5555; the IP to TV_HOST
+
+Exits 0 when the port refuses the connection or does not answer, 1 when it is still open, and 2
+when the check itself could not run (a name that does not resolve, no route to the TV).
+"""
+import os
+import socket
+import sys
+
+TIMEOUT_S = 3.0
+
+
+def main():
+ args = sys.argv[1:]
+ if len(args) > 2 or args[:1] in (["-h"], ["--help"]):
+ sys.stderr.write(__doc__)
+ return 2
+ host = args[0] if args else os.environ.get("TV_HOST", "")
+ if not host:
+ sys.stderr.write("usage: adb-closed.py <tv ip> [port]\n")
+ return 2
+ try:
+ port = int(args[1] if len(args) == 2 else os.environ.get("TV_PORT", "5555"))
+ except ValueError:
+ sys.stderr.write(f"not a port number: {args[1]!r}\n")
+ return 2
+
+ try:
+ with socket.create_connection((host, port), timeout=TIMEOUT_S):
+ pass
+ except ConnectionRefusedError:
+ print(f"{host}:{port} is closed: the TV refused the connection.")
+ return 0
+ except socket.timeout:
+ # Some firmware drops instead of refusing, but so does a TV that is off or at another address.
+ print(f"{host}:{port} did not answer in {TIMEOUT_S:.0f} s: closed, unless the TV is off or not at "
+ "this address.")
+ return 0
+ except OSError as e:
+ sys.stderr.write(f"could not check {host}:{port} ({e}). Is the TV on and on this network?\n")
+ return 2
+ sys.stderr.write(f"{host}:{port} is still open: network ADB is on. On the TV, turn USB debugging and\n"
+ "Network/ADB debugging off, reboot it, and check again.\n")
+ return 1
+
+
+if __name__ == "__main__":
+ sys.exit(main())