From ecbfd294a8d2fb3b2b6461ef225ec0d0e6652505 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 22 Oct 2019 15:24:37 +0200 Subject: [PATCH] send hash to firebase --- .../nightscout/androidaps/MainActivity.java | 2 +- .../info/nightscout/androidaps/MainApp.java | 6 +-- .../signatureVerifier/SignatureVerifier.java | 45 +++++++++++++++++-- .../versionChecker/VersionCheckerPlugin.kt | 2 +- .../versionChecker/VersionCheckerUtils.kt | 2 +- .../androidaps/utils/FabricPrivacy.java | 14 ++++-- .../VersionCheckerUtilsKtTest.kt | 2 +- 7 files changed, 58 insertions(+), 15 deletions(-) rename app/src/main/java/info/nightscout/androidaps/plugins/{general => constraints}/signatureVerifier/SignatureVerifier.java (78%) rename app/src/main/java/info/nightscout/androidaps/plugins/{general => constraints}/versionChecker/VersionCheckerPlugin.kt (97%) rename app/src/main/java/info/nightscout/androidaps/plugins/{general => constraints}/versionChecker/VersionCheckerUtils.kt (98%) rename app/src/test/java/info/nightscout/androidaps/plugins/{general => constraints}/versionChecker/VersionCheckerUtilsKtTest.kt (99%) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 9287921879..fdbb5569b1 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -54,7 +54,7 @@ import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus; -import info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerUtilsKt; +import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt; import info.nightscout.androidaps.setupwizard.SetupWizardActivity; import info.nightscout.androidaps.tabs.TabPageAdapter; import info.nightscout.androidaps.utils.AndroidPermission; diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index a1300f59bc..037e7c86a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -49,9 +49,9 @@ import info.nightscout.androidaps.plugins.general.nsclient.receivers.AckAlarmRec import info.nightscout.androidaps.plugins.general.nsclient.receivers.DBAccessReceiver; import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin; import info.nightscout.androidaps.plugins.general.persistentNotification.PersistentNotificationPlugin; -import info.nightscout.androidaps.plugins.general.signatureVerifier.SignatureVerifier; +import info.nightscout.androidaps.plugins.constraints.signatureVerifier.SignatureVerifier; import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; -import info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerPlugin; +import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerPlugin; import info.nightscout.androidaps.plugins.general.wear.WearPlugin; import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin; import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin; @@ -91,7 +91,7 @@ import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.utils.FabricPrivacy; import io.fabric.sdk.android.Fabric; -import static info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion; +import static info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion; public class MainApp extends Application { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/signatureVerifier/SignatureVerifier.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/signatureVerifier/SignatureVerifier.java similarity index 78% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/signatureVerifier/SignatureVerifier.java rename to app/src/main/java/info/nightscout/androidaps/plugins/constraints/signatureVerifier/SignatureVerifier.java index b0616ec35e..b5fc3d10f7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/signatureVerifier/SignatureVerifier.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/signatureVerifier/SignatureVerifier.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.general.signatureVerifier; +package info.nightscout.androidaps.plugins.constraints.signatureVerifier; import android.content.pm.PackageManager; import android.content.pm.Signature; @@ -124,14 +124,51 @@ public class SignatureVerifier extends PluginBase implements ConstraintsInterfac } } } - } catch (PackageManager.NameNotFoundException e) { - log.error("Error in SignatureVerifier", e); - } catch (NoSuchAlgorithmException e) { + } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) { log.error("Error in SignatureVerifier", e); } return false; } + public List shortHashes() { + List hashes = new ArrayList<>(); + try { + Signature[] signatures = MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), PackageManager.GET_SIGNATURES).signatures; + if (signatures != null) { + for (Signature signature : signatures) { + MessageDigest digest = MessageDigest.getInstance("SHA256"); + byte[] fingerprint = digest.digest(signature.toByteArray()); + String hash = Hex.toHexString(fingerprint); + log.debug("Found signature: " + hash); + log.debug("Found signature (short): " + singleCharMap(fingerprint)); + hashes.add(singleCharMap(fingerprint)); + } + } + } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) { + log.error("Error in SignatureVerifier", e); + } + return hashes; + } + + String map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"§$%&/()=?,.-;:_<>|°^`´\\@€*'#+~{}[]¿¡áéíóúàèìòùöäü`ÁÉÍÓÚÀÈÌÒÙÖÄÜßÆÇÊËÎÏԌ۟æçêëîïôœûÿĆČĐŠŽćđšžñΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡ\u03A2ΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρςστυφχψωϨϩϪϫϬϭϮϯϰϱϲϳϴϵ϶ϷϸϹϺϻϼϽϾϿЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗ"; + + private String singleCharMap(byte[] array) { + StringBuilder sb = new StringBuilder(); + for (byte b : array) { + sb.append(map.charAt(b & 0xFF)); + } + return sb.toString(); + } + + private String singleCharUnMap(String shortHash) { + byte[] array = new byte[shortHash.length()]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append(String.format("%02x",(int) map.charAt(map.indexOf(shortHash.charAt(i))))); + } + return sb.toString(); + } + private boolean shouldDownloadCerts() { return System.currentTimeMillis() - SP.getLong(R.string.key_last_revoked_certs_check, 0L) >= UPDATE_INTERVAL; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerPlugin.kt similarity index 97% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt rename to app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerPlugin.kt index bf67426787..ee267c689e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerPlugin.kt @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.general.versionChecker +package info.nightscout.androidaps.plugins.constraints.versionChecker import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt similarity index 98% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt rename to app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt index 7961b19e83..4b19e33540 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.general.versionChecker +package info.nightscout.androidaps.plugins.constraints.versionChecker import android.content.Context import android.net.ConnectivityManager diff --git a/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.java b/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.java index 3dd7fe4fd9..056fa1131f 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.java @@ -8,10 +8,13 @@ import com.google.firebase.analytics.FirebaseAnalytics; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.constraints.signatureVerifier.SignatureVerifier; /** * Created by jamorham on 21/02/2018. @@ -112,9 +115,9 @@ public class FabricPrivacy { String closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed().value() ? "CLOSED_LOOP_ENABLED" : "CLOSED_LOOP_DISABLED"; // Size is limited to 36 chars - String remote = BuildConfig.REMOTE - .replace("https://","") - .replace("http://","") + String remote = BuildConfig.REMOTE.toLowerCase() + .replace("https://", "") + .replace("http://", "") .replace(".git", "") .replace(".com/", ":") .replace(".org/", ":") @@ -125,6 +128,10 @@ public class FabricPrivacy { MainApp.getFirebaseAnalytics().setUserProperty("Version", BuildConfig.VERSION); MainApp.getFirebaseAnalytics().setUserProperty("HEAD", BuildConfig.HEAD); MainApp.getFirebaseAnalytics().setUserProperty("Remote", remote); + List hashes = SignatureVerifier.getPlugin().shortHashes(); + if (hashes.size() > 1) + MainApp.getFirebaseAnalytics().setUserProperty("Hash", hashes.get(0)); + if (ConfigBuilderPlugin.getPlugin().getActivePump() != null) MainApp.getFirebaseAnalytics().setUserProperty("Pump", ConfigBuilderPlugin.getPlugin().getActivePump().getClass().getSimpleName()); if (ConfigBuilderPlugin.getPlugin().getActiveAPS() != null) @@ -137,7 +144,6 @@ public class FabricPrivacy { MainApp.getFirebaseAnalytics().setUserProperty("Sensitivity", ConfigBuilderPlugin.getPlugin().getActiveSensitivity().getClass().getSimpleName()); if (ConfigBuilderPlugin.getPlugin().getActiveInsulin() != null) MainApp.getFirebaseAnalytics().setUserProperty("Insulin", ConfigBuilderPlugin.getPlugin().getActiveInsulin().getClass().getSimpleName()); - } } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtilsKtTest.kt similarity index 99% rename from app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt rename to app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtilsKtTest.kt index 1b9c937c24..581f51c6d9 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtilsKtTest.kt @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.general.versionChecker +package info.nightscout.androidaps.plugins.constraints.versionChecker import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R