mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-03-02 23:38:34 +00: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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import org.thoughtcrime.securesms.net.DeviceTransferBlockingInterceptor
|
||||
import org.thoughtcrime.securesms.net.RemoteDeprecationDetectorInterceptor
|
||||
import org.thoughtcrime.securesms.net.SequentialDns
|
||||
import org.thoughtcrime.securesms.net.StandardUserAgentInterceptor
|
||||
import org.thoughtcrime.securesms.net.StaticDns
|
||||
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter
|
||||
import org.thoughtcrime.securesms.util.Base64
|
||||
import org.whispersystems.libsignal.util.guava.Optional
|
||||
@@ -38,7 +39,26 @@ class SignalServiceNetworkAccess(context: Context) {
|
||||
private val TAG = Log.tag(SignalServiceNetworkAccess::class.java)
|
||||
|
||||
@JvmField
|
||||
val DNS: Dns = SequentialDns(Dns.SYSTEM, CustomDns("1.1.1.1"))
|
||||
val DNS: Dns = SequentialDns(
|
||||
Dns.SYSTEM,
|
||||
CustomDns("1.1.1.1"),
|
||||
StaticDns(
|
||||
mapOf(
|
||||
BuildConfig.SIGNAL_URL.stripProtocol() to BuildConfig.SIGNAL_SERVICE_IPS.toSet(),
|
||||
BuildConfig.STORAGE_URL.stripProtocol() to BuildConfig.SIGNAL_STORAGE_IPS.toSet(),
|
||||
BuildConfig.SIGNAL_CDN_URL.stripProtocol() to BuildConfig.SIGNAL_CDN_IPS.toSet(),
|
||||
BuildConfig.SIGNAL_CDN2_URL.stripProtocol() to BuildConfig.SIGNAL_CDN2_IPS.toSet(),
|
||||
BuildConfig.SIGNAL_CONTACT_DISCOVERY_URL.stripProtocol() to BuildConfig.SIGNAL_CDS_IPS.toSet(),
|
||||
BuildConfig.SIGNAL_KEY_BACKUP_URL.stripProtocol() to BuildConfig.SIGNAL_KBS_IPS.toSet(),
|
||||
BuildConfig.SIGNAL_SFU_URL.stripProtocol() to BuildConfig.SIGNAL_SFU_IPS.toSet(),
|
||||
BuildConfig.CONTENT_PROXY_HOST.stripProtocol() to BuildConfig.SIGNAL_CONTENT_PROXY_IPS.toSet(),
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
private fun String.stripProtocol(): String {
|
||||
return this.removePrefix("https://")
|
||||
}
|
||||
|
||||
private const val COUNTRY_CODE_EGYPT = 20
|
||||
private const val COUNTRY_CODE_UAE = 971
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.util;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
@@ -37,6 +38,42 @@ public final class NetworkUtil {
|
||||
return useLowBandwidthCalling(context, networkAdapter) ? CallManager.BandwidthMode.LOW : CallManager.BandwidthMode.NORMAL;
|
||||
}
|
||||
|
||||
public static String getNetworkTypeDescriptor(@NonNull Context context) {
|
||||
NetworkInfo info = getNetworkInfo(context);
|
||||
if (info == null || !info.isConnected()) {
|
||||
return "NOT CONNECTED";
|
||||
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
|
||||
return "WIFI";
|
||||
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
|
||||
int networkType = info.getSubtype();
|
||||
switch (networkType) {
|
||||
case TelephonyManager.NETWORK_TYPE_GPRS: return "MOBILE - GPRS";
|
||||
case TelephonyManager.NETWORK_TYPE_EDGE: return "MOBILE - EDGE";
|
||||
case TelephonyManager.NETWORK_TYPE_CDMA: return "MOBILE - CDMA";
|
||||
case TelephonyManager.NETWORK_TYPE_1xRTT: return "MOBILE - 1xRTT";
|
||||
case TelephonyManager.NETWORK_TYPE_IDEN: return "MOBILE - IDEN";
|
||||
case TelephonyManager.NETWORK_TYPE_GSM: return "MOBILE - GSM";
|
||||
case TelephonyManager.NETWORK_TYPE_UMTS: return "MOBILE - UMTS";
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_0: return "MOBILE - EVDO_0";
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_A: return "MOBILE - EVDO_A";
|
||||
case TelephonyManager.NETWORK_TYPE_HSDPA: return "MOBILE - HSDPA";
|
||||
case TelephonyManager.NETWORK_TYPE_HSUPA: return "MOBILE - HSUPA";
|
||||
case TelephonyManager.NETWORK_TYPE_HSPA: return "MOBILE - HSPA";
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_B: return "MOBILE - EVDO_B";
|
||||
case TelephonyManager.NETWORK_TYPE_EHRPD: return "MOBILE - EHRDP";
|
||||
case TelephonyManager.NETWORK_TYPE_HSPAP: return "MOBILE - HSPAP";
|
||||
case TelephonyManager.NETWORK_TYPE_TD_SCDMA: return "MOBILE - TD_SCDMA";
|
||||
case TelephonyManager.NETWORK_TYPE_LTE: return "MOBILE - LTE";
|
||||
case TelephonyManager.NETWORK_TYPE_IWLAN: return "MOBILE - IWLAN";
|
||||
case 19: return "MOBILE - LTE_CA";
|
||||
case TelephonyManager.NETWORK_TYPE_NR: return "MOBILE - NR";
|
||||
default: return "MOBILE - OTHER";
|
||||
}
|
||||
} else {
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean useLowBandwidthCalling(@NonNull Context context, @NonNull PeerConnection.AdapterType networkAdapter) {
|
||||
switch (SignalStore.settings().getCallBandwidthMode()) {
|
||||
case HIGH_ON_WIFI:
|
||||
|
||||
Reference in New Issue
Block a user