Author: n8fr8
Date: 2011-05-03 05:56:40 +0000 (Tue, 03 May 2011)
New Revision: 24714
Modified:
projects/android/trunk/Orbot/src/org/torproject/android/Orbot.java
projects/android/trunk/Orbot/src/org/torproject/android/WizardHelper.java
projects/android/trunk/Orbot/src/org/torproject/android/service/ITorService.aidl
projects/android/trunk/Orbot/src/org/torproject/android/service/TorService.java
projects/android/trunk/Orbot/src/org/torproject/android/service/TorServiceConstants.java
projects/android/trunk/Orbot/src/org/torproject/android/service/TorServiceUtils.java
Log:
general cleanup on detecting iptables status and startup/shutdown calls
Modified: projects/android/trunk/Orbot/src/org/torproject/android/Orbot.java
===================================================================
--- projects/android/trunk/Orbot/src/org/torproject/android/Orbot.java 2011-05-03 05:56:04 UTC (rev 24713)
+++ projects/android/trunk/Orbot/src/org/torproject/android/Orbot.java 2011-05-03 05:56:40 UTC (rev 24714)
@@ -75,6 +75,12 @@
prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ setContentView(R.layout.layout_main);
+
+ lblStatus = (TextView)findViewById(R.id.lblStatus);
+ lblStatus.setOnLongClickListener(this);
+ imgStatus = (ImageView)findViewById(R.id.imgStatus);
+ imgStatus.setOnLongClickListener(this);
}
/*
@@ -199,8 +205,8 @@
stopTor();
+ stopService(new Intent(ITorService.class.getName()));
-
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
@@ -321,7 +327,6 @@
protected void onResume() {
super.onResume();
- showMain();
updateStatus("");
if (getIntent() == null)
@@ -427,21 +432,6 @@
/*
- * Show the main form UI
- */
- private void showMain ()
- {
-
- setContentView(R.layout.layout_main);
-
- lblStatus = (TextView)findViewById(R.id.lblStatus);
- lblStatus.setOnLongClickListener(this);
- imgStatus = (ImageView)findViewById(R.id.imgStatus);
- imgStatus.setOnLongClickListener(this);
-
- }
-
- /*
* Launch the system activity for Uri viewing with the provided url
*/
private void openBrowser(String url)
@@ -482,11 +472,16 @@
}
}
+ private AlertDialog aDialog = null;
+
private void showAlert(String title, String msg, boolean button)
{
+ if (aDialog != null)
+ aDialog.dismiss();
+
if (button)
{
- new AlertDialog.Builder(this)
+ aDialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle(title)
.setMessage(msg)
@@ -495,7 +490,7 @@
}
else
{
- new AlertDialog.Builder(this)
+ aDialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle(title)
.setMessage(msg)
@@ -519,7 +514,7 @@
if (torStatus == STATUS_ON)
{
imgStatus.setImageResource(R.drawable.toron);
- // imgStatus.clearAnimation();
+
if (progressDialog != null)
{
progressDialog.dismiss();
@@ -535,8 +530,6 @@
if (torServiceMsg.length() > 0)
showAlert("Update", torServiceMsg, false);
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-
boolean showFirstTime = prefs.getBoolean("connect_first_time",true);
if (showFirstTime)
@@ -572,7 +565,7 @@
}
else if (torStatus == STATUS_OFF)
{
- imgStatus.setImageResource(R.drawable.torstopping);
+ imgStatus.setImageResource(R.drawable.toroff);
if (progressDialog != null)
{
@@ -615,10 +608,6 @@
private void startTor () throws RemoteException
{
- if (progressDialog == null)
- {
- progressDialog =ProgressDialog.show(this, "", getString(R.string.status_starting_up));
- }
mService.setProfile(TorServiceConstants.PROFILE_ON); //this means turn on
@@ -641,7 +630,7 @@
mHandler.sendMessage(msg);
}
- stopService(new Intent(ITorService.class.getName()));
+ // stopService(new Intent(ITorService.class.getName()));
}
@@ -720,8 +709,7 @@
String torServiceMsg = (String)msg.getData().getString(HANDLER_TOR_MSG);
- if (torServiceMsg.length() > 0)
- updateStatus(torServiceMsg);
+ updateStatus(torServiceMsg);
break;
case TorServiceConstants.LOG_MSG:
@@ -730,10 +718,19 @@
break;
case TorServiceConstants.ENABLE_TOR_MSG:
+ if (progressDialog == null)
+ {
+ progressDialog = ProgressDialog.show(Orbot.this, "", getString(R.string.status_starting_up));
+ }
+
+ updateStatus((String)msg.getData().getString(HANDLER_TOR_MSG));
+
break;
case TorServiceConstants.DISABLE_TOR_MSG:
+ updateStatus((String)msg.getData().getString(HANDLER_TOR_MSG));
+
break;
default:
Modified: projects/android/trunk/Orbot/src/org/torproject/android/WizardHelper.java
===================================================================
--- projects/android/trunk/Orbot/src/org/torproject/android/WizardHelper.java 2011-05-03 05:56:04 UTC (rev 24713)
+++ projects/android/trunk/Orbot/src/org/torproject/android/WizardHelper.java 2011-05-03 05:56:40 UTC (rev 24714)
@@ -1,5 +1,6 @@
package org.torproject.android;
+import org.torproject.android.service.TorService;
import org.torproject.android.service.TorServiceUtils;
import org.torproject.android.service.TorTransProxy;
@@ -12,6 +13,7 @@
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.preference.PreferenceManager;
+import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
@@ -88,6 +90,26 @@
boolean hasRoot = TorServiceUtils.checkRootAccess();
+ if (hasRoot)
+ {
+ try {
+ int resp = TorTransProxy.testOwnerModule(context);
+
+ if (resp < 0)
+ {
+ hasRoot = false;
+ Toast.makeText(context, "ERROR: IPTables OWNER module not available", Toast.LENGTH_LONG).show();
+
+ Log.i(TorService.TAG,"ERROR: IPTables OWNER module not available");
+ }
+
+ } catch (Exception e) {
+
+ hasRoot = false;
+ Log.d(TorService.TAG,"ERROR: IPTables OWNER module not available",e);
+ }
+ }
+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor pEdit = prefs.edit();
Modified: projects/android/trunk/Orbot/src/org/torproject/android/service/ITorService.aidl
===================================================================
--- projects/android/trunk/Orbot/src/org/torproject/android/service/ITorService.aidl 2011-05-03 05:56:04 UTC (rev 24713)
+++ projects/android/trunk/Orbot/src/org/torproject/android/service/ITorService.aidl 2011-05-03 05:56:40 UTC (rev 24714)
@@ -27,12 +27,7 @@
**/
void setProfile(int profile);
- /**
- * Update trans proxying
- **/
- boolean updateTransProxy ();
-
/**
* Set configuration
**/
Modified: projects/android/trunk/Orbot/src/org/torproject/android/service/TorService.java
===================================================================
--- projects/android/trunk/Orbot/src/org/torproject/android/service/TorService.java 2011-05-03 05:56:04 UTC (rev 24713)
+++ projects/android/trunk/Orbot/src/org/torproject/android/service/TorService.java 2011-05-03 05:56:40 UTC (rev 24714)
@@ -43,9 +43,9 @@
public class TorService extends Service implements TorServiceConstants, Runnable, EventHandler
{
- private static boolean ENABLE_DEBUG_LOG = false;
+ public static boolean ENABLE_DEBUG_LOG = false;
- private static int currentStatus = STATUS_READY;
+ private static int currentStatus = STATUS_OFF;
private TorControlConnection conn = null;
private Socket torConnSocket = null;
@@ -53,7 +53,6 @@
private static TorService _torInstance;
private static final int NOTIFY_ID = 1;
- private static int NOTIFY_ID_ERROR = 2;
private static final int MAX_START_TRIES = 3;
@@ -213,7 +212,7 @@
} catch (Exception e) {
logNotice("unable to find tor binaries: " + e.getMessage());
- showToolbarNotification(e.getMessage(), NOTIFY_ID_ERROR, R.drawable.tornotificationoff);
+ showToolbarNotification(e.getMessage(), NOTIFY_ID, R.drawable.tornotificationerr);
Log.e(TAG, "error checking tor binaries", e);
}
@@ -248,7 +247,7 @@
catch (Exception e)
{
currentStatus = STATUS_OFF;
- this.showToolbarNotification(getString(R.string.status_disabled), NOTIFY_ID_ERROR, R.drawable.tornotification);
+ this.showToolbarNotification(getString(R.string.status_disabled), NOTIFY_ID, R.drawable.tornotificationerr);
Log.d(TAG,"Unable to start Tor: " + e.getMessage(),e);
}
}
@@ -274,7 +273,7 @@
{
killTorProcess ();
- currentStatus = STATUS_READY;
+ currentStatus = STATUS_OFF;
showToolbarNotification (getString(R.string.status_disabled),NOTIFY_ID,R.drawable.tornotificationoff);
sendCallbackStatusMessage(getString(R.string.status_disabled));
@@ -314,170 +313,7 @@
}*/
- /*
- private void loadTorSettingsFromPreferences () throws RemoteException
- {
- try
- {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-
- ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
- Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
-
- boolean useBridges = prefs.getBoolean(TorConstants.PREF_BRIDGES_ENABLED, false);
-
- //boolean autoUpdateBridges = prefs.getBoolean(PREF_BRIDGES_UPDATED, false);
-
- boolean becomeRelay = prefs.getBoolean(TorConstants.PREF_OR, false);
-
- boolean ReachableAddresses = prefs.getBoolean(TorConstants.PREF_REACHABLE_ADDRESSES,false);
-
- boolean enableHiddenServices = prefs.getBoolean("pref_hs_enable", false);
-
- boolean enableTransparentProxy = prefs.getBoolean(TorConstants.PREF_TRANSPARENT, false);
- mBinder.updateTransProxy();
-
- String bridgeList = prefs.getString(TorConstants.PREF_BRIDGES_LIST,"");
-
- if (useBridges)
- {
- if (bridgeList == null || bridgeList.length() == 0)
- {
-
- showAlert("Bridge Error","In order to use the bridge feature, you must enter at least one bridge IP address." +
- "Send an email to bridges(a)torproject.org with the line \"get bridges\" by itself in the body of the mail from a gmail account.");
-
-
- return;
- }
-
-
- mBinder.updateConfiguration("UseBridges", "1", false);
-
- String bridgeDelim = "\n";
-
- if (bridgeList.indexOf(",") != -1)
- {
- bridgeDelim = ",";
- }
-
- StringTokenizer st = new StringTokenizer(bridgeList,bridgeDelim);
- while (st.hasMoreTokens())
- {
-
- mBinder.updateConfiguration("bridge", st.nextToken(), false);
-
- }
-
- mBinder.updateConfiguration("UpdateBridgesFromAuthority", "0", false);
-
- }
- else
- {
- mBinder.updateConfiguration("UseBridges", "0", false);
-
- }
-
- try
- {
- if (ReachableAddresses)
- {
- String ReachableAddressesPorts =
- prefs.getString(TorConstants.PREF_REACHABLE_ADDRESSES_PORTS, "*:80,*:443");
-
- mBinder.updateConfiguration("ReachableAddresses", ReachableAddressesPorts, false);
-
- }
- else
- {
- mBinder.updateConfiguration("ReachableAddresses", "", false);
- }
- }
- catch (Exception e)
- {
- showAlert("Config Error","Your ReachableAddresses settings caused an exception!");
- }
-
- try
- {
- if (becomeRelay && (!useBridges) && (!ReachableAddresses))
- {
- int ORPort = Integer.parseInt(prefs.getString(TorConstants.PREF_OR_PORT, "9001"));
- String nickname = prefs.getString(TorConstants.PREF_OR_NICKNAME, "Orbot");
-
- mBinder.updateConfiguration("ORPort", ORPort + "", false);
- mBinder.updateConfiguration("Nickname", nickname, false);
- mBinder.updateConfiguration("ExitPolicy", "reject *:*", false);
-
- }
- else
- {
- mBinder.updateConfiguration("ORPort", "", false);
- mBinder.updateConfiguration("Nickname", "", false);
- mBinder.updateConfiguration("ExitPolicy", "", false);
- }
- }
- catch (Exception e)
- {
- showAlert("Uh-oh!","Your relay settings caused an exception!");
-
- return;
- }
-
- if (enableHiddenServices)
- {
- mBinder.updateConfiguration("HiddenServiceDir","/data/data/org.torproject.android/", false);
-
- String hsPorts = prefs.getString("pref_hs_ports","");
-
- StringTokenizer st = new StringTokenizer (hsPorts,",");
- String hsPortConfig = null;
-
- while (st.hasMoreTokens())
- {
- hsPortConfig = st.nextToken();
-
- if (hsPortConfig.indexOf(":")==-1) //setup the port to localhost if not specifed
- {
- hsPortConfig = hsPortConfig + " 127.0.0.1:" + hsPortConfig;
- }
-
- mBinder.updateConfiguration("HiddenServicePort",hsPortConfig, false);
- }
-
- //force save now so the hostname file gets generated
- mBinder.saveConfiguration();
-
- String onionHostname = getHiddenServiceHostname();
-
- if (onionHostname != null)
- {
-
- Editor pEdit = prefs.edit();
- pEdit.putString("pref_hs_hostname",onionHostname);
- pEdit.commit();
-
- }
- }
- else
- {
- mBinder.updateConfiguration("HiddenServiceDir","", false);
- }
-
- mBinder.saveConfiguration();
-
- }
- catch (Exception e)
- {
- showAlert("Uh-oh!","There was an error updating your settings");
-
- Log.w(TAG, "processSettings()", e);
-
- return;
- }
-
- }*/
-
+
private void getHiddenServiceHostname ()
{
@@ -493,7 +329,7 @@
{
try {
String onionHostname = Utils.readString(new FileInputStream(file));
- showToolbarNotification("hidden service on: " + onionHostname, NOTIFY_ID_ERROR, R.drawable.tornotification);
+ showToolbarNotification("hidden service on: " + onionHostname, NOTIFY_ID, R.drawable.tornotification);
Editor pEdit = prefs.edit();
pEdit.putString("pref_hs_hostname",onionHostname);
pEdit.commit();
@@ -501,13 +337,13 @@
} catch (FileNotFoundException e) {
logException("unable to read onion hostname file",e);
- showToolbarNotification("unable to read hidden service name", NOTIFY_ID_ERROR, R.drawable.tornotification);
+ showToolbarNotification("unable to read hidden service name", NOTIFY_ID, R.drawable.tornotificationerr);
return;
}
}
else
{
- showToolbarNotification("unable to read hidden service name", NOTIFY_ID_ERROR, R.drawable.tornotification);
+ showToolbarNotification("unable to read hidden service name", NOTIFY_ID, R.drawable.tornotificationerr);
}
@@ -664,9 +500,10 @@
try {
+
+ setupTransProxy(true);
runTorShellCmd();
runPrivoxyShellCmd();
- setupTransProxy(true);
} catch (Exception e) {
logException("Unable to start Tor: " + e.getMessage(),e);
@@ -676,7 +513,12 @@
}
-
+ /*
+ * activate means whether to apply the users preferences
+ * or clear them out
+ *
+ * the idea is that if Tor is off then transproxy is off
+ */
private boolean setupTransProxy (boolean activate) throws Exception
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@@ -710,10 +552,36 @@
if (enableTransparentProxy)
{
- showAlert("Status", "Setting up transparent proxying...");
-
- //TorTransProxy.setDNSProxying();
- int code = TorTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this),transProxyAll);
+ //TODO: Find a nice place for the next (commented) line
+ //TorTransProxy.setDNSProxying();
+
+ int code = 0; // Default state is "okay"
+
+ if(transProxyPortFallback)
+ {
+ showAlert("Status", "Setting up port-based transparent proxying...");
+ StringTokenizer st = new StringTokenizer(portProxyList, ",");
+ int status = code;
+ while (st.hasMoreTokens())
+ {
+ status = TorTransProxy.setTransparentProxyingByPort(this, Integer.parseInt(st.nextToken()));
+ if(status != 0)
+ code = status;
+ }
+ }
+ else
+ {
+ if(transProxyAll)
+ {
+ showAlert("Status", "Setting up full transparent proxying...");
+ code = TorTransProxy.setTransparentProxyingAll(this);
+ }
+ else
+ {
+ showAlert("Status", "Setting up app-based transparent proxying...");
+ code = TorTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this));
+ }
+ }
TorService.logMessage ("TorTransProxy resp code: " + code);
@@ -725,17 +593,7 @@
{
showAlert("Status", "WARNING: error starting transparent proxying!");
}
-
- //this is for Androids w/o owner module support as a circumvention only fallback
- if (transProxyPortFallback)
- {
- StringTokenizer st = new StringTokenizer(portProxyList, ",");
-
- while (st.hasMoreTokens())
- TorTransProxy.setTransparentProxyingByPort(this, Integer.parseInt(st.nextToken()));
-
- }
-
+
return true;
}
@@ -827,7 +685,7 @@
int attempts = 0;
- while (privoxyProcId == -1 && attempts < MAX_START_TRIES)
+ if (privoxyProcId == -1)
{
log = new StringBuilder();
@@ -838,20 +696,22 @@
logNotice (cmds[0]);
- TorServiceUtils.doShellCommand(cmds, log, false, true);
+ boolean runAsRoot = false;
+ boolean waitFor = false;
+ TorServiceUtils.doShellCommand(cmds, log, runAsRoot, waitFor);
+
//wait one second to make sure it has started up
Thread.sleep(1000);
- privoxyProcId = TorServiceUtils.findProcessId(privoxyPath);
-
- if (privoxyProcId == -1)
+ while ((privoxyProcId = TorServiceUtils.findProcessId(privoxyPath)) == -1 && attempts < MAX_START_TRIES)
{
- logNotice("Couldn't start Privoxy process... retrying...\n" + log);
+ logNotice("Couldn't find Privoxy process... retrying...\n" + log);
Thread.sleep(3000);
attempts++;
}
+
logNotice(log.toString());
}
@@ -1066,7 +926,7 @@
.setPositiveButton(android.R.string.ok, null)
.show();
*/
- showToolbarNotification(msg, NOTIFY_ID_ERROR, R.drawable.tornotification);
+ showToolbarNotification(msg, NOTIFY_ID, R.drawable.tornotification);
}
public void newDescriptors(List<String> orList) {
@@ -1168,7 +1028,7 @@
catch (Exception e)
{
logNotice("unable to find tor binaries: " + e.getMessage());
- showToolbarNotification(e.getMessage(), NOTIFY_ID_ERROR, R.drawable.tornotificationoff);
+ showToolbarNotification(e.getMessage(), NOTIFY_ID, R.drawable.tornotificationerr);
Log.d(TAG,"Unable to check for Tor binaries",e);
return null;
@@ -1227,23 +1087,8 @@
}
- public boolean updateTransProxy ()
- {
-
- //turn on
- try
- {
- setupTransProxy(currentStatus == STATUS_ON);
- return true;
- }
- catch (Exception e)
- {
- Log.d(TAG, "error enabling transproxy",e);
-
- return false;
- }
- }
+
public String getConfiguration (String name)
{
try
@@ -1431,9 +1276,12 @@
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ ENABLE_DEBUG_LOG = prefs.getBoolean("pref_enable_logging",false);
+ Log.i(TAG,"debug logging:" + ENABLE_DEBUG_LOG);
+
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);
boolean becomeRelay = prefs.getBoolean(TorConstants.PREF_OR, false);
@@ -1441,16 +1289,21 @@
boolean enableHiddenServices = prefs.getBoolean("pref_hs_enable", false);
- boolean enableTransparentProxy = prefs.getBoolean(TorConstants.PREF_TRANSPARENT, false);
+ //boolean enableTransparentProxy = prefs.getBoolean(TorConstants.PREF_TRANSPARENT, false);
- try
- {
- setupTransProxy(currentStatus != STATUS_OFF);
- }
- catch (Exception e)
- {
- logException("unable to setup transproxy",e);
- }
+ if (currentStatus == STATUS_ON)
+ {
+ //reset iptables rules in active mode
+
+ try
+ {
+ setupTransProxy(true);
+ }
+ catch (Exception e)
+ {
+ logException("unable to setup transproxy",e);
+ }
+ }
if (useBridges)
{
@@ -1575,6 +1428,6 @@
return true;
}
-
+
}
Modified: projects/android/trunk/Orbot/src/org/torproject/android/service/TorServiceConstants.java
===================================================================
--- projects/android/trunk/Orbot/src/org/torproject/android/service/TorServiceConstants.java 2011-05-03 05:56:04 UTC (rev 24713)
+++ projects/android/trunk/Orbot/src/org/torproject/android/service/TorServiceConstants.java 2011-05-03 05:56:40 UTC (rev 24714)
@@ -69,8 +69,8 @@
//control port
public final static String TOR_CONTROL_PORT_MSG_BOOTSTRAP_DONE = "Bootstrapped 100%";
- public final static int STATUS_OFF = -1;
- public final static int STATUS_READY = 0;
+ public final static int STATUS_OFF = 0;
+ // public final static int STATUS_READY = 0;
public final static int STATUS_ON = 1;
public final static int STATUS_CONNECTING = 2;
Modified: projects/android/trunk/Orbot/src/org/torproject/android/service/TorServiceUtils.java
===================================================================
--- projects/android/trunk/Orbot/src/org/torproject/android/service/TorServiceUtils.java 2011-05-03 05:56:04 UTC (rev 24713)
+++ projects/android/trunk/Orbot/src/org/torproject/android/service/TorServiceUtils.java 2011-05-03 05:56:40 UTC (rev 24714)
@@ -162,6 +162,7 @@
else
proc = Runtime.getRuntime().exec("sh");
+
OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());
for (int i = 0; i < cmds.length; i++)