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.

Connect: ask for the code only when the TV sent a TLS alert

jesse-agent-821d8bdd7cc38f2
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Connector.kt b/adb/src/main/kotlin/app/tellyscope/adb/Connector.kt
index c99abea..8c12806 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Connector.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Connector.kt
@@ -20,6 +20,7 @@ import java.io.InterruptedIOException
import java.net.InetSocketAddress
import java.net.Socket
import java.nio.channels.InterruptedByTimeoutException
+import javax.net.ssl.SSLException
import java.math.BigInteger
import java.nio.ByteBuffer
import java.nio.ByteOrder
@@ -133,8 +134,9 @@ object Connector {
kadb.close()
when {
// How a TV turns down an unpaired key varies by TLS stack and Android version, and Kadb
- // maps only some alerts to AdbPairAuthException; the rest read as "didn't answer".
- asksForTls(host, port) -> ConnectResult.NeedsPairing
+ // maps only some alerts to AdbPairAuthException; the rest read as "didn't answer". Only
+ // an alert counts: a paired TV that drops mid-handshake must keep reconnecting.
+ e.isTlsAlert() && asksForTls(host, port) -> ConnectResult.NeedsPairing
// No answer times out; Deny makes adbd drop the socket. Both read as "not allowed".
e.isAuthFailure() -> ConnectResult.NotAllowed
else -> ConnectResult.Unreachable(e.message ?: e.javaClass.simpleName)
@@ -187,6 +189,13 @@ object Connector {
}
}
+ /**
+ * The TV sent a TLS alert: "Received fatal alert: …" on the JVM, "…_ALERT_…" from Conscrypt. A drop
+ * mid-handshake is an SSLException too ("EOF during handshake"), but names no alert.
+ */
+ private fun Throwable.isTlsAlert(): Boolean = generateSequence(this) { it.cause }
+ .any { it is SSLException && it.message.orEmpty().contains("alert", ignoreCase = true) }
+
private fun Throwable.isAuthFailure(): Boolean =
generateSequence(this) { it.cause }.any {
it is java.io.EOFException || it is InterruptedIOException || it is InterruptedByTimeoutException
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ConnectorTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ConnectorTest.kt
index 245198f..fa172bc 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/ConnectorTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/ConnectorTest.kt
@@ -85,16 +85,19 @@ class ConnectorTest {
}
}
+ @Test fun wirelessPortThatDropsMidHandshakeStaysUnreachable() = runBlocking<Unit> {
+ // A paired TV whose Wi-Fi blips: it asks for TLS, then the socket goes. The link must keep
+ // reconnecting, so this can't read as "needs pairing".
+ ServerSocket(0, 50, InetAddress.getByName("127.0.0.1")).use { server ->
+ thread(isDaemon = true) { while (true) runCatching { server.accept().use(::answerStls) } }
+ val r = withTimeout(30_000) { Connector.connect("127.0.0.1", server.localPort, allowWaitMs = 10_000) }
+ assertIs<ConnectResult.Unreachable>(r)
+ }
+ }
+
/** adbd's side: read CNXN, answer STLS, then turn down every client certificate in TLS. */
private fun refuseUnpairedKey(socket: java.net.Socket) {
- val input = java.io.DataInputStream(socket.getInputStream())
- val header = ByteArray(24).also(input::readFully)
- input.skipNBytes(ByteBuffer.wrap(header).order(ByteOrder.LITTLE_ENDIAN).getInt(12).toLong())
- val stls = 0x534c5453
- socket.getOutputStream().write(
- ByteBuffer.allocate(24).order(ByteOrder.LITTLE_ENDIAN)
- .putInt(stls).putInt(0x01000000).putInt(0).putInt(0).putInt(0).putInt(stls.inv()).array(),
- )
+ answerStls(socket)
// The server borrows this phone's own identity; only the client check matters here.
val id = KadbCert.ensureReady()
val cert = CertificateFactory.getInstance("X.509").generateCertificate(id.certificatePem.inputStream())
@@ -116,4 +119,16 @@ class ConnectorTest {
runCatching { tls.startHandshake() } // throws once it rejects ours
}
}
+
+ /** Reads the CNXN hello and answers STLS, as a wireless debugging port does. */
+ private fun answerStls(socket: java.net.Socket) {
+ val input = java.io.DataInputStream(socket.getInputStream())
+ val header = ByteArray(24).also(input::readFully)
+ input.skipNBytes(ByteBuffer.wrap(header).order(ByteOrder.LITTLE_ENDIAN).getInt(12).toLong())
+ val stls = 0x534c5453
+ socket.getOutputStream().write(
+ ByteBuffer.allocate(24).order(ByteOrder.LITTLE_ENDIAN)
+ .putInt(stls).putInt(0x01000000).putInt(0).putInt(0).putInt(0).putInt(stls.inv()).array(),
+ )
+ }
}