diff options
author | Christian Schneppe <kriztan@users.noreply.github.com> | 2020-02-10 07:34:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-10 07:34:29 +0100 |
commit | d7a7db1ced531f33f71bab29f96893fbb1b3daea (patch) | |
tree | 41fa6ae30cf0c28981fac0c566f1dde38267d66e | |
parent | db952297c516600547924aa6c9ebca53fd3bfea8 (diff) | |
parent | 9e24597dd28bc086ff153e7bdd17421f145d7b25 (diff) |
Merge pull request #453 from genofire/resolver-fix
improve happy eyeball
3 files changed, 244 insertions, 9 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/AndroidUsingExecLowPriority.java b/src/main/java/de/pixart/messenger/utils/AndroidUsingExecLowPriority.java new file mode 100644 index 000000000..6097fcd03 --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/AndroidUsingExecLowPriority.java @@ -0,0 +1,92 @@ +/* + * 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 de.pixart.messenger.utils; + +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; + +import de.measite.minidns.dnsserverlookup.AbstractDNSServerLookupMechanism; +import de.measite.minidns.dnsserverlookup.AndroidUsingReflection; +import de.measite.minidns.dnsserverlookup.DNSServerLookupMechanism; +import de.measite.minidns.util.PlatformDetection; + +/** + * 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<String> 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(); + } + +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/utils/AndroidUsingLinkProperties.java b/src/main/java/de/pixart/messenger/utils/AndroidUsingLinkProperties.java new file mode 100644 index 000000000..f2f33dc1b --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/AndroidUsingLinkProperties.java @@ -0,0 +1,90 @@ +package de.pixart.messenger.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; + + 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() { + final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + final Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); + if (networks == null) { + return new String[0]; + } + final Network activeNetwork = getActiveNetwork(connectivityManager); + final List<String> servers = new ArrayList<>(); + int vpnOffset = 0; + for (Network network : networks) { + LinkProperties linkProperties = connectivityManager.getLinkProperties(network); + if (linkProperties == null) { + continue; + } + final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); + final boolean isActiveNetwork = network.equals(activeNetwork); + final boolean isVpn = networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_VPN; + if (isActiveNetwork && isVpn) { + final List<String> tmp = getIPv4First(linkProperties.getDnsServers()); + servers.addAll(0, tmp); + vpnOffset += tmp.size(); + } else if (hasDefaultRoute(linkProperties) || isActiveNetwork || activeNetwork == null || isVpn) { + servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers())); + } + } + return servers.toArray(new String[0]); + } + + @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 address : in) { + if (address instanceof Inet4Address) { + out.add(0, address.getHostAddress()); + } else { + out.add(address.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; + } +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/utils/Resolver.java b/src/main/java/de/pixart/messenger/utils/Resolver.java index 596203342..e2cec35fe 100644 --- a/src/main/java/de/pixart/messenger/utils/Resolver.java +++ b/src/main/java/de/pixart/messenger/utils/Resolver.java @@ -29,6 +29,7 @@ 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; @@ -56,6 +57,9 @@ 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); @@ -177,8 +181,12 @@ public class Resolver { ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class); final List<Result> results = new ArrayList<>(); final List<Thread> threads = new ArrayList<>(); + + final List<Result> fallbackResults = new ArrayList<>(); + final List<Thread> fallbackThreads = new ArrayList<>(); + for (SRV record : result.getAnswersOrEmptySet()) { - if (record.name.length() == 0 && record.priority == 0) { + if (record.name.length() == 0) { continue; } threads.add(new Thread(() -> { @@ -193,6 +201,22 @@ public class Resolver { results.addAll(ipv4s); } })); + fallbackThreads.add(new Thread(() -> { + try { + for (CNAME cname : resolveWithFallback(record.name, CNAME.class, result.isAuthenticData()).getAnswersOrEmptySet()) { + final List<Result> ipv6s = resolveIp(record, cname.name, AAAA.class, result.isAuthenticData(), directTls); + synchronized (fallbackResults) { + fallbackResults.addAll(ipv6s); + } + final List<Result> ipv4s = resolveIp(record, cname.name, A.class, result.isAuthenticData(), directTls); + synchronized (results) { + fallbackResults.addAll(ipv4s); + } + } + } catch (Throwable throwable) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving srv cname-fallback records", throwable); + } + })); } for (Thread thread : threads) { thread.start(); @@ -205,13 +229,30 @@ public class Resolver { return Collections.emptyList(); } } - return results; + if (results.size() > 0) { + return results; + } + for (Thread thread : fallbackThreads) { + thread.start(); + } + for (Thread thread : fallbackThreads) { + try { + thread.join(); + } catch (InterruptedException e) { + return Collections.emptyList(); + } + } + return fallbackResults; } private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) { + return resolveIp(srv, srv.name, type, authenticated, directTls); + } + + private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, DNSName hostname, Class<D> type, boolean authenticated, boolean directTls) { List<Result> list = new ArrayList<>(); try { - ResolverResult<D> results = resolveWithFallback(srv.name, type, authenticated); + ResolverResult<D> results = resolveWithFallback(hostname, type, authenticated); for (D record : results.getAnswersOrEmptySet()) { Result resolverResult = Result.fromRecord(srv, directTls); resolverResult.authenticated = results.isAuthenticData() && authenticated; @@ -273,10 +314,15 @@ public class Resolver { Result result; if (r.size() == 1) { result = r.get(0); + result.setLogID(logID); result.connect(); return result; } + for (Result res : r) { + res.setLogID(logID); + } + ExecutorService executor = Executors.newFixedThreadPool(4); try { @@ -328,6 +374,8 @@ public class Resolver { private int priority; private Socket socket; + private String logID; + static Result fromRecord(SRV srv, boolean directTls) { Result result = new Result(); result.port = srv.port; @@ -410,7 +458,11 @@ public class Resolver { long time = System.currentTimeMillis(); this.socket.connect(addr, Config.SOCKET_TIMEOUT * 1000); time = System.currentTimeMillis() - time; - Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": Result connect: " + toString() + " after: " + time + " ms"); + if (!this.logID.isEmpty()) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": Result (" + this.logID + ") connect: " + toString() + " after: " + time + " ms"); + } else { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": Result connect: " + toString() + " after: " + time + " ms"); + } } catch (IOException e) { e.printStackTrace(); this.disconnect(); @@ -418,20 +470,21 @@ public class Resolver { } public void disconnect() { - this.disconnect(""); - } - public void disconnect(String logID) { if (this.socket != null ) { FileBackend.close(this.socket); this.socket = null; - if (!logID.isEmpty()) { - Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": Result (" + logID + ") disconnect: " + toString()); + if (!this.logID.isEmpty()) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": Result (" + this.logID + ") disconnect: " + toString()); } else { Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": Result disconnect: " + toString()); } } } + public void setLogID(String logID) { + this.logID = logID; + } + @Override public int compareTo(@NonNull Result result) { if (result.priority == priority) { |