diff options
author | cyBerta <cyberta@riseup.net> | 2021-11-05 02:38:26 +0100 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2021-11-05 02:38:26 +0100 |
commit | 88b7dc2eb3dcbec8d1e637096867c15211818677 (patch) | |
tree | 739dd893e230ca9362b126bbb8fd47e78bfee5ea /app/src/main/java/se/leap/bitmaskclient/providersetup | |
parent | d27af3b17d6636de8b2755090b6fe6329531f639 (diff) |
Ensure tor state is set to OFF after snowflake completely stopped.
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/providersetup')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderAPI.java | 34 | ||||
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerBase.java | 32 |
2 files changed, 26 insertions, 40 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderAPI.java b/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderAPI.java index c8cc786a..4afeb26e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderAPI.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderAPI.java @@ -154,7 +154,7 @@ public class ProviderAPI extends JobIntentService implements ProviderApiManagerB } @Override - public void startTorService() { + public void startTorService() throws InterruptedException, IllegalStateException { initTorServiceConnection(this); Intent torServiceIntent = new Intent(this, TorService.class); @@ -171,12 +171,16 @@ public class ProviderAPI extends JobIntentService implements ProviderApiManagerB @Override public int getTorHttpTunnelPort() { - initTorServiceConnection(this); - if (torServiceConnection != null) { - int tunnelPort = torServiceConnection.torService.getHttpTunnelPort(); - torServiceConnection.close(); - torServiceConnection = null; - return tunnelPort; + try { + initTorServiceConnection(this); + if (torServiceConnection != null) { + int tunnelPort = torServiceConnection.torService.getHttpTunnelPort(); + torServiceConnection.close(); + torServiceConnection = null; + return tunnelPort; + } + } catch (InterruptedException | IllegalStateException e) { + e.printStackTrace(); } return -1; @@ -195,18 +199,14 @@ public class ProviderAPI extends JobIntentService implements ProviderApiManagerB * @throws InterruptedException thrown if thread gets interrupted * @throws IllegalStateException thrown if this method was not called from a background thread */ - private void initTorServiceConnection(Context context) { + private void initTorServiceConnection(Context context) throws InterruptedException, IllegalStateException { if (PreferenceHelper.getUseBridges(context)) { - try { - if (torServiceConnection == null) { - Log.d(TAG, "serviceConnection is still null"); - if (!TorService.hasClientTransportPlugin()) { - TorService.setClientTransportPlugin(new ClientTransportPlugin(context.getApplicationContext())); - } - torServiceConnection = new TorServiceConnection(context); + if (torServiceConnection == null) { + Log.d(TAG, "serviceConnection is still null"); + if (!TorService.hasClientTransportPlugin()) { + TorService.setClientTransportPlugin(new ClientTransportPlugin(context.getApplicationContext())); } - } catch (InterruptedException | IllegalStateException e) { - e.printStackTrace(); + torServiceConnection = new TorServiceConnection(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 d646b4bb..074cc121 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerBase.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderApiManagerBase.java @@ -48,11 +48,7 @@ import java.security.interfaces.RSAPrivateKey; import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; -import java.util.Observer; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.concurrent.atomic.AtomicBoolean; import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLPeerUnverifiedException; @@ -150,7 +146,7 @@ public abstract class ProviderApiManagerBase { public interface ProviderApiServiceCallback { void broadcastEvent(Intent intent); - void startTorService(); + void startTorService() throws InterruptedException, IllegalStateException; int getTorHttpTunnelPort(); boolean isConnectedToWifi(); } @@ -190,10 +186,9 @@ public abstract class ProviderApiManagerBase { return; } - // uncomment for testing --v try { startTorProxy(); - } catch (InterruptedException | TimeoutException e) { + } catch (InterruptedException | IllegalStateException | TimeoutException e) { e.printStackTrace(); return; } @@ -295,7 +290,7 @@ public abstract class ProviderApiManagerBase { } } - protected boolean startTorProxy() throws InterruptedException, TimeoutException { + protected boolean startTorProxy() throws InterruptedException, IllegalStateException, TimeoutException { if (PreferenceHelper.getUseBridges(preferences) && EipStatus.getInstance().isDisconnected() && serviceCallback.isConnectedToWifi() @@ -303,7 +298,7 @@ public abstract class ProviderApiManagerBase { serviceCallback.startTorService(); waitForTorCircuits(); if (TorStatusObservable.isCancelled()) { - throw new InterruptedException("cancelled Tor setup"); + throw new InterruptedException("Cancelled Tor setup."); } int port = serviceCallback.getTorHttpTunnelPort(); TorStatusObservable.setProxyPort(port); @@ -316,20 +311,11 @@ public abstract class ProviderApiManagerBase { if (TorStatusObservable.getStatus() == ON) { return; } - CountDownLatch countDownLatch = new CountDownLatch(1); - AtomicBoolean stopWaiting = new AtomicBoolean(false); - Observer observer = (o, arg) -> { - 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 (!stopWaiting.get()) { - throw new TimeoutException("Timeout reached"); - } + TorStatusObservable.waitUntil(this::isTorOnOrCancelled, 180); + } + + private boolean isTorOnOrCancelled() { + return TorStatusObservable.getStatus() == ON || TorStatusObservable.isCancelled(); } void resetProviderDetails(Provider provider) { |