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.

Private DNS: provider before mode, check lookups after, Android 9+ only

jesse-agent-2325d28a7ade359
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Tweaks.kt b/adb/src/main/kotlin/app/tellyscope/adb/Tweaks.kt
index 5f0c315..b7619e0 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Tweaks.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Tweaks.kt
@@ -2,17 +2,22 @@
package app.tellyscope.adb
import kotlinx.coroutines.NonCancellable
+import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import okio.Sink
import java.io.IOException
/**
* A group of global settings read and written together, so a change can be put back exactly: a
- * key that wasn't set before is deleted again, never set to a guess at the default.
+ * key that wasn't set before is deleted again, never set to a guess at the default. Keys are
+ * written in [keys] order.
*/
enum class Tweak(val keys: List<String>) {
- /** Android 9+. Mode `hostname` sends every lookup to `private_dns_specifier` over TLS. */
- PRIVATE_DNS(listOf("private_dns_mode", "private_dns_specifier")),
+ /**
+ * Android 9+. Mode `hostname` sends every lookup to `private_dns_specifier` over TLS. The
+ * provider goes first, so a failed write never leaves strict mode aimed at the old one.
+ */
+ PRIVATE_DNS(listOf("private_dns_specifier", "private_dns_mode")),
ANIMATIONS(listOf("window_animation_scale", "transition_animation_scale", "animator_duration_scale")),
}
@@ -21,6 +26,7 @@ class Tweaks(private val session: AdbSession) {
/** Every key of [tweak]; null for a key that isn't set. */
suspend fun read(tweak: Tweak): Map<String, String?> {
+ // `settings get` prints exactly one line per key, "null" when unset.
val out = session.shell(tweak.keys.joinToString("; ") { "settings get global $it" }).output.lines()
return tweak.keys.withIndex().associate { (i, k) ->
k to out.getOrNull(i)?.trim()?.takeUnless { it.isEmpty() || it == "null" }
@@ -30,13 +36,28 @@ class Tweaks(private val session: AdbSession) {
/** Writes [values] (keys of [tweak] only); null deletes the key. False when the TV refused. */
suspend fun write(tweak: Tweak, values: Map<String, String?>): Boolean {
require(values.isNotEmpty() && tweak.keys.containsAll(values.keys)) { "not keys of $tweak: ${values.keys}" }
- val cmd = values.entries.joinToString(" && ") { (k, v) ->
+ val cmd = tweak.keys.filter { it in values }.joinToString(" && ") { k ->
+ val v = values[k]
if (v == null) "settings delete global $k" else "settings put global $k ${shellQuote(v)}"
}
val r = session.shell("$cmd 2>&1")
return r.exitCode == 0 && "Exception" !in r.output
}
+ /**
+ * Whether the TV can look names up, tried a few times while a new Private DNS provider is
+ * checked. Strict Private DNS fails closed, so a provider the network blocks takes the whole
+ * TV offline. Only "unknown host" counts as failing: a resolved name whose ping gets no
+ * answer still resolved, and a TV without ping can't tell, so it passes.
+ */
+ suspend fun resolves(host: String = "connectivitycheck.gstatic.com", tries: Int = 3, wait: Long = 2_000): Boolean {
+ repeat(tries) { i ->
+ if ("unknown host" !in session.shell("ping -c1 -W3 $host 2>&1").output.lowercase()) return true
+ if (i < tries - 1) delay(wait)
+ }
+ return false
+ }
+
/**
* Asks Android to free cache space from every app (`pm trim-caches` with more than any TV
* has); app data and sign-ins stay. Returns the space freed in KB, or null when the TV
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/TweaksTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/TweaksTest.kt
index 06e277c..3d69821 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/TweaksTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/TweaksTest.kt
@@ -15,18 +15,27 @@ import kotlin.test.assertTrue
class TweaksTest {
@Test fun unsetKeysReadAsNull() = runTest {
- val sh = FakeShell(mapOf("settings get global private_dns_mode" to ok("hostname\nnull\n")))
+ val sh = FakeShell(mapOf("settings get global private_dns_specifier" to ok("null\nhostname\n")))
assertEquals(
- mapOf("private_dns_mode" to "hostname", "private_dns_specifier" to null),
+ mapOf("private_dns_specifier" to null, "private_dns_mode" to "hostname"),
Tweaks(sh).read(Tweak.PRIVATE_DNS),
)
}
+ @Test fun providerIsWrittenBeforeTheMode() = runTest {
+ val sh = FakeShell()
+ assertTrue(Tweaks(sh).write(Tweak.PRIVATE_DNS, mapOf("private_dns_mode" to "hostname", "private_dns_specifier" to "dns.adguard-dns.com")))
+ assertEquals(
+ "settings put global private_dns_specifier 'dns.adguard-dns.com' && settings put global private_dns_mode 'hostname' 2>&1",
+ sh.ran.single(),
+ )
+ }
+
@Test fun writeDeletesWhatWasUnset() = runTest {
val sh = FakeShell()
- assertTrue(Tweaks(sh).write(Tweak.PRIVATE_DNS, mapOf("private_dns_mode" to "hostname", "private_dns_specifier" to null)))
+ assertTrue(Tweaks(sh).write(Tweak.PRIVATE_DNS, mapOf("private_dns_mode" to "opportunistic", "private_dns_specifier" to null)))
assertEquals(
- listOf("settings put global private_dns_mode 'hostname' && settings delete global private_dns_specifier 2>&1"),
+ listOf("settings delete global private_dns_specifier && settings put global private_dns_mode 'opportunistic' 2>&1"),
sh.ran,
)
}
@@ -83,6 +92,17 @@ class TweaksTest {
assertFailsWith<IOException> { Tweaks(sh).screenshot(Buffer()) }
}
+ @Test fun unknownHostEveryTryIsNoLookups() = runTest {
+ val sh = FakeShell(mapOf("ping" to ShellResult("ping: unknown host connectivitycheck.gstatic.com", 2)))
+ assertFalse(Tweaks(sh).resolves(wait = 0))
+ assertEquals(3, sh.ran.size)
+ }
+
+ @Test fun resolvedButNoReplyStillResolves() = runTest {
+ val sh = FakeShell(mapOf("ping" to ShellResult("1 packets transmitted, 0 received, 100% packet loss", 1)))
+ assertTrue(Tweaks(sh).resolves(wait = 0))
+ }
+
@Test fun hostnames() {
assertTrue(Tweaks.isHostname("dns.adguard-dns.com"))
assertFalse(Tweaks.isHostname("94.140.14.14"))
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index 3d9be56..e04ca63 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -402,14 +402,24 @@ class AdbDeviceSource(context: Context) : DeviceSource {
override suspend fun tweaks(deviceId: String): TvTweaks {
val t = Tweaks(session(deviceId))
- return TvSettings.tweaks(t.read(Tweak.PRIVATE_DNS), t.read(Tweak.ANIMATIONS))
+ return TvSettings.tweaks(t.read(Tweak.PRIVATE_DNS), t.read(Tweak.ANIMATIONS), sdk(deviceId))
}
+ private suspend fun sdk(deviceId: String) = session(deviceId).shell("getprop ro.build.version.sdk").output.trim().toIntOrNull()
+
override suspend fun change(deviceId: String, tweak: Tweak, values: Map<String, String?>, undoing: Long?): HistoryEntry {
val t = Tweaks(session(deviceId))
+ val strictDns = tweak == Tweak.PRIVATE_DNS && values["private_dns_mode"] == "hostname"
+ if (strictDns && (sdk(deviceId) ?: TvSettings.DNS_SDK) < TvSettings.DNS_SDK) throw IllegalStateException(TvSettings.DNS_TOO_OLD)
// Only the keys being written: Undo must not touch the ones this change left alone.
val before = t.read(tweak).filterKeys { it in values }
if (!t.write(tweak, values)) throw IllegalStateException("the TV didn't accept the change")
+ // Strict Private DNS fails closed: a provider the network can't reach takes the TV
+ // offline. Put it back on the spot; ADB stays up, it's the LAN by address.
+ if (strictDns && !t.resolves()) {
+ t.write(tweak, before)
+ throw IllegalStateException(TvSettings.dnsUnreachable(values["private_dns_specifier"]))
+ }
return book.record(
deviceId = key(deviceId),
at = System.currentTimeMillis(),
diff --git a/app/src/main/java/app/tellyscope/data/FakeDeviceSource.kt b/app/src/main/java/app/tellyscope/data/FakeDeviceSource.kt
index bccc9da..5800afa 100644
--- a/app/src/main/java/app/tellyscope/data/FakeDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/FakeDeviceSource.kt
@@ -311,7 +311,7 @@ class FakeDeviceSource(
override suspend fun tweaks(deviceId: String): TvTweaks {
val s = settingsFor(deviceId)
- return TvSettings.tweaks(s, s)
+ return TvSettings.tweaks(s, s, sdk = 30)
}
override suspend fun change(deviceId: String, tweak: Tweak, values: Map<String, String?>, undoing: Long?): HistoryEntry {
diff --git a/app/src/main/java/app/tellyscope/data/Model.kt b/app/src/main/java/app/tellyscope/data/Model.kt
index dd06814..3e4a0a5 100644
--- a/app/src/main/java/app/tellyscope/data/Model.kt
+++ b/app/src/main/java/app/tellyscope/data/Model.kt
@@ -142,4 +142,6 @@ data class TvTweaks(
val dnsHost: String?,
/** Window animation scale, e.g. "1.0"; null when unset (Android's 1×). */
val animationScale: String?,
+ /** Private DNS came in Android 9: older TVs accept the setting and ignore it. */
+ val dnsSupported: Boolean = true,
)
diff --git a/app/src/main/java/app/tellyscope/data/TvSettings.kt b/app/src/main/java/app/tellyscope/data/TvSettings.kt
index f88a7e2..4f59c81 100644
--- a/app/src/main/java/app/tellyscope/data/TvSettings.kt
+++ b/app/src/main/java/app/tellyscope/data/TvSettings.kt
@@ -11,17 +11,21 @@ object TvSettings {
const val FAST = "0.5"
/** History's name for a cache clear: "Cleared app caches". */
const val CACHES = "app caches"
+ /** Android 9, the first with Private DNS. */
+ const val DNS_SDK = 28
/** Off goes back to Automatic, Android's default, rather than turning encrypted DNS off. */
fun dns(host: String?): Map<String, String?> =
if (host == null) mapOf("private_dns_mode" to "opportunistic")
- else mapOf("private_dns_mode" to "hostname", "private_dns_specifier" to host)
+ else mapOf("private_dns_specifier" to host, "private_dns_mode" to "hostname")
fun animations(fast: Boolean): Map<String, String?> = Tweak.ANIMATIONS.keys.associateWith { if (fast) FAST else "1.0" }
- fun tweaks(dns: Map<String, String?>, animations: Map<String, String?>) = TvTweaks(
+ fun tweaks(dns: Map<String, String?>, animations: Map<String, String?>, sdk: Int?) = TvTweaks(
dnsHost = dns["private_dns_specifier"]?.takeIf { dns["private_dns_mode"] == "hostname" },
animationScale = animations["window_animation_scale"],
+ // A TV that doesn't say is let through: the lookup check after the change still guards it.
+ dnsSupported = sdk == null || sdk >= DNS_SDK,
)
/** History's line for a change that left the TV at [values]. */
@@ -37,4 +41,9 @@ object TvSettings {
else -> "Animations at ${v.toString().removeSuffix(".0")}×"
}
}
+
+ const val DNS_TOO_OLD = "Private DNS needs Android 9 or newer, and this TV is older"
+
+ fun dnsUnreachable(host: String?) =
+ "the TV couldn't look anything up through $host, so Private DNS was put back. The network may block encrypted DNS (port 853)"
}
diff --git a/app/src/main/java/app/tellyscope/ui/Copy.kt b/app/src/main/java/app/tellyscope/ui/Copy.kt
index 232ba0f..2adc4f0 100644
--- a/app/src/main/java/app/tellyscope/ui/Copy.kt
+++ b/app/src/main/java/app/tellyscope/ui/Copy.kt
@@ -290,9 +290,10 @@ object Copy {
const val TWEAKS = "TV tweaks"
const val DNS_TITLE = "Block ads with Private DNS"
fun dnsState(host: String?) = host?.let { "On: $it" } ?: "Off. Turn on to block ads and trackers in every app on the TV."
+ const val DNS_TOO_OLD = "Needs Android 9 or newer. This TV is older."
const val DNS_DIALOG = "Block ads on the whole TV?"
const val DNS_TEXT = "The TV will look up every address through this ad-blocking DNS provider. AdGuard is free and needs no account. " +
- "If the TV loses its internet afterwards, tap Undo in History to put it back."
+ "Tellyscope then checks the TV can still look addresses up, and puts it back if not. Undo is in History."
const val DNS_FIELD = "DNS provider"
const val DNS_BAD = "Enter a hostname like dns.adguard-dns.com. An IP address won't work here."
const val DNS_CONFIRM = "Turn on"
diff --git a/app/src/main/java/app/tellyscope/ui/TweaksBlock.kt b/app/src/main/java/app/tellyscope/ui/TweaksBlock.kt
index f853529..df11b05 100644
--- a/app/src/main/java/app/tellyscope/ui/TweaksBlock.kt
+++ b/app/src/main/java/app/tellyscope/ui/TweaksBlock.kt
@@ -41,7 +41,13 @@ fun TweaksBlock(ui: DeviceUi, actions: DeviceActions) {
var askDns by rememberSaveable { mutableStateOf(false) }
val dnsOn = tweaks?.dnsHost != null
if (ui.busy in TWEAK_BUSY) LinearProgressIndicator(Modifier.fillMaxWidth())
- SwitchRow(Copy.DNS_TITLE, Copy.dnsState(tweaks?.dnsHost), dnsOn, enabled = tweaks != null && ui.busy == null) { on ->
+ val dnsOk = tweaks?.dnsSupported != false
+ SwitchRow(
+ Copy.DNS_TITLE,
+ if (dnsOk) Copy.dnsState(tweaks?.dnsHost) else Copy.DNS_TOO_OLD,
+ dnsOn,
+ enabled = tweaks != null && dnsOk && ui.busy == null,
+ ) { on ->
if (on) askDns = true else actions.setDns(null)
}
val fast = tweaks?.animationScale == TvSettings.FAST