From 4e74f519e1ec2a5fb7bbb383db6378d6d9e21dc7 Mon Sep 17 00:00:00 2001 From: Zoltan Papp Date: Sun, 26 May 2024 14:31:26 +0200 Subject: [PATCH] fix unmodifiable List type (#20) The Android SDK returns with an unmodifiable Link type. The code tries to touch this list and it causes an exception. --- .../java/io/netbird/client/tool/DNSWatch.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 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 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 {