Refactoring

pull/214/head
M66B 8 months ago
parent 93f81499cf
commit bfc1900b90

@ -565,7 +565,7 @@ public class ActivityDMARC extends ActivityBase {
} }
try { try {
InetAddress addr = InetAddress.getByName(text); InetAddress addr = DnsHelper.getByName(context, text);
IPInfo info = IPInfo.getOrganization(addr, context); IPInfo info = IPInfo.getOrganization(addr, context);
ssb.append('(').append(info.org).append(") "); ssb.append('(').append(info.org).append(") ");
} catch (Throwable ex) { } catch (Throwable ex) {

@ -205,7 +205,7 @@ public class DnsBlockList {
for (BlockList blocklist : blocklists) for (BlockList blocklist : blocklists)
if (isEnabled(context, blocklist) && if (isEnabled(context, blocklist) &&
blocklist.numeric == numeric && blocklist.numeric == numeric &&
isJunk(host, blocklist)) { isJunk(context, host, blocklist)) {
blocked = true; blocked = true;
break; break;
} }
@ -217,11 +217,11 @@ public class DnsBlockList {
return blocked; return blocked;
} }
private static boolean isJunk(String host, BlockList blocklist) { private static boolean isJunk(Context context, String host, BlockList blocklist) {
try { try {
if (blocklist.numeric) { if (blocklist.numeric) {
long start = new Date().getTime(); long start = new Date().getTime();
InetAddress[] addresses = InetAddress.getAllByName(host); InetAddress[] addresses = DnsHelper.getAllByName(context, host);
long elapsed = new Date().getTime() - start; long elapsed = new Date().getTime() - start;
Log.i("isJunk resolved=" + host + " elapse=" + elapsed + " ms"); Log.i("isJunk resolved=" + host + " elapse=" + elapsed + " ms");
for (InetAddress addr : addresses) { for (InetAddress addr : addresses) {
@ -249,7 +249,7 @@ public class DnsBlockList {
lookup.append(blocklist.address); lookup.append(blocklist.address);
if (isJunk(lookup.toString(), blocklist.responses)) if (isJunk(context, lookup.toString(), blocklist.responses))
return true; return true;
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
@ -258,7 +258,7 @@ public class DnsBlockList {
} else { } else {
long start = new Date().getTime(); long start = new Date().getTime();
String lookup = host + "." + blocklist.address; String lookup = host + "." + blocklist.address;
boolean junk = isJunk(lookup, blocklist.responses); boolean junk = isJunk(context, lookup, blocklist.responses);
long elapsed = new Date().getTime() - start; long elapsed = new Date().getTime() - start;
Log.i("isJunk" + " " + lookup + "=" + junk + " elapsed=" + elapsed); Log.i("isJunk" + " " + lookup + "=" + junk + " elapsed=" + elapsed);
return junk; return junk;
@ -270,12 +270,12 @@ public class DnsBlockList {
return false; return false;
} }
private static boolean isJunk(String lookup, InetAddress[] responses) { private static boolean isJunk(Context context, String lookup, InetAddress[] responses) {
long start = new Date().getTime(); long start = new Date().getTime();
InetAddress result; InetAddress result;
try { try {
// Possibly blocked // Possibly blocked
result = InetAddress.getByName(lookup); result = DnsHelper.getByName(context, lookup);
} catch (UnknownHostException ignored) { } catch (UnknownHostException ignored) {
// Not blocked // Not blocked
result = null; result = null;

@ -351,6 +351,14 @@ public class DnsHelper {
return result; return result;
} }
static InetAddress getByName(Context context, String host) throws UnknownHostException {
return InetAddress.getByName(host);
}
static InetAddress[] getAllByName(Context context, String host) throws UnknownHostException {
return InetAddress.getAllByName(host);
}
static void verifyDane(X509Certificate[] chain, String server, int port) throws CertificateException { static void verifyDane(X509Certificate[] chain, String server, int port) throws CertificateException {
Handler handler = new Handler() { Handler handler = new Handler() {
@Override @Override

@ -436,7 +436,7 @@ public class EmailProvider implements Parcelable {
if (false) // Unsafe: the password could be sent to an unrelated email server if (false) // Unsafe: the password could be sent to an unrelated email server
try { try {
InetAddress iaddr = InetAddress.getByName(domain.toLowerCase(Locale.ROOT)); InetAddress iaddr = DnsHelper.getByName(context, domain.toLowerCase(Locale.ROOT));
List<String> commonNames = List<String> commonNames =
ConnectionHelper.getCommonNames(context, domain.toLowerCase(Locale.ROOT), 443, SCAN_TIMEOUT); ConnectionHelper.getCommonNames(context, domain.toLowerCase(Locale.ROOT), 443, SCAN_TIMEOUT);
EntityLog.log(context, "Website common names=" + TextUtils.join(",", commonNames)); EntityLog.log(context, "Website common names=" + TextUtils.join(",", commonNames));
@ -450,7 +450,7 @@ public class EmailProvider implements Parcelable {
if (!altName.equalsIgnoreCase(domain)) if (!altName.equalsIgnoreCase(domain))
try { try {
InetAddress ialt = InetAddress.getByName(altName); InetAddress ialt = DnsHelper.getByName(context, altName);
if (!ialt.equals(iaddr)) { if (!ialt.equals(iaddr)) {
EntityLog.log(context, "Using website common name=" + altName); EntityLog.log(context, "Using website common name=" + altName);
candidates.addAll(_fromDomain(context, altName.toLowerCase(Locale.ROOT), email, discover, intf)); candidates.addAll(_fromDomain(context, altName.toLowerCase(Locale.ROOT), email, discover, intf));
@ -509,7 +509,7 @@ public class EmailProvider implements Parcelable {
for (DnsHelper.DnsRecord record : records) for (DnsHelper.DnsRecord record : records)
try { try {
String target = record.response.toLowerCase(Locale.ROOT); String target = record.response.toLowerCase(Locale.ROOT);
InetAddress.getByName(target); DnsHelper.getByName(context, target);
EmailProvider mx1 = new EmailProvider(domain); EmailProvider mx1 = new EmailProvider(domain);
mx1.imap.score = 0; mx1.imap.score = 0;
@ -1340,7 +1340,7 @@ public class EmailProvider implements Parcelable {
@Override @Override
public Boolean call() { public Boolean call() {
try { try {
for (InetAddress iaddr : InetAddress.getAllByName(host)) { for (InetAddress iaddr : DnsHelper.getAllByName(context, host)) {
InetSocketAddress address = new InetSocketAddress(iaddr, Server.this.port); InetSocketAddress address = new InetSocketAddress(iaddr, Server.this.port);
SocketFactory factory = (starttls SocketFactory factory = (starttls
@ -1380,7 +1380,7 @@ public class EmailProvider implements Parcelable {
String similar = name; String similar = name;
if (similar.startsWith("*.")) if (similar.startsWith("*."))
similar = similar.substring(2); similar = similar.substring(2);
InetAddress isimilar = InetAddress.getByName(similar); InetAddress isimilar = DnsHelper.getByName(context, similar);
if (iaddr.equals(isimilar)) { if (iaddr.equals(isimilar)) {
score += 1; score += 1;
EntityLog.log(context, "Similar " + similar + " host=" + host); EntityLog.log(context, "Similar " + similar + " host=" + host);

@ -623,7 +623,7 @@ public class EmailService implements AutoCloseable {
String key = "dns." + host; String key = "dns." + host;
try { try {
main = InetAddress.getByName(host); main = DnsHelper.getByName(context, host);
EntityLog.log(context, EntityLog.Type.Network, "Main address=" + main); EntityLog.log(context, EntityLog.Type.Network, "Main address=" + main);
prefs.edit().putString(key, main.getHostAddress()).apply(); prefs.edit().putString(key, main.getHostAddress()).apply();
} catch (UnknownHostException ex) { } catch (UnknownHostException ex) {
@ -632,7 +632,7 @@ public class EmailService implements AutoCloseable {
throw ex; throw ex;
else { else {
EntityLog.log(context, EntityLog.Type.Network, "Using " + key + "=" + last); EntityLog.log(context, EntityLog.Type.Network, "Using " + key + "=" + last);
main = InetAddress.getByName(last); main = DnsHelper.getByName(context, last);
} }
} }
@ -641,7 +641,7 @@ public class EmailService implements AutoCloseable {
boolean[] has46 = ConnectionHelper.has46(context); boolean[] has46 = ConnectionHelper.has46(context);
if (has46[0]) if (has46[0])
try { try {
for (InetAddress iaddr : InetAddress.getAllByName(host)) for (InetAddress iaddr : DnsHelper.getAllByName(context, host))
if (iaddr instanceof Inet4Address) { if (iaddr instanceof Inet4Address) {
main = iaddr; main = iaddr;
EntityLog.log(context, EntityLog.Type.Network, "Preferring=" + main); EntityLog.log(context, EntityLog.Type.Network, "Preferring=" + main);
@ -729,7 +729,7 @@ public class EmailService implements AutoCloseable {
ex + "\n" + android.util.Log.getStackTraceString(ex)); ex + "\n" + android.util.Log.getStackTraceString(ex));
try { try {
// Some devices resolve IPv6 addresses while not having IPv6 connectivity // Some devices resolve IPv6 addresses while not having IPv6 connectivity
InetAddress[] iaddrs = InetAddress.getAllByName(host); InetAddress[] iaddrs = DnsHelper.getAllByName(context, host);
int ip4 = (main instanceof Inet4Address ? 1 : 0); int ip4 = (main instanceof Inet4Address ? 1 : 0);
int ip6 = (main instanceof Inet6Address ? 1 : 0); int ip6 = (main instanceof Inet6Address ? 1 : 0);

@ -61,7 +61,7 @@ public class IPInfo {
Log.i(ex); Log.i(ex);
} }
InetAddress address = InetAddress.getByName(host); InetAddress address = DnsHelper.getByName(context, host);
return new Pair<>(address, getOrganization(address, context)); return new Pair<>(address, getOrganization(address, context));
} }

@ -137,7 +137,7 @@ public class SSLHelper {
// Fallback: check server/certificate IP address // Fallback: check server/certificate IP address
if (!cert_strict) if (!cert_strict)
try { try {
InetAddress ip = InetAddress.getByName(server); InetAddress ip = DnsHelper.getByName(context, server);
Log.i("Checking server ip=" + ip); Log.i("Checking server ip=" + ip);
for (String name : names) { for (String name : names) {
if (name.startsWith("*.")) if (name.startsWith("*."))
@ -145,7 +145,7 @@ public class SSLHelper {
Log.i("Checking cert name=" + name); Log.i("Checking cert name=" + name);
try { try {
for (InetAddress addr : InetAddress.getAllByName(name)) for (InetAddress addr : DnsHelper.getAllByName(context, name))
if (Arrays.equals(ip.getAddress(), addr.getAddress())) { if (Arrays.equals(ip.getAddress(), addr.getAddress())) {
Log.i("Accepted " + name + " for " + server); Log.i("Accepted " + name + " for " + server);
return; return;

Loading…
Cancel
Save