diff options
author | cyBerta <cyberta@riseup.net> | 2018-03-08 21:48:47 +0100 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2018-03-08 21:48:47 +0100 |
commit | 77cf66aad037fcb9d091d2fe5d53e38b06df15ae (patch) | |
tree | 49b1fc357b3b1da251ead23928a35997977eba43 | |
parent | 203043429b2c9c2f60dbf5a66177c0b80830d5eb (diff) |
#8876 make OpenvpnServiceConnection thread safe
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/eip/EIP.java | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java index 1e553ac2..ab644c93 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -55,6 +55,7 @@ import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_OK; import static android.content.Intent.CATEGORY_DEFAULT; import static se.leap.bitmaskclient.ConfigHelper.ensureNotOnMainThread; +import static se.leap.bitmaskclient.ConfigHelper.trim; import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY; @@ -95,7 +96,8 @@ public final class EIP extends Service implements Observer { private AtomicInteger processCounter; private EipStatus eipStatus; - private OpenvpnServiceConnection openvpnServiceConnection; + // Service connection to OpenVpnService, shared between threads + private volatile OpenvpnServiceConnection openvpnServiceConnection; @Nullable @Override @@ -363,20 +365,15 @@ public final class EIP extends Service implements Observer { /** - * check if OpenVPNConnection is connected - * then terminate VPN connection + * creates a OpenVpnServiceConnection if necessary + * then terminates OpenVPN */ @WorkerThread private boolean disconnect() { - // if OpenVPNConnection is not connected remember to disconnect - // after connection is established - if (openvpnServiceConnection == null) { - try { - openvpnServiceConnection = new OpenvpnServiceConnection(this); - } catch (InterruptedException | IllegalStateException e) { - e.printStackTrace(); - return false; - } + try { + initOpenVpnServiceConnection(); + } catch (InterruptedException | IllegalStateException e) { + return false; } ProfileManager.setConntectedVpnProfileDisconnected(this); @@ -389,6 +386,20 @@ public final class EIP extends Service implements Observer { } /** + * Assigns a new OpenvpnServiceConnection to EIP's member variable openvpnServiceConnection. + * Only one thread at a time can create the service connection, that will be shared between threads + * + * @throws InterruptedException thrown if thread gets interrupted + * @throws IllegalStateException thrown if this method was not called from a background thread + */ + @WorkerThread + private synchronized void initOpenVpnServiceConnection() throws InterruptedException, IllegalStateException { + if (openvpnServiceConnection == null) { + openvpnServiceConnection = new OpenvpnServiceConnection(this); + } + } + + /** * Creates a service connection to OpenVpnService. * The constructor blocks until the service is bound to the given Context. * Pattern stolen from android.security.KeyChain.java @@ -399,8 +410,6 @@ public final class EIP extends Service implements Observer { private ServiceConnection serviceConnection; private IOpenVPNServiceInternal service; - - protected OpenvpnServiceConnection(Context context) throws InterruptedException { this.context = context; ensureNotOnMainThread(context); |