From afea4c95b3ec666ac0eea08e8fcc021d3c4056cb Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 6 Oct 2021 17:38:53 +0200 Subject: Detect broken server URLs on import instead crashing --- .../blinkt/openvpn/fragments/ImportRemoteConfig.kt | 32 +++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/ImportRemoteConfig.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/ImportRemoteConfig.kt index fe23ea98..617d6fd1 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/ImportRemoteConfig.kt +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/ImportRemoteConfig.kt @@ -212,6 +212,11 @@ class ImportRemoteConfig : DialogFragment() { } + /** + * Returns a new [HttpUrl] representing the URL that is is going to be imported. + * + * @throws IllegalArgumentException If this is not a well-formed HTTP or HTTPS URL. + */ private fun getAsUrl(url: String, autologin: Boolean): HttpUrl{ var asurl = url if (!asurl.startsWith("http")) @@ -281,8 +286,15 @@ class ImportRemoteConfig : DialogFragment() { d.getButton(AlertDialog.BUTTON_POSITIVE)?.setOnClickListener() { _ -> - viewLifecycleOwner.lifecycleScope.launch { - doAsImport(asUsername.text.toString(), asPassword.text.toString()) + try { + // Check if the URL that being built can be actually be parsed + getImportUrl(); + + viewLifecycleOwner.lifecycleScope.launch { + doAsImport(asUsername.text.toString(), asPassword.text.toString()) + } + } catch (e: IllegalArgumentException) { + Toast.makeText(context, "URL is invalid: ${e.localizedMessage}", Toast.LENGTH_LONG).show() } } } @@ -290,6 +302,15 @@ class ImportRemoteConfig : DialogFragment() { val crvMessage = Pattern.compile(".*CRV1:R,E:(.*):(.*):(.*).*", Pattern.DOTALL) + + private fun getImportUrl(): HttpUrl { + if (importChoiceAS.isChecked) + return getAsUrl(asServername.text.toString(), asUseAutologin.isChecked) + else + return asServername.text.toString().toHttpUrl() + } + + suspend internal fun doAsImport(user: String, password: String) { var pleaseWait:AlertDialog? withContext(Dispatchers.IO) @@ -305,11 +326,8 @@ class ImportRemoteConfig : DialogFragment() { Toast.makeText(context, "Downloading profile", Toast.LENGTH_LONG).show() } - val asProfileUri:HttpUrl - if (importChoiceAS.isChecked) - asProfileUri = getAsUrl(asServername.text.toString(), asUseAutologin.isChecked) - else - asProfileUri = asServername.text.toString().toHttpUrl() + val asProfileUri:HttpUrl = getImportUrl() + var e: Exception? = null try { -- cgit v1.2.3