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,91 +985,87 @@ 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)
{
try
{
int controlPort = getControlPort();
if (controlPort == -1 && i == maxAttempts)
controlPort = DEFAULT_CONTROL_PORT;
if (controlPort != -1)
{
logNotice( "Connecting to control port: " + controlPort);
torConnSocket = new Socket(IP_LOCALHOST, controlPort);
torConnSocket.setSoTimeout(CONTROL_SOCKET_TIMEOUT);
conn = TorControlConnection.getConnection(torConnSocket);
logNotice( "SUCCESS connected to Tor control port");
File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
if (fileCookie.exists())
{
byte[] cookie = new byte[(int)fileCookie.length()];
DataInputStream fis = new DataInputStream(new FileInputStream(fileCookie));
fis.read(cookie);
fis.close();
conn.authenticate(cookie);
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");
if (ENABLE_DEBUG_LOG)
{
//File fileLog = new File(getFilesDir(),"orbot-control-log.txt");
//PrintWriter pr = new PrintWriter(new FileWriter(fileLog,true));
//conn.setDebugging(pr);
File fileLog2 = new File(getFilesDir(),"orbot-tor-log.txt");
conn.setConf("Log", "debug file " + fileLog2.getCanonicalPath());
}
return Integer.parseInt(torProcId);
}
else
{
logNotice ("Tor authentication cookie does not exist yet");
conn = null;
}
}
}
catch (Exception ce)
{
conn = null;
logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce);
}
try {
logNotice("waiting...");
Thread.sleep(5000); }
catch (Exception e){}
String torProcId = conn.getInfo("process/pid");
return Integer.parseInt(torProcId);
}
}
int attempt = 0;
while (conn == null && attempt++ < maxTries)
{
try
{
controlPort = getControlPort();
if (controlPort != -1)
{
logNotice( "Connecting to control port: " + controlPort);
torConnSocket = new Socket(IP_LOCALHOST, controlPort);
torConnSocket.setSoTimeout(CONTROL_SOCKET_TIMEOUT);
conn = TorControlConnection.getConnection(torConnSocket);
logNotice( "SUCCESS connected to Tor control port");
fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
if (fileCookie.exists())
{
byte[] cookie = new byte[(int)fileCookie.length()];
DataInputStream fis = new DataInputStream(new FileInputStream(fileCookie));
fis.read(cookie);
fis.close();
conn.authenticate(cookie);
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");
if (ENABLE_DEBUG_LOG)
{
//File fileLog = new File(getFilesDir(),"orbot-control-log.txt");
//PrintWriter pr = new PrintWriter(new FileWriter(fileLog,true));
//conn.setDebugging(pr);
File fileLog2 = new File(getFilesDir(),"orbot-tor-log.txt");
conn.setConf("Log", "debug file " + fileLog2.getCanonicalPath());
}
return Integer.parseInt(torProcId);
}
else
{
logNotice ("Tor authentication cookie does not exist yet");
conn = null;
}
}
}
catch (Exception ce)
{
conn = null;
logException( "Error connecting to Tor local control port: " + ce.getMessage(),ce);
}
try {
logNotice("waiting...");
Thread.sleep(5000); }
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(":");