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
1 changed files with 8 additions and 1 deletions

View File

@ -39,13 +39,15 @@ public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism
if (networks == null) { if (networks == null) {
return new String[0]; return new String[0];
} }
final Network activeNetwork = getActiveNetwork(connectivityManager);
List<String> servers = new ArrayList<>(); 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);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
final boolean isActiveNetwork = network.equals(activeNetwork);
if (linkProperties != null) { 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()); final List<String> tmp = getIPv4First(linkProperties.getDnsServers());
servers.addAll(0, tmp); servers.addAll(0, tmp);
vpnOffset += tmp.size(); vpnOffset += tmp.size();
@ -59,6 +61,11 @@ public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism
return servers.toArray(new String[servers.size()]); 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) { private static List<String> getIPv4First(List<InetAddress> in) {
List<String> out = new ArrayList<>(); List<String> out = new ArrayList<>();
for(InetAddress addr : in) { for(InetAddress addr : in) {