From ddf45a0a441d5498e53429f5fcbf3a582cb8789b Mon Sep 17 00:00:00 2001 From: Verox001 Date: Fri, 18 Jul 2025 03:46:15 +0200 Subject: [PATCH] Fixed API fetch call --- .../java/com/cimeyclust/ezcheat/Config.java | 3 - .../java/com/cimeyclust/ezcheat/Ezcheat.java | 83 +++++++++++++------ 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/cimeyclust/ezcheat/Config.java b/src/main/java/com/cimeyclust/ezcheat/Config.java index 6779115..344749f 100644 --- a/src/main/java/com/cimeyclust/ezcheat/Config.java +++ b/src/main/java/com/cimeyclust/ezcheat/Config.java @@ -15,9 +15,6 @@ public class Config { .resolve("ezcheat.toml"); public static String modEndpointUrl = "https://modlist.ugasmp.com/api/mods"; - private static final Logger LOGGER = LoggerFactory.getLogger("ezcheat"); - - public Config() {} public static void load() { if (!Files.exists(CONFIG_PATH)) { diff --git a/src/main/java/com/cimeyclust/ezcheat/Ezcheat.java b/src/main/java/com/cimeyclust/ezcheat/Ezcheat.java index 1b7272a..f303259 100644 --- a/src/main/java/com/cimeyclust/ezcheat/Ezcheat.java +++ b/src/main/java/com/cimeyclust/ezcheat/Ezcheat.java @@ -95,44 +95,73 @@ public class Ezcheat implements ModInitializer { for (Map.Entry entry : map.entrySet()) { sb.append(entry.getKey()).append("=").append(entry.getValue()).append(", "); } + + // Truncate to max length of 256 characters + if (sb.length() > 256) { + sb.setLength(256); + sb.append("..."); + } else if (!sb.isEmpty()) { + sb.setLength(sb.length() - 2); // Remove trailing comma and space + } + + return sb.toString(); + } + + private String keysToString(Map map) { + StringBuilder sb = new StringBuilder(); + for (String key : map.keySet()) { + sb.append(key).append(", "); + } + + // Truncate to max length of 256 characters + if (sb.length() > 256) { + sb.setLength(256); + sb.append("..."); + } else if (!sb.isEmpty()) { + sb.setLength(sb.length() - 2); // Remove trailing comma and space + } + return sb.toString(); } private void verifyPlayerMods(ServerPlayerEntity player, Map hashes) { - LOGGER.info("Verifying mod hashes: {}", mapToString(hashes)); + LOGGER.info("Verifying mod hashes: {}", keysToString(hashes)); try { // fetch allowed list - HttpURLConnection conn = (HttpURLConnection) new URL(Config.modEndpointUrl).openConnection(); + URL url = new URL(Config.modEndpointUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); - conn.setDoInput(true); - try (InputStream is = conn.getInputStream(); - Reader rdr = new InputStreamReader(is)) { - Type mapType = new TypeToken>() { - }.getType(); - Map allowed = GSON.fromJson(rdr, mapType); + InputStream in = conn.getInputStream(); + String response = new String(in.readAllBytes()); + in.close(); - List bad = new ArrayList<>(); - for (var e : hashes.entrySet()) { - String modid = e.getKey(), hash = e.getValue(); - if (!allowed.containsValue(hash)) { - bad.add(modid); - } + Gson gson = new Gson(); + Type type = new com.google.common.reflect.TypeToken>() {}.getType(); + Map allowedMods = gson.fromJson(response, type); + + List bad = new ArrayList<>(); + for (Map.Entry entry : hashes.entrySet()) { + String modid = entry.getKey(); + String hash = entry.getValue(); + + if (!allowedMods.containsKey(hash)) { + bad.add(modid); + } + } + + if (!bad.isEmpty()) { + String msg = "Unallowed mods: " + String.join(", ", bad); + + // Make sure msg is not too long. If it is, truncate it. + if (msg.length() > 256) { + msg = msg.substring(0, 256) + "..."; } - if (!bad.isEmpty()) { - String msg = "Unallowed mods: " + String.join(", ", bad); - - // Make sure msg is not too long. If it is, truncate it. - if (msg.length() > 256) { - msg = msg.substring(0, 256) + "..."; - } - - player.networkHandler.disconnect(Text.literal(msg)); - LOGGER.info("Kicked {} for unallowed mods: {}", player.getName().getString(), bad); - } else { - LOGGER.info("{} passed mod check", player.getName().getString()); - } + player.networkHandler.disconnect(Text.literal(msg)); + LOGGER.info("Kicked {} for unallowed mods: {}", player.getName().getString(), bad); + } else { + LOGGER.info("{} passed mod check", player.getName().getString()); } } catch (IOException e) { LOGGER.error("Failed mod verification for {}: {}", player.getName().getString(), e.getMessage());