mirror of
https://github.com/moparisthebest/Conversations
synced 2024-11-15 05:25:01 -05:00
refactored resolver to fall back to DNS and use reliable method
This commit is contained in:
parent
4faf0f2ec9
commit
6dad1c0dde
@ -12,10 +12,14 @@ import java.util.List;
|
|||||||
|
|
||||||
import de.measite.minidns.DNSClient;
|
import de.measite.minidns.DNSClient;
|
||||||
import de.measite.minidns.DNSName;
|
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.DnssecResolverApi;
|
||||||
|
import de.measite.minidns.hla.ResolverApi;
|
||||||
import de.measite.minidns.hla.ResolverResult;
|
import de.measite.minidns.hla.ResolverResult;
|
||||||
import de.measite.minidns.record.A;
|
import de.measite.minidns.record.A;
|
||||||
import de.measite.minidns.record.AAAA;
|
import de.measite.minidns.record.AAAA;
|
||||||
|
import de.measite.minidns.record.Data;
|
||||||
import de.measite.minidns.record.InternetAddressRR;
|
import de.measite.minidns.record.InternetAddressRR;
|
||||||
import de.measite.minidns.record.SRV;
|
import de.measite.minidns.record.SRV;
|
||||||
import eu.siacs.conversations.Config;
|
import eu.siacs.conversations.Config;
|
||||||
@ -26,8 +30,6 @@ public class Resolver {
|
|||||||
private static final String STARTTLS_SERICE = "_xmpp-client";
|
private static final String STARTTLS_SERICE = "_xmpp-client";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void registerLookupMechanism(Context context) {
|
public static void registerLookupMechanism(Context context) {
|
||||||
DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(context));
|
DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(context));
|
||||||
}
|
}
|
||||||
@ -36,29 +38,33 @@ public class Resolver {
|
|||||||
List<Result> results = new ArrayList<>();
|
List<Result> results = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
results.addAll(resolveSrv(domain,true));
|
results.addAll(resolveSrv(domain,true));
|
||||||
} catch (Throwable t) {
|
} catch (IOException e) {
|
||||||
Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+t.getMessage());
|
Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+e.getMessage());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
results.addAll(resolveSrv(domain,false));
|
results.addAll(resolveSrv(domain,false));
|
||||||
} catch (Throwable t) {
|
} catch (IOException e) {
|
||||||
Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+t.getMessage());
|
Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+e.getMessage());
|
||||||
}
|
}
|
||||||
if (results.size() == 0) {
|
if (results.size() == 0) {
|
||||||
results.add(Result.createDefault(domain));
|
results.add(Result.createDefault(domain));
|
||||||
}
|
}
|
||||||
Collections.sort(results);
|
Collections.sort(results);
|
||||||
|
Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+results.toString());
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException {
|
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);
|
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<>();
|
List<Result> results = new ArrayList<>();
|
||||||
for(SRV record : result.getAnswersOrEmptySet()) {
|
for(SRV record : result.getAnswersOrEmptySet()) {
|
||||||
boolean added = results.addAll(resolveIp(record,A.class,result.isAuthenticData(),directTls));
|
final boolean addedIPv4 = results.addAll(resolveIp(record,A.class,result.isAuthenticData(),directTls));
|
||||||
added |= results.addAll(resolveIp(record,AAAA.class,result.isAuthenticData(),directTls));
|
results.addAll(resolveIp(record,AAAA.class,result.isAuthenticData(),directTls));
|
||||||
if (!added) {
|
if (!addedIPv4 && !Thread.interrupted()) {
|
||||||
Result resolverResult = Result.fromRecord(record, directTls);
|
Result resolverResult = Result.fromRecord(record, directTls);
|
||||||
resolverResult.authenticated = resolverResult.isAuthenticated();
|
resolverResult.authenticated = resolverResult.isAuthenticated();
|
||||||
results.add(resolverResult);
|
results.add(resolverResult);
|
||||||
@ -68,9 +74,12 @@ public class Resolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) {
|
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<>();
|
List<Result> list = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
ResolverResult<D> results = DnssecResolverApi.INSTANCE.resolveDnssecReliable(srv.name, type);
|
ResolverResult<D> results = resolveWithFallback(srv.name,type);
|
||||||
for (D record : results.getAnswersOrEmptySet()) {
|
for (D record : results.getAnswersOrEmptySet()) {
|
||||||
Result resolverResult = Result.fromRecord(srv, directTls);
|
Result resolverResult = Result.fromRecord(srv, directTls);
|
||||||
resolverResult.authenticated = results.isAuthenticData() && authenticated;
|
resolverResult.authenticated = results.isAuthenticData() && authenticated;
|
||||||
@ -83,6 +92,15 @@ public class Resolver {
|
|||||||
return list;
|
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> {
|
public static class Result implements Comparable<Result> {
|
||||||
private InetAddress ip;
|
private InetAddress ip;
|
||||||
private DNSName hostname;
|
private DNSName hostname;
|
||||||
@ -127,9 +145,13 @@ public class Resolver {
|
|||||||
public int compareTo(@NonNull Result result) {
|
public int compareTo(@NonNull Result result) {
|
||||||
if (result.priority == priority) {
|
if (result.priority == priority) {
|
||||||
if (directTls == result.directTls) {
|
if (directTls == result.directTls) {
|
||||||
return 0;
|
if (ip == null && result.ip == null) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return ip != null ? -1 : 1;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return directTls ? 1 : -1;
|
return directTls ? -1 : 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return priority - result.priority;
|
return priority - result.priority;
|
||||||
|
Loading…
Reference in New Issue
Block a user