From 601a77ad059b3da754c3baaf10b01dbc19b927be Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 15 Apr 2023 13:06:53 +0200 Subject: fix missing domain name of manually added providers in provider selection --- .../leap/bitmaskclient/base/models/Provider.java | 7 ++- .../base/utils/InputStreamHelper.java | 12 +---- .../providersetup/ProviderManager.java | 58 ++++++++++++---------- .../activities/ProviderListBaseActivity.java | 2 +- 4 files changed, 39 insertions(+), 40 deletions(-) (limited to 'app/src/main/java') 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 57653263..08e13cf6 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 @@ -126,8 +126,10 @@ public final class Provider implements Parcelable { setGeoipUrl(geoipUrl); } - public Provider(String mainUrl, String providerIp, String providerApiIp) { - this(mainUrl, null, null, providerIp, providerApiIp); + public static Provider createCustomProvider(String mainUrl, String domain) { + Provider p = new Provider(mainUrl); + p.domain = domain; + return p; } public Provider(String mainUrl, String geoipUrl, String motdUrl, String providerIp, String providerApiIp) { @@ -520,6 +522,7 @@ public final class Provider implements Parcelable { JSONObject json = new JSONObject(); try { json.put(Provider.MAIN_URL, mainUrl); + json.put(Provider.DOMAIN, domain); } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java index 8a526499..8e6273a7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java @@ -23,22 +23,12 @@ public class InputStreamHelper { return s.hasNext() ? s.next() : ""; } - public static String extractKeyFromInputStream(InputStream inputStream, String key) { - String value = ""; - - JSONObject fileContents = inputStreamToJson(inputStream); - if (fileContents != null) - value = fileContents.optString(key); - return value; - } - public static JSONObject inputStreamToJson(InputStream inputStream) { - JSONObject json = null; + JSONObject json = new JSONObject(); try { byte[] bytes = new byte[inputStream.available()]; if (inputStream.read(bytes) > 0) json = new JSONObject(new String(bytes)); - inputStream.reset(); } catch (IOException | JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderManager.java index 1ae2a033..775e174a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderManager.java @@ -1,11 +1,28 @@ package se.leap.bitmaskclient.providersetup; +import static se.leap.bitmaskclient.base.models.Constants.EXT_JSON; +import static se.leap.bitmaskclient.base.models.Constants.EXT_PEM; +import static se.leap.bitmaskclient.base.models.Constants.URLS; +import static se.leap.bitmaskclient.base.models.Provider.DOMAIN; +import static se.leap.bitmaskclient.base.models.Provider.GEOIP_URL; +import static se.leap.bitmaskclient.base.models.Provider.MAIN_URL; +import static se.leap.bitmaskclient.base.models.Provider.MOTD_URL; +import static se.leap.bitmaskclient.base.models.Provider.PROVIDER_API_IP; +import static se.leap.bitmaskclient.base.models.Provider.PROVIDER_IP; +import static se.leap.bitmaskclient.base.utils.FileHelper.createFile; +import static se.leap.bitmaskclient.base.utils.FileHelper.persistFile; +import static se.leap.bitmaskclient.base.utils.InputStreamHelper.getInputStreamFrom; +import static se.leap.bitmaskclient.base.utils.InputStreamHelper.inputStreamToJson; +import static se.leap.bitmaskclient.base.utils.InputStreamHelper.loadInputStreamAsString; + import android.content.res.AssetManager; import androidx.annotation.VisibleForTesting; import com.pedrogomez.renderers.AdapteeCollection; +import org.json.JSONObject; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -19,20 +36,6 @@ import java.util.Set; import se.leap.bitmaskclient.base.models.Provider; -import static se.leap.bitmaskclient.base.models.Constants.EXT_JSON; -import static se.leap.bitmaskclient.base.models.Constants.EXT_PEM; -import static se.leap.bitmaskclient.base.models.Constants.URLS; -import static se.leap.bitmaskclient.base.models.Provider.GEOIP_URL; -import static se.leap.bitmaskclient.base.models.Provider.MAIN_URL; -import static se.leap.bitmaskclient.base.models.Provider.MOTD_URL; -import static se.leap.bitmaskclient.base.models.Provider.PROVIDER_API_IP; -import static se.leap.bitmaskclient.base.models.Provider.PROVIDER_IP; -import static se.leap.bitmaskclient.base.utils.FileHelper.createFile; -import static se.leap.bitmaskclient.base.utils.FileHelper.persistFile; -import static se.leap.bitmaskclient.base.utils.InputStreamHelper.extractKeyFromInputStream; -import static se.leap.bitmaskclient.base.utils.InputStreamHelper.getInputStreamFrom; -import static se.leap.bitmaskclient.base.utils.InputStreamHelper.loadInputStreamAsString; - /** * Created by parmegv on 4/12/14. */ @@ -96,11 +99,14 @@ public class ProviderManager implements AdapteeCollection { try { String provider = file.substring(0, file.length() - ".url".length()); InputStream providerFile = assetsManager.open(directory + "/" + file); - mainUrl = extractKeyFromInputStream(providerFile, MAIN_URL); - providerIp = extractKeyFromInputStream(providerFile, PROVIDER_IP); - providerApiIp = extractKeyFromInputStream(providerFile, PROVIDER_API_IP); - geoipUrl = extractKeyFromInputStream(providerFile, GEOIP_URL); - motdUrl = extractKeyFromInputStream(providerFile, MOTD_URL); + JSONObject providerConfig = inputStreamToJson(providerFile); + if (providerConfig != null) { + mainUrl = providerConfig.optString(MAIN_URL); + providerIp = providerConfig.optString(PROVIDER_IP); + providerApiIp = providerConfig.optString(PROVIDER_API_IP); + geoipUrl = providerConfig.optString(GEOIP_URL); + motdUrl = providerConfig.optString(MOTD_URL); + } certificate = loadInputStreamAsString(assetsManager.open(provider + EXT_PEM)); providerDefinition = loadInputStreamAsString(assetsManager.open(provider + EXT_JSON)); } catch (IOException e) { @@ -116,20 +122,20 @@ public class ProviderManager implements AdapteeCollection { private void addCustomProviders(File externalFilesDir) { this.externalFilesDir = externalFilesDir; customProviders = externalFilesDir != null && externalFilesDir.isDirectory() ? - providersFromFiles(externalFilesDir.list()) : + customProvidersFromFiles(externalFilesDir.list()) : new HashSet<>(); customProviderURLs = getProviderUrlSetFromProviderSet(customProviders); } - private Set providersFromFiles(String[] files) { + private Set customProvidersFromFiles(String[] files) { Set providers = new HashSet<>(); try { for (String file : files) { InputStream inputStream = getInputStreamFrom(externalFilesDir.getAbsolutePath() + "/" + file); - String mainUrl = extractKeyFromInputStream(inputStream, MAIN_URL); - String providerIp = extractKeyFromInputStream(inputStream, PROVIDER_IP); - String providerApiIp = extractKeyFromInputStream(inputStream, PROVIDER_API_IP); - providers.add(new Provider(mainUrl, providerIp, providerApiIp)); + JSONObject providerConfig = inputStreamToJson(inputStream); + String mainUrl = providerConfig.optString(MAIN_URL); + String domain = providerConfig.optString(DOMAIN); + providers.add(Provider.createCustomProvider(mainUrl, domain)); } } catch (FileNotFoundException | NullPointerException e) { e.printStackTrace(); @@ -238,7 +244,7 @@ public class ProviderManager implements AdapteeCollection { */ private void deleteLegacyCustomProviders() throws IOException, SecurityException { Set persistedCustomProviders = externalFilesDir != null && externalFilesDir.isDirectory() ? - providersFromFiles(externalFilesDir.list()) : new HashSet(); + customProvidersFromFiles(externalFilesDir.list()) : new HashSet(); persistedCustomProviders.removeAll(customProviders); for (Provider providerToDelete : persistedCustomProviders) { File providerFile = createFile(externalFilesDir, providerToDelete.getName() + EXT_JSON); diff --git a/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/ProviderListBaseActivity.java b/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/ProviderListBaseActivity.java index 90ebfb4d..eb9898b8 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/ProviderListBaseActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/ProviderListBaseActivity.java @@ -114,7 +114,7 @@ public abstract class ProviderListBaseActivity extends ProviderSetupBaseActivity } public void showAndSelectProvider(String newURL) { - provider = new Provider(newURL, null, null); + provider = new Provider(newURL); autoSelectProvider(); } -- cgit v1.2.3