From 144460beed66ee595f37cbbc87b2245420dad128 Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Fri, 25 Apr 2014 12:44:21 -0400 Subject: [PATCH] fixes for preference handling in multi process context --- .../torproject/android/OnBootReceiver.java | 6 +- src/org/torproject/android/OrbotApp.java | 4 +- .../android/service/TorService.java | 115 +++++++++++------- .../android/service/TorServiceConstants.java | 4 +- .../android/service/TorServiceUtils.java | 15 +++ .../android/service/TorTransProxy.java | 4 +- .../android/settings/AppManager.java | 5 +- .../android/settings/SettingsPreferences.java | 21 +++- 8 files changed, 115 insertions(+), 59 deletions(-) diff --git a/src/org/torproject/android/OnBootReceiver.java b/src/org/torproject/android/OnBootReceiver.java index a5ca1d6f..92104e0b 100644 --- a/src/org/torproject/android/OnBootReceiver.java +++ b/src/org/torproject/android/OnBootReceiver.java @@ -1,12 +1,12 @@ package org.torproject.android; import org.torproject.android.service.TorService; +import org.torproject.android.service.TorServiceUtils; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.preference.PreferenceManager; public class OnBootReceiver extends BroadcastReceiver { @@ -16,7 +16,8 @@ public class OnBootReceiver extends BroadcastReceiver { if (intent.getAction() != null && intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { - SharedPreferences prefs = TorService.getSharedPrefs(context.getApplicationContext()); + + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context.getApplicationContext()); boolean startOnBoot = prefs.getBoolean("pref_start_boot",false); @@ -34,5 +35,6 @@ public class OnBootReceiver extends BroadcastReceiver { } + } diff --git a/src/org/torproject/android/OrbotApp.java b/src/org/torproject/android/OrbotApp.java index d470afb1..cb1824cf 100644 --- a/src/org/torproject/android/OrbotApp.java +++ b/src/org/torproject/android/OrbotApp.java @@ -2,6 +2,8 @@ package org.torproject.android; import java.util.Locale; +import org.torproject.android.service.TorServiceUtils; + import android.app.Application; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -18,7 +20,7 @@ public class OrbotApp extends Application implements TorConstants public void onCreate() { super.onCreate(); - settings = PreferenceManager.getDefaultSharedPreferences(this); + settings = TorServiceUtils.getSharedPrefs(getApplicationContext()); Configuration config = getResources().getConfiguration(); diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 52643706..4f897e97 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -292,7 +292,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot")) { - boolean startOnBoot = getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false); + boolean startOnBoot = TorServiceUtils.getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false); if (startOnBoot) { @@ -308,7 +308,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst // We want this service to continue running until it is explicitly // stopped, so return sticky. - return START_STICKY; + return START_NOT_STICKY; } catch (Exception e) @@ -319,14 +319,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst } - public static SharedPreferences getSharedPrefs (Context context) - { - if (Build.VERSION.SDK_INT>=11) - return context.getSharedPreferences(TorConstants.PREF_TOR_SHARED_PREFS,Context.MODE_MULTI_PROCESS); - else - return context.getSharedPreferences(TorConstants.PREF_TOR_SHARED_PREFS,Context.MODE_PRIVATE); - - } + public void run () @@ -341,11 +334,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst } catch (Exception e) { - logException("Unable to start Tor: " + e.getMessage(),e); - sendCallbackStatusMessage(getString(R.string.unable_to_start_tor) + ' ' + e.getMessage()); + + logException("Unable to start Tor: " + e.toString(),e); currentStatus = STATUS_OFF; this.showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, -1, false); - Log.d(TAG,"Unable to start Tor: " + e.getMessage(),e); } } @@ -404,7 +396,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst private String getHiddenServiceHostname () { - SharedPreferences prefs = getSharedPrefs(getApplicationContext()); + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); boolean enableHiddenServices = prefs.getBoolean("pref_hs_enable", false); @@ -538,7 +530,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst fileXtables = new File(appBinHome, IPTABLES_ASSET_KEY); - SharedPreferences prefs = getSharedPrefs(getApplicationContext()); + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); String version = prefs.getString(PREF_BINARY_TOR_VERSION_INSTALLED,null); logNotice("checking binary version: " + version); @@ -595,7 +587,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst private void updateSettings () { - SharedPreferences prefs = getSharedPrefs(getApplicationContext()); + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); mHasRoot = prefs.getBoolean(PREF_HAS_ROOT,false); mEnableTransparentProxy = prefs.getBoolean("pref_transparent", false); @@ -611,8 +603,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst public void initTor () throws Exception { - boolean portsAvail = checkPortsAvailable(); - try { initBinaries(); @@ -645,6 +635,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst //checkAddressAndCountry(); } + /** private boolean checkPortsAvailable () { int[] ports = {9050,9051,8118}; @@ -669,6 +660,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst return true; } + */ /* * activate means whether to apply the users preferences @@ -703,7 +695,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst { showToolbarNotification(getString(R.string.setting_up_app_based_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); - code = mTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this, getSharedPrefs(getApplicationContext()))); + code = mTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this, TorServiceUtils.getSharedPrefs(getApplicationContext()))); } @@ -752,7 +744,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst private void runTorShellCmd() throws Exception { - SharedPreferences prefs =getSharedPrefs(getApplicationContext()); + SharedPreferences prefs =TorServiceUtils.getSharedPrefs(getApplicationContext()); String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getAbsolutePath(); @@ -1360,8 +1352,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst } catch (IOException ioe) { - Log.e(TAG, "Unable to update Tor configuration", ioe); - logNotice("Unable to update Tor configuration: " + ioe.getMessage()); + + logException("Unable to get Tor configuration: " + ioe.getMessage(),ioe); } return null; @@ -1442,8 +1434,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst } catch (Exception ioe) { - Log.e(TAG, "Unable to update Tor configuration", ioe); - logNotice("Unable to update Tor configuration: " + ioe.getMessage()); + + logException("Unable to update Tor configuration: " + ioe.getMessage(),ioe); + if (configBuffer != null) + for (String config : configBuffer) + { + logNotice("Error applying config: " + config); + } + } return false; @@ -1569,13 +1567,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst @Override public void onReceive(Context context, Intent intent) { - SharedPreferences prefs = getSharedPrefs(getApplicationContext()); + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); mConnectivity = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); - boolean disableNetwork = prefs.getBoolean(TorConstants.PREF_DISABLE_NETWORK, true); + boolean doNetworKSleep = prefs.getBoolean(TorConstants.PREF_DISABLE_NETWORK, true); - if (currentStatus == STATUS_ON && disableNetwork) + if (doNetworKSleep && mBinder != null) { try { mBinder.updateConfiguration("DisableNetwork", mConnectivity ? "0" : "1", false); @@ -1591,13 +1589,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst { logNotice("Network connectivity is good. Waking Tor up..."); showToolbarNotification(getString(R.string.status_activated),NOTIFY_ID,R.drawable.ic_stat_tor,-1,prefPersistNotifications); - + } } catch (Exception e) { logException ("error updating state after network restart",e); } } + } }; @@ -1605,8 +1604,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst { logNotice("updating settings in Tor service"); - SharedPreferences prefs = getSharedPrefs(getApplicationContext()); + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); + enableSocks ("127.0.0.1",9050,false); + boolean useBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_ENABLED, false); //boolean autoUpdateBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_UPDATED, false); @@ -1685,14 +1686,33 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (useBridges) { - String bridgeList = prefs.getString(TorConstants.PREF_BRIDGES_LIST,getString(R.string.default_bridges)); + + logMessage ("Using bridges"); + boolean obfsBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_OBFUSCATED, false); + String bridgeCfgKey = "Bridge"; + + + if (obfsBridges) + { + String bridgeConfig = "obfs2,obfs3,scramblesuit exec " + fileObfsclient.getAbsolutePath(); + + logMessage ("Using OBFUSCATED bridges: " + bridgeConfig); + + mBinder.updateConfiguration("ClientTransportPlugin",bridgeConfig, false); + } + else + { + logMessage ("Using standard bridges"); + } + + String bridgeList = prefs.getString(TorConstants.PREF_BRIDGES_LIST,null); if (bridgeList == null || bridgeList.length() == 0) { String msgBridge = getString(R.string.bridge_requires_ip) + getString(R.string.send_email_for_bridges); showToolbarNotification(msgBridge, ERROR_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); - + logMessage(msgBridge); return false; } @@ -1708,25 +1728,15 @@ public class TorService extends Service implements TorServiceConstants, TorConst showToolbarNotification(getString(R.string.notification_using_bridges) + ": " + bridgeList, TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); - boolean obfsBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_OBFUSCATED, false); - String bridgeCfgKey = "bridge"; - - if (obfsBridges) - { - bridgeCfgKey = bridgeCfgKey + " obfsclient"; - } - + + StringTokenizer st = new StringTokenizer(bridgeList,bridgeDelim); while (st.hasMoreTokens()) { + String bridgeConfigLine = st.nextToken().trim(); + logMessage("Adding bridge: " + bridgeConfigLine); + mBinder.updateConfiguration(bridgeCfgKey, bridgeConfigLine, false); - mBinder.updateConfiguration(bridgeCfgKey, st.nextToken(), false); - - } - - if (obfsBridges) - { - mBinder.updateConfiguration("ClientTransportPlugin","obfsclient exec " + fileObfsclient.getAbsolutePath(), false); } mBinder.updateConfiguration("UpdateBridgesFromAuthority", "0", false); @@ -1813,6 +1823,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst hsPortConfig = hsPortConfig + " 0.0.0.0:" + hsPortConfig; } + logMessage("Adding hidden service on port: " + hsPortConfig); + mBinder.updateConfiguration("HiddenServicePort",hsPortConfig, false); hsPort = Integer.parseInt(hsPortConfig.split(" ")[0]); @@ -1837,6 +1849,17 @@ public class TorService extends Service implements TorServiceConstants, TorConst return true; } + private void enableSocks (String ip, int port, boolean safeSocks) throws RemoteException + { + mBinder.updateConfiguration("SOCKSPort", port + "", false); + mBinder.updateConfiguration("SOCKSListenAddress", ip, false); + mBinder.updateConfiguration("SafeSocks", safeSocks ? "1" : "0", false); + mBinder.updateConfiguration("TestSocks", "1", false); + mBinder.updateConfiguration("WarnUnsafeSocks", "1", false); + + + } + //using Google DNS for now as the public DNS server private String writeDNSFile () throws IOException { diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index 2fe9ff86..1b2fa5d0 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -77,10 +77,8 @@ public interface TorServiceConstants { public static final int DISABLE_TOR_MSG = 3; public static final int LOG_MSG = 4; - public static final String BINARY_TOR_VERSION = "0.2.4.21-openssl1.0.1g-if8"; - public static final String BINARY_PRIVOXY_VERSION = "3.0.12"; + public static final String BINARY_TOR_VERSION = "0.2.5.3-alpha-openssl1.0.1g-b87a"; public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED"; - public static final String PREF_BINARY_PRIVOXY_VERSION_INSTALLED = "BINARY_PRIVOXY_VERSION_INSTALLED"; //obfsproxy public static final String OBFSCLIENT_ASSET_KEY = "obfsclient"; diff --git a/src/org/torproject/android/service/TorServiceUtils.java b/src/org/torproject/android/service/TorServiceUtils.java index 5e87cb5f..fe6ad015 100644 --- a/src/org/torproject/android/service/TorServiceUtils.java +++ b/src/org/torproject/android/service/TorServiceUtils.java @@ -11,6 +11,11 @@ import java.util.StringTokenizer; import org.torproject.android.TorConstants; +import android.annotation.SuppressLint; +import android.annotation.TargetApi; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Build; import android.util.Log; public class TorServiceUtils implements TorServiceConstants { @@ -95,4 +100,14 @@ public class TorServiceUtils implements TorServiceConstants { return procId; } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static SharedPreferences getSharedPrefs (Context context) + { + if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) + return context.getSharedPreferences(TorConstants.PREF_TOR_SHARED_PREFS,0 | Context.MODE_MULTI_PROCESS); + else + return context.getSharedPreferences(TorConstants.PREF_TOR_SHARED_PREFS,Context.MODE_PRIVATE); + + } } diff --git a/src/org/torproject/android/service/TorTransProxy.java b/src/org/torproject/android/service/TorTransProxy.java index f7fd535a..57226124 100644 --- a/src/org/torproject/android/service/TorTransProxy.java +++ b/src/org/torproject/android/service/TorTransProxy.java @@ -34,7 +34,7 @@ public class TorTransProxy implements TorServiceConstants { String ipTablesPath = null; - SharedPreferences prefs = TorService.getSharedPrefs(context.getApplicationContext()); + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context); useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false); @@ -57,7 +57,7 @@ public class TorTransProxy implements TorServiceConstants { String ipTablesPath = null; - SharedPreferences prefs = TorService.getSharedPrefs(context.getApplicationContext()); + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context); useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false); diff --git a/src/org/torproject/android/settings/AppManager.java b/src/org/torproject/android/settings/AppManager.java index 6e2b90b3..b80fccd9 100644 --- a/src/org/torproject/android/settings/AppManager.java +++ b/src/org/torproject/android/settings/AppManager.java @@ -14,6 +14,7 @@ import java.util.StringTokenizer; import org.torproject.android.R; import org.torproject.android.TorConstants; import org.torproject.android.service.TorService; +import org.torproject.android.service.TorServiceUtils; import android.app.Activity; import android.content.Context; @@ -22,6 +23,7 @@ import android.content.SharedPreferences.Editor; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -50,7 +52,6 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC } - @Override protected void onResume() { super.onResume(); @@ -65,7 +66,7 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC } }); - mPrefs = TorService.getSharedPrefs(getApplicationContext()); + mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); loadApps(mPrefs); } diff --git a/src/org/torproject/android/settings/SettingsPreferences.java b/src/org/torproject/android/settings/SettingsPreferences.java index 84b8c91a..57d3fc4d 100644 --- a/src/org/torproject/android/settings/SettingsPreferences.java +++ b/src/org/torproject/android/settings/SettingsPreferences.java @@ -9,6 +9,7 @@ import org.sufficientlysecure.rootcommands.RootCommands; import org.torproject.android.R; import org.torproject.android.service.TorServiceUtils; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -42,8 +43,8 @@ public class SettingsPreferences super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + getPreferenceManager().setSharedPreferencesMode(Context.MODE_MULTI_PROCESS); + SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); hasRoot = prefs.getBoolean("has_root",false); @@ -104,6 +105,20 @@ public class SettingsPreferences + @Override + protected void onPause() { + + super.onPause(); + + + + + + + + } + + /* (non-Javadoc) * @see android.app.Activity#onStop() */ @@ -147,7 +162,7 @@ public class SettingsPreferences } else if (preference == prefLocale) { - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + SharedPreferences settings = TorServiceUtils.getSharedPrefs(getApplicationContext()); Configuration config = getResources().getConfiguration();