|
|
|
@ -21,6 +21,7 @@ package eu.faircode.email;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.os.SystemClock;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
@ -31,43 +32,42 @@ import java.io.IOException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
|
|
|
|
|
public class DisconnectBlacklist {
|
|
|
|
|
private static final Map<String, List<String>> map = new HashMap<>();
|
|
|
|
|
private static final ExecutorService executor = Helper.getBackgroundExecutor(1, "disconnect");
|
|
|
|
|
|
|
|
|
|
private final static int FETCH_TIMEOUT = 20 * 1000; // milliseconds
|
|
|
|
|
private final static String LIST = "https://raw.githubusercontent.com/disconnectme/disconnect-tracking-protection/master/services.json";
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
connection.connect();
|
|
|
|
|
static void init(Context context) {
|
|
|
|
|
final File file = getFile(context);
|
|
|
|
|
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
String response = Helper.readStream(connection.getInputStream(), StandardCharsets.UTF_8.name());
|
|
|
|
|
Helper.writeText(file, response);
|
|
|
|
|
} finally {
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
init(file);
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static List<String> getCategories(String domain, Context context) throws IOException, JSONException {
|
|
|
|
|
if (domain == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
File file = getFile(context);
|
|
|
|
|
if (!file.exists())
|
|
|
|
|
return null;
|
|
|
|
|
private static void init(File file) throws IOException, JSONException {
|
|
|
|
|
synchronized (map) {
|
|
|
|
|
long start = SystemClock.elapsedRealtime();
|
|
|
|
|
|
|
|
|
|
List<String> result = new ArrayList<>();
|
|
|
|
|
map.clear();
|
|
|
|
|
|
|
|
|
|
String json = Helper.readText(file);
|
|
|
|
|
JSONObject jdisconnect = new JSONObject(json);
|
|
|
|
@ -89,21 +89,58 @@ public class DisconnectBlacklist {
|
|
|
|
|
String site = sites.next();
|
|
|
|
|
String host = Uri.parse(site).getHost();
|
|
|
|
|
if (host != null)
|
|
|
|
|
domains.add(host);
|
|
|
|
|
domains.add(host.toLowerCase(Locale.ROOT));
|
|
|
|
|
|
|
|
|
|
JSONArray jdomains = jsites.getJSONArray(site);
|
|
|
|
|
for (int d = 0; d < jdomains.length(); d++)
|
|
|
|
|
domains.add(jdomains.getString(d));
|
|
|
|
|
domains.add(jdomains.getString(d).toLowerCase(Locale.ROOT));
|
|
|
|
|
|
|
|
|
|
for (String domain : domains) {
|
|
|
|
|
if (!map.containsKey(domain))
|
|
|
|
|
map.put(domain, new ArrayList<>());
|
|
|
|
|
List<String> list = map.get(domain);
|
|
|
|
|
if (!list.contains(category))
|
|
|
|
|
list.add(category);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (String d : domains)
|
|
|
|
|
if (domain.equalsIgnoreCase(d) && !result.contains(category))
|
|
|
|
|
result.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);
|
|
|
|
|
connection.connect();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
String response = Helper.readStream(connection.getInputStream(), StandardCharsets.UTF_8.name());
|
|
|
|
|
Helper.writeText(file, response);
|
|
|
|
|
} finally {
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (result.size() == 0 ? null : result);
|
|
|
|
|
static List<String> getCategories(String domain) {
|
|
|
|
|
if (domain == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
synchronized (map) {
|
|
|
|
|
List<String> result = map.get(domain.toLowerCase(Locale.ROOT));
|
|
|
|
|
return (result == null || result.size() == 0 ? null : result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static File getFile(Context context) {
|
|
|
|
|