remove unused 'service' from resolver

This commit is contained in:
Daniel Gultsch 2023-03-02 19:45:45 +01:00
parent 4addeaa356
commit 807078b24f
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 34 additions and 43 deletions

View file

@ -1,8 +1,7 @@
package im.conversations.android.dns; package im.conversations.android.dns;
import android.app.Application; import android.app.Application;
import android.content.Context; import androidx.annotation.NonNull;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering; import com.google.common.collect.Ordering;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
@ -53,10 +52,7 @@ public class Resolver {
private static final Executor EXECUTOR = Executors.newFixedThreadPool(4); private static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
private static Context SERVICE;
public static void init(final Application application) { public static void init(final Application application) {
SERVICE = application.getApplicationContext();
DNSClient.removeDNSServerLookupMechanism(AndroidUsingExec.INSTANCE); DNSClient.removeDNSServerLookupMechanism(AndroidUsingExec.INSTANCE);
DNSClient.addDnsServerLookupMechanism(AndroidUsingExecLowPriority.INSTANCE); DNSClient.addDnsServerLookupMechanism(AndroidUsingExecLowPriority.INSTANCE);
DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(application)); DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(application));
@ -116,15 +112,15 @@ public class Resolver {
} }
} }
public static List<ServiceRecord> resolve(final String domain) { public static List<ServiceRecord> resolve(final String domain, final boolean validateHostname) {
final List<ServiceRecord> ipResults = fromIpAddress(domain); final List<ServiceRecord> ipResults = fromIpAddress(domain);
if (ipResults.size() > 0) { if (ipResults.size() > 0) {
return ipResults; return ipResults;
} }
final ListenableFuture<List<ServiceRecord>> directTlsSrvRecords = final ListenableFuture<List<ServiceRecord>> directTlsSrvRecords =
Futures.submitAsync(() -> resolveSrv(domain, true), EXECUTOR); Futures.submitAsync(() -> resolveSrv(domain, true, validateHostname), EXECUTOR);
final ListenableFuture<List<ServiceRecord>> startTlsSrvRecords = final ListenableFuture<List<ServiceRecord>> startTlsSrvRecords =
Futures.submitAsync(() -> resolveSrv(domain, false), EXECUTOR); Futures.submitAsync(() -> resolveSrv(domain, false, validateHostname), EXECUTOR);
final ListenableFuture<List<ServiceRecord>> srvRecords = final ListenableFuture<List<ServiceRecord>> srvRecords =
Futures.transform( Futures.transform(
Futures.allAsList(directTlsSrvRecords, startTlsSrvRecords), Futures.allAsList(directTlsSrvRecords, startTlsSrvRecords),
@ -164,39 +160,24 @@ public class Resolver {
} catch (final UnknownHostException e) { } catch (final UnknownHostException e) {
return Collections.emptyList(); 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<List<ServiceRecord>> resolveSrv( private static ListenableFuture<List<ServiceRecord>> resolveSrv(
final String domain, final boolean directTls) throws IOException { final String domain, final boolean directTls, final boolean validateHostname)
DNSName dnsName = throws IOException {
final var dnsName =
DNSName.from( DNSName.from(
(directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain); (directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain);
final ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class); final ResolverResult<SRV> result = resolveWithFallback(dnsName, validateHostname);
final List<ListenableFuture<List<ServiceRecord>>> results = new ArrayList<>(); final List<ListenableFuture<List<ServiceRecord>>> results = new ArrayList<>();
for (final SRV record : result.getAnswersOrEmptySet()) { for (final SRV record : result.getAnswersOrEmptySet()) {
if (record.name.length() == 0 && record.priority == 0) { if (record.name.length() == 0 && record.priority == 0) {
continue; continue;
} }
results.add( final var ipv4 = Futures.submit(() -> resolveIPv4(directTls, result, record), EXECUTOR);
Futures.submit( final var ipv6 =
() -> {
final List<ServiceRecord> 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(
Futures.submit( Futures.submit(
() -> () ->
resolveIp( resolveIp(
@ -204,7 +185,9 @@ public class Resolver {
AAAA.class, AAAA.class,
result.isAuthenticData(), result.isAuthenticData(),
directTls), directTls),
EXECUTOR)); EXECUTOR);
results.add(ipv4);
results.add(ipv6);
} }
return Futures.transform( return Futures.transform(
Futures.allAsList(results), Futures.allAsList(results),
@ -212,6 +195,19 @@ public class Resolver {
MoreExecutors.directExecutor()); MoreExecutors.directExecutor());
} }
@NonNull
private static List<ServiceRecord> resolveIPv4(
boolean directTls, ResolverResult<SRV> result, SRV record) {
final List<ServiceRecord> 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 <D extends InternetAddressRR> List<ServiceRecord> resolveIp( private static <D extends InternetAddressRR> List<ServiceRecord> resolveIp(
SRV srv, Class<D> type, boolean authenticated, boolean directTls) { SRV srv, Class<D> type, boolean authenticated, boolean directTls) {
final ImmutableList.Builder<ServiceRecord> builder = new ImmutableList.Builder<>(); final ImmutableList.Builder<ServiceRecord> builder = new ImmutableList.Builder<>();
@ -254,9 +250,9 @@ public class Resolver {
return results; return results;
} }
private static <D extends Data> ResolverResult<D> resolveWithFallback( private static ResolverResult<SRV> resolveWithFallback(
DNSName dnsName, Class<D> type) throws IOException { final DNSName dnsName, boolean validateHostname) throws IOException {
return resolveWithFallback(dnsName, type, validateHostname()); return resolveWithFallback(dnsName, SRV.class, validateHostname);
} }
private static <D extends Data> ResolverResult<D> resolveWithFallback( private static <D extends Data> ResolverResult<D> resolveWithFallback(
@ -285,5 +281,4 @@ public class Resolver {
// TODO bring back in one form or another // TODO bring back in one form or another
return false; return false;
} }
} }

View file

@ -1,14 +1,11 @@
package im.conversations.android.dns; package im.conversations.android.dns;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import java.net.InetAddress;
import de.measite.minidns.DNSName; import de.measite.minidns.DNSName;
import de.measite.minidns.record.SRV; import de.measite.minidns.record.SRV;
import java.net.InetAddress;
public class ServiceRecord implements Comparable<ServiceRecord> { public class ServiceRecord implements Comparable<ServiceRecord> {
private final InetAddress ip; private final InetAddress ip;

View file

@ -327,7 +327,7 @@ public class XmppConnection implements Runnable {
if (connection != null) { if (connection != null) {
results = Resolver.fromHardCoded(connection); results = Resolver.fromHardCoded(connection);
} else { } else {
results = Resolver.resolve(domain); results = Resolver.resolve(domain, false);
} }
LOGGER.info("{}", results); LOGGER.info("{}", results);
if (Thread.currentThread().isInterrupted()) { if (Thread.currentThread().isInterrupted()) {
@ -353,8 +353,7 @@ public class XmppConnection implements Runnable {
+ storedBackupResult); + storedBackupResult);
} }
} }
for (Iterator<ServiceRecord> iterator = results.iterator(); for (Iterator<ServiceRecord> iterator = results.iterator(); iterator.hasNext(); ) {
iterator.hasNext(); ) {
final ServiceRecord result = iterator.next(); final ServiceRecord result = iterator.next();
if (Thread.currentThread().isInterrupted()) { if (Thread.currentThread().isInterrupted()) {
LOGGER.debug(account.address + ": Thread was interrupted"); LOGGER.debug(account.address + ": Thread was interrupted");