mirror of
https://github.com/TwidereProject/Twidere-Android
synced 2025-01-30 08:25:01 +01:00
added option to disable builtin dns resolver
This commit is contained in:
parent
aee2e0d049
commit
3d51817ab1
@ -216,9 +216,12 @@ public interface SharedPreferenceConstants {
|
||||
@Preference(type = STRING, exportable = false)
|
||||
String KEY_COMPOSE_ACCOUNTS = "compose_accounts";
|
||||
@Preference(type = BOOLEAN, hasDefault = true, defaultBoolean = false)
|
||||
String KEY_BUILTIN_DNS_RESOLVER = "builtin_dns_resolver";
|
||||
@Preference(type = BOOLEAN, hasDefault = true, defaultBoolean = false)
|
||||
String KEY_TCP_DNS_QUERY = "tcp_dns_query";
|
||||
@Preference(type = STRING, hasDefault = true, defaultString = "")
|
||||
String KEY_DNS_SERVER = "dns_server";
|
||||
@Preference(type = INT, hasDefault = true, defaultInt = 10)
|
||||
String KEY_CONNECTION_TIMEOUT = "connection_timeout";
|
||||
@Preference(type = BOOLEAN, hasDefault = true, defaultBoolean = true)
|
||||
String KEY_NAME_FIRST = "name_first";
|
||||
|
@ -511,6 +511,7 @@
|
||||
<action android:name="android.service.dreams.DreamService"/>
|
||||
</intent-filter>
|
||||
</service>
|
||||
<service android:name="edu.tsinghua.hotmobi.UploadLogsService"/>
|
||||
|
||||
<provider
|
||||
android:name=".provider.TwidereDataProvider"
|
||||
|
@ -21,8 +21,8 @@ package edu.tsinghua.hotmobi;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.AsyncTask;
|
||||
import android.preference.Preference;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
@ -48,6 +48,6 @@ public class UploadLogsPreferences extends Preference {
|
||||
final Context context = getContext();
|
||||
final SharedPreferences prefs = context.getSharedPreferences("spice_data_profiling", Context.MODE_PRIVATE);
|
||||
prefs.edit().remove(HotMobiConstants.KEY_LAST_UPLOAD_TIME).apply();
|
||||
AsyncTask.execute(new UploadLogsTask(context.getApplicationContext()));
|
||||
context.startService(new Intent(context, UploadLogsService.class));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
package edu.tsinghua.hotmobi;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Intent;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
import org.mariotaku.twidere.BuildConfig;
|
||||
import org.mariotaku.twidere.R;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 16/2/10.
|
||||
*/
|
||||
public class UploadLogsService extends IntentService {
|
||||
private static final int NOTIFICATION_ID_UPLOAD_LOG = 301;
|
||||
|
||||
/**
|
||||
* Creates an IntentService. Invoked by your subclass's constructor.
|
||||
*/
|
||||
public UploadLogsService() {
|
||||
super("hotmobi_log_uploader");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
|
||||
builder.setTicker("Uploading logs");
|
||||
builder.setContentTitle("HotMobi research project");
|
||||
builder.setContentText("Uploading logs");
|
||||
builder.setProgress(0, 0, true);
|
||||
builder.setSmallIcon(R.drawable.ic_stat_info, 0);
|
||||
builder.setOngoing(true);
|
||||
final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
nm.notify(NOTIFICATION_ID_UPLOAD_LOG, builder.build());
|
||||
uploadLogs();
|
||||
nm.cancel(NOTIFICATION_ID_UPLOAD_LOG);
|
||||
} else {
|
||||
uploadLogs();
|
||||
}
|
||||
}
|
||||
|
||||
private void uploadLogs() {
|
||||
final UploadLogsTask task = new UploadLogsTask(this);
|
||||
task.run();
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package org.mariotaku.twidere.fragment;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.Network;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
@ -22,9 +23,11 @@ import org.mariotaku.twidere.api.twitter.TwitterException;
|
||||
import org.mariotaku.twidere.api.twitter.model.Paging;
|
||||
import org.mariotaku.twidere.model.ParcelableCredentials;
|
||||
import org.mariotaku.twidere.util.DataStoreUtils;
|
||||
import org.mariotaku.twidere.util.SharedPreferencesWrapper;
|
||||
import org.mariotaku.twidere.util.TwitterAPIFactory;
|
||||
import org.mariotaku.twidere.util.dagger.DependencyHolder;
|
||||
import org.mariotaku.twidere.util.net.TwidereDns;
|
||||
import org.xbill.DNS.ResolverConfig;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.net.InetAddress;
|
||||
@ -99,7 +102,24 @@ public class NetworkDiagnosticsFragment extends BaseFragment {
|
||||
publishProgress(new LogText("Text below may have personal information, BE CAREFUL TO MAKE IT PUBLIC"));
|
||||
publishProgress(LogText.LINEBREAK, LogText.LINEBREAK);
|
||||
DependencyHolder holder = DependencyHolder.get(mContext);
|
||||
Dns dns = holder.getDns();
|
||||
final Dns dns = holder.getDns();
|
||||
final SharedPreferencesWrapper prefs = holder.getPreferences();
|
||||
publishProgress(new LogText("Network preferences"), LogText.LINEBREAK);
|
||||
publishProgress(new LogText("using_resolver: " + prefs.getBoolean(KEY_BUILTIN_DNS_RESOLVER)), LogText.LINEBREAK);
|
||||
publishProgress(new LogText("tcp_dns_query: " + prefs.getBoolean(KEY_TCP_DNS_QUERY)), LogText.LINEBREAK);
|
||||
publishProgress(new LogText("dns_server: " + prefs.getString(KEY_DNS_SERVER, null)), LogText.LINEBREAK);
|
||||
publishProgress(LogText.LINEBREAK);
|
||||
publishProgress(new LogText("System DNS servers"), LogText.LINEBREAK);
|
||||
|
||||
|
||||
final String[] servers = ResolverConfig.getCurrentConfig().servers();
|
||||
if (servers != null) {
|
||||
publishProgress(new LogText(Arrays.toString(servers)));
|
||||
} else {
|
||||
publishProgress(new LogText("null"));
|
||||
}
|
||||
publishProgress(LogText.LINEBREAK, LogText.LINEBREAK);
|
||||
|
||||
for (long accountId : DataStoreUtils.getAccountIds(mContext)) {
|
||||
final ParcelableCredentials credentials = ParcelableCredentials.getCredentials(mContext, accountId);
|
||||
final Twitter twitter = TwitterAPIFactory.getTwitterInstance(mContext, accountId, false);
|
||||
@ -113,7 +133,7 @@ public class NetworkDiagnosticsFragment extends BaseFragment {
|
||||
publishProgress(LogText.LINEBREAK, LogText.LINEBREAK);
|
||||
|
||||
publishProgress(new LogText("Testing DNS functionality"));
|
||||
publishProgress(LogText.LINEBREAK, LogText.LINEBREAK);
|
||||
publishProgress(LogText.LINEBREAK);
|
||||
final Endpoint endpoint = TwitterAPIFactory.getEndpoint(credentials, Twitter.class);
|
||||
final Uri uri = Uri.parse(endpoint.getUrl());
|
||||
final String host = uri.getHost();
|
||||
|
@ -24,7 +24,6 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.v4.net.ConnectivityManagerCompat;
|
||||
import android.util.Log;
|
||||
|
||||
@ -34,7 +33,7 @@ import org.mariotaku.twidere.app.TwidereApplication;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
|
||||
import edu.tsinghua.hotmobi.HotMobiLogger;
|
||||
import edu.tsinghua.hotmobi.UploadLogsTask;
|
||||
import edu.tsinghua.hotmobi.UploadLogsService;
|
||||
import edu.tsinghua.hotmobi.model.NetworkEvent;
|
||||
|
||||
public class ConnectivityStateReceiver extends BroadcastReceiver implements Constants {
|
||||
@ -67,7 +66,7 @@ public class ConnectivityStateReceiver extends BroadcastReceiver implements Cons
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
final long lastSuccessfulTime = HotMobiLogger.getLastUploadTime(appContext);
|
||||
if ((currentTime - lastSuccessfulTime) > HotMobiLogger.UPLOAD_INTERVAL_MILLIS) {
|
||||
AsyncTask.execute(new UploadLogsTask(appContext));
|
||||
appContext.startService(new Intent(appContext, UploadLogsService.class));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -196,8 +196,8 @@ public class ApplicationModule implements Constants {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public Dns dns() {
|
||||
return new TwidereDns(application);
|
||||
public Dns dns(SharedPreferencesWrapper preferences) {
|
||||
return new TwidereDns(application, preferences);
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
@ -32,7 +32,6 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.mariotaku.twidere.BuildConfig;
|
||||
import org.mariotaku.twidere.Constants;
|
||||
import org.mariotaku.twidere.util.SharedPreferencesWrapper;
|
||||
import org.mariotaku.twidere.util.TwidereMathUtils;
|
||||
import org.xbill.DNS.AAAARecord;
|
||||
import org.xbill.DNS.ARecord;
|
||||
import org.xbill.DNS.Address;
|
||||
@ -49,7 +48,6 @@ import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@ -59,28 +57,27 @@ import okhttp3.Dns;
|
||||
public class TwidereDns implements Constants, Dns {
|
||||
|
||||
private static final String RESOLVER_LOGTAG = "TwidereDns";
|
||||
private static final boolean CHECK_ADDRESS = Boolean.parseBoolean("false");
|
||||
|
||||
private final SharedPreferences mHostMapping, mPreferences;
|
||||
private final SharedPreferences mHostMapping;
|
||||
private final SharedPreferencesWrapper mPreferences;
|
||||
private final SystemHosts mSystemHosts;
|
||||
|
||||
private Resolver mResolver;
|
||||
private TimingLogger mLogger;
|
||||
private long mConnectTimeout;
|
||||
private boolean mUseResolver;
|
||||
|
||||
public TwidereDns(final Context context) {
|
||||
public TwidereDns(final Context context, SharedPreferencesWrapper preferences) {
|
||||
mLogger = new TimingLogger(RESOLVER_LOGTAG, "resolve");
|
||||
mHostMapping = SharedPreferencesWrapper.getInstance(context, HOST_MAPPING_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
mPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
mSystemHosts = new SystemHosts();
|
||||
mPreferences = preferences;
|
||||
reloadDnsSettings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized List<InetAddress> lookup(String hostname) throws UnknownHostException {
|
||||
try {
|
||||
return Arrays.asList(resolveInternal(hostname, hostname, 0, true, mUseResolver));
|
||||
return Arrays.asList(resolveInternal(hostname, hostname, 0, mUseResolver));
|
||||
} catch (IOException e) {
|
||||
if (e instanceof UnknownHostException) throw (UnknownHostException) e;
|
||||
throw new UnknownHostException("Unable to resolve address " + e.getMessage());
|
||||
@ -89,7 +86,7 @@ public class TwidereDns implements Constants, Dns {
|
||||
|
||||
public synchronized List<InetAddress> lookupResolver(String hostname) throws UnknownHostException {
|
||||
try {
|
||||
return Arrays.asList(resolveInternal(hostname, hostname, 0, true, true));
|
||||
return Arrays.asList(resolveInternal(hostname, hostname, 0, true));
|
||||
} catch (IOException e) {
|
||||
if (e instanceof UnknownHostException) throw (UnknownHostException) e;
|
||||
throw new UnknownHostException("Unable to resolve address " + e.getMessage());
|
||||
@ -98,13 +95,12 @@ public class TwidereDns implements Constants, Dns {
|
||||
|
||||
public synchronized void reloadDnsSettings() {
|
||||
mResolver = null;
|
||||
mUseResolver = !TextUtils.isEmpty(mPreferences.getString(KEY_DNS_SERVER, null));
|
||||
mConnectTimeout = TimeUnit.SECONDS.toMillis(mPreferences.getInt(KEY_CONNECTION_TIMEOUT, 10));
|
||||
mUseResolver = mPreferences.getBoolean(KEY_BUILTIN_DNS_RESOLVER);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private InetAddress[] resolveInternal(final String originalHost, final String host, final int depth,
|
||||
final boolean putCache, final boolean useResolver) throws IOException {
|
||||
final boolean useResolver) throws IOException {
|
||||
resetLog(originalHost);
|
||||
// Return if host is an address
|
||||
final InetAddress[] fromAddressString = fromAddressString(originalHost, host);
|
||||
@ -125,18 +121,19 @@ public class TwidereDns implements Constants, Dns {
|
||||
}
|
||||
return fromMapping;
|
||||
}
|
||||
// Load from /etc/hosts
|
||||
addLogSplit(originalHost, host, "start /etc/hosts resolve", depth);
|
||||
final InetAddress[] fromSystemHosts = fromSystemHosts(host);
|
||||
addLogSplit(originalHost, host, "end /etc/hosts resolve", depth);
|
||||
if (fromSystemHosts != null) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
dumpLog(fromSystemHosts);
|
||||
}
|
||||
return fromSystemHosts;
|
||||
}
|
||||
// Use DNS resolver
|
||||
if (useResolver) {
|
||||
// Load from /etc/hosts, since Dnsjava doesn't support hosts entry lookup
|
||||
addLogSplit(originalHost, host, "start /etc/hosts resolve", depth);
|
||||
final InetAddress[] fromSystemHosts = fromSystemHosts(host);
|
||||
addLogSplit(originalHost, host, "end /etc/hosts resolve", depth);
|
||||
if (fromSystemHosts != null) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
dumpLog(fromSystemHosts);
|
||||
}
|
||||
return fromSystemHosts;
|
||||
}
|
||||
|
||||
// Use DNS resolver
|
||||
addLogSplit(originalHost, host, "start resolver resolve", depth);
|
||||
final InetAddress[] fromResolver = fromResolver(originalHost, host, depth);
|
||||
addLogSplit(originalHost, host, "end resolver resolve", depth);
|
||||
@ -202,11 +199,6 @@ public class TwidereDns implements Constants, Dns {
|
||||
return addrs;
|
||||
}
|
||||
|
||||
private boolean checkAddress(InetAddress inetAddress) throws IOException {
|
||||
if (!CHECK_ADDRESS) return true;
|
||||
return inetAddress.isReachable(TwidereMathUtils.clamp((int) mConnectTimeout / 2, 1000, 3000));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private InetAddress[] getFromMapping(final String host) throws UnknownHostException {
|
||||
return getFromMappingInternal(host, host, false);
|
||||
@ -238,7 +230,7 @@ public class TwidereDns implements Constants, Dns {
|
||||
final boolean tcp = mPreferences.getBoolean(KEY_TCP_DNS_QUERY, false);
|
||||
final String address = mPreferences.getString(KEY_DNS_SERVER, null);
|
||||
final SimpleResolver resolver;
|
||||
if (isValidIpAddress(address)) {
|
||||
if (!TextUtils.isEmpty(address) && isValidIpAddress(address)) {
|
||||
resolver = new SimpleResolver(address);
|
||||
} else {
|
||||
resolver = new SimpleResolver();
|
||||
@ -255,13 +247,16 @@ public class TwidereDns implements Constants, Dns {
|
||||
}
|
||||
|
||||
|
||||
private InetAddress[] fromAddressString(String host, String address) throws UnknownHostException {
|
||||
private InetAddress[] fromAddressString(String host, String address)
|
||||
throws UnknownHostException {
|
||||
final InetAddress resolved = getResolvedIPAddress(host, address);
|
||||
if (resolved == null) return null;
|
||||
return new InetAddress[]{resolved};
|
||||
}
|
||||
|
||||
public static InetAddress getResolvedIPAddress(String host, String address) throws UnknownHostException {
|
||||
public static InetAddress getResolvedIPAddress(@NonNull final String host,
|
||||
@NonNull final String address)
|
||||
throws UnknownHostException {
|
||||
byte[] bytes;
|
||||
bytes = Address.toByteArray(address, Address.IPv4);
|
||||
if (bytes != null)
|
||||
@ -272,7 +267,7 @@ public class TwidereDns implements Constants, Dns {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int getInetAddressType(String address) {
|
||||
private static int getInetAddressType(@NonNull final String address) {
|
||||
byte[] bytes;
|
||||
bytes = Address.toByteArray(address, Address.IPv4);
|
||||
if (bytes != null)
|
||||
@ -283,7 +278,7 @@ public class TwidereDns implements Constants, Dns {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static boolean isValidIpAddress(final String address) {
|
||||
public static boolean isValidIpAddress(@NonNull final String address) {
|
||||
return getInetAddressType(address) != 0;
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@ package org.mariotaku.twidere.util.net;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Looper;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import org.mariotaku.twidere.BuildConfig;
|
||||
@ -47,11 +48,12 @@ public class TwidereProxySelector extends ProxySelector {
|
||||
@Inject
|
||||
Dns dns;
|
||||
private final Proxy.Type type;
|
||||
@NonNull
|
||||
private final String host;
|
||||
private final int port;
|
||||
private List<Proxy> proxy;
|
||||
|
||||
public TwidereProxySelector(Context context, Proxy.Type type, String host, int port) {
|
||||
public TwidereProxySelector(Context context, Proxy.Type type, @NonNull final String host, int port) {
|
||||
GeneralComponentHelper.build(context).inject(this);
|
||||
this.type = type;
|
||||
this.host = host;
|
||||
|
@ -846,4 +846,5 @@
|
||||
<string name="interactions">Interactions</string>
|
||||
<string name="network_diagnostics">Network diagnostics</string>
|
||||
<string name="action_start">Start</string>
|
||||
<string name="builtin_dns_resolver">Builtin DNS resolver</string>
|
||||
</resources>
|
@ -7,13 +7,20 @@
|
||||
android:key="category_connectivity"
|
||||
android:title="@string/connectivity">
|
||||
|
||||
<org.mariotaku.twidere.preference.AutoFixSwitchPreference
|
||||
android:defaultValue="false"
|
||||
android:key="builtin_dns_resolver"
|
||||
android:title="@string/builtin_dns_resolver"/>
|
||||
|
||||
<org.mariotaku.twidere.preference.AutoFixCheckBoxPreference
|
||||
android:defaultValue="false"
|
||||
android:dependency="builtin_dns_resolver"
|
||||
android:key="tcp_dns_query"
|
||||
android:summary="@string/tcp_dns_query_summary"
|
||||
android:title="@string/tcp_dns_query"/>
|
||||
|
||||
<org.mariotaku.twidere.preference.SummaryEditTextPreference
|
||||
android:dependency="builtin_dns_resolver"
|
||||
android:dialogTitle="@string/dns_server"
|
||||
android:inputType="textVisiblePassword"
|
||||
android:key="dns_server"
|
||||
|
Loading…
x
Reference in New Issue
Block a user