rework start/stop procedure to have clear points for ON, OFF, STARTING, STOPPING

In order to send reliable information to any app using Tor, Orbot itself
needs reliable state broadcasts.  Before, there the ON/OFF/STARTING state
were being set multiple times during the process, and sometimes not even in
a useful order (i.e. STARTING ON STARTING ON ON).

This reworks the start/stop procedure into startTor() and stopTor().
This commit is contained in:
Hans-Christoph Steiner 2015-06-09 13:20:38 -04:00
parent 4470771c0d
commit 780abf003e
1 changed files with 86 additions and 139 deletions

View File

@ -8,7 +8,45 @@
package org.torproject.android.service;
import android.annotation.SuppressLint;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.RemoteViews;
import net.freehaven.tor.control.ConfigEntry;
import net.freehaven.tor.control.EventHandler;
import net.freehaven.tor.control.TorControlConnection;
import org.json.JSONArray;
import org.json.JSONObject;
import org.sufficientlysecure.rootcommands.Shell;
import org.sufficientlysecure.rootcommands.command.SimpleCommand;
import org.torproject.android.OrbotConstants;
import org.torproject.android.OrbotMainActivity;
import org.torproject.android.Prefs;
import org.torproject.android.R;
import org.torproject.android.settings.AppManager;
import org.torproject.android.settings.TorifiedApp;
import org.torproject.android.vpn.OrbotVpnService;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
@ -42,44 +80,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;
import net.freehaven.tor.control.ConfigEntry;
import net.freehaven.tor.control.EventHandler;
import net.freehaven.tor.control.TorControlConnection;
import org.json.JSONArray;
import org.json.JSONObject;
import org.sufficientlysecure.rootcommands.Shell;
import org.sufficientlysecure.rootcommands.command.SimpleCommand;
import org.torproject.android.OrbotConstants;
import org.torproject.android.OrbotMainActivity;
import org.torproject.android.R;
import org.torproject.android.settings.AppManager;
import org.torproject.android.settings.TorifiedApp;
import org.torproject.android.vpn.OrbotVpnService;
import android.annotation.SuppressLint;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.RemoteViews;
public class TorService extends Service implements TorServiceConstants, OrbotConstants, EventHandler
{
@ -344,10 +344,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
String action = mIntent.getAction();
if (action != null) {
if (action.equals(Intent.ACTION_BOOT_COMPLETED) || action.equals(CMD_START)) {
setTorProfile(STATUS_ON);
if (action.equals(CMD_START)) {
startTor();
} else if (action.equals(CMD_STOP)) {
setTorProfile(STATUS_OFF);
stopTor();
} else if (action.equals(CMD_NEWNYM)) {
newIdentity();
} else if (action.equals(CMD_FLUSH)) {
@ -391,11 +391,13 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
super.onDestroy();
}
private void stopTor ()
{
private void stopTor() {
Log.i("TorService", "stopTor");
try {
mCurrentStatus = STATUS_STOPPING;
sendCallbackStatus(mCurrentStatus);
try
{
sendCallbackLogMessage(getString(R.string.status_shutting_down));
Log.d(TAG,"Tor is stopping NOW");
killAllDaemons();
@ -403,9 +405,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
//stop the foreground priority and make sure to remove the persistant notification
stopForeground(true);
mCurrentStatus = STATUS_OFF;
sendCallbackStatus(mCurrentStatus);
if (Prefs.useRoot() && Prefs.useTransparentProxying())
{
Shell shellRoot = Shell.startRootShell();
@ -416,7 +415,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
clearNotifications();
sendCallbackLogMessage(getString(R.string.status_disabled));
}
catch (CannotKillException e)
{
@ -431,8 +429,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
Log.d(TAG, "An error occured stopping Tor",e);
logNotice("An error occured stopping Tor: " + e.getMessage());
sendCallbackLogMessage(getString(R.string.something_bad_happened));
}
mCurrentStatus = STATUS_OFF;
sendCallbackStatus(mCurrentStatus);
}
@ -748,18 +748,32 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
return success;
}
private void startTor () throws Exception
{
private void startTor() {
if (mCurrentStatus == STATUS_STARTING || mCurrentStatus == STATUS_STOPPING) {
// these states should probably be handled better
sendCallbackLogMessage("Ignoring start request, currently " + mCurrentStatus);
return;
} else if (mCurrentStatus == STATUS_ON) {
sendCallbackLogMessage("Ignoring start request, already started.");
return;
}
mCurrentStatus = STATUS_STARTING;
sendCallbackStatus(mCurrentStatus);
sendCallbackLogMessage(getString(R.string.status_starting_up));
logNotice(getString(R.string.status_starting_up));
if (findExistingTorDaemon()) {
return; // an old tor is already running, nothing to do
}
// make sure there are no stray daemons running
killAllDaemons();
try {
if (fileTor == null)
initBinariesAndDirectories();
logNotice(getString(R.string.status_starting_up));
sendCallbackLogMessage(getString(R.string.status_starting_up));
ArrayList<String> customEnv = new ArrayList<String>();
if (Prefs.bridgesEnabled())
@ -797,10 +811,23 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
else
{
showToolbarNotification(getString(R.string.unable_to_start_tor), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
}
shellUser.close();
mCurrentStatus = STATUS_ON;
sendCallbackStatus(mCurrentStatus);
} catch (CannotKillException e) {
logException(e.getMessage(), e);
showToolbarNotification(getString(R.string.unable_to_reset_tor),
ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
stopTor();
} catch (Exception e) {
logException("Unable to start Tor: " + e.toString(), e);
showToolbarNotification(
getString(R.string.unable_to_start_tor) + ": " + e.getMessage(),
ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
stopTor();
}
}
private boolean flushTransparentProxyRules () {
@ -1110,9 +1137,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
conn.setConf("Log", "debug file " + fileLog2.getCanonicalPath());
}*/
mCurrentStatus = STATUS_STARTING;
sendCallbackStatus(mCurrentStatus);
String confSocks = conn.getInfo("net/listeners/socks");
StringTokenizer st = new StringTokenizer(confSocks," ");
@ -1291,60 +1315,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
return mPortSOCKS;
}
public void setTorProfile(String newState) {
if (newState == STATUS_ON)
{
if (mCurrentStatus == STATUS_OFF)
{
sendCallbackLogMessage (getString(R.string.status_starting_up));
try
{
boolean found = findExistingTorDaemon();
if (!found)
{
killAllDaemons();
startTor();
}
}
catch (CannotKillException e)
{
logException(e.getMessage(), e);
mCurrentStatus = STATUS_OFF;
sendCallbackStatus(mCurrentStatus);
showToolbarNotification(getString(R.string.unable_to_reset_tor),
ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
stopTor();
}
catch (Exception e)
{
logException("Unable to start Tor: " + e.toString(),e);
mCurrentStatus = STATUS_OFF;
sendCallbackStatus(mCurrentStatus);
showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
stopTor();
}
}
}
else if (newState == STATUS_OFF)
{
sendCallbackLogMessage (getString(R.string.status_shutting_down));
stopTor();
mCurrentStatus = STATUS_OFF;
sendCallbackStatus(mCurrentStatus);
}
}
public void enableVpnProxy () {
debug ("enabling VPN Proxy");
@ -1403,21 +1373,11 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
@Override
public void message(String severity, String msg) {
logNotice(severity + ": " + msg);
if (msg.indexOf(TOR_CONTROL_PORT_MSG_BOOTSTRAP_DONE)!=-1)
{
mCurrentStatus = STATUS_ON;
sendCallbackStatus(mCurrentStatus);
showToolbarNotification(getString(R.string.status_activated), NOTIFY_ID, R.drawable.ic_stat_tor);
}
}
@Override
public void newDescriptors(List<String> orList) {
}
@Override
@ -1547,23 +1507,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (Prefs.useDebugLogging())
debug(sb.toString());
else if(status.equals("BUILT"))
{
if (mCurrentStatus == STATUS_STARTING)
mCurrentStatus = STATUS_ON;
sendCallbackStatus(mCurrentStatus);
logNotice(sb.toString());
}
else if (status.equals("CLOSED"))
{
logNotice(sb.toString());
}
if (Prefs.expandedNotifications())
{
//get IP from last nodename