From 4d0986ee915d0c9f2761adfa3b1e2153372f27dc Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 27 Oct 2017 21:57:31 +0200 Subject: [PATCH] Resolver: Only prefer VPN connections when network is active --- .../conversations/utils/AndroidUsingLinkProperties.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java b/src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java index 36fec0a47..1be5168a8 100644 --- a/src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java +++ b/src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java @@ -39,13 +39,15 @@ public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism if (networks == null) { return new String[0]; } + final Network activeNetwork = getActiveNetwork(connectivityManager); List 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 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 getIPv4First(List in) { List out = new ArrayList<>(); for(InetAddress addr : in) {