Fixed API fetch call
This commit is contained in:
parent
9b5a368ed1
commit
ddf45a0a44
@ -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)) {
|
||||
|
||||
@ -95,44 +95,73 @@ public class Ezcheat implements ModInitializer {
|
||||
for (Map.Entry<String, String> 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<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();
|
||||
}
|
||||
|
||||
private void verifyPlayerMods(ServerPlayerEntity player, Map<String, String> 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<Map<String, String>>() {
|
||||
}.getType();
|
||||
Map<String, String> allowed = GSON.fromJson(rdr, mapType);
|
||||
InputStream in = conn.getInputStream();
|
||||
String response = new String(in.readAllBytes());
|
||||
in.close();
|
||||
|
||||
List<String> 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<Map<String, String>>() {}.getType();
|
||||
Map<String, String> allowedMods = gson.fromJson(response, type);
|
||||
|
||||
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()) {
|
||||
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());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user