give exec resolver a lower priority over reflection. fixes #2629

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java    |  9 
src/main/java/eu/siacs/conversations/utils/AndroidUsingExecLowPriority.java | 92 
src/main/java/eu/siacs/conversations/utils/Resolver.java                    | 11 
3 files changed, 98 insertions(+), 14 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -20,7 +20,6 @@ import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
 import android.os.IBinder;
-import android.os.ParcelFileDescriptor;
 import android.os.PowerManager;
 import android.os.PowerManager.WakeLock;
 import android.os.SystemClock;
@@ -40,16 +39,11 @@ import net.java.otr4j.session.Session;
 import net.java.otr4j.session.SessionID;
 import net.java.otr4j.session.SessionImpl;
 import net.java.otr4j.session.SessionStatus;
-import net.ypresto.androidtranscoder.MediaTranscoder;
-import net.ypresto.androidtranscoder.format.MediaFormatStrategy;
-import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets;
 
 import org.openintents.openpgp.IOpenPgpService2;
 import org.openintents.openpgp.util.OpenPgpApi;
 import org.openintents.openpgp.util.OpenPgpServiceConnection;
 
-import java.io.FileDescriptor;
-import java.io.FileNotFoundException;
 import java.math.BigInteger;
 import java.net.URL;
 import java.security.SecureRandom;
@@ -108,7 +102,6 @@ import eu.siacs.conversations.persistance.DatabaseBackend;
 import eu.siacs.conversations.persistance.FileBackend;
 import eu.siacs.conversations.ui.SettingsActivity;
 import eu.siacs.conversations.ui.UiCallback;
-import eu.siacs.conversations.ui.UiInformableCallback;
 import eu.siacs.conversations.utils.ConversationsFileObserver;
 import eu.siacs.conversations.utils.CryptoHelper;
 import eu.siacs.conversations.utils.ExceptionHelper;
@@ -909,7 +902,7 @@ public class XmppConnectionService extends Service {
 	public void onCreate() {
 		ExceptionHelper.init(getApplicationContext());
 		PRNGFixes.apply();
-		Resolver.registerXmppConnectionService(this);
+		Resolver.init(this);
 		this.mRandom = new SecureRandom();
 		updateMemorizingTrustmanager();
 		final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

src/main/java/eu/siacs/conversations/utils/AndroidUsingExecLowPriority.java 🔗

@@ -0,0 +1,92 @@
+/*
+ * Copyright 2015-2016 the original author or authors
+ *
+ * This software is licensed under the Apache License, Version 2.0,
+ * the GNU Lesser General Public License version 2 or later ("LGPL")
+ * and the WTFPL.
+ * You may choose either license to govern your use of this software only
+ * upon the condition that you accept all of the terms of either
+ * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
+ */
+package eu.siacs.conversations.utils;
+
+
+import de.measite.minidns.dnsserverlookup.AbstractDNSServerLookupMechanism;
+import de.measite.minidns.dnsserverlookup.AndroidUsingReflection;
+import de.measite.minidns.dnsserverlookup.DNSServerLookupMechanism;
+import de.measite.minidns.util.PlatformDetection;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.net.InetAddress;
+import java.util.HashSet;
+import java.util.logging.Level;
+
+/**
+ * Try to retrieve the list of DNS server by executing getprop.
+ */
+public class AndroidUsingExecLowPriority extends AbstractDNSServerLookupMechanism {
+
+	public static final DNSServerLookupMechanism INSTANCE = new AndroidUsingExecLowPriority();
+	public static final int PRIORITY = AndroidUsingReflection.PRIORITY + 1;
+
+	private AndroidUsingExecLowPriority() {
+		super(AndroidUsingExecLowPriority.class.getSimpleName(), PRIORITY);
+	}
+
+	@Override
+	public String[] getDnsServerAddresses() {
+		try {
+			Process process = Runtime.getRuntime().exec("getprop");
+			InputStream inputStream = process.getInputStream();
+			LineNumberReader lnr = new LineNumberReader(
+					new InputStreamReader(inputStream));
+			String line;
+			HashSet<String> server = new HashSet<>(6);
+			while ((line = lnr.readLine()) != null) {
+				int split = line.indexOf("]: [");
+				if (split == -1) {
+					continue;
+				}
+				String property = line.substring(1, split);
+				String value = line.substring(split + 4, line.length() - 1);
+
+				if (value.isEmpty()) {
+					continue;
+				}
+
+				if (property.endsWith(".dns") || property.endsWith(".dns1") ||
+						property.endsWith(".dns2") || property.endsWith(".dns3") ||
+						property.endsWith(".dns4")) {
+
+					// normalize the address
+
+					InetAddress ip = InetAddress.getByName(value);
+
+					if (ip == null) continue;
+
+					value = ip.getHostAddress();
+
+					if (value == null) continue;
+					if (value.length() == 0) continue;
+
+					server.add(value);
+				}
+			}
+			if (server.size() > 0) {
+				return server.toArray(new String[server.size()]);
+			}
+		} catch (IOException e) {
+			LOGGER.log(Level.WARNING, "Exception in findDNSByExec", e);
+		}
+		return null;
+	}
+
+	@Override
+	public boolean isAvailable() {
+		return PlatformDetection.isAndroid();
+	}
+
+}

src/main/java/eu/siacs/conversations/utils/Resolver.java 🔗

@@ -15,6 +15,7 @@ import java.util.List;
 import de.measite.minidns.DNSClient;
 import de.measite.minidns.DNSName;
 import de.measite.minidns.dnssec.DNSSECResultNotAuthenticException;
+import de.measite.minidns.dnsserverlookup.AndroidUsingExec;
 import de.measite.minidns.hla.DnssecResolverApi;
 import de.measite.minidns.hla.ResolverApi;
 import de.measite.minidns.hla.ResolverResult;
@@ -39,13 +40,11 @@ public class Resolver {
     private static XmppConnectionService SERVICE = null;
 
 
-    public static void registerXmppConnectionService(XmppConnectionService service) {
+    public static void init(XmppConnectionService service) {
         Resolver.SERVICE = service;
-        registerLookupMechanism(service);
-    }
-
-    private static void registerLookupMechanism(Context context) {
-        DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(context));
+        DNSClient.removeDNSServerLookupMechanism(AndroidUsingExec.INSTANCE);
+        DNSClient.addDnsServerLookupMechanism(AndroidUsingExecLowPriority.INSTANCE);
+        DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(service));
     }
 
     public static List<Result> resolve(String domain) throws NetworkIsUnreachableException {