Merge branch 'proxy-on-app-start' into 'master'

Proxy on app start

See merge request fdroid/fdroidclient!815
This commit is contained in:
Hans-Christoph Steiner 2019-06-12 12:47:03 +00:00
commit 736341b34e
6 changed files with 64 additions and 62 deletions

View File

@ -4,6 +4,7 @@
* Copyright (C) 2014-2018 Hans-Christoph Steiner <hans@eds.org>
* Copyright (C) 2015-2016 Daniel Martí <mvdan@mvdan.cc>
* Copyright (c) 2018 Senecto Limited
* Copyright (C) 2019 Michael Pöhn <michael.poehn@fsfe.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -388,7 +389,8 @@ public class FDroidApp extends Application {
PRNGFixes.apply();
curTheme = preferences.getTheme();
preferences.configureProxy();
configureProxy(preferences);
// bug specific to exactly 5.0 makes it only work with the old index
// which includes an ugly, hacky workaround
@ -485,8 +487,6 @@ public class FDroidApp extends Application {
}
});
configureTor(preferences.isTorEnabled());
if (preferences.isKeepingInstallHistory()) {
InstallHistoryService.register(this);
}
@ -640,30 +640,27 @@ public class FDroidApp extends Application {
}
}
private static boolean useTor;
/**
* Set the proxy settings based on whether Tor should be enabled or not.
* Put proxy settings (or Tor settings) globally into effect based on whats configured in Preferences.
*
* Must be called on App startup and after every proxy configuration change.
*/
private static void configureTor(boolean enabled) {
useTor = enabled;
if (useTor) {
public static void configureProxy(Preferences preferences) {
if (preferences.isTorEnabled()) {
NetCipher.useTor();
} else if (preferences.isProxyEnabled()) {
NetCipher.setProxy(preferences.getProxyHost(), preferences.getProxyPort());
} else {
NetCipher.clearProxy();
}
}
public static void checkStartTor(Context context) {
if (useTor) {
public static void checkStartTor(Context context, Preferences preferences) {
if (preferences.isTorEnabled()) {
OrbotHelper.requestStartTor(context);
}
}
public static boolean isUsingTor() {
return useTor;
}
public static Context getInstance() {
return instance;
}

View File

@ -31,13 +31,9 @@ import android.os.Build;
import android.support.v7.preference.PreferenceManager;
import android.text.format.DateUtils;
import android.util.Log;
import info.guardianproject.netcipher.NetCipher;
import org.fdroid.fdroid.installer.PrivilegedInstaller;
import org.fdroid.fdroid.net.ConnectivityMonitorService;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -481,22 +477,10 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
return preferences.getBoolean(PREF_USE_TOR, IGNORED_B);
}
private boolean isProxyEnabled() {
public boolean isProxyEnabled() {
return preferences.getBoolean(PREF_ENABLE_PROXY, IGNORED_B);
}
/**
* Configure the proxy settings based on whether its enabled and set up. This must be
* run once at app startup, then whenever any of these settings changes.
*/
public void configureProxy() {
if (isProxyEnabled()) {
// if "Use Tor" is set, NetCipher will ignore these proxy settings
SocketAddress sa = new InetSocketAddress(getProxyHost(), getProxyPort());
NetCipher.setProxy(new Proxy(Proxy.Type.HTTP, sa));
}
}
public String getProxyHost() {
return preferences.getString(PREF_PROXY_HOST, DEFAULT_PROXY_HOST);
}

View File

@ -28,6 +28,7 @@ import android.database.Cursor;
import android.text.TextUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.data.Schema.RepoTable.Cols;
@ -390,9 +391,12 @@ public class Repo extends ValueObject {
* Get the number of available mirrors, including the canonical repo.
*/
public int getMirrorCount() {
final boolean isTorEnabled = Preferences.get().isTorEnabled();
int count = 0;
for (String m : getMirrorList()) {
if (FDroidApp.isUsingTor()) {
if (isTorEnabled) {
count++;
} else if (!m.contains(".onion")) {
count++;
@ -430,11 +434,14 @@ public class Repo extends ValueObject {
}
List<String> shuffledMirrors = getMirrorList();
if (shuffledMirrors.size() > 0) {
final boolean isTorEnabled = Preferences.get().isTorEnabled();
Collections.shuffle(shuffledMirrors);
for (String m : shuffledMirrors) {
// Return a non default, and not last used mirror
if (!m.equals(mirrorToSkip)) {
if (FDroidApp.isUsingTor()) {
if (isTorEnabled) {
return m;
} else {
// Filter-out onion mirrors for non-tor connections

View File

@ -60,6 +60,7 @@ import android.widget.Toast;
import org.fdroid.fdroid.AddRepoIntentService;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.IndexUpdater;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.UpdateService;
import org.fdroid.fdroid.Utils;
@ -128,7 +129,7 @@ public class ManageReposActivity extends AppCompatActivity
@Override
protected void onResume() {
super.onResume();
FDroidApp.checkStartTor(this);
FDroidApp.checkStartTor(this, Preferences.get());
/* let's see if someone is trying to send us a new repo */
addRepoFromIntent(getIntent());

View File

@ -1,4 +1,5 @@
/*
* Copyright (C) 2019 Michael Pöhn
* Copyright (C) 2014-2018 Hans-Christoph Steiner
* Copyright (C) 2014-2017 Peter Serwylo
* Copyright (C) 2015-2016 Daniel Martí
@ -26,6 +27,7 @@
package org.fdroid.fdroid.views;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
@ -44,7 +46,7 @@ import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.WindowManager;
import info.guardianproject.netcipher.NetCipher;
import info.guardianproject.netcipher.proxy.OrbotHelper;
import org.fdroid.fdroid.CleanCacheService;
import org.fdroid.fdroid.FDroidApp;
@ -121,7 +123,9 @@ public class PreferencesFragment extends PreferenceFragment
installHistoryPref.setVisible(keepInstallHistoryPref.isChecked());
useTorCheckPref = (SwitchPreference) findPreference(Preferences.PREF_USE_TOR);
useTorCheckPref.setOnPreferenceChangeListener(useTorChangedListener);
enableProxyCheckPref = (SwitchPreference) findPreference(Preferences.PREF_ENABLE_PROXY);
enableProxyCheckPref.setOnPreferenceChangeListener(proxyEnabledChangedListener);
updateAutoDownloadPref = findPreference(Preferences.PREF_AUTO_DOWNLOAD_INSTALL_UPDATES);
overWifiSeekBar = (LiveSeekBarPreference) findPreference(Preferences.PREF_OVER_WIFI);
@ -445,31 +449,40 @@ public class PreferencesFragment extends PreferenceFragment
/**
* The default for "Use Tor" is dynamically set based on whether Orbot is installed.
*/
private void initUseTorPreference() {
boolean useTor = Preferences.get().isTorEnabled();
useTorCheckPref.setDefaultValue(useTor);
useTorCheckPref.setChecked(useTor);
useTorCheckPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object enabled) {
if ((Boolean) enabled) {
final Activity activity = getActivity();
enableProxyCheckPref.setEnabled(false);
if (OrbotHelper.isOrbotInstalled(activity)) {
NetCipher.useTor();
} else {
Intent intent = OrbotHelper.getOrbotInstallIntent(activity);
activity.startActivityForResult(intent, REQUEST_INSTALL_ORBOT);
}
} else {
enableProxyCheckPref.setEnabled(true);
NetCipher.clearProxy();
}
return true;
}
});
private void initUseTorPreference(Context context) {
useTorCheckPref.setDefaultValue(OrbotHelper.isOrbotInstalled(context));
useTorCheckPref.setChecked(Preferences.get().isTorEnabled());
}
private final Preference.OnPreferenceChangeListener useTorChangedListener =
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object enabled) {
if ((Boolean) enabled) {
enableProxyCheckPref.setChecked(false);
final Activity activity = getActivity();
if (!OrbotHelper.isOrbotInstalled(activity)) {
Intent intent = OrbotHelper.getOrbotInstallIntent(activity);
activity.startActivityForResult(intent, REQUEST_INSTALL_ORBOT);
}
// NetCipher gets configured to use Tor in onPause()
// via a call to FDroidApp.configureProxy()
}
return true;
}
};
private final Preference.OnPreferenceChangeListener proxyEnabledChangedListener =
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object enabled) {
if ((Boolean) enabled) {
useTorCheckPref.setChecked(false);
}
return true;
}
};
@Override
public void onResume() {
super.onResume();
@ -484,14 +497,14 @@ public class PreferencesFragment extends PreferenceFragment
initAutoFetchUpdatesPreference();
initPrivilegedInstallerPreference();
initUseTorPreference();
initUseTorPreference(getActivity().getApplicationContext());
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
Preferences.get().configureProxy();
FDroidApp.configureProxy(Preferences.get());
if (updateIntervalPrevious != updateIntervalSeekBar.getValue()) {
UpdateService.schedule(getActivity());

View File

@ -209,7 +209,7 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationB
protected void onResume() {
super.onResume();
FDroidApp.checkStartTor(this);
FDroidApp.checkStartTor(this, Preferences.get());
if (getIntent().hasExtra(EXTRA_VIEW_UPDATES)) {
getIntent().removeExtra(EXTRA_VIEW_UPDATES);