diff options
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/eip/EIP.java')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/eip/EIP.java | 73 |
1 files changed, 55 insertions, 18 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 0b35dc3d..a84ab941 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -19,12 +19,25 @@ package se.leap.bitmaskclient.eip; import android.app.*; import android.content.*; import android.os.*; +import android.util.Log; import org.json.*; import de.blinkt.openvpn.*; import se.leap.bitmaskclient.*; +import static se.leap.bitmaskclient.Constants.EIP_ACTION_CHECK_CERT_VALIDITY; +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_EIP; +import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP; +import static se.leap.bitmaskclient.Constants.EIP_ACTION_UPDATE; +import static se.leap.bitmaskclient.Constants.EIP_RECEIVER; +import static se.leap.bitmaskclient.Constants.EIP_REQUEST; +import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; +import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; + /** * EIP is the abstract base class for interacting with and managing the Encrypted * Internet Proxy connection. Connections are started, stopped, and queried through @@ -55,9 +68,8 @@ public final class EIP extends IntentService { @Override public void onCreate() { super.onCreate(); - context = getApplicationContext(); - preferences = getSharedPreferences(Constants.SHARED_PREFERENCES, MODE_PRIVATE); + preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); eip_definition = eipDefinitionFromPreferences(); if (gateways_manager.isEmpty()) gatewaysFromPreferences(); @@ -66,17 +78,19 @@ public final class EIP extends IntentService { @Override protected void onHandleIntent(Intent intent) { String action = intent.getAction(); - mReceiver = intent.getParcelableExtra(Constants.EIP_RECEIVER); + mReceiver = intent.getParcelableExtra(EIP_RECEIVER); - if (action.equals(Constants.EIP_ACTION_START)) + if (action.equals(EIP_ACTION_START)) startEIP(); - else if (action.equals(Constants.EIP_ACTION_STOP)) + else if (action.equals(EIP_ACTION_START_ALWAYS_ON_EIP)) + startAlwaysOnEIP(); + else if (action.equals(EIP_ACTION_STOP)) stopEIP(); - else if (action.equals(Constants.EIP_ACTION_IS_RUNNING)) + else if (action.equals(EIP_ACTION_IS_RUNNING)) isRunning(); - else if (action.equals(Constants.EIP_ACTION_UPDATE)) + else if (action.equals(EIP_ACTION_UPDATE)) updateEIPService(); - else if (action.equals(Constants.EIP_ACTION_CHECK_CERT_VALIDITY)) + else if (action.equals(EIP_ACTION_CHECK_CERT_VALIDITY)) checkCertValidity(); } @@ -88,15 +102,38 @@ public final class EIP extends IntentService { private void startEIP() { if (gateways_manager.isEmpty()) updateEIPService(); - earlyRoutes(); + if (!EipStatus.getInstance().isBlockingVpnEstablished()) { + earlyRoutes(); + } gateway = gateways_manager.select(); if (gateway != null && gateway.getProfile() != null) { mReceiver = VpnFragment.getReceiver(); launchActiveGateway(); - tellToReceiver(Constants.EIP_ACTION_START, Activity.RESULT_OK); + tellToReceiver(EIP_ACTION_START, Activity.RESULT_OK); } else - tellToReceiver(Constants.EIP_ACTION_START, Activity.RESULT_CANCELED); + tellToReceiver(EIP_ACTION_START, Activity.RESULT_CANCELED); + } + + /** + * Tries to start the last used vpn profile when the OS was rebooted and always-on-VPN is enabled. + * The {@link OnBootReceiver} will care if there is no profile. + */ + private void startAlwaysOnEIP() { + Log.d(TAG, "startAlwaysOnEIP vpn"); + + if (gateways_manager.isEmpty()) + updateEIPService(); + + gateway = gateways_manager.select(); + + if (gateway != null && gateway.getProfile() != null) { + //mReceiver = VpnFragment.getReceiver(); + Log.d(TAG, "startAlwaysOnEIP eip launch avtive gateway vpn"); + launchActiveGateway(); + } else { + Log.d(TAG, "startAlwaysOnEIP no active profile available!"); + } } /** @@ -124,7 +161,7 @@ public final class EIP extends IntentService { if (eip_status.isConnected() || eip_status.isConnecting()) result_code = Activity.RESULT_OK; - tellToReceiver(Constants.EIP_ACTION_STOP, result_code); + tellToReceiver(EIP_ACTION_STOP, result_code); } /** @@ -137,7 +174,7 @@ public final class EIP extends IntentService { int resultCode = (eip_status.isConnected()) ? Activity.RESULT_OK : Activity.RESULT_CANCELED; - tellToReceiver(Constants.EIP_ACTION_IS_RUNNING, resultCode); + tellToReceiver(EIP_ACTION_IS_RUNNING, resultCode); } /** @@ -148,13 +185,13 @@ public final class EIP extends IntentService { eip_definition = eipDefinitionFromPreferences(); if (eip_definition.length() > 0) updateGateways(); - tellToReceiver(Constants.EIP_ACTION_UPDATE, Activity.RESULT_OK); + tellToReceiver(EIP_ACTION_UPDATE, Activity.RESULT_OK); } private JSONObject eipDefinitionFromPreferences() { JSONObject result = new JSONObject(); try { - String eip_definition_string = preferences.getString(Constants.PROVIDER_KEY, ""); + String eip_definition_string = preferences.getString(PROVIDER_KEY, ""); if (!eip_definition_string.isEmpty()) { result = new JSONObject(eip_definition_string); } @@ -184,17 +221,17 @@ public final class EIP extends IntentService { } private void checkCertValidity() { - VpnCertificateValidator validator = new VpnCertificateValidator(preferences.getString(Constants.PROVIDER_VPN_CERTIFICATE, "")); + VpnCertificateValidator validator = new VpnCertificateValidator(preferences.getString(PROVIDER_VPN_CERTIFICATE, "")); int resultCode = validator.isValid() ? Activity.RESULT_OK : Activity.RESULT_CANCELED; - tellToReceiver(Constants.EIP_ACTION_CHECK_CERT_VALIDITY, resultCode); + tellToReceiver(EIP_ACTION_CHECK_CERT_VALIDITY, resultCode); } private void tellToReceiver(String action, int resultCode) { if (mReceiver != null) { Bundle resultData = new Bundle(); - resultData.putString(Constants.EIP_REQUEST, action); + resultData.putString(EIP_REQUEST, action); mReceiver.send(resultCode, resultData); } } |