fix: make lookup more reliable
This commit is contained in:
@@ -8,54 +8,57 @@ import java.net.URI;
|
|||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class Domains {
|
public class Domains {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger("AnarchyMod-Domains");
|
||||||
private static final String DOMAINS_URL = "https://www.6b6t.org/api/anarchy-mod.json";
|
private static final String DOMAINS_URL = "https://www.6b6t.org/api/anarchy-mod.json";
|
||||||
|
|
||||||
private static final Set<String> DEFAULT = Set.of(
|
private static final Set<String> DEFAULT = Set.of(
|
||||||
"*.6b6t.org", "*.10b10t.org", "*.6b6t.cc", "*.6b6t.me",
|
"*.6b6t.org", "*.10b10t.org", "*.6b6t.cc", "*.6b6t.me",
|
||||||
"*.7b7t.me", "*.8b8t.org", "*.alacity.net",
|
"*.7b7t.me", "*.8b8t.org", "*.alacity.net",
|
||||||
"*.anarchypvp.pw", "*.l2x9.org", "*.simpleanarchy.org"
|
"*.anarchypvp.pw", "*.l2x9.org", "*.simpleanarchy.org"
|
||||||
);
|
);
|
||||||
|
|
||||||
private static final Set<String> domains = new HashSet<>();
|
private static final Set<String> domains = new HashSet<>();
|
||||||
private static final Gson gson = new Gson();
|
private static final Gson gson = new Gson();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
DEFAULT.forEach(d -> domains.add(d.toLowerCase()));
|
DEFAULT.forEach(d -> domains.add(d.toLowerCase(Locale.ROOT)));
|
||||||
loadRemote();
|
loadRemote();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void loadRemote() {
|
private static void loadRemote() {
|
||||||
try {
|
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(DOMAINS_URL))
|
.uri(URI.create(DOMAINS_URL))
|
||||||
.GET()
|
.timeout(Duration.ofSeconds(15))
|
||||||
.build();
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response =
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
||||||
|
|
||||||
JsonObject json = gson.fromJson(response.body(), JsonObject.class);
|
JsonObject json = gson.fromJson(response.body(), JsonObject.class);
|
||||||
JsonArray array = json.getAsJsonArray("domains");
|
JsonArray array = json.getAsJsonArray("domains");
|
||||||
if (array == null) return;
|
if (array == null) return;
|
||||||
|
|
||||||
array.forEach(element ->
|
array.forEach(element ->
|
||||||
domains.add(element.getAsString().toLowerCase())
|
domains.add(element.getAsString().toLowerCase(Locale.ROOT))
|
||||||
);
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
} catch (Exception ignored) {
|
LOGGER.warning("Failed to load domains from remote, using defaults: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean contains(String input) {
|
public static boolean contains(String input) {
|
||||||
if (input == null) return false;
|
if (input == null) return false;
|
||||||
|
|
||||||
String domain = input.toLowerCase();
|
String domain = input.toLowerCase(Locale.ROOT);
|
||||||
|
|
||||||
if (domain.startsWith("*.")) {
|
if (domain.startsWith("*.")) {
|
||||||
domain = domain.substring(2);
|
domain = domain.substring(2);
|
||||||
@@ -67,7 +70,6 @@ public class Domains {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (String entry : domains) {
|
for (String entry : domains) {
|
||||||
|
|
||||||
if (entry.startsWith("*.")) {
|
if (entry.startsWith("*.")) {
|
||||||
String base = entry.substring(2);
|
String base = entry.substring(2);
|
||||||
if (domain.equals(base) || domain.endsWith("." + base)) {
|
if (domain.equals(base) || domain.endsWith("." + base)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user