Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hardcoded fallback DNS server #18

Merged
merged 2 commits into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading