From 01e40b1a843326036425727355550dba8bd66335 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sun, 24 Oct 2021 16:47:12 +0200 Subject: cancel tor and provider setup on pressing back button --- .../se/leap/bitmaskclient/eip/EipSetupObserver.java | 3 +-- .../providersetup/ProviderApiManagerBase.java | 16 ++++++++++------ .../activities/ProviderSetupBaseActivity.java | 4 ++-- .../se/leap/bitmaskclient/tor/TorStatusObservable.java | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipSetupObserver.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipSetupObserver.java index 23fbdc0c..203885a1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipSetupObserver.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipSetupObserver.java @@ -368,8 +368,7 @@ public class EipSetupObserver extends BroadcastReceiver implements VpnStatus.Sta observedProfileFromVpnStatus = null; this.changingGateway.set(changingGateway); if (TorStatusObservable.getStatus() != OFF) { - Intent intent = new Intent(context, TorService.class); - context.stopService(intent); + TorStatusObservable.shutdownTor(context); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerBase.java b/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerBase.java index 6e07e84b..bc173b36 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerBase.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerBase.java @@ -192,11 +192,12 @@ public abstract class ProviderApiManagerBase { } // uncomment for testing --v - /* try { + try { startTorProxy(); } catch (InterruptedException | TimeoutException e) { e.printStackTrace(); - } */ + return; + } Bundle result = new Bundle(); switch (action) { @@ -302,6 +303,9 @@ public abstract class ProviderApiManagerBase { ) { serviceCallback.startTorService(); waitForTorCircuits(); + if (TorStatusObservable.isCancelled()) { + throw new InterruptedException("cancelled Tor setup"); + } int port = serviceCallback.getTorHttpTunnelPort(); TorStatusObservable.setProxyPort(port); return port != -1; @@ -314,17 +318,17 @@ public abstract class ProviderApiManagerBase { return; } CountDownLatch countDownLatch = new CountDownLatch(1); - AtomicBoolean successfulSetup = new AtomicBoolean(false); + AtomicBoolean stopWaiting = new AtomicBoolean(false); Observer observer = (o, arg) -> { - if (TorStatusObservable.getStatus() == ON) { - successfulSetup.set(true); + if (TorStatusObservable.getStatus() == ON || TorStatusObservable.isCancelled()) { + stopWaiting.set(true); countDownLatch.countDown(); } }; TorStatusObservable.getInstance().addObserver(observer); countDownLatch.await(180, TimeUnit.SECONDS); TorStatusObservable.getInstance().deleteObserver(observer); - if (!successfulSetup.get()) { + if (!stopWaiting.get()) { throw new TimeoutException("Timeout reached"); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/ProviderSetupBaseActivity.java b/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/ProviderSetupBaseActivity.java index e4569c51..6395c7ae 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/ProviderSetupBaseActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/ProviderSetupBaseActivity.java @@ -170,8 +170,8 @@ public abstract class ProviderSetupBaseActivity extends ConfigWizardBaseActivity public void cancelSettingUpProvider(boolean stopTor) { if (stopTor && TorStatusObservable.getStatus() != OFF) { - Intent torServiceIntent = new Intent(getApplicationContext(), TorService.class); - stopService(torServiceIntent); + Log.d(TAG, "SHUTDOWN - cancelSettingUpProvider stopTor:" + stopTor); + TorStatusObservable.shutdownTor(this); } providerConfigState = PROVIDER_NOT_SET; provider = null; diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorStatusObservable.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorStatusObservable.java index 281b21c0..fb7e4d93 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorStatusObservable.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorStatusObservable.java @@ -1,10 +1,13 @@ package se.leap.bitmaskclient.tor; import android.content.Context; +import android.content.Intent; import android.util.Log; import androidx.annotation.Nullable; +import org.torproject.jni.TorService; + import java.util.Observable; import java.util.Vector; @@ -22,6 +25,8 @@ public class TorStatusObservable extends Observable { UNKOWN } + private boolean cancelled = false; + public static final String LOG_TAG_TOR = "[TOR]"; public static final String LOG_TAG_SNOWFLAKE = "[SNOWFLAKE]"; @@ -97,6 +102,7 @@ public class TorStatusObservable extends Observable { if (getInstance().status == TorStatus.OFF) { getInstance().torNotificationManager.cancelNotifications(context); + getInstance().cancelled = false; } else { if (logKey != null) { getInstance().lastTorLog = getStringFor(context, logKey); @@ -195,4 +201,16 @@ public class TorStatusObservable extends Observable { } return null; } + + public static void shutdownTor(Context context) { + getInstance().cancelled = true; + getInstance().notifyObservers(); + + Intent intent = new Intent(context, TorService.class); + boolean stopped = context.stopService(intent); + } + + public static boolean isCancelled() { + return getInstance().cancelled; + } } -- cgit v1.2.3