Replaced getByName/getAllByName

pull/214/head
M66B 8 months ago
parent b5f19f9e2d
commit 94c6fca5dc

@ -48,7 +48,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.util.ArrayList;
@ -566,7 +565,7 @@ public class ActivityDMARC extends ActivityBase {
}
try {
InetAddress addr = InetAddress.getByName(text);
InetAddress addr = DnsHelper.getByName(context, text);
IPInfo info = IPInfo.getOrganization(addr, context);
ssb.append('(').append(info.org).append(") ");
} catch (Throwable ex) {

@ -49,6 +49,8 @@ import org.minidns.record.TXT;
import org.minidns.source.AbstractDnsDataSource;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
@ -275,6 +277,37 @@ public class DnsHelper {
}
}
static InetAddress getByName(Context context, String host) throws UnknownHostException {
return getAllByName(context, host)[0];
}
static InetAddress[] getAllByName(Context context, String host) throws UnknownHostException {
List<InetAddress> result = new ArrayList<>();
boolean[] has46 = ConnectionHelper.has46(context);
if (has46[0])
for (DnsRecord a : lookup(context, host, "a"))
try {
result.add(Inet4Address.getByName(a.response));
} catch (UnknownHostException ex) {
Log.e(ex);
}
if (has46[1])
for (DnsRecord aaaa : lookup(context, host, "aaaa"))
try {
result.add(Inet6Address.getByName(aaaa.response));
} catch (UnknownHostException ex) {
Log.e(ex);
}
if (result.size() == 0)
throw new UnknownHostException(host);
else
return result.toArray(new InetAddress[0]);
}
private static List<String> getDnsServers(Context context) {
List<String> result = new ArrayList<>();
result.add(DEFAULT_DNS);

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

@ -617,7 +617,7 @@ public class EmailService implements AutoCloseable {
String key = "dns." + host;
try {
main = InetAddress.getByName(host);
main = DnsHelper.getByName(context, host);
EntityLog.log(context, EntityLog.Type.Network, "Main address=" + main);
prefs.edit().putString(key, main.getHostAddress()).apply();
} catch (UnknownHostException ex) {
@ -626,7 +626,7 @@ public class EmailService implements AutoCloseable {
throw ex;
else {
EntityLog.log(context, EntityLog.Type.Network, "Using " + key + "=" + last);
main = InetAddress.getByName(last);
main = DnsHelper.getByName(context, last);
}
}
@ -635,7 +635,7 @@ public class EmailService implements AutoCloseable {
boolean[] has46 = ConnectionHelper.has46(context);
if (has46[0])
try {
for (InetAddress iaddr : InetAddress.getAllByName(host))
for (InetAddress iaddr : DnsHelper.getAllByName(context, host))
if (iaddr instanceof Inet4Address) {
main = iaddr;
EntityLog.log(context, EntityLog.Type.Network, "Preferring=" + main);
@ -723,7 +723,7 @@ public class EmailService implements AutoCloseable {
ex + "\n" + android.util.Log.getStackTraceString(ex));
try {
// 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 ip6 = (main instanceof Inet6Address ? 1 : 0);

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

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

Loading…
Cancel
Save