Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions app/src/main/java/com/example/vpn/AwgVpnService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}

Expand Down
60 changes: 60 additions & 0 deletions app/src/test/java/com/example/vpn/AwgVpnServiceDnsBenchmarkTest.kt
Original file line number Diff line number Diff line change
@@ -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<Pair<String, InetAddress>>()
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<Pair<String, InetAddress>>
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)
}
}
}
Loading