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 3f89d9e..ca2e4fe 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); }