[tor-commits] [orbot/master] improving control port connection code

n8fr8 at torproject.org n8fr8 at torproject.org
Tue Apr 15 04:49:04 UTC 2014


commit 3824bc56e55dd252dd6fdde95ab24646212a6915
Author: Nathan Freitas <nathan at freitas.net>
Date:   Tue Apr 15 00:47:59 2014 -0400

    improving control port connection code
    adding additional logging for problems with Samsungs
---
 src/org/torproject/android/service/TorService.java |   41 +++++++++++++-------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index e8688c2..5298764 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -8,6 +8,7 @@
 package org.torproject.android.service;
 
 
+import java.io.DataInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -829,17 +830,18 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 	
 	private int initControlConnection () throws Exception, RuntimeException
 	{
-			while (conn == null)
+			int maxAttempts = 5;
+			int i = 0;
+			
+			while (conn == null && i++ < maxAttempts)
 			{
 				try
 				{
 					logNotice( "Connecting to control port: " + TOR_CONTROL_PORT);
 					
-					
 					torConnSocket = new Socket(IP_LOCALHOST, TOR_CONTROL_PORT);
 			        conn = TorControlConnection.getConnection(torConnSocket);
 			        
-			      //  conn.authenticate(new byte[0]); // See section 3.2
 
 					logNotice( "SUCCESS connected to control port");
 			        
@@ -848,31 +850,40 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			        if (fileCookie.exists())
 			        {
 				        byte[] cookie = new byte[(int)fileCookie.length()];
-				        new FileInputStream(fileCookie).read(cookie);
+				        DataInputStream fis = new DataInputStream(new FileInputStream(fileCookie));
+				        fis.read(cookie);
+				        fis.close();
 				        conn.authenticate(cookie);
 				        		
-				        logNotice( "SUCCESS authenticated to control port");
+				        logNotice( "SUCCESS - authenticated to control port");
 				        
 						sendCallbackStatusMessage(getString(R.string.tor_process_starting) + ' ' + getString(R.string.tor_process_complete));
 	
 				        addEventHandler();
 				        
-			        }
-			        
-			        String torProcId = conn.getInfo("process/pid");
+			      
 			        
-			        return Integer.parseInt(torProcId);
+				        String torProcId = conn.getInfo("process/pid");
+				        
+				        return Integer.parseInt(torProcId);
+				        
+			        }
+			        else
+			        {
+			        	logNotice ("Tor authentication cookie does not exist yet; trying again...");
+			        }
 				}
 				catch (Exception ce)
 				{
 					conn = null;
+					logNotice( "Error connecting to Tor local control port: " + ce.getLocalizedMessage());
+					 
 					Log.d(TAG,"Attempt: Error connecting to control port: " + ce.getLocalizedMessage(),ce);
-					
-					sendCallbackStatusMessage(getString(R.string.tor_process_waiting));
-
-					Thread.sleep(3000);
-										
-				}	
+				}
+				
+				sendCallbackStatusMessage(getString(R.string.tor_process_waiting));
+				Thread.sleep(3000);
+				
 			}
 		
 		



More information about the tor-commits mailing list