remove unnecessary in resolver

This commit is contained in:
Daniel Gultsch 2023-10-12 11:59:21 +02:00
parent 7f278202c6
commit a40d244bf5
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 26 additions and 10 deletions

View file

@ -28,6 +28,8 @@ import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSession;
@ -37,6 +39,7 @@ import javax.net.ssl.SSLSocketFactory;
final class DNSSocket implements Closeable { final class DNSSocket implements Closeable {
private static final int CONNECT_TIMEOUT = 5_000; 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 Semaphore semaphore = new Semaphore(1);
private final Map<Integer, SettableFuture<DNSMessage>> inFlightQueries = new HashMap<>(); private final Map<Integer, SettableFuture<DNSMessage>> inFlightQueries = new HashMap<>();
@ -109,6 +112,7 @@ final class DNSSocket implements Closeable {
new InetSocketAddress(dnsServer.inetAddress, dnsServer.port); new InetSocketAddress(dnsServer.inetAddress, dnsServer.port);
final Socket socket = new Socket(); final Socket socket = new Socket();
socket.connect(socketAddress, CONNECT_TIMEOUT); socket.connect(socketAddress, CONNECT_TIMEOUT);
socket.setSoTimeout(QUERY_TIMEOUT);
return DNSSocket.of(socket); return DNSSocket.of(socket);
} }
@ -120,9 +124,11 @@ final class DNSSocket implements Closeable {
final SocketAddress socketAddress = final SocketAddress socketAddress =
new InetSocketAddress(dnsServer.inetAddress, dnsServer.port); new InetSocketAddress(dnsServer.inetAddress, dnsServer.port);
sslSocket = (SSLSocket) factory.createSocket(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 { } else {
sslSocket = (SSLSocket) factory.createSocket(dnsServer.hostname, dnsServer.port); sslSocket = (SSLSocket) factory.createSocket(dnsServer.hostname, dnsServer.port);
sslSocket.setSoTimeout(QUERY_TIMEOUT);
final SSLSession session = sslSocket.getSession(); final SSLSession session = sslSocket.getSession();
final Certificate[] peerCertificates = session.getPeerCertificates(); final Certificate[] peerCertificates = session.getPeerCertificates();
if (peerCertificates.length == 0 || !(peerCertificates[0] instanceof X509Certificate)) { 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 { public DNSMessage query(final DNSMessage query) throws IOException, InterruptedException {
try { try {
return queryAsync(query).get(); return queryAsync(query).get(QUERY_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
final Throwable cause = e.getCause(); final Throwable cause = e.getCause();
if (cause instanceof IOException) { if (cause instanceof IOException) {
@ -146,6 +152,8 @@ final class DNSSocket implements Closeable {
} else { } else {
throw new IOException(e); throw new IOException(e);
} }
} catch (final TimeoutException e) {
throw new IOException(e);
} }
} }

View file

@ -93,7 +93,7 @@ public class NetworkDataSource extends DNSDataSource {
} catch (final IOException e) { } catch (final IOException e) {
ioExceptions.add(e); ioExceptions.add(e);
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
return null; throw new IOException(e);
} }
} }
MultipleIoException.throwIfRequired(ioExceptions); MultipleIoException.throwIfRequired(ioExceptions);

View file

@ -6,6 +6,8 @@ import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.google.common.base.Throwables;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.Inet4Address; import java.net.Inet4Address;
@ -113,7 +115,7 @@ public class Resolver {
return port == 443 || port == 5223; return port == 443 || port == 5223;
} }
public static List<Result> resolve(String domain) { public static List<Result> resolve(final String domain) {
final List<Result> ipResults = fromIpAddress(domain); final List<Result> ipResults = fromIpAddress(domain);
if (ipResults.size() > 0) { if (ipResults.size() > 0) {
return ipResults; return ipResults;
@ -127,9 +129,11 @@ public class Resolver {
synchronized (results) { synchronized (results) {
results.addAll(list); results.addAll(list);
} }
} catch (Throwable 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); Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (direct TLS)", throwable);
} }
}
}); });
threads[1] = new Thread(() -> { threads[1] = new Thread(() -> {
try { try {
@ -137,9 +141,11 @@ public class Resolver {
synchronized (results) { synchronized (results) {
results.addAll(list); results.addAll(list);
} }
} catch (Throwable throwable) { } catch (final Throwable throwable) {
if (!(Throwables.getRootCause(throwable) instanceof InterruptedException)) {
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (STARTTLS)", throwable); Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (STARTTLS)", throwable);
} }
}
}); });
threads[2] = new Thread(() -> { threads[2] = new Thread(() -> {
List<Result> list = resolveNoSrvRecords(DNSName.from(domain), true); List<Result> list = resolveNoSrvRecords(DNSName.from(domain), true);
@ -261,9 +267,11 @@ public class Resolver {
results.addAll(resolveNoSrvRecords(cname.name, false)); results.addAll(resolveNoSrvRecords(cname.name, false));
} }
} }
} catch (Throwable throwable) { } catch (final Throwable throwable) {
if (!(Throwables.getRootCause(throwable) instanceof InterruptedException)) {
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving fallback records", throwable); Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving fallback records", throwable);
} }
}
results.add(Result.createDefault(dnsName)); results.add(Result.createDefault(dnsName));
return results; return results;
} }