From 2261bde6cb27c2a2c6717029f4bfc1bdd1d2225e Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Mon, 22 Apr 2024 23:44:19 -0400 Subject: [PATCH] DNS records shouldnt be NonNull --- .../java/me/braydon/mc/model/MinecraftServer.java | 4 ++-- .../mc/model/server/BedrockMinecraftServer.java | 7 +++---- .../braydon/mc/model/server/JavaMinecraftServer.java | 6 +++--- .../java/me/braydon/mc/service/MaxMindService.java | 1 - .../java/me/braydon/mc/service/MojangService.java | 11 ++++++++--- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/API/src/main/java/me/braydon/mc/model/MinecraftServer.java b/API/src/main/java/me/braydon/mc/model/MinecraftServer.java index 4530599..b3a1027 100644 --- a/API/src/main/java/me/braydon/mc/model/MinecraftServer.java +++ b/API/src/main/java/me/braydon/mc/model/MinecraftServer.java @@ -64,9 +64,9 @@ public class MinecraftServer { @EqualsAndHashCode.Include private final int port; /** - * The DNS records resolved for this server. + * The DNS records resolved for this server, null if none. */ - @NonNull private final DNSRecord[] records; + private final DNSRecord[] records; /** * The Geo location of this server, null if unknown. diff --git a/API/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java b/API/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java index db8f947..d5c52d9 100644 --- a/API/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java +++ b/API/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java @@ -55,7 +55,7 @@ public final class BedrockMinecraftServer extends MinecraftServer { @NonNull private final GameMode gamemode; private BedrockMinecraftServer(@NonNull String id, @NonNull String hostname, String ip, int port, GeoLocation geo, - @NonNull DNSRecord[] records, @NonNull Edition edition, @NonNull Version version, + DNSRecord[] records, @NonNull Edition edition, @NonNull Version version, @NonNull Players players, @NonNull MOTD motd, @NonNull GameMode gamemode) { super(hostname, ip, port, records, geo, players, motd); this.id = id; @@ -70,13 +70,12 @@ public final class BedrockMinecraftServer extends MinecraftServer { * @param hostname the hostname of the server * @param ip the IP address of the server * @param port the port of the server - * @param records the DNS records of the server + * @param records the DNS records of the server, if any * @param token the status token * @return the Bedrock Minecraft server */ @NonNull - public static BedrockMinecraftServer create(@NonNull String hostname, String ip, int port, - @NonNull DNSRecord[] records, @NonNull String token) { + public static BedrockMinecraftServer create(@NonNull String hostname, String ip, int port, DNSRecord[] records, @NonNull String token) { String[] split = token.split(";"); // Split the token Edition edition = Edition.valueOf(split[0]); Version version = new Version(Integer.parseInt(split[2]), split[3]); diff --git a/API/src/main/java/me/braydon/mc/model/server/JavaMinecraftServer.java b/API/src/main/java/me/braydon/mc/model/server/JavaMinecraftServer.java index d967a85..e87ec67 100644 --- a/API/src/main/java/me/braydon/mc/model/server/JavaMinecraftServer.java +++ b/API/src/main/java/me/braydon/mc/model/server/JavaMinecraftServer.java @@ -123,7 +123,7 @@ public final class JavaMinecraftServer extends MinecraftServer { */ private boolean mojangBanned; - private JavaMinecraftServer(@NonNull String hostname, String ip, int port, @NonNull DNSRecord[] records, GeoLocation geo, + private JavaMinecraftServer(@NonNull String hostname, String ip, int port, DNSRecord[] records, GeoLocation geo, @NonNull Version version, @NonNull Players players, @NonNull MOTD motd, Favicon favicon, String software, Plugin[] plugins, ModInfo modInfo, ForgeData forgeData, String world, boolean queryEnabled, boolean previewsChat, boolean enforcesSecureChat, boolean preventsChatReports, @@ -149,13 +149,13 @@ public final class JavaMinecraftServer extends MinecraftServer { * @param hostname the hostname of the server * @param ip the IP address of the server * @param port the port of the server - * @param records the DNS records of the server + * @param records the DNS records of the server, if any * @param statusToken the status token * @param challengeStatusToken the challenge status token, null if none * @return the Java Minecraft server */ @NonNull - public static JavaMinecraftServer create(@NonNull String hostname, String ip, int port, @NonNull DNSRecord[] records, + public static JavaMinecraftServer create(@NonNull String hostname, String ip, int port, DNSRecord[] records, @NonNull JavaServerStatusToken statusToken, JavaServerChallengeStatusToken challengeStatusToken) { String motdString = statusToken.getDescription() instanceof String ? (String) statusToken.getDescription() : null; if (motdString == null) { // Not a string motd, convert from Json diff --git a/API/src/main/java/me/braydon/mc/service/MaxMindService.java b/API/src/main/java/me/braydon/mc/service/MaxMindService.java index 604ebd5..4367e24 100644 --- a/API/src/main/java/me/braydon/mc/service/MaxMindService.java +++ b/API/src/main/java/me/braydon/mc/service/MaxMindService.java @@ -30,7 +30,6 @@ import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import lombok.*; import lombok.extern.log4j.Log4j2; -import me.braydon.mc.common.EnvironmentUtils; import org.apache.commons.io.FileUtils; import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver; import org.springframework.beans.factory.annotation.Value; diff --git a/API/src/main/java/me/braydon/mc/service/MojangService.java b/API/src/main/java/me/braydon/mc/service/MojangService.java index 9477004..1e9cc65 100644 --- a/API/src/main/java/me/braydon/mc/service/MojangService.java +++ b/API/src/main/java/me/braydon/mc/service/MojangService.java @@ -438,9 +438,10 @@ public final class MojangService { throw new BadRequestException("Invalid port defined"); } } + String cacheKey = "%s-%s".formatted(platform.name(), lookupHostname.replace(":", "-")); // Check the cache for the server - Optional cached = minecraftServerCache.findById("%s-%s".formatted(platform.name(), lookupHostname)); + Optional cached = minecraftServerCache.findById(cacheKey); if (cached.isPresent()) { // Respond with the cache if present log.info("Found server in cache: {}", hostname); return cached.get(); @@ -466,7 +467,11 @@ public final class MojangService { CityResponse geo = null; // The server's Geo location try { log.info("Looking up Geo location data for {}...", ip); - geo = maxMindService.lookupCity(InetAddress.getByName(ip)); // Get the Geo location + + InetAddress address = InetAddress.getByName(ip); + if (!address.isAnyLocalAddress()) { // Get the Geo location + geo = maxMindService.lookupCity(address); + } } catch (Exception ex) { log.error("Failed looking up Geo location data for %s:".formatted(ip), ex); } @@ -481,7 +486,7 @@ public final class MojangService { } CachedMinecraftServer minecraftServer = new CachedMinecraftServer( - platform.name() + "-" + lookupHostname, response, System.currentTimeMillis() + cacheKey, response, System.currentTimeMillis() ); // Get the blocked status of the Java server