Throw ResourceNotFoundException if the hostname is invalid

This commit is contained in:
Braydon 2024-04-06 22:32:29 -04:00
parent c46c7f733a
commit f2bb432567

@ -6,6 +6,7 @@ import me.braydon.mc.RESTfulMC;
import me.braydon.mc.common.DNSUtils; import me.braydon.mc.common.DNSUtils;
import me.braydon.mc.common.packet.impl.PacketHandshakingInSetProtocol; import me.braydon.mc.common.packet.impl.PacketHandshakingInSetProtocol;
import me.braydon.mc.common.packet.impl.PacketStatusInStart; import me.braydon.mc.common.packet.impl.PacketStatusInStart;
import me.braydon.mc.exception.impl.ResourceNotFoundException;
import me.braydon.mc.model.server.JavaMinecraftServer; import me.braydon.mc.model.server.JavaMinecraftServer;
import me.braydon.mc.model.token.JavaServerStatusToken; import me.braydon.mc.model.token.JavaServerStatusToken;
import me.braydon.mc.service.pinger.MinecraftServerPinger; import me.braydon.mc.service.pinger.MinecraftServerPinger;
@ -16,6 +17,7 @@ import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException;
/** /**
* The {@link MinecraftServerPinger} for * The {@link MinecraftServerPinger} for
@ -33,9 +35,10 @@ public final class JavaMinecraftServerPinger implements MinecraftServerPinger<Ja
* @param hostname the hostname of the server * @param hostname the hostname of the server
* @param port the port of the server * @param port the port of the server
* @return the server that was pinged * @return the server that was pinged
* @throws ResourceNotFoundException if the hostname could not be resolved
*/ */
@Override @Override
public JavaMinecraftServer ping(@NonNull String hostname, int port) { public JavaMinecraftServer ping(@NonNull String hostname, int port) throws ResourceNotFoundException {
InetAddress inetAddress = DNSUtils.resolveA(hostname); // Resolve the hostname to an IP address InetAddress inetAddress = DNSUtils.resolveA(hostname); // Resolve the hostname to an IP address
String ip = inetAddress == null ? null : inetAddress.getHostAddress(); // Get the IP address String ip = inetAddress == null ? null : inetAddress.getHostAddress(); // Get the IP address
if (ip != null) { // Was the IP resolved? if (ip != null) { // Was the IP resolved?
@ -64,6 +67,9 @@ public final class JavaMinecraftServerPinger implements MinecraftServerPinger<Ja
return JavaMinecraftServer.create(hostname, ip, port, token); // Return the server return JavaMinecraftServer.create(hostname, ip, port, token); // Return the server
} }
} catch (IOException ex) { } catch (IOException ex) {
if (ex instanceof UnknownHostException) {
throw new ResourceNotFoundException("Unknown hostname: %s".formatted(hostname));
}
log.error("An error occurred pinging %s:%s:".formatted(hostname, port), ex); log.error("An error occurred pinging %s:%s:".formatted(hostname, port), ex);
} }
return null; return null;