make prefs called directly when needed instead of requiring refresh

The code was using global variables that were refreshed from the prefs on
certain occasions.  That means that the global vars could easily get out of
sync with the actual values.  Instead, just read the prefs directly when
the values are needed, and they will always be up-to-date.
This commit is contained in:
Hans-Christoph Steiner 2015-06-08 22:57:42 -04:00
parent 6e13a7e369
commit a3d37e8b2a
8 changed files with 191 additions and 190 deletions

View File

@ -5,8 +5,6 @@ import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.Application; import android.app.Application;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
@ -14,29 +12,26 @@ import android.util.Log;
import info.guardianproject.util.Languages; import info.guardianproject.util.Languages;
import org.torproject.android.service.TorServiceUtils;
import java.util.Locale; import java.util.Locale;
public class OrbotApp extends Application implements OrbotConstants public class OrbotApp extends Application implements OrbotConstants
{ {
private Locale locale; private Locale locale;
private SharedPreferences prefs;
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Prefs.setContext(this);
prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); setNewLocale(Prefs.getDefaultLocale());
setNewLocale(prefs.getString(PREF_DEFAULT_LOCALE, Locale.getDefault().getLanguage()));
} }
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged " + newConfig.locale.getLanguage()); Log.i(TAG, "onConfigurationChanged " + newConfig.locale.getLanguage());
setNewLocale(prefs.getString(PREF_DEFAULT_LOCALE, Locale.getDefault().getLanguage())); setNewLocale(Prefs.getDefaultLocale());
} }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@ -65,10 +60,7 @@ public class OrbotApp extends Application implements OrbotConstants
* wrong. If setting the locale causes an Exception, it should be set in * wrong. If setting the locale causes an Exception, it should be set in
* the preferences, otherwise ChatSecure will be stuck in a crash loop. * the preferences, otherwise ChatSecure will be stuck in a crash loop.
*/ */
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(this); Prefs.setDefaultLocale(language);
Editor prefEdit = prefs.edit();
prefEdit.putString(PREF_DEFAULT_LOCALE, language);
prefEdit.apply();
Log.i(TAG, "setNewLocale complete: locale: " + locale.getLanguage() Log.i(TAG, "setNewLocale complete: locale: " + locale.getLanguage()
+ " Locale.getDefault: " + Locale.getDefault().getLanguage()); + " Locale.getDefault: " + Locale.getDefault().getLanguage());
} }

View File

