fixes for preference handling in multi process context

This commit is contained in:
Nathan Freitas 2014-04-25 12:44:21 -04:00
parent 38700f9c9c
commit 144460beed
8 changed files with 115 additions and 59 deletions

View File

@ -1,12 +1,12 @@
package org.torproject.android; package org.torproject.android;
import org.torproject.android.service.TorService; import org.torproject.android.service.TorService;
import org.torproject.android.service.TorServiceUtils;
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.content.SharedPreferences;
import android.preference.PreferenceManager;
public class OnBootReceiver extends BroadcastReceiver { public class OnBootReceiver extends BroadcastReceiver {
@ -16,7 +16,8 @@ public class OnBootReceiver extends BroadcastReceiver {
if (intent.getAction() != null if (intent.getAction() != null
&& intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) && 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); boolean startOnBoot = prefs.getBoolean("pref_start_boot",false);
@ -34,5 +35,6 @@ public class OnBootReceiver extends BroadcastReceiver {
} }
} }

View File

@ -2,6 +2,8 @@ package org.torproject.android;
import java.util.Locale; import java.util.Locale;
import org.torproject.android.service.TorServiceUtils;
import android.app.Application; import android.app.Application;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
@ -18,7 +20,7 @@ public class OrbotApp extends Application implements TorConstants
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
settings = PreferenceManager.getDefaultSharedPreferences(this); settings = TorServiceUtils.getSharedPrefs(getApplicationContext());
Configuration config = getResources().getConfiguration(); Configuration config = getResources().getConfiguration();

View File

