Add check on current looper when adding dns to VPNService.builder

This commit is contained in:
Diego Romar
2025-09-22 08:58:41 -03:00
parent fe5dd6544d
commit f232a6f4c9
@@ -104,25 +104,33 @@ class IFace implements TunAdapter {
return;
}
CountDownLatch latch = new CountDownLatch(1);
if (Looper.myLooper() != Looper.getMainLooper()) {
CountDownLatch latch = new CountDownLatch(1);
// ConnectivityManager must to run on the main thread instead of a Go routine
new Handler(Looper.getMainLooper()).post(() -> {
DNSWatch dnsWatch = new DNSWatch(vpnService);
// ConnectivityManager must to run on the main thread instead of a Go routine
new Handler(Looper.getMainLooper()).post(() -> {
addDnsServer(builder, dns);
if (!dnsWatch.isPrivateDnsActive()) {
builder.addDnsServer(dns);
} else {
Log.d(LOGTAG, "ignore DNS because private dns is active");
latch.countDown();
});
try {
latch.await(); // Will block the current thread until countDown() is called
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
addDnsServer(builder, dns);
}
}
latch.countDown();
});
private void addDnsServer(VpnService.Builder builder, String dns) {
DNSWatch dnsWatch = new DNSWatch(vpnService);
try {
latch.await(); // Will block the current thread until countDown() is called
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
if (!dnsWatch.isPrivateDnsActive()) {
builder.addDnsServer(dns);
} else {
Log.d(LOGTAG, "ignore DNS because private dns is active");
}
}