Skip to content

Debloat your Android TV, install apps, browse and transfer files on it, all from your phone. Just 3 MB, no tracking, no root, no PC.

Sweep only Wi-Fi/Ethernet; refuse actions when the launcher is unknown

jesse-agent-362199234af7a01
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
index 1494ecf..95d1380 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
@@ -18,7 +18,9 @@ class Actions(private val shell: AdbShell) {
suspend fun run(action: Action, pkg: String): ActionResult {
require(PACKAGE.matches(pkg)) { "not a package name: $pkg" } // it goes into a shell line
if (action != Action.ENABLE && action != Action.OPEN) {
- Protected.reason(pkg, homePackages())?.let { return ActionResult.Refused(it) }
+ // No launcher answer (no `cmd` before Android 7, or an error): we can't tell what's safe.
+ val home = homePackages().ifEmpty { return ActionResult.Refused(ProtectedReason.UNKNOWN_HOME) }
+ Protected.reason(pkg, home)?.let { return ActionResult.Refused(it) }
}
return when (action) {
Action.STOP -> exec("am force-stop $pkg", Action.OPEN)
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/LanScanner.kt b/adb/src/main/kotlin/app/tellyscope/adb/LanScanner.kt
index 6938079..80c78d8 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/LanScanner.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/LanScanner.kt
@@ -41,8 +41,10 @@ data class ScanHit(
}
/**
- * Finds ADB on the local /24s: a TCP sweep of [port] plus, if given, mDNS for wireless debugging.
- * MAC/maker lookup is left out: Android 10+ apps can't read the ARP table.
+ * Finds ADB on the LAN: a TCP sweep of [port] over the hosts the caller passes, plus, if given, mDNS
+ * for wireless debugging. The caller picks the network: only Wi-Fi or Ethernet, never cellular or a
+ * VPN (see [subnetHosts], [lanHosts]). MAC/maker lookup is left out: Android 10+ apps can't read
+ * the ARP table.
*/
class LanScanner(
private val mdns: KadbMdns? = null,
@@ -52,7 +54,7 @@ class LanScanner(
private val parallelism: Int = 64,
) {
/** Emits each hit as soon as it answers; completes when the sweep and the mDNS window end. */
- fun scan(hosts: List<String> = localSubnetHosts()): Flow<ScanHit> = channelFlow {
+ fun scan(hosts: List<String>): Flow<ScanHit> = channelFlow {
launch { sweep(hosts) }
mdns?.let { launch { browse(it) } }
}
@@ -100,17 +102,34 @@ class LanScanner(
runCatching { InetAddress.getByName(host).canonicalHostName }.getOrNull()?.takeIf { it != host }
companion object {
- /** Every other address in the /24 of each up, non-loopback IPv4 interface. */
- fun localSubnetHosts(): List<String> =
- NetworkInterface.getNetworkInterfaces().toList()
- .filter { it.isUp && !it.isLoopback }
- .flatMap { it.inetAddresses.toList() }
- .filterIsInstance<Inet4Address>()
- .flatMap { addr ->
- val b = addr.address
- val prefix = "${b[0].toUByte()}.${b[1].toUByte()}.${b[2].toUByte()}"
- (1..254).map { "$prefix.$it" }.filter { it != addr.hostAddress }
+ /** Every other host in [address]'s subnet, capped at the surrounding /24. */
+ fun subnetHosts(address: Inet4Address, prefixLength: Int = 24): List<String> {
+ val bits = 32 - prefixLength.coerceIn(24, 30)
+ val self = address.address.fold(0) { acc, b -> (acc shl 8) or (b.toInt() and 0xff) }
+ val base = self and (-1 shl bits)
+ return (1 until (1 shl bits) - 1).map { base + it }.filter { it != self }.map { ip ->
+ (3 downTo 0).joinToString(".") { ((ip ushr (it * 8)) and 0xff).toString() }
+ }
+ }
+
+ /**
+ * Hosts on this machine's Wi-Fi and Ethernet interfaces, for the JVM CLI. Cellular (rmnet,
+ * ccmni), VPN (tun, wg, ppp) and bridges are never swept. On Android, pass the active Wi-Fi
+ * or Ethernet network's address to [subnetHosts] instead.
+ */
+ fun lanHosts(): List<String> = lanHosts(
+ NetworkInterface.getNetworkInterfaces().toList().filter { it.isUp && !it.isLoopback }.flatMap { i ->
+ i.interfaceAddresses.mapNotNull { a ->
+ (a.address as? Inet4Address)?.let { Triple(i.name, it, a.networkPrefixLength.toInt()) }
}
- .distinct()
+ },
+ )
+
+ /** [addresses]: interface name, its IPv4 address and prefix length. */
+ internal fun lanHosts(addresses: List<Triple<String, Inet4Address, Int>>): List<String> =
+ addresses.filter { LAN_INTERFACE.matches(it.first) }.flatMap { subnetHosts(it.second, it.third) }.distinct()
+
+ // eth0, wlan0, and predictable names: enp3s0, wlp2s0, macOS en0.
+ private val LAN_INTERFACE = Regex("eth\\d+|(en|wl)\\w+")
}
}
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt b/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt
index 93803d6..b36aee6 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt
@@ -3,7 +3,12 @@
package app.tellyscope.adb
/** Why a package may not be stopped or disabled. The UI shows the matching copy. */
-enum class ProtectedReason { HOME_SCREEN, SYSTEM, SETTINGS, PLAY_SERVICES, KEYBOARD, TV_INPUTS_REMOTE, SETUP_WIZARD }
+enum class ProtectedReason {
+ HOME_SCREEN, SYSTEM, SETTINGS, PLAY_SERVICES, KEYBOARD, TV_INPUTS_REMOTE, SETUP_WIZARD,
+
+ /** The TV didn't say which app is its home screen, so nothing is stopped or disabled. */
+ UNKNOWN_HOME,
+}
/**
* Packages Tellyscope never stops or disables. Over-matching is fine: a protected package just
@@ -34,6 +39,7 @@ object Protected {
/** Name fragments, checked in order; manufacturer names vary, so these match loosely. */
private val PATTERNS: List<Pair<String, ProtectedReason>> = listOf(
+ "launcher" to ProtectedReason.HOME_SCREEN, // second guard behind the live home lookup
"networkstack" to ProtectedReason.SYSTEM, // Wi-Fi and Ethernet setup; we'd cut our own link
"inputmethod" to ProtectedReason.KEYBOARD,
"keyboard" to ProtectedReason.KEYBOARD,
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
index b500f1e..fb62571 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
@@ -36,6 +36,19 @@ class ActionsTest {
assertEquals(ActionResult.Refused(ProtectedReason.HOME_SCREEN), Actions(sh).run(Action.DISABLE, "com.spocky.projengmenu"))
}
+ @Test fun unknownHomeRefusesStopAndDisable() = runTest {
+ // Android 6 and older have no `cmd`: both launcher lookups come back empty.
+ val sh = FakeShell(mapOf("cmd package" to ShellResult("/system/bin/sh: cmd: not found\n", 127)))
+ val a = Actions(sh)
+ assertEquals(ActionResult.Refused(ProtectedReason.UNKNOWN_HOME), a.run(Action.DISABLE, "com.example.tvlauncher"))
+ assertEquals(ActionResult.Refused(ProtectedReason.UNKNOWN_HOME), a.run(Action.STOP, "com.foo.ads"))
+ assertTrue(sh.ran.none { it.startsWith("pm ") || it.startsWith("am ") })
+ }
+
+ @Test fun launcherNamesAreProtectedEvenWhenNotDefault() = runTest {
+ assertEquals(ActionResult.Refused(ProtectedReason.HOME_SCREEN), Actions(FakeShell(mapOf(home))).run(Action.DISABLE, "com.example.tvlauncher"))
+ }
+
@Test fun refusesProtectedPackages() = runTest {
val a = Actions(FakeShell(mapOf(home)))
assertEquals(ActionResult.Refused(ProtectedReason.PLAY_SERVICES), a.run(Action.DISABLE, "com.google.android.gms"))
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/LanScannerTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/LanScannerTest.kt
index c1c1e8c..dc94ace 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/LanScannerTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/LanScannerTest.kt
@@ -3,10 +3,12 @@ package app.tellyscope.adb
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
+import java.net.Inet4Address
import java.net.InetAddress
import java.net.ServerSocket
import kotlin.test.Test
import kotlin.test.assertEquals
+import kotlin.test.assertTrue
class LanScannerTest {
@Test fun findsOnlyOpenPorts() = runBlocking {
@@ -16,4 +18,30 @@ class LanScannerTest {
assertEquals(ScanHit.Kind.CLASSIC, hits.single().kind)
}
}
+
+ private fun v4(ip: String) = InetAddress.getByName(ip) as Inet4Address
+
+ @Test fun sweepsOnlyWifiAndEthernet() {
+ val hosts = LanScanner.lanHosts(listOf(
+ Triple("rmnet_data0", v4("100.64.12.7"), 24), // cellular, carrier CGNAT
+ Triple("tun0", v4("172.20.0.2"), 24), // VPN
+ Triple("wlan0", v4("192.168.1.20"), 24),
+ ))
+ assertEquals(253, hosts.size)
+ assertTrue(hosts.all { it.startsWith("192.168.1.") } && "192.168.1.20" !in hosts)
+ }
+
+ @Test fun cellularOrVpnAloneSweepsNothing() {
+ assertEquals(emptyList(), LanScanner.lanHosts(listOf(
+ Triple("rmnet_data0", v4("100.64.12.7"), 24),
+ Triple("ccmni0", v4("100.70.1.2"), 22),
+ Triple("tun0", v4("172.20.0.2"), 24),
+ Triple("wg0", v4("172.21.0.2"), 24),
+ )))
+ }
+
+ @Test fun subnetCapsAtTheSurroundingSlash24() {
+ assertEquals(253, LanScanner.subnetHosts(v4("172.16.5.9"), 16).size)
+ assertEquals(listOf("192.168.1.1", "192.168.1.3", "192.168.1.4", "192.168.1.5", "192.168.1.6"), LanScanner.subnetHosts(v4("192.168.1.2"), 29))
+ }
}
diff --git a/cli/src/main/kotlin/app/tellyscope/cli/Main.kt b/cli/src/main/kotlin/app/tellyscope/cli/Main.kt
index 69e6e01..21f2194 100644
--- a/cli/src/main/kotlin/app/tellyscope/cli/Main.kt
+++ b/cli/src/main/kotlin/app/tellyscope/cli/Main.kt
@@ -21,7 +21,7 @@ import java.io.File
fun main(args: Array<String>) = runBlocking {
Connector.init(File(System.getProperty("user.home"), ".tellyscope/adbkey.pem"))
when (args.firstOrNull()) {
- "scan" -> LanScanner(KadbMdnsJvm()).scan().collect { println("${it.host}:${it.port} ${it.kind} ${it.name ?: "-"}") }
+ "scan" -> LanScanner(KadbMdnsJvm()).scan(LanScanner.lanHosts()).collect { println("${it.host}:${it.port} ${it.kind} ${it.name ?: "-"}") }
"pair" -> Connector.pair(args[1], args[2].toInt(), args[3]).also { println("paired") }
"stats" -> {
val link = DeviceLink(args[1], args.getOrNull(2)?.toInt() ?: 5555, this)