diff options
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/Resolver.java | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/Resolver.java b/src/main/java/de/pixart/messenger/utils/Resolver.java index 3f4ffa43f..02663b283 100644 --- a/src/main/java/de/pixart/messenger/utils/Resolver.java +++ b/src/main/java/de/pixart/messenger/utils/Resolver.java @@ -12,10 +12,14 @@ import java.util.List; import de.measite.minidns.DNSClient; import de.measite.minidns.DNSName; +import de.measite.minidns.dnssec.DNSSECResultNotAuthenticException; +import de.measite.minidns.dnssec.DNSSECValidationFailedException; import de.measite.minidns.hla.DnssecResolverApi; +import de.measite.minidns.hla.ResolverApi; import de.measite.minidns.hla.ResolverResult; import de.measite.minidns.record.A; import de.measite.minidns.record.AAAA; +import de.measite.minidns.record.Data; import de.measite.minidns.record.InternetAddressRR; import de.measite.minidns.record.SRV; import de.pixart.messenger.Config; @@ -33,29 +37,33 @@ public class Resolver { List<Result> results = new ArrayList<>(); try { results.addAll(resolveSrv(domain, true)); - } catch (Throwable t) { - Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + t.getMessage()); + } catch (IOException e) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + e.getMessage()); } try { results.addAll(resolveSrv(domain, false)); - } catch (Throwable t) { - Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + t.getMessage()); + } catch (IOException e) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + e.getMessage()); } if (results.size() == 0) { results.add(Result.createDefault(domain)); } Collections.sort(results); + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + results.toString()); return results; } private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException { + if (Thread.interrupted()) { + return Collections.emptyList(); + } DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERICE) + "._tcp." + domain); - ResolverResult<SRV> result = DnssecResolverApi.INSTANCE.resolveDnssecReliable(dnsName, SRV.class); + ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class); List<Result> results = new ArrayList<>(); for (SRV record : result.getAnswersOrEmptySet()) { - boolean added = results.addAll(resolveIp(record, A.class, result.isAuthenticData(), directTls)); - added |= results.addAll(resolveIp(record, AAAA.class, result.isAuthenticData(), directTls)); - if (!added) { + final boolean addedIPv4 = results.addAll(resolveIp(record, A.class, result.isAuthenticData(), directTls)); + results.addAll(resolveIp(record, AAAA.class, result.isAuthenticData(), directTls)); + if (!addedIPv4 && !Thread.interrupted()) { Result resolverResult = Result.fromRecord(record, directTls); resolverResult.authenticated = resolverResult.isAuthenticated(); results.add(resolverResult); @@ -65,9 +73,12 @@ public class Resolver { } private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) { + if (Thread.interrupted()) { + return Collections.emptyList(); + } List<Result> list = new ArrayList<>(); try { - ResolverResult<D> results = DnssecResolverApi.INSTANCE.resolveDnssecReliable(srv.name, type); + ResolverResult<D> results = resolveWithFallback(srv.name, type); for (D record : results.getAnswersOrEmptySet()) { Result resolverResult = Result.fromRecord(srv, directTls); resolverResult.authenticated = results.isAuthenticData() && authenticated; @@ -80,6 +91,15 @@ public class Resolver { return list; } + private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type) throws IOException { + try { + return DnssecResolverApi.INSTANCE.resolveDnssecReliable(dnsName, type); + } catch (DNSSECValidationFailedException | DNSSECResultNotAuthenticException e) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving " + type.getSimpleName() + " with DNSSEC. Trying DNS instead " + e.getMessage()); + return ResolverApi.INSTANCE.resolve(dnsName, type); + } + } + public static class Result implements Comparable<Result> { private InetAddress ip; private DNSName hostname; @@ -124,9 +144,13 @@ public class Resolver { public int compareTo(@NonNull Result result) { if (result.priority == priority) { if (directTls == result.directTls) { - return 0; + if (ip == null && result.ip == null) { + return 0; + } else { + return ip != null ? -1 : 1; + } } else { - return directTls ? 1 : -1; + return directTls ? -1 : 1; } } else { return priority - result.priority; |