summaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorFup Duck <fupduck@sacknagel.com>2018-02-26 20:31:48 +0100
committerFup Duck <fupduck@sacknagel.com>2018-02-26 20:36:58 +0100
commit44322f28b28676a8938f4c1066a75ba47397aa7d (patch)
tree965c41181e779555d2eea01d873903eada27b263 /app/src/main/java
parent304efac028b1a2e2b145e5b2cba474c4b84fd3cf (diff)
8865 - allow disconnecting without internet
* moved logic from MainActivity to EIP
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/EipFragment.java3
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/MainActivity.java74
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/EIP.java103
3 files changed, 99 insertions, 81 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
index c4f4dbc9..956cd67e 100644
--- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
+++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
@@ -37,7 +37,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
-import android.widget.TextView;
import java.util.Observable;
import java.util.Observer;
@@ -252,7 +251,7 @@ public class EipFragment extends Fragment implements Observer {
}
private void handleSwitchOff() {
- if (eipStatus.isConnecting()) {
+ if (isOpenVpnRunningWithoutNetwork() || eipStatus.isConnecting()) {
askPendingStartCancellation();
} else if (eipStatus.isConnected()) {
askToStopEIP();
diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
index b4be55a4..ee754b32 100644
--- a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
+++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
@@ -2,15 +2,11 @@ package se.leap.bitmaskclient;
import android.content.BroadcastReceiver;
-import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
-import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
-import android.os.IBinder;
-import android.os.RemoteException;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
@@ -26,14 +22,9 @@ import org.json.JSONObject;
import java.util.Observable;
import java.util.Observer;
-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.drawer.NavigationDrawerFragment;
import se.leap.bitmaskclient.eip.EipCommand;
import se.leap.bitmaskclient.eip.EipStatus;
-import se.leap.bitmaskclient.eip.VoidVpnService;
import se.leap.bitmaskclient.fragments.LogFragment;
import static android.content.Intent.CATEGORY_DEFAULT;
@@ -43,9 +34,7 @@ import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP;
-import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP_BLOCKING_VPN;
import static se.leap.bitmaskclient.Constants.EIP_REQUEST;
-import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT;
import static se.leap.bitmaskclient.Constants.PROVIDER_KEY;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_CONFIGURE_LEAP;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_LOG_IN;
@@ -62,31 +51,15 @@ import static se.leap.bitmaskclient.R.string.downloading_vpn_certificate_failed;
import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message;
-public class MainActivity extends AppCompatActivity implements Observer {
+public class MainActivity extends AppCompatActivity {
public final static String TAG = MainActivity.class.getSimpleName();
private Provider provider = new Provider();
private SharedPreferences preferences;
- private EipStatus eipStatus;
private NavigationDrawerFragment navigationDrawerFragment;
private MainActivityBroadcastReceiver mainActivityBroadcastReceiver;
- private IOpenVPNServiceInternal mService;
- private ServiceConnection openVpnConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName className,
- IBinder service) {
- mService = IOpenVPNServiceInternal.Stub.asInterface(service);
- }
-
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- mService = null;
- }
-
- };
-
public final static String ACTION_SHOW_VPN_FRAGMENT = "action_show_vpn_fragment";
public final static String ACTION_SHOW_LOG_FRAGMENT = "action_show_log_fragment";
@@ -114,14 +87,12 @@ public class MainActivity extends AppCompatActivity implements Observer {
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
- eipStatus = EipStatus.getInstance();
handleIntentAction(getIntent());
}
@Override
protected void onResume() {
super.onResume();
- bindOpenVpnService();
}
@Override
@@ -205,7 +176,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
@Override
protected void onPause() {
super.onPause();
- unbindService(openVpnConnection);
}
@Override
@@ -215,14 +185,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
super.onDestroy();
}
-
- @Override
- public void update(Observable observable, Object data) {
- if (observable instanceof EipStatus) {
- eipStatus = (EipStatus) observable;
- }
- }
-
private void setUpBroadcastReceiver() {
IntentFilter updateIntentFilter = new IntentFilter(BROADCAST_EIP_EVENT);
updateIntentFilter.addAction(BROADCAST_PROVIDER_API_EVENT);
@@ -283,7 +245,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
case EIP_ACTION_STOP:
switch (resultCode) {
case RESULT_OK:
- stop();
break;
case RESULT_CANCELED:
break;
@@ -343,39 +304,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
}
-
- private void stop() {
- preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, false).apply();
- if (eipStatus.isBlockingVpnEstablished()) {
- stopBlockingVpn();
- }
- disconnect();
- }
-
- private void stopBlockingVpn() {
- Log.d(TAG, "stop VoidVpn!");
- Intent stopVoidVpnIntent = new Intent(this, VoidVpnService.class);
- stopVoidVpnIntent.setAction(EIP_ACTION_STOP_BLOCKING_VPN);
- startService(stopVoidVpnIntent);
- }
-
- private void disconnect() {
- ProfileManager.setConntectedVpnProfileDisconnected(this);
- if (mService != null) {
- try {
- mService.stopVPN(false);
- } catch (RemoteException e) {
- VpnStatus.logException(e);
- }
- }
- }
-
- private void bindOpenVpnService() {
- Intent intent = new Intent(this, OpenVPNService.class);
- intent.setAction(OpenVPNService.START_SERVICE);
- bindService(intent, openVpnConnection, Context.BIND_AUTO_CREATE);
- }
-
private void askUserToLogIn(String userMessage) {
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra(PROVIDER_KEY, provider);
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 5cf180d3..0c83c261 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
@@ -17,9 +17,13 @@
package se.leap.bitmaskclient.eip;
import android.app.IntentService;
+import android.content.ComponentName;
import android.content.Intent;
+import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
+import android.os.IBinder;
+import android.os.RemoteException;
import android.os.ResultReceiver;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
@@ -28,13 +32,20 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.lang.ref.WeakReference;
+import java.util.Observable;
+import java.util.Observer;
import de.blinkt.openvpn.LaunchVPN;
+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 static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
import static android.content.Intent.CATEGORY_DEFAULT;
+import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_NONETWORK;
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;
@@ -43,6 +54,7 @@ import static se.leap.bitmaskclient.Constants.EIP_ACTION_IS_RUNNING;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START_ALWAYS_ON_VPN;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP;
+import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP_BLOCKING_VPN;
import static se.leap.bitmaskclient.Constants.EIP_EARLY_ROUTES;
import static se.leap.bitmaskclient.Constants.EIP_RECEIVER;
import static se.leap.bitmaskclient.Constants.EIP_REQUEST;
@@ -63,7 +75,7 @@ import static se.leap.bitmaskclient.R.string.vpn_certificate_is_invalid;
* @author Sean Leonard <meanderingcode@aetherislands.net>
* @author Parménides GV <parmegv@sdf.org>
*/
-public final class EIP extends IntentService {
+public final class EIP extends IntentService implements Observer{
public final static String TAG = EIP.class.getSimpleName(),
SERVICE_API_PATH = "config/eip-service.json",
@@ -73,6 +85,27 @@ public final class EIP extends IntentService {
private WeakReference<ResultReceiver> mReceiverRef = new WeakReference<>(null);
private SharedPreferences preferences;
+ private EipStatus eipStatus;
+ private IOpenVPNServiceInternal mService;
+ private boolean stopEipIfConnectionIsEstablished = false;
+
+ private ServiceConnection openVpnConnection = new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName className,
+ IBinder service) {
+ mService = IOpenVPNServiceInternal.Stub.asInterface(service);
+ if (stopEipIfConnectionIsEstablished) {
+ stopEipIfConnectionIsEstablished = false;
+ stopEIP();
+ }
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName arg0) {
+ mService = null;
+ }
+ };
+
public EIP() {
super(TAG);
}
@@ -80,10 +113,31 @@ public final class EIP extends IntentService {
@Override
public void onCreate() {
super.onCreate();
+ eipStatus = EipStatus.getInstance();
+ eipStatus.addObserver(this);
+ Intent intent = new Intent(this, OpenVPNService.class);
+ intent.setAction(OpenVPNService.START_SERVICE);
+ bindOpenVpnService();
preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
}
@Override
+ public void onDestroy() {
+ super.onDestroy();
+ unbindService(openVpnConnection);
+ eipStatus.deleteObserver(this);
+ eipStatus = null;
+ mService = null;
+ }
+
+ @Override
+ public void update(Observable observable, Object data) {
+ if (observable instanceof EipStatus) {
+ eipStatus = (EipStatus) observable;
+ }
+ }
+
+ @Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if (intent.getParcelableExtra(EIP_RECEIVER) != null) {
@@ -183,22 +237,41 @@ public final class EIP extends IntentService {
}
private void stopEIP() {
- // TODO stop eip from here if possible...
- EipStatus eipStatus = EipStatus.getInstance();
+ if (mService == null) {
+ stopEipIfConnectionIsEstablished = true;
+ return;
+ }
+
int resultCode = RESULT_CANCELED;
- if (eipStatus.isConnected() || eipStatus.isConnecting())
+ if (isOpenVpnRunningWithoutNetwork() || eipStatus.isConnected() || eipStatus.isConnecting()) {
+ preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, false).apply();
+ if (eipStatus.isBlockingVpnEstablished()) {
+ stopBlockingVpn();
+ }
+ ProfileManager.setConntectedVpnProfileDisconnected(this);
+ try {
+ mService.stopVPN(false);
+ } catch (RemoteException e) {
+ VpnStatus.logException(e);
+ }
resultCode = RESULT_OK;
-
+ }
tellToReceiverOrBroadcast(EIP_ACTION_STOP, resultCode);
}
+ private void stopBlockingVpn() {
+ Log.d(TAG, "stop VoidVpn!");
+ Intent stopVoidVpnIntent = new Intent(this, VoidVpnService.class);
+ stopVoidVpnIntent.setAction(EIP_ACTION_STOP_BLOCKING_VPN);
+ startService(stopVoidVpnIntent);
+ }
+
/**
* Checks the last stored status notified by ics-openvpn
* Sends <code>Activity.RESULT_CANCELED</code> to the ResultReceiver that made the
* request if it's not connected, <code>Activity.RESULT_OK</code> otherwise.
*/
private void isRunning() {
- EipStatus eipStatus = EipStatus.getInstance();
int resultCode = (eipStatus.isConnected()) ?
RESULT_OK :
RESULT_CANCELED;
@@ -278,4 +351,22 @@ public final class EIP extends IntentService {
e.printStackTrace();
}
}
+
+ private boolean isOpenVpnRunningWithoutNetwork() {
+ boolean isRunning = false;
+ try {
+ isRunning = eipStatus.getLevel() == LEVEL_NONETWORK &&
+ mService.isVpnRunning();
+ } catch (Exception e) {
+ //eat me
+ e.printStackTrace();
+ }
+ return isRunning;
+ }
+
+ private void bindOpenVpnService() {
+ Intent intent = new Intent(this, OpenVPNService.class);
+ intent.setAction(OpenVPNService.START_SERVICE);
+ bindService(intent, openVpnConnection, BIND_AUTO_CREATE);
+ }
}