|
| 1 | +package net.kollnig.missioncontrol.analysis; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.util.Log; |
| 5 | + |
| 6 | +import net.kollnig.missioncontrol.data.ExodusTracker; |
| 7 | + |
| 8 | +import org.json.JSONObject; |
| 9 | + |
| 10 | +import java.io.BufferedReader; |
| 11 | +import java.io.File; |
| 12 | +import java.io.FileReader; |
| 13 | +import java.io.FileWriter; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.Reader; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import okhttp3.OkHttpClient; |
| 22 | +import okhttp3.Request; |
| 23 | +import okhttp3.Response; |
| 24 | + |
| 25 | +/** |
| 26 | + * Manages the fetch and cache of tracker signatures from Exodus Privacy. |
| 27 | + */ |
| 28 | +public class TrackerSignatureManager { |
| 29 | + private static final String TAG = "TrackerSignatureManager"; |
| 30 | + private static final String EXODUS_URL = "https://reports.exodus-privacy.eu.org/api/trackers"; |
| 31 | + private static final String CACHE_FILE = "trackers.json"; |
| 32 | + |
| 33 | + private final Context context; |
| 34 | + private final OkHttpClient client; |
| 35 | + |
| 36 | + public TrackerSignatureManager(Context context) { |
| 37 | + this.context = context.getApplicationContext(); |
| 38 | + this.client = new OkHttpClient(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Downloads the latest signatures from Exodus API and caches them. |
| 43 | + * Use this method in a background thread. |
| 44 | + * |
| 45 | + * @return true if signatures were updated (changed), false otherwise. |
| 46 | + */ |
| 47 | + public boolean updateSignatures() { |
| 48 | + Request request = new Request.Builder() |
| 49 | + .url(EXODUS_URL) |
| 50 | + .build(); |
| 51 | + |
| 52 | + try (Response response = client.newCall(request).execute()) { |
| 53 | + if (!response.isSuccessful()) { |
| 54 | + Log.e(TAG, "Failed to download signatures: " + response.code()); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + String json = response.body().string(); |
| 59 | + |
| 60 | + // Validate JSON before saving |
| 61 | + JSONObject root = new JSONObject(json); |
| 62 | + if (root.has("trackers")) { |
| 63 | + // Check if content changed |
| 64 | + File file = new File(context.getFilesDir(), CACHE_FILE); |
| 65 | + if (file.exists()) { |
| 66 | + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { |
| 67 | + String cachedJson = readAll(reader); |
| 68 | + if (json.equals(cachedJson)) { |
| 69 | + Log.i(TAG, "Signatures are up to date."); |
| 70 | + return false; |
| 71 | + } |
| 72 | + } catch (Exception e) { |
| 73 | + Log.w(TAG, "Could not read existing cache for comparison: " + e.getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + saveToCache(json); |
| 78 | + Log.i(TAG, "Successfully updated signatures."); |
| 79 | + return true; |
| 80 | + } |
| 81 | + } catch (Exception e) { |
| 82 | + Log.e(TAG, "Error updating signatures", e); |
| 83 | + } |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + private void saveToCache(String json) { |
| 88 | + File file = new File(context.getFilesDir(), CACHE_FILE); |
| 89 | + try (FileWriter writer = new FileWriter(file)) { |
| 90 | + writer.write(json); |
| 91 | + } catch (IOException e) { |
| 92 | + Log.e(TAG, "Failed to cache signatures", e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets the list of trackers. |
| 98 | + * Tries to read from cache first. If cache is missing or invalid, falls back to |
| 99 | + * bundled resources. |
| 100 | + */ |
| 101 | + public List<ExodusTracker> getTrackers() { |
| 102 | + List<ExodusTracker> cached = loadFromCache(); |
| 103 | + if (cached != null && !cached.isEmpty()) { |
| 104 | + return cached; |
| 105 | + } |
| 106 | + return loadFromAssets(); |
| 107 | + } |
| 108 | + |
| 109 | + private List<ExodusTracker> loadFromCache() { |
| 110 | + File file = new File(context.getFilesDir(), CACHE_FILE); |
| 111 | + if (!file.exists()) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { |
| 116 | + return parseTrackers(readAll(reader)); |
| 117 | + } catch (Exception e) { |
| 118 | + Log.e(TAG, "Failed to load signatures from cache", e); |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + private List<ExodusTracker> loadFromAssets() { |
| 124 | + Log.i(TAG, "Loading signatures from assets (fallback)"); |
| 125 | + try (BufferedReader reader = new BufferedReader( |
| 126 | + new java.io.InputStreamReader(context.getAssets().open("trackers.json")))) { |
| 127 | + return parseTrackers(readAll(reader)); |
| 128 | + } catch (IOException e) { |
| 129 | + Log.e(TAG, "Failed to load signatures from assets", e); |
| 130 | + } |
| 131 | + return Collections.emptyList(); |
| 132 | + } |
| 133 | + |
| 134 | + private List<ExodusTracker> parseTrackers(String jsonText) { |
| 135 | + try { |
| 136 | + JSONObject root = new JSONObject(jsonText); |
| 137 | + if (root.has("trackers")) { |
| 138 | + JSONObject trackersNode = root.getJSONObject("trackers"); |
| 139 | + List<ExodusTracker> list = new ArrayList<>(); |
| 140 | + Iterator<String> keys = trackersNode.keys(); |
| 141 | + |
| 142 | + while (keys.hasNext()) { |
| 143 | + String key = keys.next(); |
| 144 | + JSONObject obj = trackersNode.getJSONObject(key); |
| 145 | + |
| 146 | + ExodusTracker t = new ExodusTracker(); |
| 147 | + t.id = obj.optInt("id"); |
| 148 | + t.name = obj.optString("name"); |
| 149 | + t.website = obj.optString("website"); |
| 150 | + t.codeSignature = obj.optString("code_signature").isEmpty() ? null |
| 151 | + : obj.getString("code_signature"); |
| 152 | + t.networkSignature = obj.optString("network_signature"); |
| 153 | + |
| 154 | + // Filter out "good" trackers |
| 155 | + if (t.name != null && (t.name.equals("Acrarium") || t.name.equals("ACRA") |
| 156 | + || t.name.equals("Custom Activity On Crash"))) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + |
| 160 | + list.add(t); |
| 161 | + } |
| 162 | + return list; |
| 163 | + } |
| 164 | + } catch (Exception e) { |
| 165 | + Log.e(TAG, "Error parsing trackers JSON", e); |
| 166 | + } |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + private String readAll(Reader rd) throws IOException { |
| 171 | + StringBuilder sb = new StringBuilder(); |
| 172 | + int cp; |
| 173 | + while ((cp = rd.read()) != -1) { |
| 174 | + sb.append((char) cp); |
| 175 | + } |
| 176 | + return sb.toString(); |
| 177 | + } |
| 178 | +} |
0 commit comments