From 63ae29d808da4c85c223f2e2b6824322b8fc45df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Wed, 17 Dec 2014 12:25:31 +0100 Subject: Stop normal and blocking VPNs correctly --- .../java/se/leap/bitmaskclient/EipFragment.java | 10 ++++++--- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 2 +- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 24 ++++++++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 92d2a8bd..fc412449 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -26,6 +26,7 @@ import de.blinkt.openvpn.activities.DisconnectVPN; import se.leap.bitmaskclient.eip.Constants; import se.leap.bitmaskclient.eip.EIP; import se.leap.bitmaskclient.eip.EipStatus; +import se.leap.bitmaskclient.eip.VoidVpnService; public class EipFragment extends Fragment implements Observer { @@ -306,14 +307,17 @@ public class EipFragment extends Fragment implements Observer { case Activity.RESULT_OK: break; case Activity.RESULT_CANCELED: + break; } } else if (request.equals(Constants.ACTION_STOP_EIP)) { switch (resultCode){ case Activity.RESULT_OK: - Intent disconnect_vpn = new Intent(parent_activity, DisconnectVPN.class); - parent_activity.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); - eip_status.setDisconnecting(); + if(eip_status.isConnecting()) + VoidVpnService.stop(); + Intent disconnect_vpn = new Intent(parent_activity, DisconnectVPN.class); + parent_activity.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); + eip_status.setDisconnecting(); break; case Activity.RESULT_CANCELED: break; 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 7017e874..3d3070c8 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -149,7 +149,7 @@ public final class EIP extends IntentService { EipStatus eip_status = EipStatus.getInstance(); Log.d(TAG, "stopEip(): eip is connected? " + eip_status.isConnected()); int result_code = Activity.RESULT_CANCELED; - if(eip_status.isConnected()) + if(eip_status.isConnected() || eip_status.isConnecting()) result_code = Activity.RESULT_OK; tellToReceiver(ACTION_STOP_EIP, result_code); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index a6f9fe76..0c773208 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -2,14 +2,18 @@ package se.leap.bitmaskclient.eip; import android.content.Intent; import android.net.VpnService; +import android.os.ParcelFileDescriptor; + +import java.io.IOException; public class VoidVpnService extends VpnService { static final String TAG = VoidVpnService.class.getSimpleName(); + static ParcelFileDescriptor fd; @Override public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent.getAction(); + String action = intent != null ? intent.getAction() : ""; if (action == Constants.START_BLOCKING_VPN_PROFILE) { new Thread(new Runnable() { public void run() { @@ -20,7 +24,7 @@ public class VoidVpnService extends VpnService { builder.addRoute("192.168.1.0", 24); builder.addDnsServer("10.42.0.1"); try { - builder.establish(); + fd = builder.establish(); } catch (Exception e) { e.printStackTrace(); } @@ -30,4 +34,20 @@ public class VoidVpnService extends VpnService { } return 0; } + + @Override + public void onRevoke() { + super.onRevoke(); + } + + public static boolean stop() { + try { + fd.close(); + return true; + } catch (IOException | NullPointerException e) { + android.util.Log.d(TAG, "VoidVpnService didn't stop"); + e.printStackTrace(); + return false; + } + } } -- cgit v1.2.3 From 5d05a628fbd833860abfc85bb450186ec8dedf19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Wed, 17 Dec 2014 12:39:41 +0100 Subject: Small refactor to stop EIP methods. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 2 +- .../main/java/se/leap/bitmaskclient/EipFragment.java | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index cca9c63c..e32dbdee 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -292,7 +292,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn if (preferences.getBoolean(Constants.AUTHED_EIP, false)) { logOut(); } - eip_fragment.stopEIP(); + eip_fragment.askToStopEIP(); } preferences.edit().clear().apply(); startActivityForResult(new Intent(this,ConfigurationWizard.class), SWITCH_PROVIDER); diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index fc412449..420da7a1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -161,7 +161,7 @@ public class EipFragment extends Fragment implements Observer { if(eip_status.isConnecting()) { askPendingStartCancellation(); } else if(eip_status.isConnected()) { - stopEIP(); + askToStopEIP(); } } @@ -172,7 +172,7 @@ public class EipFragment extends Fragment implements Observer { .setPositiveButton((R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - stopEIP(); + askToStopEIP(); } }) .setNegativeButton(parent_activity.getString(R.string.no), new DialogInterface.OnClickListener() { @@ -198,7 +198,15 @@ public class EipFragment extends Fragment implements Observer { eipCommand(Constants.ACTION_START_EIP); } - protected void stopEIP() { + private void stopEIP() { + if(eip_status.isConnecting()) + VoidVpnService.stop(); + Intent disconnect_vpn = new Intent(parent_activity, DisconnectVPN.class); + parent_activity.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); + eip_status.setDisconnecting(); + } + + protected void askToStopEIP() { hideProgressBar(); String status = parent_activity.getString(R.string.eip_state_not_connected); @@ -313,11 +321,7 @@ public class EipFragment extends Fragment implements Observer { } else if (request.equals(Constants.ACTION_STOP_EIP)) { switch (resultCode){ case Activity.RESULT_OK: - if(eip_status.isConnecting()) - VoidVpnService.stop(); - Intent disconnect_vpn = new Intent(parent_activity, DisconnectVPN.class); - parent_activity.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); - eip_status.setDisconnecting(); + stopEIP(); break; case Activity.RESULT_CANCELED: break; -- cgit v1.2.3