diff options
author | cyberta <cyberta@riseup.net> | 2021-02-26 08:53:28 +0000 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2021-02-26 08:53:28 +0000 |
commit | 8bb73089566e78a195f21f4746c9db416abf95a4 (patch) | |
tree | bba73954ec2754f85060622f60d69a19f00980c3 /app/src/main/java | |
parent | c62389eaae3768e236875ee5fd20bde29e381c94 (diff) | |
parent | cb1510c9f3ea27d5cffc0c0e370f8270d454e39b (diff) |
Merge branch 'fix_custom_preshipping' into 'master'
ensure the private api and the preshipped certificate will be used to setup...
Closes #9029
See merge request leap/bitmask_android!130
Diffstat (limited to 'app/src/main/java')
4 files changed, 69 insertions, 29 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java b/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java index d649aaf5..e60019fc 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java @@ -104,6 +104,13 @@ public interface Constants { String PROVIDER_PROFILE_UUID = "Constants.PROVIDER_PROFILE_UUID"; String PROVIDER_PROFILE = "Constants.PROVIDER_PROFILE"; + //////////////////////////////////////////////// + // PRESHIPPED PROVIDER CONFIG + //////////////////////////////////////////////// + String URLS = "urls"; + String EXT_JSON = ".json"; + String EXT_PEM = ".pem"; + ////////////////////////////////////////////// // CREDENTIAL CONSTANTS ///////////////////////////////////////////// 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 77189dff..8a526499 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 @@ -1,7 +1,11 @@ package se.leap.bitmaskclient.base.utils; +import org.json.JSONException; +import org.json.JSONObject; + import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; /** @@ -18,4 +22,26 @@ public class InputStreamHelper { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); 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; + 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(); + } + return json; + } } 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 d33a175f..654fb8e2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderManager.java @@ -22,12 +22,16 @@ 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.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; @@ -45,10 +49,6 @@ public class ProviderManager implements AdapteeCollection<Provider> { private static ProviderManager instance; - final private static String URLS = "urls"; - final private static String EXT_JSON = ".json"; - final private static String EXT_PEM = ".pem"; - public static ProviderManager getInstance(AssetManager assetsManager, File externalFilesDir) { if (instance == null) instance = new ProviderManager(assetsManager, externalFilesDir); @@ -138,28 +138,6 @@ public class ProviderManager implements AdapteeCollection<Provider> { return providers; } - private String extractKeyFromInputStream(InputStream inputStream, String key) { - String value = ""; - - JSONObject fileContents = inputStreamToJson(inputStream); - if (fileContents != null) - value = fileContents.optString(key); - return value; - } - - private JSONObject inputStreamToJson(InputStream inputStream) { - JSONObject json = null; - 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(); - } - return json; - } - public List<Provider> providers() { List<Provider> allProviders = new ArrayList<>(); allProviders.addAll(defaultProviders); diff --git a/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/CustomProviderSetupActivity.java b/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/CustomProviderSetupActivity.java index 161c53d3..b90d14f8 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/CustomProviderSetupActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/CustomProviderSetupActivity.java @@ -17,19 +17,33 @@ package se.leap.bitmaskclient.providersetup.activities; import android.content.Intent; +import android.content.res.AssetManager; import android.os.Bundle; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; + import se.leap.bitmaskclient.BuildConfig; +import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.base.models.Provider; import se.leap.bitmaskclient.providersetup.ProviderAPICommand; -import se.leap.bitmaskclient.R; +import static se.leap.bitmaskclient.BuildConfig.customProviderApiIp; +import static se.leap.bitmaskclient.BuildConfig.customProviderIp; +import static se.leap.bitmaskclient.BuildConfig.customProviderUrl; +import static se.leap.bitmaskclient.BuildConfig.geoipUrl; +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.REQUEST_CODE_CONFIGURE_LEAP; +import static se.leap.bitmaskclient.base.utils.ConfigHelper.preferAnonymousUsage; +import static se.leap.bitmaskclient.base.utils.InputStreamHelper.loadInputStreamAsString; import static se.leap.bitmaskclient.providersetup.ProviderAPI.SET_UP_PROVIDER; import static se.leap.bitmaskclient.providersetup.ProviderSetupInterface.ProviderConfigState.SETTING_UP_PROVIDER; -import static se.leap.bitmaskclient.base.utils.ConfigHelper.preferAnonymousUsage; /** * Created by cyberta on 17.08.18. @@ -42,7 +56,7 @@ public class CustomProviderSetupActivity extends ProviderSetupBaseActivity { super.onCreate(savedInstanceState); setUpInitialUI(); restoreState(savedInstanceState); - setProvider(new Provider(BuildConfig.customProviderUrl, BuildConfig.geoipUrl, BuildConfig.customProviderIp, BuildConfig.customProviderApiIp)); + setDefaultProvider(); } @Override @@ -54,6 +68,21 @@ public class CustomProviderSetupActivity extends ProviderSetupBaseActivity { } } + private void setDefaultProvider() { + try { + AssetManager assetsManager = getAssets(); + Provider customProvider = new Provider(customProviderUrl, geoipUrl, customProviderIp, customProviderApiIp); + String certificate = loadInputStreamAsString(assetsManager.open(customProvider.getDomain() + EXT_PEM)); + String providerDefinition = loadInputStreamAsString(assetsManager.open(customProvider.getDomain() + EXT_JSON)); + customProvider.setCaCert(certificate); + customProvider.define(new JSONObject(providerDefinition)); + setProvider(customProvider); + } catch (IOException | JSONException e) { + e.printStackTrace(); + setProvider(new Provider(customProviderUrl, geoipUrl, customProviderIp, customProviderApiIp)); + } + } + private void setUpInitialUI() { setContentView(R.layout.a_custom_provider_setup); setProviderHeaderText(R.string.setup_provider); |