Skip to content

Commit 2ba582e

Browse files
committed
Support tunnelling connections via ADB, as a connectivity fallback
1 parent 655d8b1 commit 2ba582e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

app/src/main/java/tech/httptoolkit/android/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
398398
ProxyInfo(
399399
listOf(lastProxy.ip),
400400
lastProxy.port,
401+
null,
401402
getCertificateFingerprint(lastProxy.certificate as X509Certificate)
402403
)
403404
)

app/src/main/java/tech/httptoolkit/android/ProxyDetails.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ data class ProxyInfo(
2727
*/
2828
val port: Int,
2929

30+
/**
31+
* A local tunnel port, with an ADB tunnel connected to the proxy. This is less good than a
32+
* direct network connection, since it's dependent on the ADB server, but it's a useful fallback
33+
* especially if the proxy computer has a firewall or the network blocks connections.
34+
*/
35+
val localTunnelPort: Int?,
36+
3037
/**
3138
* The expected PK hash of the proxy certificate. The certificate itself is obtained from the
3239
* proxy, as part of validating the connection. This hash is included in QR codes to

app/src/main/java/tech/httptoolkit/android/ProxySetup.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ suspend fun getProxyConfig(proxyInfo: ProxyInfo): ProxyConfig {
6161

6262
// Return with the first working proxy config (cert & address)
6363
// (or throw if all addresses are unreachable/invalid)
64-
return@supervisorScope proxyTests.awaitFirst()
64+
try {
65+
return@supervisorScope proxyTests.awaitFirst()
66+
} catch (e: Exception) {
67+
if (proxyInfo.localTunnelPort == null) throw e;
68+
69+
// If all network connections fail, and we have a local ADB tunnel, fallback to
70+
// using that connection instead.
71+
return@supervisorScope testProxyAddress(
72+
"127.0.0.1",
73+
proxyInfo.localTunnelPort,
74+
proxyInfo.certFingerprint
75+
)
76+
}
6577
}
6678
}
6779
}

0 commit comments

Comments
 (0)