diff options
author | Arne Schwabe <arne@rfc2549.org> | 2021-10-06 17:38:53 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2021-10-06 17:38:53 +0200 |
commit | afea4c95b3ec666ac0eea08e8fcc021d3c4056cb (patch) | |
tree | 71a097872aaa6dcae10207a2c5f745ba780034d3 | |
parent | 38175d6d0e8c6e68f4dca57ef78d560a190b29e4 (diff) |
Detect broken server URLs on import instead crashing
-rw-r--r-- | main/src/ui/java/de/blinkt/openvpn/fragments/ImportRemoteConfig.kt | 32 |
1 files 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(".*<Message>CRV1:R,E:(.*):(.*):(.*)</Message>.*", 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 { |