[tor-commits] [orbot/master] improve how we start/stop Tor, Polipo

n8fr8 at torproject.org n8fr8 at torproject.org
Fri May 2 14:10:38 UTC 2014


commit cbbc5e45e18bef6ad40d1a61bf31a4917110c0f7
Author: Nathan Freitas <nathan at freitas.net>
Date:   Thu May 1 21:36:03 2014 -0400

    improve how we start/stop Tor, Polipo
    to find process id, do ps of all, then filter (most compat)
    use async service for Tor start/stop instead of threads
---
 src/org/torproject/android/service/TorService.java |  140 +++++++++-----------
 .../android/service/TorServiceConstants.java       |    2 +-
 .../android/service/TorServiceUtils.java           |    6 +-
 3 files changed, 67 insertions(+), 81 deletions(-)

diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index 4f897e9..05f4376 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -15,8 +15,6 @@ import java.io.FileNotFoundException;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.net.InetSocketAddress;
-import java.net.ServerSocket;
 import java.net.Socket;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -29,7 +27,6 @@ import net.freehaven.tor.control.EventHandler;
 import net.freehaven.tor.control.TorControlConnection;
 
 import org.sufficientlysecure.rootcommands.Shell;
-import org.sufficientlysecure.rootcommands.Toolbox;
 import org.sufficientlysecure.rootcommands.command.SimpleCommand;
 import org.torproject.android.Orbot;
 import org.torproject.android.R;
@@ -50,15 +47,16 @@ import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.graphics.Color;
 import android.net.ConnectivityManager;
-import android.os.Build;
+import android.os.AsyncTask;
 import android.os.IBinder;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.app.NotificationCompat.Builder;
 import android.util.Log;
+import android.widget.TextView;
 
