Fixed API fetch call

This commit is contained in:
Verox001 2025-07-18 03:46:15 +02:00
parent 9b5a368ed1
commit ddf45a0a44
2 changed files with 56 additions and 30 deletions

View File

@ -15,9 +15,6 @@ public class Config {
.resolve("ezcheat.toml"); .resolve("ezcheat.toml");
public static String modEndpointUrl = "https://modlist.ugasmp.com/api/mods"; public static String modEndpointUrl = "https://modlist.ugasmp.com/api/mods";
private static final Logger LOGGER = LoggerFactory.getLogger("ezcheat");
public Config() {}
public static void load() { public static void load() {
if (!Files.exists(CONFIG_PATH)) { if (!Files.exists(CONFIG_PATH)) {

View File

@ -95,44 +95,73 @@ public class Ezcheat implements ModInitializer {
for (Map.Entry<String, String> entry : map.entrySet()) { for (Map.Entry<String, String> entry : map.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append(", "); 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<String, String> 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(); return sb.toString();
} }
private void verifyPlayerMods(ServerPlayerEntity player, Map<String, String> hashes) { private void verifyPlayerMods(ServerPlayerEntity player, Map<String, String> hashes) {
LOGGER.info("Verifying mod hashes: {}", mapToString(hashes)); LOGGER.info("Verifying mod hashes: {}", keysToString(hashes));
try { try {
// fetch allowed list // 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.setRequestMethod("GET");
conn.setDoInput(true);
try (InputStream is = conn.getInputStream(); InputStream in = conn.getInputStream();
Reader rdr = new InputStreamReader(is)) { String response = new String(in.readAllBytes());
Type mapType = new TypeToken<Map<String, String>>() { in.close();
}.getType();
Map<String, String> allowed = GSON.fromJson(rdr, mapType);
List<String> bad = new ArrayList<>(); Gson gson = new Gson();
for (var e : hashes.entrySet()) { Type type = new com.google.common.reflect.TypeToken<Map<String, String>>() {}.getType();
String modid = e.getKey(), hash = e.getValue(); Map<String, String> allowedMods = gson.fromJson(response, type);
if (!allowed.containsValue(hash)) {
bad.add(modid); List<String> bad = new ArrayList<>();
} for (Map.Entry<String, String> 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()) { player.networkHandler.disconnect(Text.literal(msg));
String msg = "Unallowed mods: " + String.join(", ", bad); LOGGER.info("Kicked {} for unallowed mods: {}", player.getName().getString(), bad);
} else {
// Make sure msg is not too long. If it is, truncate it. LOGGER.info("{} passed mod check", player.getName().getString());
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());
}
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Failed mod verification for {}: {}", player.getName().getString(), e.getMessage()); LOGGER.error("Failed mod verification for {}: {}", player.getName().getString(), e.getMessage());