diff options
author | cyBerta <cyberta@riseup.net> | 2019-10-25 17:10:13 +0200 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2019-10-25 17:10:13 +0200 |
commit | 761b604e14b14a86a357816b266e77d458137c83 (patch) | |
tree | f3dfefaeee6b71738fd1ae1d670d6392c38a8f2b /app/src/main/java/se/leap/bitmaskclient/eip | |
parent | 6b4f3597733ced8d312ed263d53b9669d6274d65 (diff) |
implement error handling for edge case when Android
throws an nullpointer exception while it tries to prepare
the VpnService
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/eip')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/eip/EIP.java | 67 | ||||
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java | 77 |
2 files changed, 87 insertions, 57 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 5ce8b6e2..e37305f2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -52,17 +52,13 @@ import de.blinkt.openvpn.core.connection.Connection; import se.leap.bitmaskclient.OnBootReceiver; import se.leap.bitmaskclient.ProviderObservable; import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.utils.ConfigHelper; import se.leap.bitmaskclient.utils.PreferenceHelper; 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.connection.Connection.TransportType.OBFS4; import static de.blinkt.openvpn.core.connection.Connection.TransportType.OPENVPN; -import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT; import static se.leap.bitmaskclient.Constants.BROADCAST_GATEWAY_SETUP_OBSERVER_EVENT; -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_CHECK_CERT_VALIDITY; import static se.leap.bitmaskclient.Constants.EIP_ACTION_IS_RUNNING; @@ -73,7 +69,6 @@ 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_N_CLOSEST_GATEWAY; import static se.leap.bitmaskclient.Constants.EIP_RECEIVER; -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_PROFILE; import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; @@ -82,6 +77,7 @@ import static se.leap.bitmaskclient.R.string.vpn_certificate_is_invalid; import static se.leap.bitmaskclient.R.string.warning_client_parsing_error_gateways; import static se.leap.bitmaskclient.eip.EIP.EIPErrors.ERROR_INVALID_VPN_CERTIFICATE; import static se.leap.bitmaskclient.eip.EIP.EIPErrors.NO_MORE_GATEWAYS; +import static se.leap.bitmaskclient.eip.EipResultBroadcast.tellToReceiverOrBroadcast; import static se.leap.bitmaskclient.utils.ConfigHelper.ensureNotOnMainThread; import static se.leap.bitmaskclient.utils.PreferenceHelper.getUsePluggableTransports; @@ -117,7 +113,8 @@ public final class EIP extends JobIntentService implements Observer { public enum EIPErrors { UNKNOWN, ERROR_INVALID_VPN_CERTIFICATE, - NO_MORE_GATEWAYS + NO_MORE_GATEWAYS, + ERROR_VPN_PREPARE } /** @@ -212,24 +209,24 @@ public final class EIP extends JobIntentService implements Observer { if (!isVPNCertificateValid()) { setErrorResult(result, vpn_certificate_is_invalid, ERROR_INVALID_VPN_CERTIFICATE.toString()); - tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_CANCELED, result); + tellToReceiverOrBroadcast(this, EIP_ACTION_START, RESULT_CANCELED, result); return; } GatewaysManager gatewaysManager = gatewaysFromPreferences(); if (gatewaysManager.isEmpty()) { setErrorResult(result, warning_client_parsing_error_gateways, null); - tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_CANCELED, result); + tellToReceiverOrBroadcast(this, EIP_ACTION_START, RESULT_CANCELED, result); return; } Gateway gateway = gatewaysManager.select(nClosestGateway); if (launchActiveGateway(gateway, nClosestGateway)) { - tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_OK); + tellToReceiverOrBroadcast(this, EIP_ACTION_START, RESULT_OK); } else { setErrorResult(result, NO_MORE_GATEWAYS.toString(), getStringResourceForNoMoreGateways(), getString(R.string.app_name)); - tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_CANCELED, result); + tellToReceiverOrBroadcast(this, EIP_ACTION_START, RESULT_CANCELED, result); } } @@ -284,7 +281,7 @@ public final class EIP extends JobIntentService implements Observer { 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); + tellToReceiverOrBroadcast(this, EIP_ACTION_STOP, resultCode); } /** @@ -296,7 +293,7 @@ public final class EIP extends JobIntentService implements Observer { int resultCode = (eipStatus.isConnected()) ? RESULT_OK : RESULT_CANCELED; - tellToReceiverOrBroadcast(EIP_ACTION_IS_RUNNING, resultCode); + tellToReceiverOrBroadcast(this, EIP_ACTION_IS_RUNNING, resultCode); } /** @@ -316,7 +313,7 @@ public final class EIP extends JobIntentService implements Observer { int resultCode = isVPNCertificateValid() ? RESULT_OK : RESULT_CANCELED; - tellToReceiverOrBroadcast(EIP_ACTION_CHECK_CERT_VALIDITY, resultCode); + tellToReceiverOrBroadcast(this, EIP_ACTION_CHECK_CERT_VALIDITY, resultCode); } /** @@ -330,50 +327,6 @@ public final class EIP extends JobIntentService implements Observer { } /** - * send resultCode and resultData to receiver or - * broadcast the result if no receiver is defined - * - * @param action the action that has been performed - * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise - * @param resultData other data to broadcast or return to receiver - */ - private void tellToReceiverOrBroadcast(String action, int resultCode, Bundle resultData) { - resultData.putString(EIP_REQUEST, action); - if (mResultRef.get() != null) { - mResultRef.get().send(resultCode, resultData); - } else { - broadcastEvent(resultCode, resultData); - } - } - - /** - * send resultCode and resultData to receiver or - * broadcast the result if no receiver is defined - * - * @param action the action that has been performed - * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise - */ - private void tellToReceiverOrBroadcast(String action, int resultCode) { - tellToReceiverOrBroadcast(action, resultCode, new Bundle()); - } - - /** - * broadcast result - * - * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise - * @param resultData other data to broadcast or return to receiver - */ - private void broadcastEvent(int resultCode, Bundle resultData) { - Intent intentUpdate = new Intent(BROADCAST_EIP_EVENT); - intentUpdate.addCategory(CATEGORY_DEFAULT); - intentUpdate.putExtra(BROADCAST_RESULT_CODE, resultCode); - intentUpdate.putExtra(BROADCAST_RESULT_KEY, resultData); - Log.d(TAG, "sending broadcast"); - LocalBroadcastManager.getInstance(this).sendBroadcast(intentUpdate); - } - - - /** * helper function to add error to result bundle * * @param result - result of an action diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java new file mode 100644 index 00000000..e1efb375 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java @@ -0,0 +1,77 @@ +package se.leap.bitmaskclient.eip; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.ResultReceiver; +import android.support.v4.content.LocalBroadcastManager; +import android.util.Log; + +import static android.content.Intent.CATEGORY_DEFAULT; +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; +import static se.leap.bitmaskclient.Constants.EIP_REQUEST; + +public class EipResultBroadcast { + private static final String TAG = EipResultBroadcast.class.getSimpleName(); + + + /** + * send resultCode and resultData to receiver or + * broadcast the result if no receiver is defined + * + * @param action the action that has been performed + * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise + */ + public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode) { + tellToReceiverOrBroadcast(context, action, resultCode, null, new Bundle()); + } + + /** + * send resultCode and resultData to receiver or + * broadcast the result if no receiver is defined + * + * @param action the action that has been performed + * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise + * @param resultData other data to broadcast or return to receiver + */ + public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode, ResultReceiver receiver, Bundle resultData) { + resultData.putString(EIP_REQUEST, action); + if (receiver != null) { + receiver.send(resultCode, resultData); + } else { + broadcastEvent(context, resultCode, resultData); + } + } + + /** + * send resultCode and resultData to receiver or + * broadcast the result if no receiver is defined + * + * @param action the action that has been performed + * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise + * @param resultData other data to broadcast or return to receiver + */ + public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode, Bundle resultData) { + resultData.putString(EIP_REQUEST, action); + broadcastEvent(context, resultCode, resultData); + } + + + + /** + * broadcast result + * + * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise + * @param resultData other data to broadcast or return to receiver + */ + public static void broadcastEvent(Context context, int resultCode, Bundle resultData) { + Intent intentUpdate = new Intent(BROADCAST_EIP_EVENT); + intentUpdate.addCategory(CATEGORY_DEFAULT); + intentUpdate.putExtra(BROADCAST_RESULT_CODE, resultCode); + intentUpdate.putExtra(BROADCAST_RESULT_KEY, resultData); + Log.d(TAG, "sending broadcast"); + LocalBroadcastManager.getInstance(context).sendBroadcast(intentUpdate); + } +} |