Commit 5087fa81 authored by cyberta's avatar cyberta

improve vpn shutdown regarding the new gateway selection

(cherry picked from commit fcc04ae3)
parent 210332df
......@@ -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
......
......@@ -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;
}
}
/**
......
......@@ -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;
......
......@@ -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");
}
......
......@@ -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!");
......
......@@ -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);
}
......
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment