From 3790d4a312fde5109c68b76ca436cf9f170f4988 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 10 Apr 2024 12:26:47 +0100 Subject: [PATCH] change how the port is fetched from the hostname and lowered the timeout for server pings --- .../cc.fascinated/common/ServerUtils.java | 12 +-------- .../controller/ServerController.java | 10 +++---- .../cc.fascinated/service/ServerService.java | 27 +++++++++++++------ .../impl/BedrockMinecraftServerPinger.java | 2 +- .../impl/JavaMinecraftServerPinger.java | 4 +-- 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/main/java/cc.fascinated/common/ServerUtils.java b/src/main/java/cc.fascinated/common/ServerUtils.java index 3c0a33c..945fc48 100644 --- a/src/main/java/cc.fascinated/common/ServerUtils.java +++ b/src/main/java/cc.fascinated/common/ServerUtils.java @@ -1,21 +1,11 @@ package cc.fascinated.common; +import cc.fascinated.exception.impl.BadRequestException; import lombok.experimental.UtilityClass; @UtilityClass public class ServerUtils { - /** - * Get the hostname and port from a hostname string - * - * @param hostname the hostname string - * @return the hostname and port - */ - public static Tuple getHostnameAndPort(String hostname) { - String[] split = hostname.split(":"); - return new Tuple<>(split[0], split.length == 1 ? -1 : Integer.parseInt(split[1])); - } - /** * Gets the address of the server. * diff --git a/src/main/java/cc.fascinated/controller/ServerController.java b/src/main/java/cc.fascinated/controller/ServerController.java index 8061b55..a69c2c3 100644 --- a/src/main/java/cc.fascinated/controller/ServerController.java +++ b/src/main/java/cc.fascinated/controller/ServerController.java @@ -34,8 +34,7 @@ public class ServerController { public CachedMinecraftServer getServer( @Parameter(description = "The platform of the server", example = "java") @PathVariable String platform, @Parameter(description = "The hostname and port of the server", example = "play.hypixel.net") @PathVariable String hostname) { - Tuple host = ServerUtils.getHostnameAndPort(hostname); - return serverService.getServer(platform, host.getLeft(), host.getRight()); + return serverService.getServer(platform, hostname); } @ResponseBody @@ -43,15 +42,12 @@ public class ServerController { public ResponseEntity getServerIcon( @Parameter(description = "The hostname and port of the server", example = "play.hypixel.net") @PathVariable String hostname, @Parameter(description = "Whether to download the image") @RequestParam(required = false, defaultValue = "false") boolean download) { - Tuple host = ServerUtils.getHostnameAndPort(hostname); - hostname = host.getLeft(); - int port = host.getRight(); String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png"; return ResponseEntity.ok() .contentType(MediaType.IMAGE_PNG) - .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(ServerUtils.getAddress(hostname, port))) - .body(serverService.getServerFavicon(hostname, port)); + .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname)) + .body(serverService.getServerFavicon(hostname)); } @ResponseBody diff --git a/src/main/java/cc.fascinated/service/ServerService.java b/src/main/java/cc.fascinated/service/ServerService.java index a0994cb..fa6d33b 100644 --- a/src/main/java/cc.fascinated/service/ServerService.java +++ b/src/main/java/cc.fascinated/service/ServerService.java @@ -34,25 +34,36 @@ public class ServerService { * * @param platformName the name of the platform * @param hostname the hostname of the server - * @param port the port of the server * @return the server */ - public CachedMinecraftServer getServer(String platformName, String hostname, int port) { + public CachedMinecraftServer getServer(String platformName, String hostname) { MinecraftServer.Platform platform = EnumUtils.getEnumConstant(MinecraftServer.Platform.class, platformName.toUpperCase()); if (platform == null) { log.info("Invalid platform: {} for server {}", platformName, hostname); throw new BadRequestException("Invalid platform: %s".formatted(platformName)); } - port = port == -1 ? platform.getDefaultPort() : port; // Use the default port if the port is -1 (not provided) + int port = platform.getDefaultPort(); + if (hostname.contains(":")) { + String[] parts = hostname.split(":"); + hostname = parts[0]; + try { + port = Integer.parseInt(parts[1]); + } catch (NumberFormatException e) { + log.info("Invalid port: {} for server {}", parts[1], hostname); + throw new BadRequestException("Invalid port: %s".formatted(parts[1])); + } + } String key = "%s-%s:%s".formatted(platformName, hostname, port); - log.info("Getting server: {}:{}", hostname, port); + + // Check if the server is cached Optional cached = serverCacheRepository.findById(key); if (cached.isPresent()) { log.info("Server {}:{} is cached", hostname, port); return cached.get(); } + // Resolve the SRV record if the platform is Java InetSocketAddress address = platform == MinecraftServer.Platform.JAVA ? DNSUtils.resolveSRV(hostname) : null; if (address != null) { hostname = address.getHostName(); @@ -64,7 +75,8 @@ public class ServerService { System.currentTimeMillis() ); - if (platform == MinecraftServer.Platform.JAVA) { // Check if the server is blocked by Mojang + // Check if the server is blocked by Mojang + if (platform == MinecraftServer.Platform.JAVA) { ((JavaMinecraftServer) server.getServer()).setMojangBanned(mojangService.isServerBlocked(hostname)); } @@ -78,13 +90,12 @@ public class ServerService { * Gets the server favicon. * * @param hostname the hostname of the server - * @param port the port of the server * @return the server favicon, null if not found */ - public byte[] getServerFavicon(String hostname, int port) { + public byte[] getServerFavicon(String hostname) { String icon = null; // The server base64 icon try { - JavaMinecraftServer.Favicon favicon = ((JavaMinecraftServer) getServer(MinecraftServer.Platform.JAVA.name(), hostname, port).getServer()).getFavicon(); + JavaMinecraftServer.Favicon favicon = ((JavaMinecraftServer) getServer(MinecraftServer.Platform.JAVA.name(), hostname).getServer()).getFavicon(); if (favicon != null) { // Use the server's favicon icon = favicon.getBase64(); icon = icon.substring(icon.indexOf(",") + 1); // Remove the data type from the server icon diff --git a/src/main/java/cc.fascinated/service/pinger/impl/BedrockMinecraftServerPinger.java b/src/main/java/cc.fascinated/service/pinger/impl/BedrockMinecraftServerPinger.java index dd57e55..8ca1aa5 100644 --- a/src/main/java/cc.fascinated/service/pinger/impl/BedrockMinecraftServerPinger.java +++ b/src/main/java/cc.fascinated/service/pinger/impl/BedrockMinecraftServerPinger.java @@ -21,7 +21,7 @@ import java.net.*; */ @Log4j2(topic = "Bedrock MC Server Pinger") public final class BedrockMinecraftServerPinger implements MinecraftServerPinger { - private static final int TIMEOUT = 3000; // The timeout for the socket + private static final int TIMEOUT = 1500; // The timeout for the socket /** * Ping the server with the given hostname and port. diff --git a/src/main/java/cc.fascinated/service/pinger/impl/JavaMinecraftServerPinger.java b/src/main/java/cc.fascinated/service/pinger/impl/JavaMinecraftServerPinger.java index d8265fe..60aec37 100644 --- a/src/main/java/cc.fascinated/service/pinger/impl/JavaMinecraftServerPinger.java +++ b/src/main/java/cc.fascinated/service/pinger/impl/JavaMinecraftServerPinger.java @@ -22,9 +22,7 @@ import java.net.*; */ @Log4j2(topic = "Java Pinger") public final class JavaMinecraftServerPinger implements MinecraftServerPinger { - public static final JavaMinecraftServerPinger INSTANCE = new JavaMinecraftServerPinger(); - - private static final int TIMEOUT = 3000; // The timeout for the socket + private static final int TIMEOUT = 1500; // The timeout for the socket @Override public JavaMinecraftServer ping(String hostname, int port) {