From 5955d67700ef6d674c50202247ab21d0de83048d Mon Sep 17 00:00:00 2001 From: cyBerta Date: Fri, 11 Nov 2022 01:43:18 +0100 Subject: add motd properties to Provider model --- .../leap/bitmaskclient/base/models/Provider.java | 156 ++++++++++++++++++--- 1 file changed, 135 insertions(+), 21 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 13463167..b0f3d09f 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 @@ -16,8 +16,23 @@ */ package se.leap.bitmaskclient.base.models; +import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4; +import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4_KCP; +import static se.leap.bitmaskclient.base.models.Constants.CAPABILITIES; +import static se.leap.bitmaskclient.base.models.Constants.GATEWAYS; +import static se.leap.bitmaskclient.base.models.Constants.LOCATIONS; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_ALLOWED_REGISTERED; +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.ObfsVpnHelper.useObfsVpn; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORS; + import android.os.Parcel; import android.os.Parcelable; +import android.util.Log; + +import androidx.annotation.NonNull; import com.google.gson.Gson; @@ -27,22 +42,14 @@ import org.json.JSONObject; import java.net.MalformedURLException; import java.net.URL; +import java.util.ArrayList; +import java.util.HashSet; import java.util.Locale; +import java.util.Set; -import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4; -import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4_KCP; -import static se.leap.bitmaskclient.base.models.Constants.CAPABILITIES; -import static se.leap.bitmaskclient.base.models.Constants.GATEWAYS; -import static se.leap.bitmaskclient.base.models.Constants.LOCATIONS; -import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_ALLOWED_REGISTERED; -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.ObfsVpnHelper.useObfsVpn; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORS; - -import de.blinkt.openvpn.core.connection.Connection; import de.blinkt.openvpn.core.connection.Connection.TransportType; +import motd.IStringCollection; +import motd.Motd; /** * @author Sean Leonard @@ -50,14 +57,17 @@ import de.blinkt.openvpn.core.connection.Connection.TransportType; */ public final class Provider implements Parcelable { - private static long EIP_SERVICE_TIMEOUT = 1000 * 60 * 60 * 24 * 3; - private static long GEOIP_SERVICE_TIMEOUT = 1000 * 60 * 60; + private static final long EIP_SERVICE_TIMEOUT = 1000 * 60 * 60 * 24 * 3; + private static final long GEOIP_SERVICE_TIMEOUT = 1000 * 60 * 60; + private static final long MOTD_TIMEOUT = 1000 * 60 * 60 * 24; private JSONObject definition = new JSONObject(); // Represents our Provider's provider.json private JSONObject eipServiceJson = new JSONObject(); private JSONObject geoIpJson = new JSONObject(); + private JSONObject motdJson = new JSONObject(); private DefaultedURL mainUrl = new DefaultedURL(); private DefaultedURL apiUrl = new DefaultedURL(); private DefaultedURL geoipUrl = new DefaultedURL(); + private DefaultedURL motdUrl = new DefaultedURL(); private String domain = ""; private String providerIp = ""; // ip of the provider main url private String providerApiIp = ""; // ip of the provider api url @@ -69,6 +79,9 @@ public final class Provider implements Parcelable { private String vpnCertificate = ""; private long lastEipServiceUpdate = 0L; private long lastGeoIpUpdate = 0L; + private long lastMotdUpdate = 0L; + private long lastMotdSeen = 0L; + private Set lastMotdSeenHashes = new HashSet<>(); private boolean shouldUpdateVpnCertificate; private boolean allowAnonymous; @@ -90,7 +103,8 @@ public final class Provider implements Parcelable { MAIN_URL = "main_url", PROVIDER_IP = "provider_ip", PROVIDER_API_IP = "provider_api_ip", - GEOIP_URL = "geoip_url"; + GEOIP_URL = "geoip_url", + MOTD_URL = "motd_url"; private static final String API_TERM_NAME = "name"; @@ -110,10 +124,10 @@ public final class Provider implements Parcelable { } public Provider(String mainUrl, String providerIp, String providerApiIp) { - this(mainUrl, null, providerIp, providerApiIp); + this(mainUrl, null, null, providerIp, providerApiIp); } - public Provider(String mainUrl, String geoipUrl, String providerIp, String providerApiIp) { + public Provider(String mainUrl, String geoipUrl, String motdUrl, String providerIp, String providerApiIp) { try { this.mainUrl.setUrl(new URL(mainUrl)); if (providerIp != null) { @@ -127,11 +141,11 @@ public final class Provider implements Parcelable { return; } setGeoipUrl(geoipUrl); + setMotdUrl(motdUrl); } - - public Provider(String mainUrl, String geoipUrl, String providerIp, String providerApiIp, String caCert, String definition) { - this(mainUrl, geoipUrl, providerIp, providerApiIp); + public Provider(String mainUrl, String geoipUrl, String motdUrl, String providerIp, String providerApiIp, String caCert, String definition) { + this(mainUrl, geoipUrl, motdUrl, providerIp, providerApiIp); if (caCert != null) { this.caCert = caCert; } @@ -281,6 +295,21 @@ public final class Provider implements Parcelable { } } + public DefaultedURL getMotdUrl() { + return this.motdUrl; + } + + public void setMotdUrl(String url) { + try { + this.motdUrl.setUrl(new URL(url)); + Log.d("PROVIDER", "setMotdUrl provider: " + this.domain + " - motd url: " + motdUrl.toString()); + } catch (MalformedURLException e) { + e.printStackTrace(); + Log.d("PROVIDER", "setMotdUrl exception!!! provider: " + this.domain + " - motd url: " + motdUrl.toString() + " obj:" + this.toString()); + this.motdUrl = new DefaultedURL(); + } + } + public String getApiUrlWithVersion() { return getApiUrlString() + "/" + getApiVersion(); } @@ -375,14 +404,19 @@ public final class Provider implements Parcelable { parcel.writeString(getProviderIp()); parcel.writeString(getProviderApiIp()); parcel.writeString(getGeoipUrl().toString()); + parcel.writeString(getMotdUrl().toString()); parcel.writeString(getDefinitionString()); parcel.writeString(getCaCert()); parcel.writeString(getEipServiceJsonString()); parcel.writeString(getGeoIpJsonString()); + parcel.writeString(getMotdJsonString()); parcel.writeString(getPrivateKey()); parcel.writeString(getVpnCertificate()); parcel.writeLong(lastEipServiceUpdate); parcel.writeLong(lastGeoIpUpdate); + parcel.writeLong(lastMotdUpdate); + parcel.writeLong(lastMotdSeen); + parcel.writeStringList(new ArrayList<>(lastMotdSeenHashes)); parcel.writeInt(shouldUpdateVpnCertificate ? 0 : 1); } @@ -405,6 +439,10 @@ public final class Provider implements Parcelable { geoipUrl.setUrl(new URL(tmpString)); } tmpString = in.readString(); + if (!tmpString.isEmpty()) { + motdUrl.setUrl(new URL(tmpString)); + } + tmpString = in.readString(); if (!tmpString.isEmpty()) { definition = new JSONObject((tmpString)); parseDefinition(definition); @@ -422,6 +460,10 @@ public final class Provider implements Parcelable { this.setGeoIpJson(new JSONObject(tmpString)); } tmpString = in.readString(); + if (!tmpString.isEmpty()) { + this.setMotdJson(new JSONObject(tmpString)); + } + tmpString = in.readString(); if (!tmpString.isEmpty()) { this.setPrivateKey(tmpString); } @@ -431,6 +473,11 @@ public final class Provider implements Parcelable { } this.lastEipServiceUpdate = in.readLong(); this.lastGeoIpUpdate = in.readLong(); + this.lastMotdUpdate = in.readLong(); + this.lastMotdSeen = in.readLong(); + ArrayList lastMotdSeenHashes = new ArrayList<>(); + in.readStringList(lastMotdSeenHashes); + this.lastMotdSeenHashes = new HashSet<>(lastMotdSeenHashes); this.shouldUpdateVpnCertificate = in.readInt() == 0; } catch (MalformedURLException | JSONException e) { e.printStackTrace(); @@ -447,10 +494,12 @@ public final class Provider implements Parcelable { definition.toString().equals(p.getDefinition().toString()) && eipServiceJson.toString().equals(p.getEipServiceJsonString()) && geoIpJson.toString().equals(p.getGeoIpJsonString()) && + motdJson.toString().equals(p.getMotdJsonString()) && providerIp.equals(p.getProviderIp()) && providerApiIp.equals(p.getProviderApiIp()) && apiUrl.equals(p.getApiUrl()) && geoipUrl.equals(p.getGeoipUrl()) && + motdUrl.equals(p.getMotdUrl()) && certificatePin.equals(p.getCertificatePin()) && certificatePinEncoding.equals(p.getCertificatePinEncoding()) && caCert.equals(p.getCaCert()) && @@ -527,6 +576,70 @@ public final class Provider implements Parcelable { return shouldUpdateVpnCertificate; } + public void setLastMotdSeen(long timestamp) { + lastMotdSeen = timestamp; + } + + public long getLastMotdSeen() { + return lastMotdSeen; + } + + /** + * shouldShowMotdSeen + * @return true if last message of the day was shown more than 24h ago + */ + public boolean shouldShowMotdSeen() { + return !motdUrl.isDefault() && System.currentTimeMillis() - lastMotdSeen >= MOTD_TIMEOUT; + } + + /** + * setLastSeenHashes + * @param hashes hashes of messages of type 'once' that have already been seen + */ + public void setMotdLastSeenHashes(Set hashes) { + lastMotdSeenHashes = hashes; + } + + public Set getMotdLastSeenHashes() { + return lastMotdSeenHashes; + } + + /** + * getLastSeenHashCollection + * @return go ffi compatible IStringCollection interface of message hashes of type 'once' + */ + public IStringCollection getMotdLastSeenHashCollection() { + IStringCollection stringCollection = Motd.newStringCollection(); + for (String hash : lastMotdSeenHashes) { + stringCollection.add(hash); + } + return stringCollection; + } + + public void setLastMotdUpdate(long timestamp) { + lastMotdUpdate = timestamp; + } + + public long getLastMotdUpdate() { + return lastMotdUpdate; + } + + public boolean shouldUpdateMotdJson() { + return !motdUrl.isDefault() && System.currentTimeMillis() - lastMotdUpdate >= MOTD_TIMEOUT; + } + + public void setMotdJson(@NonNull JSONObject motdJson) { + this.motdJson = motdJson; + } + + public JSONObject getMotdJson() { + return motdJson; + } + + public String getMotdJsonString() { + return motdJson.toString(); + } + public void setLastGeoIpUpdate(long timestamp) { lastGeoIpUpdate = timestamp; } @@ -621,6 +734,7 @@ public final class Provider implements Parcelable { definition = new JSONObject(); eipServiceJson = new JSONObject(); geoIpJson = new JSONObject(); + motdJson = new JSONObject(); apiUrl = new DefaultedURL(); certificatePin = ""; certificatePinEncoding = ""; -- cgit v1.2.3