@ -6,7 +6,6 @@ package org.torproject.android;
public interface OrbotConstants { public interface OrbotConstants {
public final static String TAG = "Orbot"; public final static String TAG = "Orbot";
public final static String PREFS_KEY = "OrbotPrefs"; public final static String PREFS_KEY = "OrbotPrefs";
public final static String PREFS_KEY_TORIFIED = "PrefTord"; public final static String PREFS_KEY_TORIFIED = "PrefTord";
@ -22,27 +21,15 @@ public interface OrbotConstants {
public final static String HANDLER_TOR_MSG = "torServiceMsg"; public final static String HANDLER_TOR_MSG = "torServiceMsg";
public final static String PREF_BRIDGES_ENABLED = "pref_bridges_enabled";
public final static String PREF_BRIDGES_UPDATED = "pref_bridges_enabled"; public final static String PREF_BRIDGES_UPDATED = "pref_bridges_enabled";
public final static String PREF_BRIDGES_LIST = "pref_bridges_list";
//public final static String PREF_BRIDGES_OBFUSCATED = "pref_bridges_obfuscated"; //public final static String PREF_BRIDGES_OBFUSCATED = "pref_bridges_obfuscated";
public final static String PREF_OR = "pref_or"; public final static String PREF_OR = "pref_or";
public final static String PREF_OR_PORT = "pref_or_port"; public final static String PREF_OR_PORT = "pref_or_port";
public final static String PREF_OR_NICKNAME = "pref_or_nickname"; public final static String PREF_OR_NICKNAME = "pref_or_nickname";
public final static String PREF_REACHABLE_ADDRESSES = "pref_reachable_addresses"; public final static String PREF_REACHABLE_ADDRESSES = "pref_reachable_addresses";
public final static String PREF_REACHABLE_ADDRESSES_PORTS = "pref_reachable_addresses_ports"; public final static String PREF_REACHABLE_ADDRESSES_PORTS = "pref_reachable_addresses_ports";
public final static String PREF_TRANSPARENT = "pref_transparent";
public final static String PREF_TRANSPARENT_ALL = "pref_transparent_all";
public final static String PREF_HAS_ROOT = "has_root";
public final static int RESULT_CLOSE_ALL = 0; public final static int RESULT_CLOSE_ALL = 0;
public final static String PREF_USE_SYSTEM_IPTABLES = "pref_use_sys_iptables";
public final static String PREF_PERSIST_NOTIFICATIONS = "pref_persistent_notifications";
public final static String PREF_DEFAULT_LOCALE = "pref_default_locale";
public final static String PREF_DISABLE_NETWORK = "pref_disable_network"; public final static String PREF_DISABLE_NETWORK = "pref_disable_network";
public final static String PREF_TOR_SHARED_PREFS = "org.torproject.android_preferences"; public final static String PREF_TOR_SHARED_PREFS = "org.torproject.android_preferences";

View File

@ -92,14 +92,13 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
private boolean autoStartFromIntent = false; private boolean autoStartFromIntent = false;
private final static long INIT_DELAY = 100;
private final static int REQUEST_VPN = 8888; private final static int REQUEST_VPN = 8888;
private final static int REQUEST_SETTINGS = 0x9874; private final static int REQUEST_SETTINGS = 0x9874;
/** Called when the activity is first created. */ /** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
mPrefs.registerOnSharedPreferenceChangeListener(this); mPrefs.registerOnSharedPreferenceChangeListener(this);
@ -238,7 +237,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
mBtnVPN = (ToggleButton)findViewById(R.id.btnVPN); mBtnVPN = (ToggleButton)findViewById(R.id.btnVPN);
boolean useVPN = mPrefs.getBoolean("pref_vpn", false); boolean useVPN = Prefs.useVpn();
mBtnVPN.setChecked(useVPN); mBtnVPN.setChecked(useVPN);
if (useVPN) if (useVPN)
@ -262,12 +261,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
}); });
mBtnBridges = (ToggleButton)findViewById(R.id.btnBridges); mBtnBridges = (ToggleButton)findViewById(R.id.btnBridges);
boolean useBridges = mPrefs.getBoolean("pref_bridges_enabled", false); mBtnBridges.setChecked(Prefs.bridgesEnabled());
mBtnBridges.setChecked(useBridges);
mBtnBridges.setOnClickListener(new View.OnClickListener () mBtnBridges.setOnClickListener(new View.OnClickListener ()
{ {
@ -407,7 +402,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
else if (item.getItemId() == R.id.menu_share_bridge) else if (item.getItemId() == R.id.menu_share_bridge)
{ {
String bridges = mPrefs.getString(OrbotConstants.PREF_BRIDGES_LIST, null); String bridges = Prefs.getBridgesList();
if (bridges != null && bridges.length() > 0) if (bridges != null && bridges.length() > 0)
{ {
@ -655,12 +650,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
showAlert(getString(R.string.bridges_updated),getString(R.string.restart_orbot_to_use_this_bridge_) + newBridgeValue,false); showAlert(getString(R.string.bridges_updated),getString(R.string.restart_orbot_to_use_this_bridge_) + newBridgeValue,false);
Editor pEdit = mPrefs.edit(); Prefs.setBridgesList(newBridgeValue); //set the string to a preference
Prefs.putBridgesEnabled(true);
pEdit.putString(OrbotConstants.PREF_BRIDGES_LIST,newBridgeValue); //set the string to a preference
pEdit.putBoolean(OrbotConstants.PREF_BRIDGES_ENABLED,true);
pEdit.commit();
setResult(RESULT_OK); setResult(RESULT_OK);
@ -675,7 +666,6 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
private void openBrowser(final String browserLaunchUrl,boolean forceExternal) private void openBrowser(final String browserLaunchUrl,boolean forceExternal)
{ {
boolean isOrwebInstalled = appInstalledOrNot("info.guardianproject.browser"); boolean isOrwebInstalled = appInstalledOrNot("info.guardianproject.browser");
boolean isTransProxy = mPrefs.getBoolean("pref_transparent", false);
if (mBtnVPN.isChecked()||forceExternal) if (mBtnVPN.isChecked()||forceExternal)
{ {
@ -684,7 +674,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); startActivity(intent);
} }
else if (isTransProxy) else if (Prefs.useTransparentProxying())
{ {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserLaunchUrl)); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserLaunchUrl));
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
@ -793,7 +783,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
} }
else if (torStatus == TorServiceConstants.STATUS_ON) else if (torStatus == TorServiceConstants.STATUS_ON)
{ {
updateSettings(); updateTransProxy();
Toast.makeText(this, R.string.you_may_need_to_stop_and_start_orbot_for_settings_change_to_be_enabled_, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.you_may_need_to_stop_and_start_orbot_for_settings_change_to_be_enabled_, Toast.LENGTH_SHORT).show();
} }
@ -867,17 +857,17 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
break; break;
case 3: //azure case 3: //azure
mPrefs.edit().putString(OrbotConstants.PREF_BRIDGES_LIST,"2").commit(); Prefs.setBridgesList("2");
enableBridges(true); enableBridges(true);
break; break;
case 4: //amazon case 4: //amazon
mPrefs.edit().putString(OrbotConstants.PREF_BRIDGES_LIST,"1").commit(); Prefs.setBridgesList("1");
enableBridges(true); enableBridges(true);
break; break;
case 5: //google case 5: //google
mPrefs.edit().putString(OrbotConstants.PREF_BRIDGES_LIST,"0").commit(); Prefs.setBridgesList("0");
enableBridges(true); enableBridges(true);
break; break;
@ -974,24 +964,17 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
private void enableBridges (boolean enable) private void enableBridges (boolean enable)
{ {
Prefs.putBridgesEnabled(enable);
Editor edit = mPrefs.edit();
edit.putBoolean(OrbotConstants.PREF_BRIDGES_ENABLED, enable);
edit.commit();
updateSettings();
if (torStatus == TorServiceConstants.STATUS_ON) if (torStatus == TorServiceConstants.STATUS_ON)
{ {
String bridgeList = mPrefs.getString(OrbotConstants.PREF_BRIDGES_LIST,null); String bridgeList = Prefs.getBridgesList();
if (bridgeList != null && bridgeList.length() > 0) if (bridgeList != null && bridgeList.length() > 0)
{ {
restartTor (); restartTor ();
} }
} }
} }
private void restartTor () private void restartTor ()
@ -1038,11 +1021,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Prefs.putUseVpn(true);
mPrefs.edit().putBoolean("pref_vpn", true).commit();
startVpnService(); startVpnService();
} }
@ -1090,10 +1070,9 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
return true; return true;
} }
private boolean updateSettings () private boolean updateTransProxy ()
{ {
//todo send service command startService(TorServiceConstants.CMD_UPDATE_TRANS_PROXY);
startService(TorServiceConstants.CMD_UPDATE);
return true; return true;
} }
@ -1103,11 +1082,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
if (mPrefs != null) if (mPrefs != null)
{ {
boolean useVPN = mPrefs.getBoolean("pref_vpn", false); mBtnVPN.setChecked(Prefs.useVpn());
mBtnVPN.setChecked(useVPN); mBtnBridges.setChecked(Prefs.bridgesEnabled());
boolean useBridges = mPrefs.getBoolean("pref_bridges_enabled", false);
mBtnBridges.setChecked(useBridges);
} }
mHandler.postDelayed(new Runnable () mHandler.postDelayed(new Runnable ()

View File

@ -0,0 +1,116 @@
package org.torproject.android;
import android.content.Context;
import android.content.SharedPreferences;
import org.torproject.android.service.TorServiceUtils;
import java.util.Locale;
public class Prefs {
private final static String PREF_BRIDGES_ENABLED = "pref_bridges_enabled";
private final static String PREF_BRIDGES_LIST = "pref_bridges_list";
private final static String PREF_DEFAULT_LOCALE = "pref_default_locale";
private final static String PREF_ENABLE_LOGGING = "pref_enable_logging";
private final static String PREF_EXPANDED_NOTIFICATIONS = "pref_expanded_notifications";
private final static String PREF_HAS_ROOT = "has_root";
private final static String PREF_PERSIST_NOTIFICATIONS = "pref_persistent_notifications";
private final static String PREF_START_ON_BOOT = "pref_start_boot";
private final static String PREF_TRANSPARENT = "pref_transparent";
private final static String PREF_TRANSPARENT_ALL = "pref_transparent_all";
private final static String PREF_TRANSPARENT_TETHERING = "pref_transparent_tethering";
private final static String PREF_TRANSPROXY_REFRESH = "pref_transproxy_refresh";
private final static String PREF_USE_SYSTEM_IPTABLES = "pref_use_sys_iptables";
private final static String PREF_USE_VPN = "pref_vpn";
private static SharedPreferences prefs;
public static void setContext(Context context) {
if (prefs == null)
prefs = TorServiceUtils.getSharedPrefs(context);
}
private static void putBoolean(String key, boolean value) {
prefs.edit().putBoolean(key, value).apply();
}
private static void putString(String key, String value) {
prefs.edit().putString(key, value).apply();
}
public static boolean bridgesEnabled() {
return prefs.getBoolean(PREF_BRIDGES_ENABLED, false);
}
public static void putBridgesEnabled(boolean value) {
putBoolean(PREF_BRIDGES_ENABLED, value);
}
public static String getBridgesList() {
return prefs.getString(PREF_BRIDGES_LIST, "");
}
public static void setBridgesList(String value) {
putString(PREF_BRIDGES_LIST, value);
}
public static String getDefaultLocale() {
return prefs.getString(PREF_DEFAULT_LOCALE, Locale.getDefault().getLanguage());
}
public static void setDefaultLocale(String value) {
putString(PREF_DEFAULT_LOCALE, value);
}
public static boolean useSystemIpTables() {
return prefs.getBoolean(PREF_USE_SYSTEM_IPTABLES, false);
}
public static boolean useRoot() {
return prefs.getBoolean(PREF_HAS_ROOT, false);
}
public static boolean useTransparentProxying() {
return prefs.getBoolean(PREF_TRANSPARENT, false);
}
public static boolean transparentProxyAll() {
return prefs.getBoolean(PREF_TRANSPARENT_ALL, false);
}
public static boolean transparentTethering() {
return prefs.getBoolean(PREF_TRANSPARENT_TETHERING, false);
}
public static boolean transProxyNetworkRefresh() {
return prefs.getBoolean(PREF_TRANSPROXY_REFRESH, false);
}
public static boolean expandedNotifications() {
return prefs.getBoolean(PREF_EXPANDED_NOTIFICATIONS, false);
}
public static boolean useDebugLogging() {
return prefs.getBoolean(PREF_ENABLE_LOGGING, false);
}
public static boolean persistNotifications() {
return prefs.getBoolean(PREF_PERSIST_NOTIFICATIONS, true);
}
public static boolean useVpn() {
return prefs.getBoolean(PREF_USE_VPN, false);
}
public static void putUseVpn(boolean value) {
putBoolean(PREF_USE_VPN, value);
}
public static boolean startOnBoot() {
return prefs.getBoolean(PREF_START_ON_BOOT, true);
}
public static void putStartOnBoot(boolean value) {
putBoolean(PREF_START_ON_BOOT, value);
}
}

View File

@ -5,27 +5,22 @@ import android.annotation.SuppressLint;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.net.VpnService; import android.net.VpnService;
import org.torproject.android.Prefs;
public class OnBootReceiver extends BroadcastReceiver { public class OnBootReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Prefs.setContext(context);
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context.getApplicationContext()); if (Prefs.startOnBoot())
boolean startOnBoot = prefs.getBoolean("pref_start_boot",true);
boolean useVPN = prefs.getBoolean("pref_vpn",true);
if (startOnBoot)
{ {
startService(TorServiceConstants.CMD_START,context); startService(TorServiceConstants.CMD_START,context);
if (useVPN) if (Prefs.useVpn())
startVpnService(context); startVpnService(context);
} }
} }
@SuppressLint("NewApi") @SuppressLint("NewApi")

View File

@ -8,6 +8,7 @@
package org.torproject.android.service; package org.torproject.android.service;
import org.torproject.android.Prefs;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
@ -82,8 +83,6 @@ import android.widget.RemoteViews;
public class TorService extends Service implements TorServiceConstants, OrbotConstants, EventHandler public class TorService extends Service implements TorServiceConstants, OrbotConstants, EventHandler
{ {
public static boolean ENABLE_DEBUG_LOG = true;
private String mCurrentStatus = STATUS_OFF; private String mCurrentStatus = STATUS_OFF;
private final static int CONTROL_SOCKET_TIMEOUT = 0; private final static int CONTROL_SOCKET_TIMEOUT = 0;
@ -101,8 +100,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
private static final int ERROR_NOTIFY_ID = 3; private static final int ERROR_NOTIFY_ID = 3;
private static final int HS_NOTIFY_ID = 4; private static final int HS_NOTIFY_ID = 4;
private boolean prefPersistNotifications = true;
private static final int MAX_START_TRIES = 3; private static final int MAX_START_TRIES = 3;
private ArrayList<String> configBuffer = null; private ArrayList<String> configBuffer = null;
@ -134,16 +131,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
private NotificationManager mNotificationManager = null; private NotificationManager mNotificationManager = null;
private Builder mNotifyBuilder; private Builder mNotifyBuilder;
private Notification mNotification; private Notification mNotification;
private boolean mShowExpandedNotifications = false;
private boolean mNotificationShowing = false; private boolean mNotificationShowing = false;
private boolean mHasRoot = false;
private boolean mEnableTransparentProxy = false;
private boolean mTransProxyAll = false;
private boolean mTransProxyTethering = false;
private boolean mTransProxyNetworkRefresh = false;
private boolean mUseVPN = false;
boolean mIsLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; boolean mIsLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
private ExecutorService mExecutor = Executors.newFixedThreadPool(1); private ExecutorService mExecutor = Executors.newFixedThreadPool(1);
@ -152,7 +141,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
public void debug(String msg) public void debug(String msg)
{ {
if (ENABLE_DEBUG_LOG) if (Prefs.useDebugLogging())
{ {
Log.d(TAG,msg); Log.d(TAG,msg);
sendCallbackLogMessage(msg); sendCallbackLogMessage(msg);
@ -162,7 +151,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
public void logException(String msg, Exception e) public void logException(String msg, Exception e)
{ {
if (ENABLE_DEBUG_LOG) if (Prefs.useDebugLogging())
{ {
Log.e(TAG,msg,e); Log.e(TAG,msg,e);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -268,11 +257,11 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
mNotifyBuilder.setTicker(null); mNotifyBuilder.setTicker(null);
} }
mNotifyBuilder.setOngoing(prefPersistNotifications); mNotifyBuilder.setOngoing(Prefs.persistNotifications());
mNotification = mNotifyBuilder.build(); mNotification = mNotifyBuilder.build();
if (Build.VERSION.SDK_INT >= 16 && mShowExpandedNotifications) { if (Build.VERSION.SDK_INT >= 16 && Prefs.expandedNotifications()) {
// Create remote view that needs to be set as bigContentView for the notification. // Create remote view that needs to be set as bigContentView for the notification.
RemoteViews expandedView = new RemoteViews(this.getPackageName(), RemoteViews expandedView = new RemoteViews(this.getPackageName(),
R.layout.layout_notification_expanded); R.layout.layout_notification_expanded);
@ -317,7 +306,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
mNotification.bigContentView = expandedView; mNotification.bigContentView = expandedView;
} }
if (prefPersistNotifications && (!mNotificationShowing)) if (Prefs.persistNotifications() && (!mNotificationShowing))
{ {
startForeground(NOTIFY_ID, mNotification); startForeground(NOTIFY_ID, mNotification);
logNotice("Set background service to FOREGROUND"); logNotice("Set background service to FOREGROUND");
@ -364,8 +353,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
newIdentity(); newIdentity();
} else if (action.equals(CMD_FLUSH)) { } else if (action.equals(CMD_FLUSH)) {
flushTransparentProxyRules(); flushTransparentProxyRules();
} else if (action.equals(CMD_UPDATE)) { } else if (action.equals(CMD_UPDATE_TRANS_PROXY)) {
processSettings(); processTransparentProxying();
} else if (action.equals(CMD_VPN)) { } else if (action.equals(CMD_VPN)) {
enableVpnProxy(); enableVpnProxy();
} else if (action.equals(CMD_VPN_CLEAR)) { } else if (action.equals(CMD_VPN_CLEAR)) {
@ -394,6 +383,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
@Override @Override
public void onDestroy () public void onDestroy ()
{ {
Log.i("TorService", "onDestroy");
String msg = ("TorService is being DESTROYED... shutting down!"); String msg = ("TorService is being DESTROYED... shutting down!");
Log.d(TAG, msg); Log.d(TAG, msg);
sendCallbackLogMessage(msg); sendCallbackLogMessage(msg);
@ -417,7 +407,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
mCurrentStatus = STATUS_OFF; mCurrentStatus = STATUS_OFF;
sendCallbackStatus(mCurrentStatus); sendCallbackStatus(mCurrentStatus);
if (mHasRoot && mEnableTransparentProxy) if (Prefs.useRoot() && Prefs.useTransparentProxying())
{ {
Shell shellRoot = Shell.startRootShell(); Shell shellRoot = Shell.startRootShell();
disableTransparentProxy(shellRoot); disableTransparentProxy(shellRoot);
@ -564,7 +554,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
* uninstall/reinstall with different UID. * uninstall/reinstall with different UID.
*/ */
Shell shell; Shell shell;
if (mHasRoot && killAttempts > 2) { if (Prefs.useRoot() && killAttempts > 2) {
shell = Shell.startRootShell(); shell = Shell.startRootShell();
Log.i(TAG, "using a root shell"); Log.i(TAG, "using a root shell");
} else { } else {
@ -589,7 +579,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
{ {
if (msg != null && msg.trim().length() > 0) if (msg != null && msg.trim().length() > 0)
{ {
if (ENABLE_DEBUG_LOG) if (Prefs.useDebugLogging())
Log.d(TAG, msg); Log.d(TAG, msg);
sendCallbackLogMessage(msg); sendCallbackLogMessage(msg);
@ -599,6 +589,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Log.i("TorService", "onCreate");
try try
{ {
mNumberFormat = NumberFormat.getInstance(Locale.getDefault()); //localized numbers! mNumberFormat = NumberFormat.getInstance(Locale.getDefault()); //localized numbers!
@ -614,7 +606,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
initBinariesAndDirectories(); initBinariesAndDirectories();
updateSettings();
new Thread(new Runnable () new Thread(new Runnable ()
{ {
@ -703,7 +694,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
fileControlPort = new File(appBinHome,"control.txt"); fileControlPort = new File(appBinHome,"control.txt");
extraLines.append(TORRC_CONTROLPORT_FILE_KEY).append(' ').append(fileControlPort.getCanonicalPath()).append('\n'); extraLines.append(TORRC_CONTROLPORT_FILE_KEY).append(' ').append(fileControlPort.getCanonicalPath()).append('\n');
if (mTransProxyTethering) if (Prefs.transparentTethering())
{ {
extraLines.append("TransListenAddress 0.0.0.0").append('\n'); extraLines.append("TransListenAddress 0.0.0.0").append('\n');
extraLines.append("DNSListenAddress 0.0.0.0").append('\n'); extraLines.append("DNSListenAddress 0.0.0.0").append('\n');
@ -768,29 +759,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
return fileBin.canExecute(); return fileBin.canExecute();
} }
private void updateSettings () throws TimeoutException, IOException
{
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
mHasRoot = prefs.getBoolean(PREF_HAS_ROOT,false);
mEnableTransparentProxy = prefs.getBoolean("pref_transparent", false);
mTransProxyAll = prefs.getBoolean("pref_transparent_all", false);
mTransProxyTethering = prefs.getBoolean("pref_transparent_tethering", false);
mTransProxyNetworkRefresh = prefs.getBoolean("pref_transproxy_refresh", false);
mShowExpandedNotifications = prefs.getBoolean("pref_expanded_notifications", false);
ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
prefPersistNotifications = prefs.getBoolean(OrbotConstants.PREF_PERSIST_NOTIFICATIONS, true);
mUseVPN = prefs.getBoolean("pref_vpn", false);
}
private void startTor () throws Exception private void startTor () throws Exception
{ {
@ -805,19 +773,14 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
enableBinExec(fileObfsclient); enableBinExec(fileObfsclient);
enableBinExec(fileMeekclient); enableBinExec(fileMeekclient);
enableBinExec(fileXtables); enableBinExec(fileXtables);
updateSettings ();
logNotice(getString(R.string.status_starting_up)); logNotice(getString(R.string.status_starting_up));
sendCallbackLogMessage(getString(R.string.status_starting_up)); sendCallbackLogMessage(getString(R.string.status_starting_up));
ArrayList<String> customEnv = new ArrayList<String>(); ArrayList<String> customEnv = new ArrayList<String>();
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
boolean useBridges = prefs.getBoolean(OrbotConstants.PREF_BRIDGES_ENABLED, false);
if (useBridges) if (Prefs.bridgesEnabled())
if (mUseVPN && !mIsLollipop) if (Prefs.useVpn() && !mIsLollipop)
customEnv.add("TOR_PT_PROXY=socks5://127.0.0.1:" + OrbotVpnService.mSocksProxyPort); customEnv.add("TOR_PT_PROXY=socks5://127.0.0.1:" + OrbotVpnService.mSocksProxyPort);
String baseDirectory = fileTor.getParent(); String baseDirectory = fileTor.getParent();
@ -830,7 +793,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (mPortHTTP != -1) if (mPortHTTP != -1)
runPolipoShellCmd(shellUser); runPolipoShellCmd(shellUser);
if (mHasRoot && mEnableTransparentProxy) if (Prefs.useRoot() && Prefs.useTransparentProxying())
{ {
Shell shellRoot = Shell.startRootShell(); Shell shellRoot = Shell.startRootShell();
@ -840,7 +803,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
shellRoot.close(); shellRoot.close();
} }
if (mUseVPN) //we need to turn on VPN here so the proxy is running if (Prefs.useVpn()) //we need to turn on VPN here so the proxy is running
{ {
enableVpnProxy(); enableVpnProxy();
} }
@ -858,7 +821,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
private boolean flushTransparentProxyRules () { private boolean flushTransparentProxyRules () {
if (mHasRoot) if (Prefs.useRoot())
{ {
if (mTransProxy == null) if (mTransProxy == null)
mTransProxy = new TorTransProxy(this, fileXtables); mTransProxy = new TorTransProxy(this, fileXtables);
@ -909,7 +872,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
int code = 0; // Default state is "okay" int code = 0; // Default state is "okay"
if(mTransProxyAll) if(Prefs.transparentProxyAll())
{ {
code = mTransProxy.setTransparentProxyingAll(this, true, shell); code = mTransProxy.setTransparentProxyingAll(this, true, shell);
@ -926,7 +889,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (code == 0) if (code == 0)
{ {
if (mTransProxyTethering) if (Prefs.transparentTethering())
{ {
showToolbarNotification(getString(R.string.transproxy_enabled_for_tethering_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor); showToolbarNotification(getString(R.string.transproxy_enabled_for_tethering_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor);
@ -1404,17 +1367,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
public void enableVpnProxy () { public void enableVpnProxy () {
debug ("enabling VPN Proxy"); debug ("enabling VPN Proxy");
mUseVPN = true;
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); Prefs.putUseVpn(true);
Editor ePrefs = prefs.edit(); processTransparentProxying();
ePrefs.putBoolean("pref_vpn", true);
ePrefs.commit();
processSettings();
Intent intent = new Intent(TorService.this, OrbotVpnService.class); Intent intent = new Intent(TorService.this, OrbotVpnService.class);
intent.setAction("start"); intent.setAction("start");
@ -1458,16 +1414,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
public void clearVpnProxy () public void clearVpnProxy ()
{ {
debug ("clearing VPN Proxy"); debug ("clearing VPN Proxy");
Prefs.putUseVpn(false);
mUseVPN = false; processTransparentProxying();
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
Editor ePrefs = prefs.edit();
ePrefs.putBoolean("pref_vpn", false);
ePrefs.commit();
processSettings();
Intent intent = new Intent(TorService.this, OrbotVpnService.class); Intent intent = new Intent(TorService.this, OrbotVpnService.class);
intent.setAction("stop"); intent.setAction("stop");
@ -1556,7 +1504,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (read > 0 || written > 0) if (read > 0 || written > 0)
iconId = R.drawable.ic_stat_tor_xfer; iconId = R.drawable.ic_stat_tor_xfer;
if (mConnectivity && prefPersistNotifications) if (mConnectivity && Prefs.persistNotifications())
showToolbarNotification(sb.toString(), NOTIFY_ID, iconId); showToolbarNotification(sb.toString(), NOTIFY_ID, iconId);
mTotalTrafficWritten += written; mTotalTrafficWritten += written;
@ -1631,7 +1579,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
sb.append (" > "); sb.append (" > ");
} }
if (ENABLE_DEBUG_LOG) if (Prefs.useDebugLogging())
debug(sb.toString()); debug(sb.toString());
else if(status.equals("BUILT")) else if(status.equals("BUILT"))
{ {
@ -1651,7 +1599,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
if (mShowExpandedNotifications) if (Prefs.expandedNotifications())
{ {
//get IP from last nodename //get IP from last nodename
if(status.equals("BUILT")){ if(status.equals("BUILT")){
@ -1707,7 +1655,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
Proxy proxy = null; Proxy proxy = null;
if (!mUseVPN) //if not on the VPN then we should proxy if (!Prefs.useVpn()) //if not on the VPN then we should proxy
{ {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8118)); proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8118));
conn = new URL(ONIONOO_BASE_URL + mNode.id).openConnection(proxy); conn = new URL(ONIONOO_BASE_URL + mNode.id).openConnection(proxy);
@ -1787,21 +1735,16 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
else else
return node; return node;
} }
public void processTransparentProxying() {
public void processSettings (){
try{ try{
boolean hadEnableTransparentProxy = mEnableTransparentProxy; if (Prefs.useRoot())
updateSettings ();
if (mHasRoot)
{ {
Shell shell = Shell.startRootShell(); Shell shell = Shell.startRootShell();
if (hadEnableTransparentProxy){ if (Prefs.useTransparentProxying()){
disableTransparentProxy(shell);
}
if (mEnableTransparentProxy){
enableTransparentProxy(shell); enableTransparentProxy(shell);
} else {
disableTransparentProxy(shell);
} }
shell.close(); shell.close();
} }
@ -2101,7 +2044,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
{ {
if (mConnectivity) if (mConnectivity)
{ {
if (mHasRoot && mEnableTransparentProxy && mTransProxyNetworkRefresh) if (Prefs.useRoot() && Prefs.useTransparentProxying() && Prefs.transProxyNetworkRefresh())
{ {
Shell shell = Shell.startRootShell(); Shell shell = Shell.startRootShell();
@ -2111,7 +2054,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
shell.close(); shell.close();
} }
else if (mUseVPN) //we need to turn on VPN here so the proxy is running else if (Prefs.useVpn()) //we need to turn on VPN here so the proxy is running
refreshVpnProxy(); refreshVpnProxy();
} }
@ -2145,7 +2088,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
enableTransProxyAndDNSPorts(transPort, dnsPort); enableTransProxyAndDNSPorts(transPort, dnsPort);
*/ */
boolean useBridges = prefs.getBoolean(OrbotConstants.PREF_BRIDGES_ENABLED, false); boolean useBridges = Prefs.bridgesEnabled();
boolean becomeRelay = prefs.getBoolean(OrbotConstants.PREF_OR, false); boolean becomeRelay = prefs.getBoolean(OrbotConstants.PREF_OR, false);
boolean ReachableAddresses = prefs.getBoolean(OrbotConstants.PREF_REACHABLE_ADDRESSES,false); boolean ReachableAddresses = prefs.getBoolean(OrbotConstants.PREF_REACHABLE_ADDRESSES,false);
@ -2161,7 +2104,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
extraLines.append("UseBridges 0").append('\n'); extraLines.append("UseBridges 0").append('\n');
if (mUseVPN) //set the proxy here if we aren't using a bridge if (Prefs.useVpn()) //set the proxy here if we aren't using a bridge
{ {
if (!mIsLollipop) if (!mIsLollipop)
@ -2212,7 +2155,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
extraLines.append("UseBridges 1").append('\n'); extraLines.append("UseBridges 1").append('\n');
String bridgeList = new String(prefs.getString(OrbotConstants.PREF_BRIDGES_LIST,"").getBytes("ISO-8859-1")); String bridgeList = new String(Prefs.getBridgesList().getBytes("ISO-8859-1"));
if (bridgeList != null && bridgeList.length() > 1) //longer then 1 = some real values here if (bridgeList != null && bridgeList.length() > 1) //longer then 1 = some real values here
{ {
@ -2411,7 +2354,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
if (mUseVPN) if (Prefs.useVpn())
{ {
extraLines.append("DNSListenAddress" + ' ' + "10.0.0.1:" + TorServiceConstants.TOR_DNS_PORT_DEFAULT).append('\n'); extraLines.append("DNSListenAddress" + ' ' + "10.0.0.1:" + TorServiceConstants.TOR_DNS_PORT_DEFAULT).append('\n');
} }

View File

@ -89,7 +89,7 @@ public interface TorServiceConstants {
public static final String CMD_NEWNYM = "newnym"; public static final String CMD_NEWNYM = "newnym";
public static final String CMD_VPN = "vpn"; public static final String CMD_VPN = "vpn";
public static final String CMD_VPN_CLEAR = "vpnclear"; public static final String CMD_VPN_CLEAR = "vpnclear";
public static final String CMD_UPDATE = "update"; public static final String CMD_UPDATE_TRANS_PROXY = "update";
public static final String BINARY_TOR_VERSION = "0.2.6.7"; public static final String BINARY_TOR_VERSION = "0.2.6.7";

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import org.sufficientlysecure.rootcommands.Shell; import org.sufficientlysecure.rootcommands.Shell;
import org.sufficientlysecure.rootcommands.command.SimpleCommand; import org.sufficientlysecure.rootcommands.command.SimpleCommand;
import org.torproject.android.OrbotConstants; import org.torproject.android.OrbotConstants;
import org.torproject.android.Prefs;
import org.torproject.android.settings.TorifiedApp; import org.torproject.android.settings.TorifiedApp;
import android.content.Context; import android.content.Context;
@ -13,7 +14,6 @@ import android.content.SharedPreferences;
public class TorTransProxy implements TorServiceConstants { public class TorTransProxy implements TorServiceConstants {
private boolean useSystemIpTables = false;
private String mSysIptables = null; private String mSysIptables = null;
private TorService mTorService = null; private TorService mTorService = null;
private File mFileXtables = null; private File mFileXtables = null;
@ -43,12 +43,8 @@ public class TorTransProxy implements TorServiceConstants {
{ {
String ipTablesPath = null; String ipTablesPath = null;
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context);
useSystemIpTables = prefs.getBoolean(OrbotConstants.PREF_USE_SYSTEM_IPTABLES, false); if (Prefs.useSystemIpTables())
if (useSystemIpTables)
{ {
ipTablesPath = findSystemIPTables(); ipTablesPath = findSystemIPTables();
} }
@ -67,11 +63,7 @@ public class TorTransProxy implements TorServiceConstants {
String ipTablesPath = null; String ipTablesPath = null;
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context); if (Prefs.useSystemIpTables())
useSystemIpTables = prefs.getBoolean(OrbotConstants.PREF_USE_SYSTEM_IPTABLES, false);
if (useSystemIpTables)
{ {
ipTablesPath = findSystemIP6Tables(); ipTablesPath = findSystemIP6Tables();
} }
@ -674,7 +666,7 @@ public class TorTransProxy implements TorServiceConstants {
script = new StringBuilder(); script = new StringBuilder();
if (TorService.ENABLE_DEBUG_LOG) if (Prefs.useDebugLogging())
{ {
//XXX: Comment the following rules for non-debug builds //XXX: Comment the following rules for non-debug builds
script.append(ipTablesPath); script.append(ipTablesPath);