Skip to content

Commit

Permalink
fix unmodifiable List type (#20)
Browse files Browse the repository at this point in the history
The Android SDK returns with an unmodifiable Link type. The code tries to touch this list and it causes an exception.
  • Loading branch information
pappz authored May 26, 2024
1 parent 1b2dd91 commit 4e74f51
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions android/tool/src/main/java/io/netbird/client/tool/DNSWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,8 +68,7 @@ private DNSList readActiveDns() {
isPrivateDnsActive = props.isPrivateDnsActive();
}

List<InetAddress> list = props.getDnsServers();
extendWithFallbackDNS(list);
List<InetAddress> list = extendWithFallbackDNS(props.getDnsServers());
return toDnsList(list);
}

Expand All @@ -84,8 +84,8 @@ private void registerNetworkCallback() {
}

private synchronized void onNewDNSList(LinkProperties linkProperties) {
List<InetAddress> newDNSList = linkProperties.getDnsServers();
extendWithFallbackDNS(newDNSList);
List<InetAddress> newDNSList = extendWithFallbackDNS(linkProperties.getDnsServers());


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
isPrivateDnsActive = linkProperties.isPrivateDnsActive();
Expand Down Expand Up @@ -117,16 +117,22 @@ private synchronized void onNewDNSList(LinkProperties linkProperties) {
}
}

private void extendWithFallbackDNS(List<InetAddress> dnsServers) {
private List<InetAddress> extendWithFallbackDNS(List<InetAddress> dnsServers) {
List<InetAddress> 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 {
Expand Down

0 comments on commit 4e74f51

Please sign in to comment.