Add fallback static DNS resolver.

This commit is contained in:
Greyson Parrelli
2022-03-02 14:32:24 -05:00
committed by Alex Hart
parent 2483a92975
commit 56a8451d07
12 changed files with 198 additions and 39 deletions

View File

@@ -5,6 +5,8 @@ import androidx.annotation.NonNull;
import com.annimon.stream.Stream;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.util.NetworkUtil;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
@@ -14,6 +16,7 @@ import org.xbill.DNS.Type;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import okhttp3.Dns;

View File

@@ -3,6 +3,8 @@ package org.thoughtcrime.securesms.net;
import androidx.annotation.NonNull;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.util.NetworkUtil;
import java.net.InetAddress;
import java.net.UnknownHostException;
@@ -36,10 +38,10 @@ public class SequentialDns implements Dns {
Log.w(TAG, String.format(Locale.ENGLISH, "Didn't find any addresses for %s using %s. Continuing.", hostname, dns.getClass().getSimpleName()));
}
} catch (UnknownHostException e) {
Log.w(TAG, String.format(Locale.ENGLISH, "Failed to resolve %s using %s. Continuing.", hostname, dns.getClass().getSimpleName()));
Log.w(TAG, String.format(Locale.ENGLISH, "Failed to resolve %s using %s. Continuing. Network Type: %s", hostname, dns.getClass().getSimpleName(), NetworkUtil.getNetworkTypeDescriptor(ApplicationDependencies.getApplication())));
}
}
Log.w(TAG, "Failed to resolve using any DNS.");
Log.w(TAG, "Failed to resolve using any DNS. Network Type: " + NetworkUtil.getNetworkTypeDescriptor(ApplicationDependencies.getApplication()));
throw new UnknownHostException(hostname);
}
}

View File

@@ -1,35 +0,0 @@
package org.thoughtcrime.securesms.net;
import androidx.annotation.NonNull;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import okhttp3.Dns;
/**
* A super simple {@link Dns} implementation that maps hostnames to a static IP addresses.
*/
public class StaticDns implements Dns {
private final Map<String, String> hostnameMap;
public StaticDns(@NonNull Map<String, String> hostnameMap) {
this.hostnameMap = hostnameMap;
}
@Override
public @NonNull List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException {
String ip = hostnameMap.get(hostname);
if (ip != null) {
return Collections.singletonList(InetAddress.getByName(ip));
} else {
throw new UnknownHostException(hostname);
}
}
}

View File

@@ -0,0 +1,22 @@
package org.thoughtcrime.securesms.net
import okhttp3.Dns
import java.net.InetAddress
import java.net.UnknownHostException
/**
* A super simple [Dns] implementation that maps hostnames to a static IP addresses.
*/
class StaticDns(private val hostnameMap: Map<String, Set<String>>) : Dns {
@Throws(UnknownHostException::class)
override fun lookup(hostname: String): List<InetAddress> {
val ips = hostnameMap[hostname]
return if (ips != null && ips.isNotEmpty()) {
listOf(InetAddress.getByName(ips.random()))
} else {
throw UnknownHostException(hostname)
}
}
}