fixes for Tor processs tracking, control port, and start/stop

This commit is contained in:
Nathan Freitas 2014-08-03 14:39:01 -04:00
parent 8481e6199f
commit e3385ab137
1 changed files with 94 additions and 112 deletions

View File

@ -411,7 +411,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
try
{
killTorProcess ();
shutdownTorProcess ();
//stop the foreground priority and make sure to remove the persistant notification
stopForeground(true);
@ -504,11 +504,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
}
private void killTorProcess () throws Exception
private void shutdownTorProcess () throws Exception
{
//stopTorMinder();
if (conn != null)
{
@ -519,9 +517,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
logNotice("sending HALT signal to Tor process");
conn.shutdownTor("HALT");
//logNotice("closing tor socket");
// torConnSocket.close();
} catch (Exception e) {
Log.d(TAG,"error shutting down Tor via connection",e);
}
@ -852,42 +847,36 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return true;
}
Shell mShellTor;
private void runTorShellCmd() throws Exception
{
SharedPreferences prefs =TorServiceUtils.getSharedPrefs(getApplicationContext());
String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getCanonicalPath();
int torRetryWaitTimeMS = 1000;
sendCallbackStatusMessage(getString(R.string.status_starting_up));
if (mShellTor != null)
mShellTor.close();
//start Tor in the background
Shell shell = Shell.startShell();
mShellTor = Shell.startShell();
SimpleCommand cmdTor = new SimpleCommand(fileTor.getCanonicalPath()
SimpleCommand shellTorCommand = new SimpleCommand(fileTor.getCanonicalPath()
+ " DataDirectory " + appCacheHome.getCanonicalPath()
+ " --defaults-torrc " + torrcPath
+ " -f " + torrcPath + ".custom"
+ " &");
+ " -f " + torrcPath + ".custom");
shell.add(cmdTor);
Thread.sleep(torRetryWaitTimeMS);
mShellTor.add(shellTorCommand);
//now try to connect
mLastProcessId = initControlConnection (3);
mLastProcessId = initControlConnection (100);
if (mLastProcessId == -1)
{
logNotice(getString(R.string.couldn_t_start_tor_process_) + "; exit=" + cmdTor.getExitCode() + ": " + cmdTor.getOutput());
logNotice(getString(R.string.couldn_t_start_tor_process_) + "; exit=" + shellTorCommand.getExitCode() + ": " + shellTorCommand.getOutput());
sendCallbackStatusMessage(getString(R.string.couldn_t_start_tor_process_));
logNotice("Tor exit code: " + cmdTor.getExitCode());
throw new Exception ("Unable to start Tor");
}
else
@ -897,11 +886,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
processSettingsImpl();
// startTorMinder ();
}
shell.close();
}
@ -981,13 +968,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return null;
}*/
private synchronized int initControlConnection (int maxAttempts) throws Exception, RuntimeException
private synchronized int initControlConnection (int maxTries) throws Exception, RuntimeException
{
int i = 0;
int controlPort = getControlPort();
File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
if (conn != null)
{
File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
if (fileCookie.exists())
{
@ -997,22 +985,20 @@ public class TorService extends Service implements TorServiceConstants, TorConst
fis.close();
conn.authenticate(cookie);
}
String torProcId = conn.getInfo("process/pid");
return Integer.parseInt(torProcId);
}
else
{
while (conn == null && i++ < maxAttempts)
}
int attempt = 0;
while (conn == null && attempt++ < maxTries)
{
try
{
int controlPort = getControlPort();
if (controlPort == -1 && i == maxAttempts)
controlPort = DEFAULT_CONTROL_PORT;
controlPort = getControlPort();
if (controlPort != -1)
{
@ -1023,10 +1009,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
conn = TorControlConnection.getConnection(torConnSocket);
logNotice( "SUCCESS connected to Tor control port");
File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
if (fileCookie.exists())
{
@ -1080,7 +1065,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
catch (Exception e){}
}
}
return -1;
@ -1098,8 +1082,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileControlPort));
String line = bufferedReader.readLine();
//PORT=127.0.0.1:45051
if (line != null)
{
String[] lineParts = line.split(":");