do not include DNS servers from networks know to be inactive

* we still include DNS servers from VPNs because of edge cases where the XMPP server is hosted in the VPN
* on older Android versions we don’t know if a network is active or not (activeNetwork == null)

fixes #3465
This commit is contained in:
Daniel Gultsch 2019-07-18 11:23:07 +02:00
parent 6de6d054ac
commit 3837ec8122

View file

@ -21,7 +21,7 @@ public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism
private final Context context; private final Context context;
protected AndroidUsingLinkProperties(Context context) { AndroidUsingLinkProperties(Context context) {
super(AndroidUsingLinkProperties.class.getSimpleName(), AndroidUsingExec.PRIORITY - 1); super(AndroidUsingLinkProperties.class.getSimpleName(), AndroidUsingExec.PRIORITY - 1);
this.context = context; this.context = context;
} }
@ -34,32 +34,31 @@ public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism
@Override @Override
@TargetApi(21) @TargetApi(21)
public String[] getDnsServerAddresses() { public String[] getDnsServerAddresses() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); final Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks();
if (networks == null) { if (networks == null) {
return new String[0]; return new String[0];
} }
final Network activeNetwork = getActiveNetwork(connectivityManager); final Network activeNetwork = getActiveNetwork(connectivityManager);
List<String> servers = new ArrayList<>(); final List<String> servers = new ArrayList<>();
int vpnOffset = 0; int vpnOffset = 0;
for(Network network : networks) { for(Network network : networks) {
LinkProperties linkProperties = connectivityManager.getLinkProperties(network); LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
if (linkProperties == null) { if (linkProperties == null) {
continue; continue;
} }
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
final boolean isActiveNetwork = network.equals(activeNetwork); final boolean isActiveNetwork = network.equals(activeNetwork);
if (networkInfo != null && isActiveNetwork && networkInfo.getType() == ConnectivityManager.TYPE_VPN) { final boolean isVpn = networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_VPN;
if (isActiveNetwork && isVpn) {
final List<String> tmp = getIPv4First(linkProperties.getDnsServers()); final List<String> tmp = getIPv4First(linkProperties.getDnsServers());
servers.addAll(0, tmp); servers.addAll(0, tmp);
vpnOffset += tmp.size(); vpnOffset += tmp.size();
} else if (hasDefaultRoute(linkProperties) || isActiveNetwork) { } else if (hasDefaultRoute(linkProperties) || isActiveNetwork || activeNetwork == null || isVpn) {
servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers())); servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers()));
} else {
servers.addAll(getIPv4First(linkProperties.getDnsServers()));
} }
} }
return servers.toArray(new String[servers.size()]); return servers.toArray(new String[0]);
} }
@TargetApi(23) @TargetApi(23)
@ -69,11 +68,11 @@ public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism
private static List<String> getIPv4First(List<InetAddress> in) { private static List<String> getIPv4First(List<InetAddress> in) {
List<String> out = new ArrayList<>(); List<String> out = new ArrayList<>();
for(InetAddress addr : in) { for(InetAddress address : in) {
if (addr instanceof Inet4Address) { if (address instanceof Inet4Address) {
out.add(0, addr.getHostAddress()); out.add(0, address.getHostAddress());
} else { } else {
out.add(addr.getHostAddress()); out.add(address.getHostAddress());
} }
} }
return out; return out;