found and fixed major bug in per-app trans proxing - list of apps was being cached and iptables rules were not properly updated as the user changed the selection in the list

svn:r22802
This commit is contained in:
Nathan Freitas 2010-08-04 10:16:38 +00:00
parent e157ecd92a
commit e62186bf82
6 changed files with 78 additions and 84 deletions

View File

@ -17,6 +17,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
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;
@ -145,10 +146,8 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
public static TorifiedApp[] getApps (Context context) public static TorifiedApp[] getApps (Context context)
{ {
if (apps != null)
return apps;
final SharedPreferences prefs = context.getSharedPreferences(PREFS_KEY, 0); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String tordAppString = prefs.getString(PREFS_KEY_TORIFIED, ""); String tordAppString = prefs.getString(PREFS_KEY_TORIFIED, "");
String[] tordApps; String[] tordApps;
@ -205,12 +204,14 @@ public class AppManager extends Activity implements OnCheckedChangeListener, OnC
} }
public static void saveAppSettings (Context context) public void saveAppSettings (Context context)
{ {
if (apps == null) if (apps == null)
return; return;
final SharedPreferences prefs = context.getSharedPreferences(PREFS_KEY, 0); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// final SharedPreferences prefs = context.getSharedPreferences(PREFS_KEY, 0);
StringBuilder tordApps = new StringBuilder(); StringBuilder tordApps = new StringBuilder();

View File

@ -256,15 +256,6 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll(); mNotificationManager.cancelAll();
if (mService != null)
{
try {
processSettings();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mOrbot); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mOrbot);
@ -281,8 +272,14 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
showHelp(); showHelp();
} }
else
{
}
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -332,7 +329,7 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
lblStatus = (TextView)findViewById(R.id.lblStatus); lblStatus = (TextView)findViewById(R.id.lblStatus);
imgStatus = (ImageView)findViewById(R.id.imgStatus); imgStatus = (ImageView)findViewById(R.id.imgStatus);
//updateStatus(""); updateStatus("");
} }
/* /*
@ -379,18 +376,24 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
{ {
startActivity(new Intent(this, SettingsPreferences.class)); startActivityForResult(new Intent(this, SettingsPreferences.class), 1);
} }
/* @Override
* Read in the Preferences and write then to the .torrc file protected void onActivityResult(int requestCode, int resultCode, Intent data) {
*/ super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
{
try {
processSettings();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
private void processSettings () throws RemoteException private void processSettings () throws RemoteException
{ {
@ -408,6 +411,7 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
boolean enableTransparentProxy = prefs.getBoolean(PREF_TRANSPARENT, false); boolean enableTransparentProxy = prefs.getBoolean(PREF_TRANSPARENT, false);
mService.updateTransProxy();
String bridgeList = prefs.getString(PREF_BRIDGES_LIST,""); String bridgeList = prefs.getString(PREF_BRIDGES_LIST,"");
@ -490,6 +494,7 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
} }
private void showAlert(String title, String msg) private void showAlert(String title, String msg)
{ {
@ -511,12 +516,6 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
if (mService != null) if (mService != null)
torStatus = mService.getStatus(); torStatus = mService.getStatus();
if (this.currentView == R.layout.layout_log)
{
txtMessageLog.append(torServiceMsg);
txtMessageLog.append("\n");
}
if (imgStatus != null) if (imgStatus != null)
{ {
@ -526,8 +525,9 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
imgStatus.setImageResource(R.drawable.toron); imgStatus.setImageResource(R.drawable.toron);
imgStatus.clearAnimation(); imgStatus.clearAnimation();
lblStatus.setText(getString(R.string.status_activated)); String lblMsg = getString(R.string.status_activated) + ": " + torServiceMsg;
lblStatus.setText(lblMsg);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mOrbot); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mOrbot);
@ -546,16 +546,8 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
} }
/*
if (progressDialog != null)
{
progressDialog.cancel();
progressDialog.hide();
progressDialog = null;
}*/
} }
else if (torStatus == STATUS_CONNECTING) else if (torStatus == STATUS_CONNECTING)
@ -564,33 +556,6 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
imgStatus.setImageResource(R.drawable.torstarting); imgStatus.setImageResource(R.drawable.torstarting);
/*
if (imgStatus.getAnimation()==null)
{
imgStatus.setAnimation(AnimationUtils.loadAnimation(this, R.anim.starting));
imgStatus.getAnimation().setRepeatMode(Animation.INFINITE);
imgStatus.getAnimation().setRepeatCount(Animation.INFINITE);
}*/
/*
if (progressDialog == null)
{
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.setMessage(getString(R.string.status_starting_up));
progressDialog.show();
progressDialog.setProgress(10);
}
progressDialog.setMessage(torServiceMsg);
*/
lblStatus.setText(torServiceMsg); lblStatus.setText(torServiceMsg);
@ -617,20 +582,13 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
{ {
/*
if (progressDialog != null)
{
progressDialog.cancel();
progressDialog.hide();
progressDialog = null;
}
*/
imgStatus.clearAnimation(); imgStatus.clearAnimation();
imgStatus.setImageResource(R.drawable.toroff); imgStatus.setImageResource(R.drawable.toroff);
lblStatus.setText(getString(R.string.status_disabled)); lblStatus.setText(getString(R.string.status_disabled));
} }
} }
@ -653,7 +611,6 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
Message msg = mHandler.obtainMessage(ENABLE_TOR_MSG); Message msg = mHandler.obtainMessage(ENABLE_TOR_MSG);
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
// updateStatus("");
} }
private void stopTor () throws RemoteException private void stopTor () throws RemoteException
@ -663,7 +620,6 @@ public class Orbot extends Activity implements OnClickListener, TorConstants
Message msg = mHandler.obtainMessage(DISABLE_TOR_MSG); Message msg = mHandler.obtainMessage(DISABLE_TOR_MSG);
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
//updateStatus("");
} }