-public class TorService extends Service implements TorServiceConstants, TorConstants, Runnable, EventHandler
+public class TorService extends Service implements TorServiceConstants, TorConstants, EventHandler
 {
 	
 	public static boolean ENABLE_DEBUG_LOG = false;
@@ -136,7 +134,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     	{
 	    	try
 	    	{
-		    	
 	
 	 			int procId = initControlConnection(1);
 				
@@ -278,6 +275,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 	 */
 	public int onStartCommand(Intent intent, int flags, int startId) {
 	
+		android.os.Debug.waitForDebugger();
 
 		try
 		{
@@ -321,34 +319,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 	
 
 	
-	 
-	public void run ()
-	{
-		
-		if (currentStatus == STATUS_CONNECTING)
-		{
-			
-		     try
-		     {
-			   initTor();
-		     }
-		     catch (Exception e)
-		     {				
-		    	
-		    	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);
-		     }
-			
-		}
-		else if (currentStatus == STATUS_OFF)
-		{
-
-			stopTor();
-
-		}
-	}
-
 	
     public void onDestroy ()
     {
@@ -634,33 +604,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		
 		//checkAddressAndCountry();
     }
-    
-    /**
-    private boolean checkPortsAvailable ()
-    {
-    	int[] ports = {9050,9051,8118};
-    	
-    	for (int port: ports)
-    	{
-	    	try
-	    	{
-	    		logNotice("checking local port is available: " + port);
-	    		
-	    		ServerSocket ss = new ServerSocket();
-	    		ss.bind(new InetSocketAddress(IP_LOCALHOST,port));
-	    		ss.close();
-	    	}
-	    	catch (Exception e)
-	    	{
-	    		logException ("Tor socket " + port + " is not available",e);
-	    		return false;
-	    	}
-    	}
-    	
-    	return true;
-    	
-    }
-    */
+   
     
     /*
      * activate means whether to apply the users preferences
@@ -773,7 +717,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 
 		if (procId == -1)
 		{
-			
 			logNotice(getString(R.string.couldn_t_start_tor_process_) + "; exit=" + cmdTor.getExitCode() + ": " + cmdTor.getOutput());
 			sendCallbackStatusMessage(getString(R.string.couldn_t_start_tor_process_));
 			
@@ -786,6 +729,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			
 			processSettingsImpl();
 	    }
+		
+		shell.close();
     }
     
     private void runPolipoShellCmd () throws Exception
@@ -898,7 +843,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 						conn = null;
 						logNotice( "Error connecting to Tor local control port: " + ce.getLocalizedMessage());
 						 
-						Log.d(TAG,"Attempt: Error connecting to control port: " + ce.getLocalizedMessage(),ce);
+					//	Log.d(TAG,"Attempt: Error connecting to control port: " + ce.getLocalizedMessage(),ce);
 					}
 					
 					sendCallbackStatusMessage(getString(R.string.tor_process_waiting));
@@ -1011,25 +956,66 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			
 			if (profile == PROFILE_ON)
 			{
- 				currentStatus = STATUS_CONNECTING;
-	            sendCallbackStatusMessage (getString(R.string.status_starting_up));
-
-	            Thread thread = new Thread(this);
-	            thread.start();
-	           
+ 				
+	           new StartStopTorOperation().execute(true);
 			}
 			else if (profile == PROFILE_OFF)
 			{
-				currentStatus = STATUS_OFF;
-	            sendCallbackStatusMessage (getString(R.string.status_shutting_down));
-	          
-	            Thread thread = new Thread(this);
-	            thread.start();
-	            
+
+		       new StartStopTorOperation().execute(false);
 				
 			}
 		}
 		
+		private class StartStopTorOperation extends AsyncTask<Boolean, Void, Boolean> {
+	        @Override
+	        protected Boolean doInBackground(Boolean... params) {
+	          
+	        	if (params[0].booleanValue() == true)
+	        	{
+	        		
+	        		currentStatus = STATUS_CONNECTING;
+		            sendCallbackStatusMessage (getString(R.string.status_starting_up));
+
+		            try
+		   		     {
+		   			   initTor();
+		   		     }
+		   		     catch (Exception e)
+		   		     {				
+		   		    	
+		   		    	logException("Unable to start Tor: " + e.toString(),e);	
+		   		    	 currentStatus = STATUS_OFF;
+		   		    	 showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, -1, false);
+		   		     }
+	        	}
+	        	else
+	        	{
+	        		currentStatus = STATUS_OFF;
+		            sendCallbackStatusMessage (getString(R.string.status_shutting_down));
+		          
+		            stopTor();
+		            
+	        	}
+	        	
+	        	
+	            return true;
+	        }
+
+	        @Override
+	        protected void onPostExecute(Boolean result) {
+	        }
+
+	        @Override
+	        protected void onPreExecute() {
+	        }
+
+	        @Override
+	        protected void onProgressUpdate(Void... values) {
+	        }
+	    }
+		
+		
 		
 	
 
@@ -1622,14 +1608,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst
         String excludeNodes = prefs.getString("pref_exclude_nodes", "");
         
         String proxyType = prefs.getString("pref_proxy_type", null);
-        if (proxyType != null)
+        if (proxyType != null && proxyType.length() > 0)
         {
         	String proxyHost = prefs.getString("pref_proxy_host", null);
         	String proxyPort = prefs.getString("pref_proxy_port", null);
         	String proxyUser = prefs.getString("pref_proxy_username", null);
         	String proxyPass = prefs.getString("pref_proxy_password", null);
         	
-        	if (proxyHost != null && proxyPort != null)
+        	if ((proxyHost != null && proxyHost.length()>0) && (proxyPort != null && proxyPort.length() > 0))
         	{
         		mBinder.updateConfiguration(proxyType + "Proxy", proxyHost + ':' + proxyPort, false);
         		
diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java
index 1b2fa5d..f89a892 100644
--- a/src/org/torproject/android/service/TorServiceConstants.java
+++ b/src/org/torproject/android/service/TorServiceConstants.java
@@ -77,7 +77,7 @@ 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.5.3-alpha-openssl1.0.1g-b87a";
+    public static final String BINARY_TOR_VERSION = "0.2.5.3-alpha-openssl1.0.1g-b88";
     public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INSTALLED";
     
     //obfsproxy 
diff --git a/src/org/torproject/android/service/TorServiceUtils.java b/src/org/torproject/android/service/TorServiceUtils.java
index fe6ad01..be1694e 100644
--- a/src/org/torproject/android/service/TorServiceUtils.java
+++ b/src/org/torproject/android/service/TorServiceUtils.java
@@ -78,16 +78,16 @@ public class TorServiceUtils implements TorServiceConstants {
 		    	
 		Process procPs = null;
 		
-		String processKey = new File(command).getName();
+		//String processKey = new File(command).getName();
 		
-        procPs = r.exec(SHELL_CMD_PS + ' ' + processKey); // this is the android ps <name> command
+        procPs = r.exec(SHELL_CMD_PS); // this is the android ps <name> command
             
         BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
         String line = reader.readLine(); //read first line "headers" USER PID PPID etc
         
         while ((line = reader.readLine())!=null)
         {
-        	if (line.contains(processKey))
+        	if (line.contains(command))
         	{
         		String[] lineParts = line.split("\\s+");
         		





More information about the tor-commits mailing list