@@ -8,10 +8,7 @@ import android.util.Base64
88import android.util.Log
99import androidx.core.content.ContextCompat.getSystemService
1010import com.beust.klaxon.Klaxon
11- import kotlinx.coroutines.Dispatchers
12- import kotlinx.coroutines.async
13- import kotlinx.coroutines.supervisorScope
14- import kotlinx.coroutines.withContext
11+ import kotlinx.coroutines.*
1512import okhttp3.OkHttpClient
1613import okhttp3.Request
1714import java.io.ByteArrayInputStream
@@ -45,10 +42,9 @@ fun parseConnectUri(uri: Uri): ProxyInfo {
4542
4643suspend fun getProxyConfig (proxyInfo : ProxyInfo ): ProxyConfig {
4744 return withContext(Dispatchers .IO ) {
48- Log .v(TAG , " Validating proxy info $proxyInfo " )
49-
50- val proxyTests = proxyInfo.addresses.map { address ->
51- supervisorScope {
45+ return @withContext supervisorScope {
46+ Log .v(TAG , " Validating proxy info $proxyInfo " )
47+ val proxyTests = proxyInfo.addresses.map { address ->
5248 async {
5349 testProxyAddress(
5450 address,
@@ -57,16 +53,13 @@ suspend fun getProxyConfig(proxyInfo: ProxyInfo): ProxyConfig {
5753 )
5854 }
5955 }
60- }
6156
62- // Returns with the first working proxy config (cert & address),
63- // or throws if all possible addresses are unreachable/invalid
64- // Once the first test succeeds, we cancel any others
65- val result = proxyTests.awaitFirst()
66- proxyTests.forEach { test ->
67- test.cancel()
57+ Log .v(TAG , " Proxy tests started" )
58+
59+ // Return with the first working proxy config (cert & address)
60+ // (or throw if all addresses are unreachable/invalid)
61+ return @supervisorScope proxyTests.awaitFirst()
6862 }
69- return @withContext result
7063 }
7164}
7265
@@ -84,6 +77,8 @@ private suspend fun testProxyAddress(
8477 .readTimeout(2 , TimeUnit .SECONDS )
8578 .build()
8679
80+ Log .i(TAG , " Testing proxy $address :$port " )
81+
8782 val request = Request .Builder ()
8883 .url(" http://android.httptoolkit.tech/config" )
8984 .build()
0 commit comments