improve error handling of tor startup sequence

don't use executor thread pool for time critical commands
This commit is contained in:
Nathan Freitas 2014-08-28 23:23:55 -04:00
parent c98509d8e6
commit b8912adbfb
1 changed files with 66 additions and 31 deletions

View File

@ -177,7 +177,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
try try
{ {
mLastProcessId = initControlConnection(1); mLastProcessId = initControlConnection(3);
if (mLastProcessId != -1 && conn != null) if (mLastProcessId != -1 && conn != null)
{ {
@ -346,7 +346,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
updateSettings (); updateSettings ();
mExecutor.execute (new TorStarter(intent)); new Thread (new TorStarter(intent)).start();
return Service.START_STICKY; return Service.START_STICKY;
@ -381,12 +381,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
//if this is a start on boot launch turn tor on
if (mIntent != null && mIntent.getAction()!=null && mIntent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) if (mIntent != null && mIntent.getAction()!=null && mIntent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{ {
setTorProfile(PROFILE_ON); setTorProfile(PROFILE_ON);
} }
} }
}
catch (Exception e) catch (Exception e)
{ {
Log.e(TAG,"error onBind",e); Log.e(TAG,"error onBind",e);
@ -406,8 +408,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
logNotice("TorService is being destroyed... shutting down!"); logNotice("TorService is being destroyed... shutting down!");
//stopTor();
unregisterReceiver(mNetworkStateReceiver); unregisterReceiver(mNetworkStateReceiver);
} }
@ -587,6 +587,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
catch (Exception e) catch (Exception e)
{ {
Log.e(TAG,"error onBind",e); Log.e(TAG,"error onBind",e);
logNotice("error finding exiting process: " + e.toString());
} }
} }
@ -738,7 +739,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
prefPersistNotifications = prefs.getBoolean(TorConstants.PREF_PERSIST_NOTIFICATIONS, true); prefPersistNotifications = prefs.getBoolean(TorConstants.PREF_PERSIST_NOTIFICATIONS, true);
updateTorConfigFile();
} }
public void initTor () throws Exception public void initTor () throws Exception
@ -759,7 +759,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
logNotice(getString(R.string.status_starting_up)); logNotice(getString(R.string.status_starting_up));
sendCallbackStatusMessage(getString(R.string.status_starting_up)); sendCallbackStatusMessage(getString(R.string.status_starting_up));
runTorShellCmd(); boolean success = runTorShellCmd();
if (success)
{
runPolipoShellCmd(); runPolipoShellCmd();
if (mHasRoot && mEnableTransparentProxy) if (mHasRoot && mEnableTransparentProxy)
@ -769,8 +772,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
getHiddenServiceHostname (); getHiddenServiceHostname ();
}
else
{
showToolbarNotification(getString(R.string.unable_to_start_tor), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
} }
}
private boolean flushTransparentProxyRules () throws Exception private boolean flushTransparentProxyRules () throws Exception
{ {
@ -908,11 +916,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
Shell mShellTor; Shell mShellTor;
private void runTorShellCmd() throws Exception private boolean runTorShellCmd() throws Exception
{ {
String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getCanonicalPath(); String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getCanonicalPath();
updateTorConfigFile();
sendCallbackStatusMessage(getString(R.string.status_starting_up)); sendCallbackStatusMessage(getString(R.string.status_starting_up));
if (mShellTor != null) if (mShellTor != null)
@ -921,12 +931,40 @@ public class TorService extends Service implements TorServiceConstants, TorConst
//start Tor in the background //start Tor in the background
mShellTor = Shell.startShell(); mShellTor = Shell.startShell();
SimpleCommand shellTorCommand = new SimpleCommand(fileTor.getCanonicalPath() String torCmdString = fileTor.getCanonicalPath()
+ " DataDirectory " + appCacheHome.getCanonicalPath() + " DataDirectory " + appCacheHome.getCanonicalPath()
+ " --defaults-torrc " + torrcPath + " --defaults-torrc " + torrcPath
+ " -f " + torrcPath + ".custom"); + " -f " + torrcPath + ".custom";
debug(torCmdString);
SimpleCommand shellTorCommand = new SimpleCommand(torCmdString + " --verify-config");
mShellTor.add(shellTorCommand).waitForFinish();
int exitCode = shellTorCommand.getExitCode();
String output = shellTorCommand.getOutput();
if (exitCode != 0 && output != null && output.length() > 0)
{
logNotice("Tor (" + exitCode + "): " + output);
throw new Exception ("Torrc config did not verify");
}
shellTorCommand = new SimpleCommand(torCmdString);
mShellTor.add(shellTorCommand).waitForFinish();
exitCode = shellTorCommand.getExitCode();
output = shellTorCommand.getOutput();
if (exitCode != 0 && output != null && output.length() > 0)
{
logNotice("Tor (" + exitCode + "): " + output);
//throw new Exception ("unable to start");
return false;
}
mShellTor.add(shellTorCommand);
//now try to connect //now try to connect
mLastProcessId = initControlConnection (100); mLastProcessId = initControlConnection (100);
@ -948,7 +986,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
return true;
} }
private void updatePolipoConfig () throws FileNotFoundException, IOException private void updatePolipoConfig () throws FileNotFoundException, IOException
@ -1014,7 +1052,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
private synchronized int initControlConnection (int maxTries) throws Exception, RuntimeException private int initControlConnection (int maxTries) throws Exception, RuntimeException
{ {
int i = 0; int i = 0;
int controlPort = -1; int controlPort = -1;
@ -1275,10 +1313,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
catch (Exception e) catch (Exception e)
{ {
stopTor();
logException("Unable to start Tor: " + e.toString(),e); logException("Unable to start Tor: " + e.toString(),e);
currentStatus = STATUS_OFF; currentStatus = STATUS_OFF;
showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr); showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
stopTor();
} }
} }
else else
@ -1573,12 +1611,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return mBinder; return mBinder;
} }
/** /**
* The IRemoteInterface is defined through IDL * The IRemoteInterface is defined through IDL
*/ */
@ -1592,7 +1627,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void setProfile (final int profileNew) public void setProfile (final int profileNew)
{ {
mExecutor.execute(new Runnable() new Thread(new Runnable()
{ {
@Override @Override
@ -1600,7 +1635,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
setTorProfile(profileNew); setTorProfile(profileNew);
} }
}); }).start();
} }