From 5087fa81221937259f9275d6efa0cc1c7fad1454 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 16 Feb 2019 14:41:57 +0100 Subject: improve vpn shutdown regarding the new gateway selection (cherry picked from commit fcc04ae) --- .../de/blinkt/openvpn/core/ConnectionStatus.java | 3 ++- .../de/blinkt/openvpn/core/OpenVPNService.java | 22 ++++++++-------------- .../java/de/blinkt/openvpn/core/VpnStatus.java | 19 +++++++++++++++---- .../java/se/leap/bitmaskclient/EipFragment.java | 2 +- .../se/leap/bitmaskclient/EipSetupObserver.java | 18 ++++-------------- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 7 +++---- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 1 + 7 files changed, 34 insertions(+), 38 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/de/blinkt/openvpn/core/ConnectionStatus.java b/app/src/main/java/de/blinkt/openvpn/core/ConnectionStatus.java index 3e6d23f7..8ee46d77 100644 --- a/app/src/main/java/de/blinkt/openvpn/core/ConnectionStatus.java +++ b/app/src/main/java/de/blinkt/openvpn/core/ConnectionStatus.java @@ -21,7 +21,8 @@ public enum ConnectionStatus implements Parcelable { LEVEL_START, LEVEL_AUTH_FAILED, LEVEL_WAITING_FOR_USER_INPUT, - LEVEL_BLOCKING, // used for Bitmask's VoidVPN + LEVEL_BLOCKING, // used for Bitmask's VoidVPN + LEVEL_STOPPING, UNKNOWN_LEVEL; @Override diff --git a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java index 11bc4da3..55e9cfe9 100644 --- a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -7,12 +7,8 @@ package de.blinkt.openvpn.core; import android.Manifest.permission; import android.annotation.TargetApi; -import android.app.Activity; import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; import android.app.UiModeManager; -import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; @@ -20,17 +16,14 @@ import android.content.pm.ShortcutManager; import android.content.res.Configuration; import android.content.res.Resources; import android.net.ConnectivityManager; -import android.net.Uri; import android.net.VpnService; import android.os.Build; -import android.os.Bundle; import android.os.Handler; import android.os.Handler.Callback; import android.os.IBinder; import android.os.Message; import android.os.ParcelFileDescriptor; import android.os.RemoteException; -import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.system.OsConstants; import android.text.TextUtils; @@ -39,7 +32,6 @@ import android.widget.Toast; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.Inet6Address; import java.net.InetAddress; import java.net.UnknownHostException; @@ -47,8 +39,6 @@ import java.util.Collection; import java.util.Locale; import java.util.Vector; -import de.blinkt.openvpn.LaunchVPN; -import se.leap.bitmaskclient.R; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.VpnStatus.ByteCountListener; import de.blinkt.openvpn.core.VpnStatus.StateListener; @@ -248,10 +238,14 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac @Override public boolean stopVPN(boolean replaceConnection) throws RemoteException { - if (getManagement() != null) - return getManagement().stopVPN(replaceConnection); - else - return false; + if (getManagement() != null && getManagement().stopVPN(replaceConnection)) { + if (!replaceConnection) { + VpnStatus.updateStateString("NOPROCESS", "VPN STOPPED", R.string.state_noprocess, ConnectionStatus.LEVEL_NOTCONNECTED); + } + return true; + } else { + return false; + } } /** diff --git a/app/src/main/java/de/blinkt/openvpn/core/VpnStatus.java b/app/src/main/java/de/blinkt/openvpn/core/VpnStatus.java index 0fae6183..64a87df0 100644 --- a/app/src/main/java/de/blinkt/openvpn/core/VpnStatus.java +++ b/app/src/main/java/de/blinkt/openvpn/core/VpnStatus.java @@ -212,7 +212,6 @@ public class VpnStatus { } public interface StateListener { - String STATE_CONNECTRETRY = "CONNECTRETRY"; void updateState(String state, String logmessage, int localizedResId, ConnectionStatus level); void setConnectedVPN(String uuid); @@ -328,22 +327,29 @@ public class VpnStatus { String[] connected = {"CONNECTED"}; String[] notconnected = {"DISCONNECTED", "EXITING"}; + /** + * ignore incoming connection/reconnecting states if vpn is about to shut down + */ for (String x : noreplyet) - if (state.equals(x)) + if (state.equals(x) && !mLaststate.equals("STOPPING")) return ConnectionStatus.LEVEL_CONNECTING_NO_SERVER_REPLY_YET; for (String x : reply) - if (state.equals(x)) + if (state.equals(x) && !mLaststate.equals("STOPPING")) return ConnectionStatus.LEVEL_CONNECTING_SERVER_REPLIED; for (String x : connected) - if (state.equals(x)) + if (state.equals(x) && !mLaststate.equals("STOPPING")) return ConnectionStatus.LEVEL_CONNECTED; for (String x : notconnected) if (state.equals(x)) return ConnectionStatus.LEVEL_NOTCONNECTED; + if (mLaststate.equals("STOPPING")) { + return ConnectionStatus.LEVEL_STOPPING; + } + return ConnectionStatus.UNKNOWN_LEVEL; } @@ -377,6 +383,11 @@ public class VpnStatus { return; } + if (mLaststate.equals("STOPPING") && !state.equals("DISCONNECTED") && !state.equals("EXITING")) { + newLogItem(new LogItem((LogLevel.DEBUG), String.format("Ignoring OpenVPN Status while exiting (%s->%s)", mLastLevel.toString(), level.toString()))); + return; + } + mLaststate = state; mLaststatemsg = msg; mLastStateresid = resid; diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index a535b0cb..2af455f8 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -350,7 +350,7 @@ public class EipFragment extends Fragment implements Observer { eipStatus = (EipStatus) observable; Activity activity = getActivity(); if (activity != null) { - activity.runOnUiThread(() -> handleNewState()); + activity.runOnUiThread(this::handleNewState); } else { Log.e("EipFragment", "activity is null"); } diff --git a/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java b/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java index 037a6952..16b8c240 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java @@ -102,8 +102,6 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe @Override public void onReceive(Context context, Intent intent) { - Log.d(TAG, "received Broadcast"); - String action = intent.getAction(); if (action == null) { return; @@ -125,8 +123,6 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe } private void handleProviderApiEvent(Intent intent) { - - int resultCode = intent.getIntExtra(BROADCAST_RESULT_CODE, RESULT_CANCELED); Bundle resultData = intent.getParcelableExtra(BROADCAST_RESULT_KEY); if (resultData == null) { @@ -152,12 +148,9 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe break; } - - for (EipSetupListener listener : listeners) { listener.handleProviderApiEvent(intent); } - } @@ -176,10 +169,6 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe finishGatewaySetup(false); } break; - case EIP_ACTION_STOP: - //setup was manually cancelled - finishGatewaySetup(false); - break; default: break; } @@ -234,9 +223,10 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe return; } - Log.d(TAG, "trying gateway: " + setupVpnProfile.getName()); - - if ("CONNECTRETRY".equals(state) && LEVEL_CONNECTING_NO_SERVER_REPLY_YET.equals(level)) { + if (ConnectionStatus.LEVEL_STOPPING == level) { + finishGatewaySetup(false); + } else if ("CONNECTRETRY".equals(state) && LEVEL_CONNECTING_NO_SERVER_REPLY_YET.equals(level)) { + Log.d(TAG, "trying gateway: " + setupVpnProfile.getName()); if (TIMEOUT.equals(logmessage)) { Log.e(TAG, "Timeout reached! Try next gateway!"); VpnStatus.logError("Timeout reached! Try next gateway!"); 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 0779c69d..29e2199f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -43,12 +43,13 @@ import java.util.Observer; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; -import de.blinkt.openvpn.LaunchVPN; +import de.blinkt.openvpn.core.ConnectionStatus; import de.blinkt.openvpn.core.IOpenVPNServiceInternal; import de.blinkt.openvpn.core.OpenVPNService; import de.blinkt.openvpn.core.ProfileManager; import de.blinkt.openvpn.core.VpnStatus; import se.leap.bitmaskclient.OnBootReceiver; +import se.leap.bitmaskclient.R; import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_OK; @@ -215,13 +216,10 @@ public final class EIP extends JobIntentService implements Observer { * The {@link OnBootReceiver} will care if there is no profile. */ private void startEIPAlwaysOnVpn() { - Log.d(TAG, "startEIPAlwaysOnVpn vpn"); - GatewaysManager gatewaysManager = gatewaysFromPreferences(); Gateway gateway = gatewaysManager.select(0); if (gateway != null && gateway.getProfile() != null) { - Log.d(TAG, "startEIPAlwaysOnVpn eip launch closest gateway."); launchActiveGateway(gateway, 0); } else { Log.d(TAG, "startEIPAlwaysOnVpn no active profile available!"); @@ -257,6 +255,7 @@ public final class EIP extends JobIntentService implements Observer { * terminates EIP if currently connected or connecting */ private void stopEIP() { + VpnStatus.updateStateString("STOPPING", "STOPPING VPN", R.string.state_exiting, ConnectionStatus.LEVEL_STOPPING); int resultCode = stop() ? RESULT_OK : RESULT_CANCELED; tellToReceiverOrBroadcast(EIP_ACTION_STOP, resultCode); } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index 861f5fd3..20154ac1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -115,6 +115,7 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { break; case LEVEL_AUTH_FAILED: case LEVEL_NOTCONNECTED: + case LEVEL_STOPPING: currentEipLevel = EipLevel.DISCONNECTED; break; case LEVEL_NONETWORK: -- cgit v1.2.3