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:
parent
4470771c0d
commit
780abf003e
|
@ -8,7 +8,45 @@
|
||||||
package org.torproject.android.service;
|
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.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.BufferedReader;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
|
@ -42,44 +80,6 @@ import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeoutException;
|
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
|
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();
|
String action = mIntent.getAction();
|
||||||
|
|
||||||
if (action != null) {
|
if (action != null) {
|
||||||
if (action.equals(Intent.ACTION_BOOT_COMPLETED) || action.equals(CMD_START)) {
|
if (action.equals(CMD_START)) {
|
||||||
setTorProfile(STATUS_ON);
|
startTor();
|
||||||
} else if (action.equals(CMD_STOP)) {
|
} else if (action.equals(CMD_STOP)) {
|
||||||
setTorProfile(STATUS_OFF);
|
stopTor();
|
||||||
} else if (action.equals(CMD_NEWNYM)) {
|
} else if (action.equals(CMD_NEWNYM)) {
|
||||||
newIdentity();
|
newIdentity();
|
||||||
} else if (action.equals(CMD_FLUSH)) {
|
} else if (action.equals(CMD_FLUSH)) {
|
||||||
|
@ -391,21 +391,20 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
super.onDestroy();
|
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");
|
Log.d(TAG,"Tor is stopping NOW");
|
||||||
|
|
||||||
killAllDaemons ();
|
killAllDaemons();
|
||||||
|
|
||||||
//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);
|
||||||
|
|
||||||
mCurrentStatus = STATUS_OFF;
|
|
||||||
sendCallbackStatus(mCurrentStatus);
|
|
||||||
|
|
||||||
if (Prefs.useRoot() && Prefs.useTransparentProxying())
|
if (Prefs.useRoot() && Prefs.useTransparentProxying())
|
||||||
{
|
{
|
||||||
Shell shellRoot = Shell.startRootShell();
|
Shell shellRoot = Shell.startRootShell();
|
||||||
|
@ -416,7 +415,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
clearNotifications();
|
clearNotifications();
|
||||||
|
|
||||||
sendCallbackLogMessage(getString(R.string.status_disabled));
|
sendCallbackLogMessage(getString(R.string.status_disabled));
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (CannotKillException e)
|
catch (CannotKillException e)
|
||||||
{
|
{
|
||||||
|
@ -431,8 +429,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
Log.d(TAG, "An error occured stopping Tor",e);
|
Log.d(TAG, "An error occured stopping Tor",e);
|
||||||
logNotice("An error occured stopping Tor: " + e.getMessage());
|
logNotice("An error occured stopping Tor: " + e.getMessage());
|
||||||
sendCallbackLogMessage(getString(R.string.something_bad_happened));
|
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;
|
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;
|
mCurrentStatus = STATUS_STARTING;
|
||||||
sendCallbackStatus(mCurrentStatus);
|
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)
|
if (fileTor == null)
|
||||||
initBinariesAndDirectories();
|
initBinariesAndDirectories();
|
||||||
|
|
||||||
logNotice(getString(R.string.status_starting_up));
|
|
||||||
sendCallbackLogMessage(getString(R.string.status_starting_up));
|
|
||||||
|
|
||||||
ArrayList<String> customEnv = new ArrayList<String>();
|
ArrayList<String> customEnv = new ArrayList<String>();
|
||||||
|
|
||||||
if (Prefs.bridgesEnabled())
|
if (Prefs.bridgesEnabled())
|
||||||
|
@ -797,10 +811,23 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
showToolbarNotification(getString(R.string.unable_to_start_tor), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
|
showToolbarNotification(getString(R.string.unable_to_start_tor), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shellUser.close();
|
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 () {
|
private boolean flushTransparentProxyRules () {
|
||||||
|
@ -1110,9 +1137,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
conn.setConf("Log", "debug file " + fileLog2.getCanonicalPath());
|
conn.setConf("Log", "debug file " + fileLog2.getCanonicalPath());
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
mCurrentStatus = STATUS_STARTING;
|
|
||||||
sendCallbackStatus(mCurrentStatus);
|
|
||||||
|
|
||||||
String confSocks = conn.getInfo("net/listeners/socks");
|
String confSocks = conn.getInfo("net/listeners/socks");
|
||||||
StringTokenizer st = new StringTokenizer(confSocks," ");
|
StringTokenizer st = new StringTokenizer(confSocks," ");
|
||||||
|
|
||||||
|
@ -1291,60 +1315,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
return mPortSOCKS;
|
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 () {
|
public void enableVpnProxy () {
|
||||||
debug ("enabling VPN Proxy");
|
debug ("enabling VPN Proxy");
|
||||||
|
|
||||||
|
@ -1403,21 +1373,11 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void message(String severity, String msg) {
|
public void message(String severity, String msg) {
|
||||||
|
|
||||||
logNotice(severity + ": " + 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
|
@Override
|
||||||
public void newDescriptors(List<String> orList) {
|
public void newDescriptors(List<String> orList) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1547,23 +1507,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
|
||||||
if (Prefs.useDebugLogging())
|
if (Prefs.useDebugLogging())
|
||||||
debug(sb.toString());
|
debug(sb.toString());
|
||||||
else if(status.equals("BUILT"))
|
else if(status.equals("BUILT"))
|
||||||
{
|
|
||||||
|
|
||||||
if (mCurrentStatus == STATUS_STARTING)
|
|
||||||
mCurrentStatus = STATUS_ON;
|
|
||||||
|
|
||||||
sendCallbackStatus(mCurrentStatus);
|
|
||||||
|
|
||||||
|
|
||||||
logNotice(sb.toString());
|
logNotice(sb.toString());
|
||||||
|
|
||||||
}
|
|
||||||
else if (status.equals("CLOSED"))
|
else if (status.equals("CLOSED"))
|
||||||
{
|
|
||||||
logNotice(sb.toString());
|
logNotice(sb.toString());
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Prefs.expandedNotifications())
|
if (Prefs.expandedNotifications())
|
||||||
{
|
{
|
||||||
//get IP from last nodename
|
//get IP from last nodename
|
||||||
|
|
Loading…
Reference in New Issue