Added options for custom resolver and enforcing DNSSEC

This commit is contained in:
M66B
2024-01-05 19:21:26 +01:00
parent 9f1d809c79
commit bd8fceca35
5 changed files with 77 additions and 13 deletions

View File

@@ -90,7 +90,7 @@ public class DnsHelper {
String domain = UriHelper.getEmailDomain(email);
if (domain == null)
continue;
DnsRecord[] records = _lookup(context, domain, "mx", CHECK_TIMEOUT, false);
DnsRecord[] records = _lookup(context, domain, "mx", CHECK_TIMEOUT);
if (records.length == 0)
throw new UnknownHostException(domain);
}
@@ -98,12 +98,14 @@ public class DnsHelper {
@NonNull
static DnsRecord[] lookup(Context context, String name, String type) {
return _lookup(context, name, type, LOOKUP_TIMEOUT, false);
return _lookup(context, name, type, LOOKUP_TIMEOUT);
}
@NonNull
private static DnsRecord[] _lookup(
Context context, String name, String type, int timeout, boolean require_authentic) {
private static DnsRecord[] _lookup(Context context, String name, String type, int timeout) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean dns_secure = prefs.getBoolean("dns_secure", false);
String filter = null;
int colon = type.indexOf(':');
if (colon > 0) {
@@ -234,7 +236,7 @@ public class DnsHelper {
data.throwIfErrorResponse();
boolean secure = (data.getUnverifiedReasons() != null);
if (secure && require_authentic) {
if (secure && dns_secure) {
DnssecResultNotAuthenticException ex = data.getDnssecResultNotAuthenticException();
if (ex != null)
throw ex;
@@ -321,8 +323,9 @@ public class DnsHelper {
return result.toArray(new DnsRecord[0]);
} catch (Throwable ex) {
if (ex instanceof DnssecValidationFailedException ||
ex instanceof MultipleIoException)
if (ex instanceof MultipleIoException ||
ex instanceof DnssecValidationFailedException ||
ex instanceof DnssecResultNotAuthenticException)
Log.i(ex);
else
Log.e(ex);
@@ -332,7 +335,7 @@ public class DnsHelper {
static InetAddress getByName(Context context, String host) throws UnknownHostException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean dns_custom = prefs.getBoolean("dns_custom", BuildConfig.DEBUG);
boolean dns_custom = prefs.getBoolean("dns_custom", false);
if (!dns_custom)
return InetAddress.getByName(host);
@@ -341,12 +344,11 @@ public class DnsHelper {
return InetAddress.getByName(host);
return getAllByName(context, host)[0];
}
static InetAddress[] getAllByName(Context context, String host) throws UnknownHostException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean dns_custom = prefs.getBoolean("dns_custom", BuildConfig.DEBUG);
boolean dns_custom = prefs.getBoolean("dns_custom", false);
if (!dns_custom)
return InetAddress.getAllByName(host);
@@ -436,6 +438,15 @@ public class DnsHelper {
return result;
}
static void clear(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
for (String key : prefs.getAll().keySet())
if (key != null && key.startsWith("dns."))
editor.remove(key);
editor.apply();
}
static void test(Context context) throws UnknownHostException {
test(context, "gmail.com", "ns");
test(context, "web.de", "mx");