diff options
author | cyBerta <cyberta@riseup.net> | 2024-11-07 19:13:53 +0100 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2024-12-11 00:09:34 +0000 |
commit | bbf98bb94648bb9c7a8aebe866a7bc61a79c14ca (patch) | |
tree | 0147212802abbc01c64b4c36c26ff1464a3719a2 /app/src/main/java/se/leap/bitmaskclient/base | |
parent | ab4c3397c501e059849d16f05fa211d5ebd3c4db (diff) |
update bitmask-core API, store v5 provider details
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/base')
3 files changed, 122 insertions, 33 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 b49061c9..5a5d1d6e 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 @@ -126,6 +126,10 @@ public interface Constants { String PROVIDER_MOTD_HASHES = "Constants.PROVIDER_MOTD_HASHES"; String PROVIDER_MOTD_LAST_SEEN = "Constants.PROVIDER_MOTD_LAST_SEEN"; String PROVIDER_MOTD_LAST_UPDATED = "Constants.PROVIDER_MOTD_LAST_UPDATED"; + String PROVIDER_MODELS_PROVIDER = "Constants.PROVIDER_MODELS_PROVIDER"; + String PROVIDER_MODELS_EIPSERVICE = "Constants.PROVIDER_MDOELS_EIPSERVICE"; + String PROVIDER_MODELS_GATEWAYS = "Constants.PROVIDER_MODELS_GATEWAYS"; + String PROVIDER_MODELS_BRIDGES = "Constants.PROVIDER_MODELS_BRIDGES"; //////////////////////////////////////////////// // PRESHIPPED PROVIDER CONFIG 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 51183e5e..cdec9e7a 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 @@ -39,12 +39,14 @@ import android.os.Parcelable; import androidx.annotation.NonNull; import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; import java.security.PrivateKey; import java.util.ArrayList; @@ -145,7 +147,7 @@ public final class Provider implements Parcelable { setGeoipUrl(geoipUrl); } - public static Provider createCustomProvider(String mainUrl, String domain) { + public static Provider createCustomProvider(String mainUrl, String domain, Introducer introducer) { Provider p = new Provider(mainUrl); p.domain = domain; return p; @@ -189,29 +191,93 @@ public final class Provider implements Parcelable { } }; - public void setBridges(ModelsBridge[] bridges) { - this.modelsBridges = bridges; + public void setBridges(String bridgesJson) { + if (bridgesJson == null) { + this.modelsBridges = null; + return; + } + try { + this.modelsBridges = JSON.createGson().create().fromJson(bridgesJson, ModelsBridge[].class); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + } } public ModelsBridge[] getBridges() { return this.modelsBridges; } - public void setGateways(ModelsGateway[] gateways) { - this.modelsGateways = gateways; + + public String getBridgesJson() { + return getJsonString(modelsBridges); + } + + public void setGateways(String gatewaysJson) { + if (gatewaysJson == null) { + this.modelsGateways = null; + return; + } + try { + this.modelsGateways = JSON.createGson().create().fromJson(gatewaysJson, ModelsGateway[].class); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + } } public ModelsGateway[] getGateways() { return modelsGateways; } - public void setService(ModelsEIPService service) { - this.modelsEIPService = service; + public String getGatewaysJson() { + return getJsonString(modelsGateways); } + public void setService(String serviceJson) { + if (serviceJson == null) { + this.modelsEIPService = null; + return; + } + try { + this.modelsEIPService = JSON.createGson().create().fromJson(serviceJson, ModelsEIPService.class); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + } + } public ModelsEIPService getService() { return this.modelsEIPService; } + public String getServiceJson() { + return getJsonString(modelsEIPService); + } + + public void setModelsProvider(String json) { + if (json == null) { + this.modelsProvider = null; + return; + } + try { + this.modelsProvider = JSON.createGson().create().fromJson(json, ModelsProvider.class); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + } + } + + public String getModelsProviderJson() { + return getJsonString(modelsProvider); + } + + private String getJsonString(Object model) { + if (model == null) { + return null; + } + try { + return JSON.createGson().create().toJson(model); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + return null; + } + } + public boolean isConfigured() { if (apiVersion < 5) { return !mainUrl.isEmpty() && @@ -583,23 +649,10 @@ public final class Provider implements Parcelable { this.shouldUpdateVpnCertificate = in.readInt() == 0; this.introducer = in.readParcelable(Introducer.class.getClassLoader()); if (this.apiVersion == 5) { - Gson gson = JSON.createGson().create(); - tmpString = in.readString(); - if (!tmpString.isEmpty()) { - this.setModelsProvider(gson.fromJson(tmpString, ModelsProvider.class)); - } - tmpString = in.readString(); - if (!tmpString.isEmpty()) { - this.setService(gson.fromJson(tmpString, ModelsEIPService.class)); - } - tmpString = in.readString(); - if (!tmpString.isEmpty()) { - this.setBridges(gson.fromJson(tmpString, ModelsBridge[].class)); - } - tmpString = in.readString(); - if (!tmpString.isEmpty()) { - this.setGateways(gson.fromJson(tmpString, ModelsGateway[].class)); - } + this.setModelsProvider(in.readString()); + this.setService(in.readString()); + this.setBridges(in.readString()); + this.setGateways(in.readString()); } } catch (MalformedURLException | JSONException e) { e.printStackTrace(); @@ -904,6 +957,10 @@ public final class Provider implements Parcelable { return introducer; } + public void setIntroducer(String introducerUrl) throws URISyntaxException { + this.introducer = Introducer.fromUrl(introducerUrl); + } + /** * resets everything except the main url, the providerIp and the geoip * service url (currently preseeded) @@ -926,7 +983,4 @@ public final class Provider implements Parcelable { lastEipServiceUpdate = 0L; } - public void setModelsProvider(ModelsProvider p) { - this.modelsProvider = p; - } } diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java index bb342f0f..5cc3ff70 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java @@ -31,6 +31,10 @@ import static se.leap.bitmaskclient.base.models.Constants.PREFERRED_CITY; import static se.leap.bitmaskclient.base.models.Constants.PREFER_UDP; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_CONFIGURED; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_MODELS_BRIDGES; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_MODELS_EIPSERVICE; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_MODELS_GATEWAYS; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_MODELS_PROVIDER; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_MOTD; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_MOTD_HASHES; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_MOTD_LAST_SEEN; @@ -60,6 +64,8 @@ import androidx.annotation.WorkerThread; import androidx.security.crypto.EncryptedSharedPreferences; import androidx.security.crypto.MasterKey; +import com.google.gson.Gson; + import org.json.JSONException; import org.json.JSONObject; @@ -74,7 +80,10 @@ import java.util.Set; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.NativeUtils; +import io.swagger.client.JSON; +import mobile.BitmaskMobile; import se.leap.bitmaskclient.BuildConfig; +import se.leap.bitmaskclient.base.models.Introducer; import se.leap.bitmaskclient.base.models.Provider; import se.leap.bitmaskclient.tor.TorStatusObservable; @@ -156,7 +165,14 @@ public class PreferenceHelper { provider.setLastMotdSeen(preferences.getLong(PROVIDER_MOTD_LAST_SEEN, 0L)); provider.setLastMotdUpdate(preferences.getLong(PROVIDER_MOTD_LAST_UPDATED, 0L)); provider.setMotdLastSeenHashes(preferences.getStringSet(PROVIDER_MOTD_HASHES, new HashSet<>())); - } catch (MalformedURLException | JSONException e) { + provider.setModelsProvider(preferences.getString(PROVIDER_MODELS_PROVIDER, null)); + provider.setService(preferences.getString(PROVIDER_MODELS_EIPSERVICE, null)); + provider.setBridges(preferences.getString(PROVIDER_MODELS_BRIDGES, null)); + provider.setGateways(preferences.getString(PROVIDER_MODELS_GATEWAYS, null)); + BitmaskMobile bm = new BitmaskMobile(new SharedPreferenceStore()); + provider.setIntroducer(bm.getIntroducerURLByDomain(provider.getDomain())); + + } catch (Exception e) { e.printStackTrace(); } } @@ -201,12 +217,23 @@ public class PreferenceHelper { public static HashMap<String, Provider> getCustomProviders() { Set<String> providerDomains = getCustomProviderDomains(); HashMap<String, Provider> customProviders = new HashMap<>(); - for (String domain : providerDomains) { - String mainURL = preferences.getString(Provider.MAIN_URL + "." + domain, null); - if (mainURL != null) { - customProviders.put(mainURL, Provider.createCustomProvider(mainURL, domain)); + if (providerDomains.size() > 0) { + BitmaskMobile bm = new BitmaskMobile(new PreferenceHelper.SharedPreferenceStore()); + for (String domain : providerDomains) { + String mainURL = preferences.getString(Provider.MAIN_URL + "." + domain, null); + if (mainURL != null) { + Introducer introducer = null; + try { + introducer = Introducer.fromUrl(bm.getIntroducerURLByDomain(domain)); + } catch (Exception e) { + e.printStackTrace(); + } + customProviders.put(mainURL, Provider.createCustomProvider(mainURL, domain, introducer)); + } } + } + return customProviders; } @@ -254,7 +281,11 @@ public class PreferenceHelper { putString(PROVIDER_MOTD, provider.getMotdJsonString()). putStringSet(PROVIDER_MOTD_HASHES, provider.getMotdLastSeenHashes()). putLong(PROVIDER_MOTD_LAST_SEEN, provider.getLastMotdSeen()). - putLong(PROVIDER_MOTD_LAST_UPDATED, provider.getLastMotdUpdate()); + putLong(PROVIDER_MOTD_LAST_UPDATED, provider.getLastMotdUpdate()). + putString(PROVIDER_MODELS_GATEWAYS, provider.getGatewaysJson()). + putString(PROVIDER_MODELS_BRIDGES, provider.getBridgesJson()). + putString(PROVIDER_MODELS_EIPSERVICE, provider.getServiceJson()). + putString(PROVIDER_MODELS_PROVIDER, provider.getModelsProviderJson()); if (async) { editor.apply(); } else { |