fixes for sharing preferences across processes

This commit is contained in:
Nathan Freitas 2013-08-23 16:44:15 -04:00
parent 9a10cdda9d
commit e632b3b04f
6 changed files with 175 additions and 203 deletions

View File

@ -7,6 +7,7 @@ import java.util.Locale;
import org.torproject.android.service.ITorService; import org.torproject.android.service.ITorService;
import org.torproject.android.service.ITorServiceCallback; import org.torproject.android.service.ITorServiceCallback;
import org.torproject.android.service.TorService;
import org.torproject.android.service.TorServiceConstants; import org.torproject.android.service.TorServiceConstants;
import org.torproject.android.settings.ProcessSettingsAsyncTask; import org.torproject.android.settings.ProcessSettingsAsyncTask;
import org.torproject.android.settings.SettingsPreferences; import org.torproject.android.settings.SettingsPreferences;
@ -81,12 +82,6 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick
SharedPreferences mPrefs; SharedPreferences mPrefs;
public static Orbot currentInstance = null;
private static void setCurrent(Orbot current){
Orbot.currentInstance = current;
}
/** 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);
@ -103,11 +98,9 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick
} }
*/ */
mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); mPrefs = getPrefs();
mPrefs.registerOnSharedPreferenceChangeListener(this); mPrefs.registerOnSharedPreferenceChangeListener(this);
Orbot.setCurrent(this);
//if Tor binary is not running, then start the service up //if Tor binary is not running, then start the service up
//might want to look at whether we need to call this every time //might want to look at whether we need to call this every time
//or whether binding to the service is enough //or whether binding to the service is enough
@ -164,7 +157,7 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
ClipboardManager cm = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); ClipboardManager cm = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(mTxtOrbotLog.getText()); cm.setText(mTxtOrbotLog.getText());
Toast.makeText(Orbot.this, "LOG COPIED TO CLIPBOARD. PLEASE EMAIL TO help@guardianproject.info TO DEBUG PROBLEM", Toast.LENGTH_SHORT).show(); Toast.makeText(Orbot.this, "LOG COPIED TO CLIPBOARD", Toast.LENGTH_SHORT).show();
return true; return true;
} }
}); });
@ -1265,4 +1258,9 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick
} }
private SharedPreferences getPrefs ()
{
return TorService.getSharedPrefs(getApplicationContext());
}
} }

View File

@ -52,5 +52,5 @@ public interface TorConstants {
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 = "torprefs"; public final static String PREF_TOR_SHARED_PREFS = "org.torproject.android_preferences";
} }

View File

