From a40d244bf59478bb137dd60825dea5b1177f99bc Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 12 Oct 2023 11:59:21 +0200 Subject: [PATCH] remove unnecessary in resolver --- .../java/de/gultsch/minidns/DNSSocket.java | 12 ++++++++-- .../de/gultsch/minidns/NetworkDataSource.java | 2 +- .../siacs/conversations/utils/Resolver.java | 22 +++++++++++++------ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/gultsch/minidns/DNSSocket.java b/src/main/java/de/gultsch/minidns/DNSSocket.java index 4b096e0b2..f69b6fb27 100644 --- a/src/main/java/de/gultsch/minidns/DNSSocket.java +++ b/src/main/java/de/gultsch/minidns/DNSSocket.java @@ -28,6 +28,8 @@ import java.util.Iterator; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSession; @@ -37,6 +39,7 @@ import javax.net.ssl.SSLSocketFactory; final class DNSSocket implements Closeable { private static final int CONNECT_TIMEOUT = 5_000; + public static final int QUERY_TIMEOUT = 5_000; private final Semaphore semaphore = new Semaphore(1); private final Map> inFlightQueries = new HashMap<>(); @@ -109,6 +112,7 @@ final class DNSSocket implements Closeable { new InetSocketAddress(dnsServer.inetAddress, dnsServer.port); final Socket socket = new Socket(); socket.connect(socketAddress, CONNECT_TIMEOUT); + socket.setSoTimeout(QUERY_TIMEOUT); return DNSSocket.of(socket); } @@ -120,9 +124,11 @@ final class DNSSocket implements Closeable { final SocketAddress socketAddress = new InetSocketAddress(dnsServer.inetAddress, dnsServer.port); sslSocket = (SSLSocket) factory.createSocket(dnsServer.inetAddress, dnsServer.port); - sslSocket.connect(socketAddress, 5_000); + sslSocket.connect(socketAddress, CONNECT_TIMEOUT); + sslSocket.setSoTimeout(QUERY_TIMEOUT); } else { sslSocket = (SSLSocket) factory.createSocket(dnsServer.hostname, dnsServer.port); + sslSocket.setSoTimeout(QUERY_TIMEOUT); final SSLSession session = sslSocket.getSession(); final Certificate[] peerCertificates = session.getPeerCertificates(); if (peerCertificates.length == 0 || !(peerCertificates[0] instanceof X509Certificate)) { @@ -138,7 +144,7 @@ final class DNSSocket implements Closeable { public DNSMessage query(final DNSMessage query) throws IOException, InterruptedException { try { - return queryAsync(query).get(); + return queryAsync(query).get(QUERY_TIMEOUT, TimeUnit.MILLISECONDS); } catch (final ExecutionException e) { final Throwable cause = e.getCause(); if (cause instanceof IOException) { @@ -146,6 +152,8 @@ final class DNSSocket implements Closeable { } else { throw new IOException(e); } + } catch (final TimeoutException e) { + throw new IOException(e); } } diff --git a/src/main/java/de/gultsch/minidns/NetworkDataSource.java b/src/main/java/de/gultsch/minidns/NetworkDataSource.java index 1ba56f3c0..93909891d 100644 --- a/src/main/java/de/gultsch/minidns/NetworkDataSource.java +++ b/src/main/java/de/gultsch/minidns/NetworkDataSource.java @@ -93,7 +93,7 @@ public class NetworkDataSource extends DNSDataSource { } catch (final IOException e) { ioExceptions.add(e); } catch (final InterruptedException e) { - return null; + throw new IOException(e); } } MultipleIoException.throwIfRequired(ioExceptions); diff --git a/src/main/java/eu/siacs/conversations/utils/Resolver.java b/src/main/java/eu/siacs/conversations/utils/Resolver.java index 444deda4a..d6d1adaf0 100644 --- a/src/main/java/eu/siacs/conversations/utils/Resolver.java +++ b/src/main/java/eu/siacs/conversations/utils/Resolver.java @@ -6,6 +6,8 @@ import android.util.Log; import androidx.annotation.NonNull; +import com.google.common.base.Throwables; + import java.io.IOException; import java.lang.reflect.Field; import java.net.Inet4Address; @@ -113,7 +115,7 @@ public class Resolver { return port == 443 || port == 5223; } - public static List resolve(String domain) { + public static List resolve(final String domain) { final List ipResults = fromIpAddress(domain); if (ipResults.size() > 0) { return ipResults; @@ -127,8 +129,10 @@ public class Resolver { synchronized (results) { results.addAll(list); } - } catch (Throwable throwable) { - Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (direct TLS)", throwable); + } catch (final Throwable throwable) { + if (!(Throwables.getRootCause(throwable) instanceof InterruptedException)) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (direct TLS)", throwable); + } } }); threads[1] = new Thread(() -> { @@ -137,8 +141,10 @@ public class Resolver { synchronized (results) { results.addAll(list); } - } catch (Throwable throwable) { - Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (STARTTLS)", throwable); + } catch (final Throwable throwable) { + if (!(Throwables.getRootCause(throwable) instanceof InterruptedException)) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (STARTTLS)", throwable); + } } }); threads[2] = new Thread(() -> { @@ -261,8 +267,10 @@ public class Resolver { results.addAll(resolveNoSrvRecords(cname.name, false)); } } - } catch (Throwable throwable) { - Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving fallback records", throwable); + } catch (final Throwable throwable) { + if (!(Throwables.getRootCause(throwable) instanceof InterruptedException)) { + Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving fallback records", throwable); + } } results.add(Result.createDefault(dnsName)); return results;