diff options
author | cyBerta <cyberta@riseup.net> | 2024-10-26 01:54:45 +0200 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2024-12-11 00:09:34 +0000 |
commit | c0da385377c22f445bef0b70aa8da52e1fda1037 (patch) | |
tree | 1ccb9734d5bb54cdf5477c3b8fc6e18416a15f15 | |
parent | 980b0038ac33ea650450fe70025f2a72402307c3 (diff) |
Improve setting valid URL as main URL string. Fixes parceling exception of Provider objects
3 files changed, 33 insertions, 23 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/models/Provider.java b/app/src/main/java/se/leap/bitmaskclient/base/models/Provider.java index 5db87f50..a2bf0d8e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/models/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/models/Provider.java @@ -28,6 +28,8 @@ import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_ALLOWED_REGIS import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_ALLOW_ANONYMOUS; import static se.leap.bitmaskclient.base.models.Constants.TRANSPORT; import static se.leap.bitmaskclient.base.models.Constants.TYPE; +import static se.leap.bitmaskclient.base.utils.ConfigHelper.isDomainName; +import static se.leap.bitmaskclient.base.utils.ConfigHelper.isNetworkUrl; import static se.leap.bitmaskclient.base.utils.PrivateKeyHelper.parsePrivateKeyFromString; import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORS; @@ -132,15 +134,11 @@ public final class Provider implements Parcelable { } public Provider(String mainUrl) { - this(mainUrl, null); + this(mainUrl, null); } public Provider(String mainUrl, String geoipUrl) { - try { - this.mainUrl = new URL(mainUrl).toString(); - } catch (MalformedURLException e) { - this.mainUrl = ""; - } + setMainUrl(mainUrl); setGeoipUrl(geoipUrl); } @@ -151,17 +149,12 @@ public final class Provider implements Parcelable { } public Provider(String mainUrl, String geoipUrl, String motdUrl, String providerIp, String providerApiIp) { - try { - this.mainUrl = new URL(mainUrl).toString(); - if (providerIp != null) { - this.providerIp = providerIp; - } - if (providerApiIp != null) { - this.providerApiIp = providerApiIp; - } - } catch (MalformedURLException e) { - e.printStackTrace(); - return; + setMainUrl(mainUrl); + if (providerIp != null) { + this.providerIp = providerIp; + } + if (providerApiIp != null) { + this.providerApiIp = providerApiIp; } setGeoipUrl(geoipUrl); setMotdUrl(motdUrl); @@ -338,10 +331,16 @@ public final class Provider implements Parcelable { public void setMainUrl(String url) { try { - mainUrl = new URL(url).toString(); + if (isNetworkUrl(url)) { + this.mainUrl = new URL(url).toString(); + } else if (isDomainName(url)){ + this.mainUrl = new URL("https://" + url).toString(); + } else { + this.mainUrl = ""; + } } catch (MalformedURLException e) { e.printStackTrace(); - mainUrl = ""; + this.mainUrl = ""; } } @@ -525,7 +524,7 @@ public final class Provider implements Parcelable { private Provider(Parcel in) { try { domain = in.readString(); - mainUrl = new URL(in.readString()).toString(); + setMainUrl(in.readString()); String tmpString = in.readString(); if (!tmpString.isEmpty()) { providerIp = tmpString; diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java index 428d0a4f..0e192882 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java @@ -21,6 +21,8 @@ import android.content.Context; import android.content.res.Resources; import android.os.Build; import android.os.Looper; +import android.util.Patterns; +import android.webkit.URLUtil; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -221,6 +223,13 @@ public class ConfigHelper { return matcher.matches(); } + public static boolean isNetworkUrl(String url) { + return url != null && URLUtil.isNetworkUrl(url) && Patterns.WEB_URL.matcher(url).matches(); + } + + public static boolean isDomainName(String url) { + return url != null && Patterns.DOMAIN_NAME.matcher(url).matches(); + } public static String getDomainFromMainURL(@NonNull String mainUrl) throws NullPointerException { return PublicSuffixDatabase.Companion.get().getEffectiveTldPlusOne(mainUrl).replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); } diff --git a/app/src/main/java/se/leap/bitmaskclient/providersetup/fragments/viewmodel/ProviderSelectionViewModel.java b/app/src/main/java/se/leap/bitmaskclient/providersetup/fragments/viewmodel/ProviderSelectionViewModel.java index 00117336..5f9a0f3d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/fragments/viewmodel/ProviderSelectionViewModel.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/fragments/viewmodel/ProviderSelectionViewModel.java @@ -1,11 +1,13 @@ package se.leap.bitmaskclient.providersetup.fragments.viewmodel; +import static se.leap.bitmaskclient.base.utils.ConfigHelper.isDomainName; +import static se.leap.bitmaskclient.base.utils.ConfigHelper.isNetworkUrl; + import android.content.Context; import android.content.res.AssetManager; import android.text.InputType; import android.util.Patterns; import android.view.View; -import android.webkit.URLUtil; import androidx.lifecycle.ViewModel; @@ -51,7 +53,7 @@ public class ProviderSelectionViewModel extends ViewModel { public boolean isValidConfig() { if (selected == ADD_PROVIDER) { - return customUrl != null && (Patterns.DOMAIN_NAME.matcher(customUrl).matches() || (URLUtil.isNetworkUrl(customUrl) && Patterns.WEB_URL.matcher(customUrl).matches())); + return isNetworkUrl(customUrl) || isDomainName(customUrl); } if (selected == INVITE_CODE_PROVIDER) { try { @@ -121,7 +123,7 @@ public class ProviderSelectionViewModel extends ViewModel { } public String getCustomUrl() { - if (customUrl != null && Patterns.DOMAIN_NAME.matcher(customUrl).matches()) { + if (isDomainName(customUrl)) { return "https://" + customUrl; } return customUrl; |