FairEmail/app/src/main/java/eu/faircode/email/DisconnectBlacklist.java

194 lines
6.9 KiB
Java
Raw Normal View History

package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
2020-07-20 06:25:25 +00:00
import android.content.SharedPreferences;
2020-07-19 13:50:24 +00:00
import android.os.SystemClock;
2020-07-20 06:25:25 +00:00
import androidx.preference.PreferenceManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
2023-12-29 12:06:50 +00:00
import java.io.BufferedOutputStream;
import java.io.File;
2021-05-19 15:20:42 +00:00
import java.io.FileNotFoundException;
2023-12-29 12:06:50 +00:00
import java.io.FileOutputStream;
import java.io.IOException;
2023-12-29 12:06:50 +00:00
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
2020-07-19 13:50:24 +00:00
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
2020-07-19 13:50:24 +00:00
import java.util.Locale;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class DisconnectBlacklist {
2020-07-19 13:50:24 +00:00
private static final Map<String, List<String>> map = new HashMap<>();
2023-06-09 12:52:07 +00:00
private static final List<String> all = new ArrayList<>();
2020-07-19 13:50:24 +00:00
private final static int FETCH_TIMEOUT = 20 * 1000; // milliseconds
2020-07-19 12:31:08 +00:00
private final static String LIST = "https://raw.githubusercontent.com/disconnectme/disconnect-tracking-protection/master/services.json";
2023-06-09 12:52:07 +00:00
final static String URI_CATEGORIES = "https://disconnect.me/trackerprotection#categories-of-trackers";
2020-07-19 13:50:24 +00:00
static void init(Context context) {
final File file = getFile(context);
2022-12-13 09:52:39 +00:00
Helper.getSerialExecutor().submit(new Runnable() {
2020-07-19 13:50:24 +00:00
@Override
public void run() {
try {
2020-07-19 18:14:05 +00:00
if (file.exists())
init(file);
2020-07-19 13:50:24 +00:00
} catch (Throwable ex) {
Log.e(ex);
}
}
});
}
private static void init(File file) throws IOException, JSONException {
synchronized (map) {
long start = SystemClock.elapsedRealtime();
map.clear();
2023-06-09 12:52:07 +00:00
all.clear();
2020-07-19 13:50:24 +00:00
String json = Helper.readText(file);
JSONObject jdisconnect = new JSONObject(json);
JSONObject jcategories = (JSONObject) jdisconnect.get("categories");
Iterator<String> categories = jcategories.keys();
while (categories.hasNext()) {
String category = categories.next();
2023-06-09 12:52:07 +00:00
all.add(category);
2020-07-19 13:50:24 +00:00
JSONArray jcategory = jcategories.getJSONArray(category);
for (int c = 0; c < jcategory.length(); c++) {
JSONObject jblock = (JSONObject) jcategory.get(c);
Iterator<String> names = jblock.keys();
if (names.hasNext()) {
String name = names.next();
JSONObject jsites = (JSONObject) jblock.get(name);
Iterator<String> sites = jsites.keys();
if (sites.hasNext()) {
String site = sites.next();
JSONArray jdomains = jsites.getJSONArray(site);
2020-07-19 17:53:51 +00:00
for (int d = 0; d < jdomains.length(); d++) {
String domain = jdomains.getString(d).toLowerCase(Locale.ROOT);
2020-07-19 13:50:24 +00:00
if (!map.containsKey(domain))
map.put(domain, new ArrayList<>());
List<String> list = map.get(domain);
if (!list.contains(category))
list.add(category);
}
}
}
}
}
long elapsed = SystemClock.elapsedRealtime() - start;
Log.i("Disconnect domains=" + map.size() + " elapsed=" + elapsed + " ms");
}
}
static void download(Context context) throws IOException, JSONException {
File file = getFile(context);
URL url = new URL(LIST);
Log.i("GET " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(FETCH_TIMEOUT);
connection.setConnectTimeout(FETCH_TIMEOUT);
ConnectionHelper.setUserAgent(context, connection);
connection.connect();
try {
2021-05-19 15:20:42 +00:00
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK)
2021-07-15 06:36:55 +00:00
throw new FileNotFoundException("Error " + status + ": " + connection.getResponseMessage());
2021-05-19 15:20:42 +00:00
2023-12-29 12:06:50 +00:00
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
Helper.copy(connection.getInputStream(), os);
}
} finally {
connection.disconnect();
}
2020-07-19 13:50:24 +00:00
init(file);
}
2023-06-09 12:52:07 +00:00
static List<String> getCategories() {
synchronized (all) {
return new ArrayList<>(all);
}
}
static boolean isEnabled(Context context, String category) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean("disconnect_" + category, !"Content".equals(category));
}
static void setEnabled(Context context, String category, boolean value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean("disconnect_" + category, value).apply();
}
2020-07-19 13:50:24 +00:00
static List<String> getCategories(String domain) {
2020-07-19 17:53:51 +00:00
return _getCategories(domain);
}
2023-06-09 12:52:07 +00:00
static boolean isTrackingImage(Context context, String host) {
2020-07-19 17:53:51 +00:00
List<String> categories = _getCategories(host);
if (categories == null || categories.size() == 0)
return false;
2023-06-09 12:52:07 +00:00
for (String category : categories)
if (isEnabled(context, category))
return true;
return false;
2020-07-19 17:53:51 +00:00
}
private static List<String> _getCategories(String domain) {
if (domain == null)
return null;
synchronized (map) {
2020-07-19 17:53:51 +00:00
String d = domain.toLowerCase(Locale.ROOT);
while (d.contains(".")) {
List<String> result = map.get(d);
if (result != null)
return result;
int dot = d.indexOf(".");
d = d.substring(dot + 1);
}
}
2020-07-19 17:53:51 +00:00
return null;
}
private static File getFile(Context context) {
return new File(context.getFilesDir(), "disconnect-blacklist.json");
}
}