From 9a49c7c3c69982644476b961a010b791fea98437 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Tue, 4 Jun 2019 10:26:21 +0200 Subject: [PATCH] Drop own implementation of DNS-Server selection --- .../utils/AndroidUsingExecLowPriority.java | 92 ------------------- .../utils/AndroidUsingLinkProperties.java | 91 ------------------ .../siacs/conversations/utils/Resolver.java | 4 - 3 files changed, 187 deletions(-) delete mode 100644 src/main/java/eu/siacs/conversations/utils/AndroidUsingExecLowPriority.java delete mode 100644 src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java diff --git a/src/main/java/eu/siacs/conversations/utils/AndroidUsingExecLowPriority.java b/src/main/java/eu/siacs/conversations/utils/AndroidUsingExecLowPriority.java deleted file mode 100644 index d06c3afeb..000000000 --- a/src/main/java/eu/siacs/conversations/utils/AndroidUsingExecLowPriority.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2015-2016 the original author or authors - * - * This software is licensed under the Apache License, Version 2.0, - * the GNU Lesser General Public License version 2 or later ("LGPL") - * and the WTFPL. - * You may choose either license to govern your use of this software only - * upon the condition that you accept all of the terms of either - * the Apache License 2.0, the LGPL 2.1+ or the WTFPL. - */ -package eu.siacs.conversations.utils; - - -import de.measite.minidns.dnsserverlookup.AbstractDNSServerLookupMechanism; -import de.measite.minidns.dnsserverlookup.AndroidUsingReflection; -import de.measite.minidns.dnsserverlookup.DNSServerLookupMechanism; -import de.measite.minidns.util.PlatformDetection; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.LineNumberReader; -import java.net.InetAddress; -import java.util.HashSet; -import java.util.logging.Level; - -/** - * Try to retrieve the list of DNS server by executing getprop. - */ -public class AndroidUsingExecLowPriority extends AbstractDNSServerLookupMechanism { - - public static final DNSServerLookupMechanism INSTANCE = new AndroidUsingExecLowPriority(); - public static final int PRIORITY = AndroidUsingReflection.PRIORITY + 1; - - private AndroidUsingExecLowPriority() { - super(AndroidUsingExecLowPriority.class.getSimpleName(), PRIORITY); - } - - @Override - public String[] getDnsServerAddresses() { - try { - Process process = Runtime.getRuntime().exec("getprop"); - InputStream inputStream = process.getInputStream(); - LineNumberReader lnr = new LineNumberReader( - new InputStreamReader(inputStream)); - String line; - HashSet server = new HashSet<>(6); - while ((line = lnr.readLine()) != null) { - int split = line.indexOf("]: ["); - if (split == -1) { - continue; - } - String property = line.substring(1, split); - String value = line.substring(split + 4, line.length() - 1); - - if (value.isEmpty()) { - continue; - } - - if (property.endsWith(".dns") || property.endsWith(".dns1") || - property.endsWith(".dns2") || property.endsWith(".dns3") || - property.endsWith(".dns4")) { - - // normalize the address - - InetAddress ip = InetAddress.getByName(value); - - if (ip == null) continue; - - value = ip.getHostAddress(); - - if (value == null) continue; - if (value.length() == 0) continue; - - server.add(value); - } - } - if (server.size() > 0) { - return server.toArray(new String[server.size()]); - } - } catch (IOException e) { - LOGGER.log(Level.WARNING, "Exception in findDNSByExec", e); - } - return null; - } - - @Override - public boolean isAvailable() { - return PlatformDetection.isAndroid(); - } - -} diff --git a/src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java b/src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java deleted file mode 100644 index d6a9f9a1a..000000000 --- a/src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java +++ /dev/null @@ -1,91 +0,0 @@ -package eu.siacs.conversations.utils; - -import android.annotation.TargetApi; -import android.content.Context; -import android.net.ConnectivityManager; -import android.net.LinkProperties; -import android.net.Network; -import android.net.NetworkInfo; -import android.net.RouteInfo; -import android.os.Build; - -import java.net.Inet4Address; -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.List; - -import de.measite.minidns.dnsserverlookup.AbstractDNSServerLookupMechanism; -import de.measite.minidns.dnsserverlookup.AndroidUsingExec; - -public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism { - - private final Context context; - - protected AndroidUsingLinkProperties(Context context) { - super(AndroidUsingLinkProperties.class.getSimpleName(), AndroidUsingExec.PRIORITY - 1); - this.context = context; - } - - @Override - public boolean isAvailable() { - return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; - } - - @Override - @TargetApi(21) - public String[] getDnsServerAddresses() { - ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); - 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); - if (linkProperties == null) { - continue; - } - NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); - final boolean isActiveNetwork = network.equals(activeNetwork); - if (networkInfo != null && isActiveNetwork && networkInfo.getType() == ConnectivityManager.TYPE_VPN) { - final List tmp = getIPv4First(linkProperties.getDnsServers()); - servers.addAll(0, tmp); - vpnOffset += tmp.size(); - } else if (hasDefaultRoute(linkProperties) || isActiveNetwork) { - servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers())); - } else { - servers.addAll(getIPv4First(linkProperties.getDnsServers())); - } - } - 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) { - if (addr instanceof Inet4Address) { - out.add(0, addr.getHostAddress()); - } else { - out.add(addr.getHostAddress()); - } - } - return out; - } - - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - private static boolean hasDefaultRoute(LinkProperties linkProperties) { - for(RouteInfo route: linkProperties.getRoutes()) { - if (route.isDefaultRoute()) { - return true; - } - } - return false; - } -} diff --git a/src/main/java/eu/siacs/conversations/utils/Resolver.java b/src/main/java/eu/siacs/conversations/utils/Resolver.java index 5a63a97b4..32961c6d8 100644 --- a/src/main/java/eu/siacs/conversations/utils/Resolver.java +++ b/src/main/java/eu/siacs/conversations/utils/Resolver.java @@ -17,7 +17,6 @@ import de.measite.minidns.DNSName; import de.measite.minidns.Question; import de.measite.minidns.Record; import de.measite.minidns.dnssec.DNSSECResultNotAuthenticException; -import de.measite.minidns.dnsserverlookup.AndroidUsingExec; import de.measite.minidns.hla.DnssecResolverApi; import de.measite.minidns.hla.ResolverApi; import de.measite.minidns.hla.ResolverResult; @@ -41,9 +40,6 @@ public class Resolver { public static void init(XmppConnectionService service) { Resolver.SERVICE = service; - DNSClient.removeDNSServerLookupMechanism(AndroidUsingExec.INSTANCE); - DNSClient.addDnsServerLookupMechanism(AndroidUsingExecLowPriority.INSTANCE); - DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(service)); final AbstractDNSClient client = ResolverApi.INSTANCE.getClient(); if (client instanceof ReliableDNSClient) { disableHardcodedDnsServers((ReliableDNSClient) client);