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 960aacf..d976769 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 @@ -13,6 +13,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.ArrayList; import java.util.List; import io.netbird.gomobile.android.DNSList; @@ -67,8 +68,7 @@ private DNSList readActiveDns() { isPrivateDnsActive = props.isPrivateDnsActive(); } - List list = props.getDnsServers(); - extendWithFallbackDNS(list); + List list = extendWithFallbackDNS(props.getDnsServers()); return toDnsList(list); } @@ -84,8 +84,8 @@ private void registerNetworkCallback() { } private synchronized void onNewDNSList(LinkProperties linkProperties) { - List newDNSList = linkProperties.getDnsServers(); - extendWithFallbackDNS(newDNSList); + List newDNSList = extendWithFallbackDNS(linkProperties.getDnsServers()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { isPrivateDnsActive = linkProperties.isPrivateDnsActive(); @@ -117,16 +117,22 @@ private synchronized void onNewDNSList(LinkProperties linkProperties) { } } - private void extendWithFallbackDNS(List dnsServers) { + private List extendWithFallbackDNS(List dnsServers) { + List modifiableDnsServers = new ArrayList<>(dnsServers); + if (dnsServers.isEmpty()) { - return; + return modifiableDnsServers; } - if (dnsServers.get(0).isLinkLocalAddress()) { - try { - InetAddress addr = InetAddress.getByName("1.1.1.1"); - dnsServers.add(0, addr); - } catch (UnknownHostException e) {} + + if (!dnsServers.get(0).isLinkLocalAddress()) { + return modifiableDnsServers; } + + try { + InetAddress addr = InetAddress.getByName("1.1.1.1"); + modifiableDnsServers.add(0, addr); + } catch (UnknownHostException e) {} + return modifiableDnsServers; } private void notifyDnsWatcher(DNSList dnsServers) throws Exception {