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