From 1b2dd919ca99a2ac2afcc9257d3707f6e5cd3761 Mon Sep 17 00:00:00 2001 From: Zoltan Papp Date: Wed, 22 May 2024 11:16:28 +0200 Subject: [PATCH] Add hardcoded DNS server (#18) Extend the host's DNS list with the hardcoded Cloudflare DNS server in case the first assigned server type is link local --- .../java/io/netbird/client/tool/DNSWatch.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/android/tool/src/main/java/io/netbird/client/tool/DNSWatch.java b/android/tool/src/main/java/io/netbird/client/tool/DNSWatch.java index 51aad1b..960aacf 100644 --- a/android/tool/src/main/java/io/netbird/client/tool/DNSWatch.java +++ b/android/tool/src/main/java/io/netbird/client/tool/DNSWatch.java @@ -12,6 +12,7 @@ import androidx.annotation.NonNull; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.List; import io.netbird.gomobile.android.DNSList; @@ -26,7 +27,7 @@ public class DNSWatch { DNSWatch(Context context) { - connectivityManager = (ConnectivityManager) context.getSystemService(ConnectivityManager.class); + connectivityManager = context.getSystemService(ConnectivityManager.class); dnsServers = readActiveDns(); } @@ -65,7 +66,10 @@ private DNSList readActiveDns() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { isPrivateDnsActive = props.isPrivateDnsActive(); } - return toDnsList(props.getDnsServers()); + + List list = props.getDnsServers(); + extendWithFallbackDNS(list); + return toDnsList(list); } private void registerNetworkCallback() { @@ -81,6 +85,7 @@ private void registerNetworkCallback() { private synchronized void onNewDNSList(LinkProperties linkProperties) { List newDNSList = linkProperties.getDnsServers(); + extendWithFallbackDNS(newDNSList); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { isPrivateDnsActive = linkProperties.isPrivateDnsActive(); @@ -112,6 +117,18 @@ private synchronized void onNewDNSList(LinkProperties linkProperties) { } } + private void extendWithFallbackDNS(List dnsServers) { + if (dnsServers.isEmpty()) { + return; + } + if (dnsServers.get(0).isLinkLocalAddress()) { + try { + InetAddress addr = InetAddress.getByName("1.1.1.1"); + dnsServers.add(0, addr); + } catch (UnknownHostException e) {} + } + } + private void notifyDnsWatcher(DNSList dnsServers) throws Exception { listener.onChanged(dnsServers); }