@ -44,13 +44,12 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteCallbackList; import android.os.RemoteCallbackList;
import android.os.RemoteException; import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
@ -249,23 +248,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
super.onStart(intent, startId); super.onStart(intent, startId);
_torInstance = this; _torInstance = this;
/*
prefs = getSharedPreferences(TorConstants.PREF_TOR_SHARED_PREFS,Context.MODE_MULTI_PROCESS);
*/
/*
mPrefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener()
{
@Override android.os.Debug.waitForDebugger();
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
updateSettings();
}
});*/
initTorPaths(); initTorPaths();
@ -297,7 +281,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 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("pref_start_boot",false); boolean startOnBoot = getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false);
if (startOnBoot) if (startOnBoot)
{ {
@ -305,6 +289,16 @@ 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 ()
{ {
@ -366,7 +360,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
currentStatus = STATUS_OFF; currentStatus = STATUS_OFF;
boolean hasRoot = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(PREF_HAS_ROOT,false); boolean hasRoot = getSharedPrefs(getApplicationContext()).getBoolean("has_root", false);
try try
{ {
@ -424,7 +418,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private String getHiddenServiceHostname () private String getHiddenServiceHostname ()
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences prefs = getSharedPrefs(getApplicationContext());
boolean enableHiddenServices = prefs.getBoolean("pref_hs_enable", false); boolean enableHiddenServices = prefs.getBoolean("pref_hs_enable", false);
@ -547,7 +541,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public boolean checkTorBinaries (boolean forceInstall) throws Exception public boolean checkTorBinaries (boolean forceInstall) throws Exception
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences prefs =getSharedPrefs(getApplicationContext());
//check and install iptables //check and install iptables
TorBinaryInstaller.assertIpTablesBinaries(this, true); TorBinaryInstaller.assertIpTablesBinaries(this, true);
@ -651,7 +645,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private void updateSettings () private void updateSettings ()
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences prefs = 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);
@ -666,6 +660,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void initTor () throws Exception public void initTor () throws Exception
{ {
android.os.Debug.waitForDebugger();
updateSettings (); updateSettings ();
@ -725,7 +720,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); showToolbarNotification(getString(R.string.setting_up_app_based_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1);
code = mTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this)); code = mTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this, getSharedPrefs(getApplicationContext())));
} }
@ -767,7 +762,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
mTransProxy = new TorTransProxy(this); mTransProxy = new TorTransProxy(this);
mTransProxy.clearTransparentProxyingAll(this); mTransProxy.clearTransparentProxyingAll(this);
mTransProxy.clearTransparentProxyingByApp(this,AppManager.getApps(this)); // mTransProxy.clearTransparentProxyingByApp(this,AppManager.getApps(this));
clearNotifications(); clearNotifications();
@ -776,7 +771,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private void runTorShellCmd() throws Exception private void runTorShellCmd() throws Exception
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences prefs =getSharedPrefs(getApplicationContext());
StringBuilder log = new StringBuilder(); StringBuilder log = new StringBuilder();
@ -1083,6 +1078,12 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
if (mNotificationManager == null)
{
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
mNotifyBuilder.setOngoing(persistent); mNotifyBuilder.setOngoing(persistent);
mNotifyBuilder.setContentText(message); mNotifyBuilder.setContentText(message);
@ -1235,28 +1236,32 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void circuitStatus(String status, String circID, String path) { public void circuitStatus(String status, String circID, String path) {
StringBuilder sb = new StringBuilder(); if (status.equals("BUILT") || status.equals("CLOSED"))
sb.append("Circuit (");
sb.append((circID));
sb.append(") ");
sb.append(status);
sb.append(": ");
StringTokenizer st = new StringTokenizer(path,",");
String node = null;
while (st.hasMoreTokens())
{ {
node = st.nextToken(); StringBuilder sb = new StringBuilder();
sb.append("Circuit (");
sb.append((circID));
sb.append(") ");
sb.append(status);
sb.append(": ");
sb.append(parseNodeName(node)); StringTokenizer st = new StringTokenizer(path,",");
String node = null;
while (st.hasMoreTokens())
{
node = st.nextToken();
sb.append(parseNodeName(node));
if (st.hasMoreTokens())
sb.append (" > ");
}
if (st.hasMoreTokens()) logNotice(sb.toString());
sb.append (" > ");
} }
logNotice(sb.toString());
} }
@ -1618,7 +1623,7 @@ 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 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences prefs = getSharedPrefs(getApplicationContext());
mConnectivity = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); mConnectivity = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
@ -1650,7 +1655,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private boolean processSettingsImpl () throws RemoteException private boolean processSettingsImpl () throws RemoteException
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences prefs = getSharedPrefs(getApplicationContext());
boolean useBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_ENABLED, false); boolean useBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_ENABLED, false);

View File

