mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-19 16:19:33 +01:00
Add fallback static DNS resolver.
This commit is contained in:
committed by
Alex Hart
parent
2483a92975
commit
56a8451d07
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user