Resolver: Only prefer VPN connections when network is active

This commit is contained in:
Daniel Gultsch 2017-10-27 21:57:31 +02:00
parent 962add97f9
commit 4d0986ee91

View file

@ -39,13 +39,15 @@ public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism
if (networks == null) {
return new String[0];
}
final Network activeNetwork = getActiveNetwork(connectivityManager);
List<String> servers = new ArrayList<>();
int vpnOffset = 0;
for(Network network : networks) {
LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
final boolean isActiveNetwork = network.equals(activeNetwork);
if (linkProperties != null) {
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_VPN) {
if (networkInfo != null && isActiveNetwork && networkInfo.getType() == ConnectivityManager.TYPE_VPN) {
final List<String> tmp = getIPv4First(linkProperties.getDnsServers());
servers.addAll(0, tmp);
vpnOffset += tmp.size();
@ -59,6 +61,11 @@ public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism
return servers.toArray(new String[servers.size()]);
}
@TargetApi(23)
private static Network getActiveNetwork(ConnectivityManager cm) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? cm.getActiveNetwork() : null;
}
private static List<String> getIPv4First(List<InetAddress> in) {
List<String> out = new ArrayList<>();
for(InetAddress addr : in) {