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.

Who has access: look up network names for other computers

jesse-agent-8217b9fe6c8182f
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index fa08d68..4580304 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -98,6 +98,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
/** TVs whose Apps tab got Not now on the card about a better home screen. */
private val launcherCards = FoldBook(File(appContext.filesDir, "launcher-card.properties"))
private val mine = MineBook(File(appContext.filesDir, "mine.tsv"))
+ private val networkNames = NetworkNames()
/** Last label seen per package, so history entries read like the app list. */
private val labels = ConcurrentHashMap<String, String>()
/** Per TV, the session [fireHome] read on and the launcher it found. */
@@ -767,7 +768,10 @@ class AdbDeviceSource(context: Context) : DeviceSource {
override suspend fun access(deviceId: String): TvAccess {
val a = Access(session(deviceId))
val port = links[deviceId]?.port ?: Access.CLASSIC_PORT
- return TvAccess.of(a.clients(port), a.keys(), phoneAddresses(), deviceId, mine[key(deviceId)], phoneFingerprint())
+ val access = TvAccess.of(a.clients(port), a.keys(), phoneAddresses(), deviceId, mine[key(deviceId)], phoneFingerprint())
+ // Other computers only: this phone and apps on the TV itself need no name.
+ val lookup = access.connected.orEmpty().filter { it.who == AccessClient.Who.Other || it.who == AccessClient.Who.Mine }
+ return access.named(networkNames.of(lookup.map { it.address }))
}
override suspend fun markMine(deviceId: String, address: String) {
diff --git a/app/src/main/java/app/tellyscope/data/NetworkNames.kt b/app/src/main/java/app/tellyscope/data/NetworkNames.kt
new file mode 100644
index 0000000..fb19828
--- /dev/null
+++ b/app/src/main/java/app/tellyscope/data/NetworkNames.kt
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-3.0-only
+
+package app.tellyscope.data
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Deferred
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
+import kotlinx.coroutines.withTimeoutOrNull
+import java.net.InetAddress
+import java.util.concurrent.ConcurrentHashMap
+import kotlin.time.Duration
+import kotlin.time.Duration.Companion.minutes
+import kotlin.time.Duration.Companion.seconds
+
+/**
+ * Network names for the other computers in Who has access, by reverse DNS: the name the router or
+ * the device gave itself, so any device can claim any name. The caller waits [wait] at most; a
+ * lookup still running then finishes on its own (a DNS call can't be interrupted) and its answer
+ * is there next time. Answers, none included, are kept for [keep], so a screen that reads access
+ * every second doesn't ask again each time.
+ */
+class NetworkNames(
+ private val wait: Duration = 1.5.seconds,
+ private val keep: Duration = 10.minutes,
+ private val clock: () -> Long = System::currentTimeMillis,
+ private val resolve: (String) -> String? = { ip -> InetAddress.getByName(ip).canonicalHostName },
+) {
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
+ private val known = ConcurrentHashMap<String, Pair<Long, String?>>()
+ private val asking = ConcurrentHashMap<String, Deferred<String?>>()
+
+ /** [addresses] to their names; an address with no name, or none yet, is left out. */
+ suspend fun of(addresses: Collection<String>): Map<String, String> {
+ val now = clock()
+ val asks = addresses.distinct()
+ .filter { a -> known[a]?.let { now - it.first > keep.inWholeMilliseconds } ?: true }
+ .map { a -> asking.computeIfAbsent(a) { ask(a) } }
+ if (asks.isNotEmpty()) withTimeoutOrNull(wait) { asks.awaitAll() }
+ return addresses.mapNotNull { a -> known[a]?.second?.let { a to it } }.toMap()
+ }
+
+ private fun ask(address: String) = scope.async {
+ val name = runCatching { clean(resolve(address), address) }.getOrNull()
+ known[address] = clock() to name
+ asking.remove(address)
+ name
+ }
+
+ companion object {
+ /** Null for no answer or the address itself; a trailing dot and a `.local` suffix go. */
+ fun clean(name: String?, address: String): String? =
+ name?.trim()?.removeSuffix(".")?.let { if (it.endsWith(".local", ignoreCase = true)) it.dropLast(6) else it }
+ ?.takeIf { it.isNotEmpty() && it != address }
+ }
+}
diff --git a/app/src/main/java/app/tellyscope/data/TvAccess.kt b/app/src/main/java/app/tellyscope/data/TvAccess.kt
index b3597e0..b9d0afb 100644
--- a/app/src/main/java/app/tellyscope/data/TvAccess.kt
+++ b/app/src/main/java/app/tellyscope/data/TvAccess.kt
@@ -20,6 +20,10 @@ data class TvAccess(
val warning: Boolean get() = others.isNotEmpty()
+ /** The same, with [names] on the computers they belong to (address to name). */
+ fun named(names: Map<String, String>): TvAccess =
+ copy(connected = connected?.map { c -> names[c.address]?.let { c.copy(name = it) } ?: c })
+
/** Checked, not assumed: both lists read, no other connection, and no allowed key but this phone's. */
val onlyThisPhone: Boolean get() = connected != null && allowed != null && others.isEmpty() && allowed.none { !it.phone }
@@ -56,7 +60,11 @@ data class TvAccess(
}
}
-data class AccessClient(val address: String, val connections: Int, val who: Who) {
+/**
+ * [name]: the address's network name (see [NetworkNames]), null when none answered in time. It
+ * describes the address only, never which key is behind it.
+ */
+data class AccessClient(val address: String, val connections: Int, val who: Who, val name: String? = null) {
enum class Who { Phone, Mine, Other, OnTv }
}
diff --git a/app/src/test/java/app/tellyscope/NetworkNamesTest.kt b/app/src/test/java/app/tellyscope/NetworkNamesTest.kt
new file mode 100644
index 0000000..56241b0
--- /dev/null
+++ b/app/src/test/java/app/tellyscope/NetworkNamesTest.kt
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-3.0-only
+
+package app.tellyscope
+
+import app.tellyscope.data.NetworkNames
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.runBlocking
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import java.util.concurrent.atomic.AtomicInteger
+import kotlin.time.Duration.Companion.milliseconds
+import kotlin.time.Duration.Companion.minutes
+
+/** Real time throughout: the lookups run on their own threads, as a DNS call does. */
+class NetworkNamesTest {
+
+ @Test fun cleansTheName() {
+ assertEquals("Alex-MacBook", NetworkNames.clean("Alex-MacBook.local", "192.0.2.47"))
+ assertEquals("Alex-MacBook", NetworkNames.clean("Alex-MacBook.LOCAL.", "192.0.2.47"))
+ assertEquals("desk.lan", NetworkNames.clean("desk.lan", "192.0.2.47"))
+ assertNull(NetworkNames.clean("192.0.2.47", "192.0.2.47")) // no PTR: Java hands the address back
+ assertNull(NetworkNames.clean(".local", "192.0.2.47"))
+ assertNull(NetworkNames.clean(null, "192.0.2.47"))
+ }
+
+ @Test fun namesWhatAnswersAndLeavesOutWhatDoesnt() = runBlocking {
+ val names = NetworkNames(resolve = { if (it == "192.0.2.47") "alex-laptop.local" else it })
+ assertEquals(mapOf("192.0.2.47" to "alex-laptop"), names.of(listOf("192.0.2.47", "192.0.2.48")))
+ }
+
+ @Test fun aSlowLookupNeverHoldsTheScreenUp() = runBlocking {
+ val names = NetworkNames(wait = 100.milliseconds, resolve = { Thread.sleep(600); "slow-box" })
+ val start = System.nanoTime()
+ assertEquals(emptyMap<String, String>(), names.of(listOf("192.0.2.47")))
+ assertTrue((System.nanoTime() - start) / 1_000_000 < 500)
+ delay(800) // it finishes on its own, so the next read has it
+ assertEquals(mapOf("192.0.2.47" to "slow-box"), names.of(listOf("192.0.2.47")))
+ }
+
+ @Test fun answersAreKeptUntilTheyAreOld() = runBlocking {
+ val asked = AtomicInteger()
+ var now = 0L
+ val names = NetworkNames(keep = 10.minutes, clock = { now }, resolve = { asked.incrementAndGet(); null })
+ names.of(listOf("192.0.2.47"))
+ names.of(listOf("192.0.2.47"))
+ assertEquals(1, asked.get())
+ now += 11 * 60_000
+ names.of(listOf("192.0.2.47"))
+ assertEquals(2, asked.get())
+ }
+
+ @Test fun aFailingLookupIsNoName() = runBlocking {
+ val names = NetworkNames(resolve = { throw java.net.UnknownHostException(it) })
+ assertEquals(emptyMap<String, String>(), names.of(listOf("192.0.2.47")))
+ }
+}
diff --git a/app/src/test/java/app/tellyscope/TvAccessTest.kt b/app/src/test/java/app/tellyscope/TvAccessTest.kt
index f871685..3d12282 100644
--- a/app/src/test/java/app/tellyscope/TvAccessTest.kt
+++ b/app/src/test/java/app/tellyscope/TvAccessTest.kt
@@ -30,6 +30,12 @@ class TvAccessTest {
private fun access(vararg clients: AdbClient, keys: List<AllowedKey>? = listOf(phoneKey), mine: Set<String> = emptySet(), tv: String = "192.0.2.20") =
TvAccess.of(clients.toList(), keys, setOf(phone, "fe80::1"), tv, mine, phoneFingerprint = "AA")
+ @Test fun namesGoOnTheirAddressOnly() {
+ val a = access(AdbClient(phone, 40000), AdbClient(other, 51234)).named(mapOf(other to "Alex-MacBook"))
+ assertEquals(listOf(null, "Alex-MacBook"), a.connected!!.map { it.name })
+ assertTrue(a.warning) // a name never makes a computer trusted
+ }
+
@Test fun onlyThisPhoneConnectedIsNoWarning() {
val a = access(AdbClient(phone, 40000))
assertFalse(a.warning)