@ -292,7 +292,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot")) 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) 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 // We want this service to continue running until it is explicitly
// stopped, so return sticky. // stopped, so return sticky.
return START_STICKY; return START_NOT_STICKY;
} }
catch (Exception e) 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 () public void run ()
@ -341,11 +334,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (Exception e) 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; currentStatus = STATUS_OFF;
this.showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, -1, false); 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 () private String getHiddenServiceHostname ()
{ {
SharedPreferences prefs = getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
boolean enableHiddenServices = prefs.getBoolean("pref_hs_enable", false); 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); 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); String version = prefs.getString(PREF_BINARY_TOR_VERSION_INSTALLED,null);
logNotice("checking binary version: " + version); logNotice("checking binary version: " + version);
@ -595,7 +587,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private void updateSettings () private void updateSettings ()
{ {
SharedPreferences prefs = getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
mHasRoot = prefs.getBoolean(PREF_HAS_ROOT,false); mHasRoot = prefs.getBoolean(PREF_HAS_ROOT,false);
mEnableTransparentProxy = prefs.getBoolean("pref_transparent", false); mEnableTransparentProxy = prefs.getBoolean("pref_transparent", false);
@ -611,8 +603,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void initTor () throws Exception public void initTor () throws Exception
{ {
boolean portsAvail = checkPortsAvailable();
try try
{ {
initBinaries(); initBinaries();
@ -645,6 +635,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
//checkAddressAndCountry(); //checkAddressAndCountry();
} }
/**
private boolean checkPortsAvailable () private boolean checkPortsAvailable ()
{ {
int[] ports = {9050,9051,8118}; int[] ports = {9050,9051,8118};
@ -669,6 +660,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return true; return true;
} }
*/
/* /*
* activate means whether to apply the users preferences * 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); 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 private void runTorShellCmd() throws Exception
{ {
SharedPreferences prefs =getSharedPrefs(getApplicationContext()); SharedPreferences prefs =TorServiceUtils.getSharedPrefs(getApplicationContext());
String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getAbsolutePath(); String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getAbsolutePath();
@ -1360,8 +1352,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (IOException ioe) 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; return null;
@ -1442,8 +1434,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (Exception ioe) 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; return false;
@ -1569,13 +1567,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
mConnectivity = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 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 { try {
mBinder.updateConfiguration("DisableNetwork", mConnectivity ? "0" : "1", false); 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..."); logNotice("Network connectivity is good. Waking Tor up...");
showToolbarNotification(getString(R.string.status_activated),NOTIFY_ID,R.drawable.ic_stat_tor,-1,prefPersistNotifications); showToolbarNotification(getString(R.string.status_activated),NOTIFY_ID,R.drawable.ic_stat_tor,-1,prefPersistNotifications);
} }
} catch (Exception e) { } catch (Exception e) {
logException ("error updating state after network restart",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"); 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 useBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_ENABLED, false);
//boolean autoUpdateBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_UPDATED, false); //boolean autoUpdateBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_UPDATED, false);
@ -1685,14 +1686,33 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (useBridges) 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) if (bridgeList == null || bridgeList.length() == 0)
{ {
String msgBridge = getString(R.string.bridge_requires_ip) + String msgBridge = getString(R.string.bridge_requires_ip) +
getString(R.string.send_email_for_bridges); getString(R.string.send_email_for_bridges);
showToolbarNotification(msgBridge, ERROR_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); showToolbarNotification(msgBridge, ERROR_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false);
logMessage(msgBridge);
return false; 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); 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); StringTokenizer st = new StringTokenizer(bridgeList,bridgeDelim);
while (st.hasMoreTokens()) 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); mBinder.updateConfiguration("UpdateBridgesFromAuthority", "0", false);
@ -1813,6 +1823,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
hsPortConfig = hsPortConfig + " 0.0.0.0:" + hsPortConfig; hsPortConfig = hsPortConfig + " 0.0.0.0:" + hsPortConfig;
} }
logMessage("Adding hidden service on port: " + hsPortConfig);
mBinder.updateConfiguration("HiddenServicePort",hsPortConfig, false); mBinder.updateConfiguration("HiddenServicePort",hsPortConfig, false);
hsPort = Integer.parseInt(hsPortConfig.split(" ")[0]); hsPort = Integer.parseInt(hsPortConfig.split(" ")[0]);
@ -1837,6 +1849,17 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return true; 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 //using Google DNS for now as the public DNS server
private String writeDNSFile () throws IOException private String writeDNSFile () throws IOException
{ {

View File

@ -77,10 +77,8 @@ public interface TorServiceConstants {
public static final int DISABLE_TOR_MSG = 3; public static final int DISABLE_TOR_MSG = 3;
public static final int LOG_MSG = 4; 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_TOR_VERSION = "0.2.5.3-alpha-openssl1.0.1g-b87a";
public static final String BINARY_PRIVOXY_VERSION = "3.0.12";
public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED"; 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 //obfsproxy
public static final String OBFSCLIENT_ASSET_KEY = "obfsclient"; public static final String OBFSCLIENT_ASSET_KEY = "obfsclient";

View File

@ -11,6 +11,11 @@ import java.util.StringTokenizer;
import org.torproject.android.TorConstants; 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; import android.util.Log;
public class TorServiceUtils implements TorServiceConstants { public class TorServiceUtils implements TorServiceConstants {
@ -95,4 +100,14 @@ public class TorServiceUtils implements TorServiceConstants {
return procId; 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);
}
} }

View File

@ -34,7 +34,7 @@ public class TorTransProxy implements TorServiceConstants {
String ipTablesPath = null; String ipTablesPath = null;
SharedPreferences prefs = TorService.getSharedPrefs(context.getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context);
useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false); useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false);
@ -57,7 +57,7 @@ public class TorTransProxy implements TorServiceConstants {
String ipTablesPath = null; String ipTablesPath = null;
SharedPreferences prefs = TorService.getSharedPrefs(context.getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context);
useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false); useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false);

View File

@ -14,6 +14,7 @@ import java.util.StringTokenizer;
import org.torproject.android.R; import org.torproject.android.R;
import org.torproject.android.TorConstants; import org.torproject.android.TorConstants;
import org.torproject.android.service.TorService; import org.torproject.android.service.TorService;
import org.torproject.android.service.TorServiceUtils;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
@ -22,6 +23,7 @@ import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -50,7 +52,6 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.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); loadApps(mPrefs);
} }

View File

@ -9,6 +9,7 @@ import org.sufficientlysecure.rootcommands.RootCommands;
import org.torproject.android.R; import org.torproject.android.R;
import org.torproject.android.service.TorServiceUtils; import org.torproject.android.service.TorServiceUtils;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
@ -42,8 +43,8 @@ public class SettingsPreferences
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
getPreferenceManager().setSharedPreferencesMode(Context.MODE_MULTI_PROCESS);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
hasRoot = prefs.getBoolean("has_root",false); hasRoot = prefs.getBoolean("has_root",false);
@ -104,6 +105,20 @@ public class SettingsPreferences
@Override
protected void onPause() {
super.onPause();
}
/* (non-Javadoc) /* (non-Javadoc)
* @see android.app.Activity#onStop() * @see android.app.Activity#onStop()
*/ */
@ -147,7 +162,7 @@ public class SettingsPreferences
} }
else if (preference == prefLocale) else if (preference == prefLocale)
{ {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences settings = TorServiceUtils.getSharedPrefs(getApplicationContext());
Configuration config = getResources().getConfiguration(); Configuration config = getResources().getConfiguration();