@ -132,7 +132,8 @@ public class TorServiceUtils implements TorServiceConstants {
for (int i = 0; i < cmds.length; i++) for (int i = 0; i < cmds.length; i++)
{ {
// TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); if (TorService.ENABLE_DEBUG_LOG)
Log.d(TorService.TAG,"executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);
out.write(cmds[i]); out.write(cmds[i]);
out.write("\n"); out.write("\n");
@ -169,4 +170,54 @@ public class TorServiceUtils implements TorServiceConstants {
return exitCode; return exitCode;
} }
public static int doShellCommand(String cmd, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception
{
Process proc = null;
int exitCode = -1;
if (runAsRoot)
proc = Runtime.getRuntime().exec("su");
else
proc = Runtime.getRuntime().exec("sh");
OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());
// TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);
out.write(cmd);
out.write("\n");
out.flush();
out.write("exit\n");
out.flush();
if (waitFor)
{
final char buf[] = new char[10];
// Consume the "stdout"
InputStreamReader reader = new InputStreamReader(proc.getInputStream());
int read=0;
while ((read=reader.read(buf)) != -1) {
if (log != null) log.append(buf, 0, read);
}
// Consume the "stderr"
reader = new InputStreamReader(proc.getErrorStream());
read=0;
while ((read=reader.read(buf)) != -1) {
if (log != null) log.append(buf, 0, read);
}
exitCode = proc.waitFor();
}
return exitCode;
}
} }

View File

@ -31,7 +31,8 @@ public class TorTransProxy implements TorServiceConstants {
String ipTablesPath = null; String ipTablesPath = null;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); SharedPreferences prefs = TorService.getSharedPrefs(context.getApplicationContext());
useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false); useSystemIpTables = prefs.getBoolean(TorConstants.PREF_USE_SYSTEM_IPTABLES, false);
if (useSystemIpTables) if (useSystemIpTables)
@ -233,7 +234,7 @@ public class TorTransProxy implements TorServiceConstants {
} }
*/ */
/*
public int clearTransparentProxyingByApp (Context context, ArrayList<TorifiedApp> apps) throws Exception public int clearTransparentProxyingByApp (Context context, ArrayList<TorifiedApp> apps) throws Exception
{ {
boolean runRoot = true; boolean runRoot = true;
@ -270,7 +271,7 @@ public class TorTransProxy implements TorServiceConstants {
logMessage(cmdAdd[0] + ";errCode=" + code + ";resp=" + msg); logMessage(cmdAdd[0] + ";errCode=" + code + ";resp=" + msg);
return code; return code;
} }*/
public int setTransparentProxyingByApp(Context context, ArrayList<TorifiedApp> apps) throws Exception public int setTransparentProxyingByApp(Context context, ArrayList<TorifiedApp> apps) throws Exception
{ {
@ -285,21 +286,8 @@ public class TorTransProxy implements TorServiceConstants {
StringBuilder res = new StringBuilder(); StringBuilder res = new StringBuilder();
int code = -1; int code = -1;
String chainName = "ORBOT"; String srcChainName = "OUTPUT";
String jumpChainName = "OUTPUT";
script.append(ipTablesPath);
script.append(" --flush ").append(chainName); //delete previous user-defined chain
script.append(" || exit\n");
script.append(ipTablesPath);
script.append(" -D ").append(jumpChainName);
script.append(" -j ").append(chainName);
script.append(" || exit\n");
script.append(ipTablesPath);
script.append(" -X ").append(chainName); //delete previous user-defined chain
script.append(" || exit\n");
//run the delete commands in a separate process as it might error out //run the delete commands in a separate process as it might error out
String[] cmdExecClear = {script.toString()}; String[] cmdExecClear = {script.toString()};
@ -307,16 +295,7 @@ public class TorTransProxy implements TorServiceConstants {
//reset script //reset script
script = new StringBuilder(); script = new StringBuilder();
script.append(ipTablesPath);
script.append(" -N ").append(chainName); //create user-defined chain
script.append(" || exit\n");
script.append(ipTablesPath);
script.append(" -A ").append(jumpChainName);
script.append(" -j ").append(chainName);
script.append(" || exit\n");
//build up array of shell cmds to execute under one root context //build up array of shell cmds to execute under one root context
for (TorifiedApp tApp:apps) for (TorifiedApp tApp:apps)
{ {
@ -332,7 +311,7 @@ public class TorTransProxy implements TorServiceConstants {
// Set up port redirection // Set up port redirection
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t nat"); script.append(" -t nat");
script.append(" -A ").append(jumpChainName); script.append(" -A ").append(srcChainName);
script.append(" -p tcp"); script.append(" -p tcp");
script.append(" ! -d 127.0.0.1"); //allow access to localhost script.append(" ! -d 127.0.0.1"); //allow access to localhost
script.append(" -m owner --uid-owner "); script.append(" -m owner --uid-owner ");
@ -345,7 +324,7 @@ public class TorTransProxy implements TorServiceConstants {
// Same for DNS // Same for DNS
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t nat"); script.append(" -t nat");
script.append(" -A ").append(jumpChainName); script.append(" -A ").append(srcChainName);
script.append(" -p udp -m owner --uid-owner "); script.append(" -p udp -m owner --uid-owner ");
script.append(tApp.getUid()); script.append(tApp.getUid());
script.append(" -m udp --dport "); script.append(" -m udp --dport ");
@ -361,7 +340,7 @@ public class TorTransProxy implements TorServiceConstants {
// Allow packets to localhost (contains all the port-redirected ones) // Allow packets to localhost (contains all the port-redirected ones)
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(jumpChainName); script.append(" -A ").append(srcChainName);
script.append(" -m owner --uid-owner "); script.append(" -m owner --uid-owner ");
script.append(tApp.getUid()); script.append(tApp.getUid());
script.append(" -p tcp"); script.append(" -p tcp");
@ -375,7 +354,7 @@ public class TorTransProxy implements TorServiceConstants {
// Allow loopback // Allow loopback
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(jumpChainName); script.append(" -A ").append(srcChainName);
script.append(" -m owner --uid-owner "); script.append(" -m owner --uid-owner ");
script.append(tApp.getUid()); script.append(tApp.getUid());
script.append(" -p tcp"); script.append(" -p tcp");
@ -386,7 +365,7 @@ public class TorTransProxy implements TorServiceConstants {
// Reject all other outbound TCP packets // Reject all other outbound TCP packets
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(jumpChainName); script.append(" -A ").append(srcChainName);
script.append(" -m owner --uid-owner "); script.append(" -m owner --uid-owner ");
script.append(tApp.getUid()); script.append(tApp.getUid());
script.append(" -p tcp"); script.append(" -p tcp");
@ -397,7 +376,7 @@ public class TorTransProxy implements TorServiceConstants {
// Reject all other outbound UDP packets // Reject all other outbound UDP packets
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(jumpChainName); script.append(" -A ").append(srcChainName);
script.append(" -m owner --uid-owner "); script.append(" -m owner --uid-owner ");
script.append(tApp.getUid()); script.append(tApp.getUid());
script.append(" -p udp"); script.append(" -p udp");
@ -475,37 +454,31 @@ public class TorTransProxy implements TorServiceConstants {
boolean runRoot = true; boolean runRoot = true;
boolean waitFor = true; boolean waitFor = true;
//redirectDNSResolvConf(); //not working yet
String ipTablesPath = getIpTablesPath(context); String ipTablesPath = getIpTablesPath(context);
StringBuilder script = new StringBuilder(); StringBuilder script = new StringBuilder();
StringBuilder res = new StringBuilder(); StringBuilder res = new StringBuilder();
int code = -1; int code = -1;
String chainName = "ORBOT"; String chainName = "OUTPUT";
String jumpChainName = "OUTPUT";
script.append(ipTablesPath);
script.append(" --flush ").append(chainName); //delete previous user-defined chain
script.append(" || exit\n");
script = new StringBuilder();
res = new StringBuilder();
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -D ").append(jumpChainName); script.append(" -t nat");
script.append(" -j ").append(chainName); script.append(" -F ").append(chainName); //delete previous user-defined chain
script.append(" || exit\n");
script.append(ipTablesPath);
script.append(" -X ").append(chainName); //delete previous user-defined chain
script.append(" || exit\n"); script.append(" || exit\n");
code = TorServiceUtils.doShellCommand(script.toString(), res, runRoot, waitFor);
logMessage("Exec resp: cmd> " + script.toString() + "; errCode=" + code + ";resp=" + res.toString());
String[] cmdExec = {script.toString()}; script = new StringBuilder();
res = new StringBuilder();
code = TorServiceUtils.doShellCommand(cmdExec, res, runRoot, waitFor); script.append(ipTablesPath);
String msg = res.toString(); script.append(" -t filter");
script.append(" -F ").append(chainName); //delete previous user-defined chain
logMessage("Exec resp: errCode=" + code + ";resp=" + msg); script.append(" || exit\n");
code = TorServiceUtils.doShellCommand(script.toString(), res, runRoot, waitFor);
logMessage("Exec resp: cmd> " + script.toString() + "; errCode=" + code + ";resp=" + res.toString());
return code; return code;
} }
@ -527,51 +500,21 @@ public class TorTransProxy implements TorServiceConstants {
int torUid = context.getApplicationInfo().uid; int torUid = context.getApplicationInfo().uid;
String chainName = "ORBOT"; String srcChainName = "OUTPUT";
String jumpChainName = "OUTPUT";
script.append(ipTablesPath);
script.append(" --flush ").append(chainName); //delete previous user-defined chain
script.append(" || exit\n");
script.append(ipTablesPath);
script.append(" -D ").append(jumpChainName);
script.append(" -j ").append(chainName);
script.append(" || exit\n");
script.append(ipTablesPath);
script.append(" -X ").append(chainName); //delete previous user-defined chain
script.append(" || exit\n");
//run the delete commands in a separate process as it might error out
String[] cmdExecClear = {script.toString()};
code = TorServiceUtils.doShellCommand(cmdExecClear, res, runRoot, waitFor);
//reset script
script = new StringBuilder();
script.append(ipTablesPath);
script.append(" -N ").append(chainName); //create user-defined chain
script.append(" || exit\n");
script.append(ipTablesPath);
script.append(" -A ").append(jumpChainName);
script.append(" -j ").append(chainName);
script.append(" || exit\n");
// Allow everything for Tor // Allow everything for Tor
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(chainName); script.append(" -A ").append(srcChainName);
script.append(" -m owner --uid-owner "); script.append(" -m owner --uid-owner ");
script.append(torUid); script.append(torUid);
script.append(" -j ACCEPT"); script.append(" -j ACCEPT");
script.append(" || exit\n"); script.append(" || exit\n");
// Set up port redirection // Set up port redirection
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t nat"); script.append(" -t nat");
script.append(" -A ").append(jumpChainName); script.append(" -A ").append(srcChainName);
script.append(" -p tcp"); script.append(" -p tcp");
script.append(" ! -d 127.0.0.1"); //allow access to localhost script.append(" ! -d 127.0.0.1"); //allow access to localhost
script.append(" -m owner ! --uid-owner "); script.append(" -m owner ! --uid-owner ");
@ -584,7 +527,7 @@ public class TorTransProxy implements TorServiceConstants {
// Same for DNS // Same for DNS
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t nat"); script.append(" -t nat");
script.append(" -A ").append(jumpChainName); script.append(" -A ").append(srcChainName);
script.append(" -p udp -m owner ! --uid-owner "); script.append(" -p udp -m owner ! --uid-owner ");
script.append(torUid); script.append(torUid);
script.append(" -m udp --dport "); script.append(" -m udp --dport ");
@ -598,9 +541,9 @@ public class TorTransProxy implements TorServiceConstants {
for (int port : ports) for (int port : ports)
{ {
// Allow packets to localhost (contains all the port-redirected ones) // Allow packets to localhost (contains all the port-redirected ones)
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(chainName); script.append(" -A ").append(srcChainName);
script.append(" -m owner ! --uid-owner "); script.append(" -m owner ! --uid-owner ");
script.append(torUid); script.append(torUid);
script.append(" -p tcp"); script.append(" -p tcp");
@ -615,7 +558,7 @@ public class TorTransProxy implements TorServiceConstants {
// Allow loopback // Allow loopback
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(chainName); script.append(" -A ").append(srcChainName);
script.append(" -p tcp"); script.append(" -p tcp");
script.append(" -o lo"); script.append(" -o lo");
script.append(" -j ACCEPT"); script.append(" -j ACCEPT");
@ -625,9 +568,9 @@ public class TorTransProxy implements TorServiceConstants {
if (TorService.ENABLE_DEBUG_LOG) if (TorService.ENABLE_DEBUG_LOG)
{ {
//XXX: Comment the following rules for non-debug builds //XXX: Comment the following rules for non-debug builds
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(chainName); script.append(" -A ").append(srcChainName);
script.append(" -p udp"); script.append(" -p udp");
script.append(" --dport "); script.append(" --dport ");
script.append(STANDARD_DNS_PORT); script.append(STANDARD_DNS_PORT);
@ -636,9 +579,9 @@ public class TorTransProxy implements TorServiceConstants {
script.append(" --log-uid"); script.append(" --log-uid");
script.append(" || exit\n"); script.append(" || exit\n");
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(chainName); script.append(" -A ").append(srcChainName);
script.append(" -p tcp"); script.append(" -p tcp");
script.append(" -j LOG"); script.append(" -j LOG");
script.append(" --log-prefix='ORBOT_TCPLEAK_PROTECTION'"); script.append(" --log-prefix='ORBOT_TCPLEAK_PROTECTION'");
@ -650,7 +593,7 @@ public class TorTransProxy implements TorServiceConstants {
// Reject all other outbound TCP packets // Reject all other outbound TCP packets
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(chainName); script.append(" -A ").append(srcChainName);
script.append(" -m owner ! --uid-owner "); script.append(" -m owner ! --uid-owner ");
script.append(torUid); script.append(torUid);
script.append(" -p tcp"); script.append(" -p tcp");
@ -661,7 +604,7 @@ public class TorTransProxy implements TorServiceConstants {
// Reject all other outbound UDP packets // Reject all other outbound UDP packets
script.append(ipTablesPath); script.append(ipTablesPath);
script.append(" -t filter"); script.append(" -t filter");
script.append(" -A ").append(chainName); script.append(" -A ").append(srcChainName);
script.append(" -m owner ! --uid-owner "); script.append(" -m owner ! --uid-owner ");
script.append(torUid); script.append(torUid);
script.append(" -p udp"); script.append(" -p udp");

View File

@ -6,12 +6,14 @@ package org.torproject.android.settings;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; 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 android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
@ -21,7 +23,6 @@ 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.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@ -36,24 +37,15 @@ import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
public class AppManager extends Activity implements OnCheckedChangeListener, OnClickListener, TorConstants { public class AppManager extends Activity implements OnCheckedChangeListener, OnClickListener, TorConstants {
private static ArrayList<TorifiedApp> apps = null;
private ListView listApps; private ListView listApps;
private AppManager mAppManager;
private boolean appsLoaded = false;
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
this.setContentView(R.layout.layout_apps); this.setContentView(R.layout.layout_apps);
mAppManager = this;
} }
@ -73,16 +65,17 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
} }
}); });
if (!appsLoaded) mPrefs = TorService.getSharedPrefs(getApplicationContext());
loadApps(); loadApps(mPrefs);
} }
SharedPreferences mPrefs = null;
ArrayList<TorifiedApp> mApps = null;
private void loadApps ()
private void loadApps (SharedPreferences prefs)
{ {
resetApps(this);
final ArrayList<TorifiedApp> apps = getApps(this); mApps = getApps(getApplicationContext(), prefs);
/* /*
Arrays.sort(apps, new Comparator<TorifiedApp>() { Arrays.sort(apps, new Comparator<TorifiedApp>() {
@ -91,12 +84,11 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
if (o1.isTorified()) return -1; if (o1.isTorified()) return -1;
return 1; return 1;
} }
}); });*/
*/
final LayoutInflater inflater = getLayoutInflater(); final LayoutInflater inflater = getLayoutInflater();
final ListAdapter adapter = new ArrayAdapter<TorifiedApp>(this,R.layout.layout_apps_item,R.id.itemtext,apps) { ListAdapter adapter = new ArrayAdapter<TorifiedApp>(this,R.layout.layout_apps_item,R.id.itemtext,mApps) {
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
ListEntry entry; ListEntry entry;
if (convertView == null) { if (convertView == null) {
@ -107,19 +99,19 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
entry.box = (CheckBox) convertView.findViewById(R.id.itemcheck); entry.box = (CheckBox) convertView.findViewById(R.id.itemcheck);
entry.text = (TextView) convertView.findViewById(R.id.itemtext); entry.text = (TextView) convertView.findViewById(R.id.itemtext);
entry.text.setOnClickListener(mAppManager); entry.text.setOnClickListener(AppManager.this);
entry.text.setOnClickListener(mAppManager); entry.text.setOnClickListener(AppManager.this);
convertView.setTag(entry); convertView.setTag(entry);
entry.box.setOnCheckedChangeListener(mAppManager); entry.box.setOnCheckedChangeListener(AppManager.this);
} else { } else {
// Convert an existing view // Convert an existing view
entry = (ListEntry) convertView.getTag(); entry = (ListEntry) convertView.getTag();
} }
final TorifiedApp app = apps.get(position); final TorifiedApp app = mApps.get(position);
entry.icon.setImageDrawable(app.getIcon()); entry.icon.setImageDrawable(app.getIcon());
@ -138,8 +130,6 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
listApps.setAdapter(adapter); listApps.setAdapter(adapter);
appsLoaded = true;
} }
private static class ListEntry { private static class ListEntry {
@ -157,19 +147,10 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
} }
public static ArrayList<TorifiedApp> getApps (Context context)
{
if (apps == null)
resetApps(context);
return apps;
}
public static ArrayList<TorifiedApp> resetApps (Context context) public static ArrayList<TorifiedApp> getApps (Context context, SharedPreferences prefs)
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String tordAppString = prefs.getString(PREFS_KEY_TORIFIED, ""); String tordAppString = prefs.getString(PREFS_KEY_TORIFIED, "");
String[] tordApps; String[] tordApps;
@ -190,7 +171,7 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
Iterator<ApplicationInfo> itAppInfo = lAppInfo.iterator(); Iterator<ApplicationInfo> itAppInfo = lAppInfo.iterator();
apps = new ArrayList<TorifiedApp>(); ArrayList<TorifiedApp> apps = new ArrayList<TorifiedApp>();
ApplicationInfo aInfo = null; ApplicationInfo aInfo = null;
@ -269,16 +250,10 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
public void saveAppSettings (Context context) public void saveAppSettings (Context context)
{ {
if (apps == null)
return;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// final SharedPreferences prefs = context.getSharedPreferences(PREFS_KEY, 0);
StringBuilder tordApps = new StringBuilder(); StringBuilder tordApps = new StringBuilder();
for (TorifiedApp tApp:apps) for (TorifiedApp tApp:mApps)
{ {
if (tApp.isTorified()) if (tApp.isTorified())
{ {
@ -287,7 +262,7 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
} }
} }
Editor edit = prefs.edit(); Editor edit = mPrefs.edit();
edit.putString(PREFS_KEY_TORIFIED, tordApps.toString()); edit.putString(PREFS_KEY_TORIFIED, tordApps.toString());
edit.commit(); edit.commit();