diff --git a/app/src/main/java/com/example/vpn/AwgVpnService.kt b/app/src/main/java/com/example/vpn/AwgVpnService.kt index 7888a5f..6c975c4 100644 --- a/app/src/main/java/com/example/vpn/AwgVpnService.kt +++ b/app/src/main/java/com/example/vpn/AwgVpnService.kt @@ -20,8 +20,11 @@ import com.example.util.ConfigParser import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll import kotlinx.coroutines.cancel import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import java.net.DatagramSocket import java.net.InetAddress import java.net.Socket @@ -205,21 +208,38 @@ class AwgVpnService : VpnService() { private fun configureDnsServers(builder: Builder, dnsString: String) { val dnsServers = dnsString.split(",").map { it.trim() }.filter { it.isNotBlank() } if (dnsServers.isNotEmpty()) { - for (dns in dnsServers) { + val resolvedList = runBlocking(Dispatchers.IO) { + dnsServers.map { dns -> + async { + runCatching { + dns to InetAddress.getByName(dns) + }.getOrNull() + } + }.awaitAll().filterNotNull() + } + for ((dns, inetAddress) in resolvedList) { runCatching { - val inet = parseNumericAddressOrNull(dns) ?: return@runCatching - builder.addDnsServer(inet) + builder.addDnsServer(inetAddress) App.instance.tunnelManager.log("VPN_ROUTING", "Configured DNS Server: $dns") } } } else { - runCatching { - val dns1 = parseNumericAddressOrNull("1.1.1.1") ?: return@runCatching - val dns2 = parseNumericAddressOrNull("8.8.8.8") ?: return@runCatching - builder.addDnsServer(dns1) - builder.addDnsServer(dns2) - App.instance.tunnelManager.log("VPN_ROUTING", "Configured default DNS Servers: 1.1.1.1, 8.8.8.8") + val defaultDns = listOf("1.1.1.1", "8.8.8.8") + val resolvedList = runBlocking(Dispatchers.IO) { + defaultDns.map { dns -> + async { + runCatching { + dns to InetAddress.getByName(dns) + }.getOrNull() + } + }.awaitAll().filterNotNull() + } + for ((dns, inetAddress) in resolvedList) { + runCatching { + builder.addDnsServer(inetAddress) + } } + App.instance.tunnelManager.log("VPN_ROUTING", "Configured default DNS Servers: 1.1.1.1, 8.8.8.8") } } diff --git a/app/src/test/java/com/example/vpn/AwgVpnServiceDnsBenchmarkTest.kt b/app/src/test/java/com/example/vpn/AwgVpnServiceDnsBenchmarkTest.kt new file mode 100644 index 0000000..8f38de1 --- /dev/null +++ b/app/src/test/java/com/example/vpn/AwgVpnServiceDnsBenchmarkTest.kt @@ -0,0 +1,60 @@ +package com.example.vpn + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.runBlocking +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import java.net.InetAddress +import kotlin.system.measureTimeMillis + +class AwgVpnServiceDnsBenchmarkTest { + + @Test + fun testDnsResolution_sequentialVsConcurrentDispatchersIO() { + val dnsList = listOf( + "one.one.one.one", + "dns.google", + "dns.adguard.com", + "dns.quad9.net", + "cloudflare-dns.com", + "1.1.1.1", + "8.8.8.8" + ) + + // 1. Sequential resolution (legacy approach) + val sequentialResults = mutableListOf>() + val sequentialTime = measureTimeMillis { + for (dns in dnsList) { + runCatching { + sequentialResults.add(dns to InetAddress.getByName(dns)) + } + } + } + + // 2. Concurrent resolution on Dispatchers.IO (optimized approach) + val concurrentResults: List> + val concurrentTime = measureTimeMillis { + concurrentResults = runBlocking(Dispatchers.IO) { + dnsList.map { dns -> + async { + runCatching { + dns to InetAddress.getByName(dns) + }.getOrNull() + } + }.awaitAll().filterNotNull() + } + } + + println("Sequential DNS resolution time (hostnames & IPs): ${sequentialTime}ms") + println("Concurrent Dispatchers.IO DNS resolution time (hostnames & IPs): ${concurrentTime}ms") + + assertEquals("Resolved list count should match", sequentialResults.size, concurrentResults.size) + for (i in sequentialResults.indices) { + assertEquals("Order should be preserved for element $i", sequentialResults[i].first, concurrentResults[i].first) + assertEquals("InetAddress should match for element $i", sequentialResults[i].second, concurrentResults[i].second) + } + } +}