From 807078b24ff346fe7274cdc827fa83e413d0d46e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 2 Mar 2023 19:45:45 +0100 Subject: [PATCH] remove unused 'service' from resolver --- .../conversations/android/dns/Resolver.java | 67 +++++++++---------- .../android/dns/ServiceRecord.java | 5 +- .../android/xmpp/XmppConnection.java | 5 +- 3 files changed, 34 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/im/conversations/android/dns/Resolver.java b/app/src/main/java/im/conversations/android/dns/Resolver.java index 4db9d2d4c..2415f3916 100644 --- a/app/src/main/java/im/conversations/android/dns/Resolver.java +++ b/app/src/main/java/im/conversations/android/dns/Resolver.java @@ -1,8 +1,7 @@ package im.conversations.android.dns; import android.app.Application; -import android.content.Context; - +import androidx.annotation.NonNull; import com.google.common.collect.ImmutableList; import com.google.common.collect.Ordering; import com.google.common.util.concurrent.Futures; @@ -53,10 +52,7 @@ public class Resolver { private static final Executor EXECUTOR = Executors.newFixedThreadPool(4); - private static Context SERVICE; - public static void init(final Application application) { - SERVICE = application.getApplicationContext(); DNSClient.removeDNSServerLookupMechanism(AndroidUsingExec.INSTANCE); DNSClient.addDnsServerLookupMechanism(AndroidUsingExecLowPriority.INSTANCE); DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(application)); @@ -116,15 +112,15 @@ public class Resolver { } } - public static List resolve(final String domain) { + public static List resolve(final String domain, final boolean validateHostname) { final List ipResults = fromIpAddress(domain); if (ipResults.size() > 0) { return ipResults; } final ListenableFuture> directTlsSrvRecords = - Futures.submitAsync(() -> resolveSrv(domain, true), EXECUTOR); + Futures.submitAsync(() -> resolveSrv(domain, true, validateHostname), EXECUTOR); final ListenableFuture> startTlsSrvRecords = - Futures.submitAsync(() -> resolveSrv(domain, false), EXECUTOR); + Futures.submitAsync(() -> resolveSrv(domain, false, validateHostname), EXECUTOR); final ListenableFuture> srvRecords = Futures.transform( Futures.allAsList(directTlsSrvRecords, startTlsSrvRecords), @@ -164,39 +160,24 @@ public class Resolver { } catch (final UnknownHostException e) { return Collections.emptyList(); } - return Collections.singletonList(new ServiceRecord(ip, null, DEFAULT_PORT_XMPP, false, 0, false)); + return Collections.singletonList( + new ServiceRecord(ip, null, DEFAULT_PORT_XMPP, false, 0, false)); } private static ListenableFuture> resolveSrv( - final String domain, final boolean directTls) throws IOException { - DNSName dnsName = + final String domain, final boolean directTls, final boolean validateHostname) + throws IOException { + final var dnsName = DNSName.from( (directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain); - final ResolverResult result = resolveWithFallback(dnsName, SRV.class); + final ResolverResult result = resolveWithFallback(dnsName, validateHostname); final List>> results = new ArrayList<>(); for (final SRV record : result.getAnswersOrEmptySet()) { if (record.name.length() == 0 && record.priority == 0) { continue; } - results.add( - Futures.submit( - () -> { - final List ipv4s = - resolveIp( - record, - A.class, - result.isAuthenticData(), - directTls); - if (ipv4s.isEmpty()) { - return Collections.singletonList( - ServiceRecord.fromRecord( - record, directTls, result.isAuthenticData())); - } else { - return ipv4s; - } - }, - EXECUTOR)); - results.add( + final var ipv4 = Futures.submit(() -> resolveIPv4(directTls, result, record), EXECUTOR); + final var ipv6 = Futures.submit( () -> resolveIp( @@ -204,7 +185,9 @@ public class Resolver { AAAA.class, result.isAuthenticData(), directTls), - EXECUTOR)); + EXECUTOR); + results.add(ipv4); + results.add(ipv6); } return Futures.transform( Futures.allAsList(results), @@ -212,6 +195,19 @@ public class Resolver { MoreExecutors.directExecutor()); } + @NonNull + private static List resolveIPv4( + boolean directTls, ResolverResult result, SRV record) { + final List ipv4s = + resolveIp(record, A.class, result.isAuthenticData(), directTls); + if (ipv4s.isEmpty()) { + return Collections.singletonList( + ServiceRecord.fromRecord(record, directTls, result.isAuthenticData())); + } else { + return ipv4s; + } + } + private static List resolveIp( SRV srv, Class type, boolean authenticated, boolean directTls) { final ImmutableList.Builder builder = new ImmutableList.Builder<>(); @@ -254,9 +250,9 @@ public class Resolver { return results; } - private static ResolverResult resolveWithFallback( - DNSName dnsName, Class type) throws IOException { - return resolveWithFallback(dnsName, type, validateHostname()); + private static ResolverResult resolveWithFallback( + final DNSName dnsName, boolean validateHostname) throws IOException { + return resolveWithFallback(dnsName, SRV.class, validateHostname); } private static ResolverResult resolveWithFallback( @@ -285,5 +281,4 @@ public class Resolver { // TODO bring back in one form or another return false; } - } diff --git a/app/src/main/java/im/conversations/android/dns/ServiceRecord.java b/app/src/main/java/im/conversations/android/dns/ServiceRecord.java index eb6a3dd8d..7b3fcbfac 100644 --- a/app/src/main/java/im/conversations/android/dns/ServiceRecord.java +++ b/app/src/main/java/im/conversations/android/dns/ServiceRecord.java @@ -1,14 +1,11 @@ package im.conversations.android.dns; import androidx.annotation.NonNull; - import com.google.common.base.MoreObjects; import com.google.common.base.Objects; - -import java.net.InetAddress; - import de.measite.minidns.DNSName; import de.measite.minidns.record.SRV; +import java.net.InetAddress; public class ServiceRecord implements Comparable { private final InetAddress ip; diff --git a/app/src/main/java/im/conversations/android/xmpp/XmppConnection.java b/app/src/main/java/im/conversations/android/xmpp/XmppConnection.java index 11c94ca54..8eb1cbd55 100644 --- a/app/src/main/java/im/conversations/android/xmpp/XmppConnection.java +++ b/app/src/main/java/im/conversations/android/xmpp/XmppConnection.java @@ -327,7 +327,7 @@ public class XmppConnection implements Runnable { if (connection != null) { results = Resolver.fromHardCoded(connection); } else { - results = Resolver.resolve(domain); + results = Resolver.resolve(domain, false); } LOGGER.info("{}", results); if (Thread.currentThread().isInterrupted()) { @@ -353,8 +353,7 @@ public class XmppConnection implements Runnable { + storedBackupResult); } } - for (Iterator iterator = results.iterator(); - iterator.hasNext(); ) { + for (Iterator iterator = results.iterator(); iterator.hasNext(); ) { final ServiceRecord result = iterator.next(); if (Thread.currentThread().isInterrupted()) { LOGGER.debug(account.address + ": Thread was interrupted");