View File

@ -89,6 +89,7 @@ public class SettingsPreferences
{ {
startActivity(new Intent(this, AppManager.class)); startActivity(new Intent(this, AppManager.class));
} }
/*
else if (preference == prefWebProxy) else if (preference == prefWebProxy)
{ {
Intent intent = new Intent(); Intent intent = new Intent();
@ -99,7 +100,7 @@ public class SettingsPreferences
startActivity(intent); startActivity(intent);
} }*/
else else
{ {
prefcBTransProxyAll.setEnabled(prefCBTransProxy.isChecked()); prefcBTransProxyAll.setEnabled(prefCBTransProxy.isChecked());

View File

@ -27,6 +27,12 @@ interface ITorService {
**/ **/
void setProfile(int profile); void setProfile(int profile);
/**
* Update trans proxying
**/
boolean updateTransProxy ();
/** /**
* Set configuration * Set configuration
**/ **/

View File

@ -943,6 +943,14 @@ public class TorService extends Service implements TorServiceConstants, Runnable
} }
public boolean updateTransProxy ()
{
//turn on
return setupTransProxy(currentStatus == STATUS_ON);
}
public String getConfiguration (String name) public String getConfiguration (String name)
{ {
try try
@ -1160,10 +1168,10 @@ public class TorService extends Service implements TorServiceConstants, Runnable
} }
private void setupTransProxy (boolean enabled) private boolean setupTransProxy (boolean enabled)
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplication());
boolean enableTransparentProxy = prefs.getBoolean("pref_transparent", false); boolean enableTransparentProxy = prefs.getBoolean("pref_transparent", false);
boolean transProxyAll = prefs.getBoolean("pref_transparent_all", false); boolean transProxyAll = prefs.getBoolean("pref_transparent_all", false);
@ -1186,10 +1194,14 @@ public class TorService extends Service implements TorServiceConstants, Runnable
logNotice ("TorTransProxy enabled: " + success); logNotice ("TorTransProxy enabled: " + success);
return true;
} catch (Exception e) { } catch (Exception e) {
logNotice("WARNING: Error configuring transparenty proxying: " + e.getMessage()); logNotice("WARNING: Error configuring transparenty proxying: " + e.getMessage());
Log.w(TAG, "error refreshing iptables: err=" + e.getMessage(), e); Log.w(TAG, "error refreshing iptables: err=" + e.getMessage(), e);
return false;
} }
} }
@ -1206,5 +1218,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
TorTransProxy.purgeIptables(); TorTransProxy.purgeIptables();
} }
} }
return true;
} }
} }

View File

@ -53,6 +53,7 @@ public class TorTransProxy {
private static String findBaseDir () private static String findBaseDir ()
{ {
/*
String[] cmds = {"/system/bin/iptables -t nat --list"}; String[] cmds = {"/system/bin/iptables -t nat --list"};
StringBuilder res = new StringBuilder(); StringBuilder res = new StringBuilder();
@ -69,7 +70,9 @@ public class TorTransProxy {
} catch (Exception e) { } catch (Exception e) {
return BASE_DIR; return BASE_DIR;
} }*/
return "/system/bin/";
} }
@ -139,6 +142,15 @@ public class TorTransProxy {
final StringBuilder script = new StringBuilder(); final StringBuilder script = new StringBuilder();
//first we have to flush old settings
script.append(baseDir);
script.append(CMD_NAT_FLUSH);
script.append(" || exit\n");
script.append(baseDir);
script.append(CMD_FILTER_FLUSH);
script.append(" || exit\n");
StringBuilder res = new StringBuilder(); StringBuilder res = new StringBuilder();
int code = -1; int code = -1;
@ -156,6 +168,8 @@ public class TorTransProxy {
Log.i(TAG,"enabling transproxy for app: " + apps[i].getUsername() + "(" + apps[i].getUid() + ")"); Log.i(TAG,"enabling transproxy for app: " + apps[i].getUsername() + "(" + apps[i].getUid() + ")");
//TCP //TCP
script.append(baseDir); script.append(baseDir);
script.append("iptables -t nat"); script.append("iptables -t nat");
@ -173,12 +187,14 @@ public class TorTransProxy {
script.append(" --dport 53 -j REDIRECT --to-ports 5400"); //drop all UDP packets as Tor won't handle them script.append(" --dport 53 -j REDIRECT --to-ports 5400"); //drop all UDP packets as Tor won't handle them
script.append(" || exit\n"); script.append(" || exit\n");
/*
script.append(baseDir); script.append(baseDir);
script.append("iptables -t nat"); script.append("iptables -t nat");
script.append(" -A OUTPUT -m owner --uid-owner "); script.append(" -A OUTPUT -m owner --uid-owner ");
script.append(apps[i].getUid()); script.append(apps[i].getUid());
script.append(" -j DROP"); //drop all other packets as Tor won't handle them script.append(" -j DROP"); //drop all other packets as Tor won't handle them
script.append(" || exit\n"); script.append(" || exit\n");
*/
/* /*