Skip to content

Commit

Permalink
Add hardcoded DNS server (#18)
Browse files Browse the repository at this point in the history
Extend the host's DNS list with the hardcoded Cloudflare DNS server in case the first assigned server type is link local
  • Loading branch information
pappz authored May 22, 2024
1 parent 6957850 commit 1b2dd91
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions android/tool/src/main/java/io/netbird/client/tool/DNSWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +27,7 @@ public class DNSWatch {


DNSWatch(Context context) {
connectivityManager = (ConnectivityManager) context.getSystemService(ConnectivityManager.class);
connectivityManager = context.getSystemService(ConnectivityManager.class);
dnsServers = readActiveDns();
}

Expand Down Expand Up @@ -65,7 +66,10 @@ private DNSList readActiveDns() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
isPrivateDnsActive = props.isPrivateDnsActive();
}
return toDnsList(props.getDnsServers());

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

private void registerNetworkCallback() {
Expand All @@ -81,6 +85,7 @@ private void registerNetworkCallback() {

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

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

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

0 comments on commit 1b2dd91

Please sign in to comment.