0
0
mirror of https://github.com/TrianguloY/UrlChecker.git synced 2024-09-19 20:02:16 +02:00

check host subdomains too

fixes #370
This commit is contained in:
TrianguloY 2024-08-19 18:42:14 +02:00
parent 5eb9e5a45e
commit 52bc3919e2

View File

@ -9,7 +9,6 @@ import com.trianguloy.urlchecker.R;
import com.trianguloy.urlchecker.utilities.generics.JsonCatalog;
import com.trianguloy.urlchecker.utilities.methods.HttpUtils;
import com.trianguloy.urlchecker.utilities.methods.JavaUtils;
import com.trianguloy.urlchecker.utilities.methods.StreamUtils;
import com.trianguloy.urlchecker.utilities.wrappers.InternalFile;
import com.trianguloy.urlchecker.utilities.wrappers.ProgressDialog;
@ -19,9 +18,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Represents and manages the hosts data
*/
/** Represents and manages the hosts data */
public class Hosts {
private final HostsCatalog data;
@ -39,9 +36,7 @@ public class Hosts {
data = new HostsCatalog(cntx);
}
/**
* Builds the hosts database (asks first)
*/
/** Builds the hosts database (asks first) */
public void build(boolean showEditor, Runnable onFinished) {
var builder = new AlertDialog.Builder(cntx)
.setTitle(R.string.mHosts_buildTitle)
@ -60,16 +55,12 @@ public class Hosts {
builder.show();
}
/**
* @see JsonCatalog#showEditor
*/
/** @see JsonCatalog#showEditor */
public void showEditor() {
data.showEditor();
}
/**
* Background thread that builds the database (and notifies progress)
*/
/** Background thread that builds the database (and notifies progress) */
private void _build(ProgressDialog progress, Runnable onFinished) {
var catalog = data.getCatalog();
@ -149,17 +140,21 @@ public class Hosts {
});
}
/**
* return true if the database is built
*/
/** return true if the database is built */
public boolean isUninitialized() {
return buckets.isEmpty() && getFileNames().isEmpty();
}
/**
* Returns the label and color for the host, null if not in the database
*/
/** Returns the label and color for the host, or bigger partial host (no subdomain). null if not in the database */
public Pair<String, String> contains(String host) {
var contains = containsExact(host);
if (contains != null) return contains;
if (!host.contains(".")) return null; // no more subdomains to check
return contains(host.split("\\.", 2)[1]); // remove leftmost subdomain
}
/** Returns the label and color for the exact host, null if not in the database */
public Pair<String, String> containsExact(String host) {
return getBucket(host, key -> {
var values = new HashMap<String, Pair<String, String>>();
new InternalFile(PREFIX + key, cntx).stream(line -> {
@ -173,9 +168,7 @@ public class Hosts {
/* ------------------- internal ------------------- */
/**
* returns all the files from this database
*/
/** returns all the files from this database */
private ArrayList<String> getFileNames() {
var files = new ArrayList<String>();
for (var fileName : cntx.fileList()) {
@ -184,11 +177,9 @@ public class Hosts {
return files;
}
/**
* The number of hosts.
* I miss streams :(
*/
/** The number of hosts. */
public int size() {
// I miss streams :(
var s = 0;
for (var value : buckets.values()) {
s += value.size();
@ -205,9 +196,7 @@ public class Hosts {
if (replace || !bucket.containsKey(host)) bucket.put(host, data);
}
/**
* returns the bucket of a value, if not ready computes it
*/
/** returns the bucket of a value, if not ready computes it */
private HashMap<String, Pair<String, String>> getBucket(String value, JavaUtils.Function<Integer, HashMap<String, Pair<String, String>>> compute) {
// HASHING
var hash = Math.floorMod(value.hashCode(), FILES);