add dns records to the server response
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 3m4s

This commit is contained in:
Lee
2024-04-10 16:21:07 +01:00
parent d156d2cb3b
commit 0ea69f86f9
13 changed files with 202 additions and 65 deletions

View File

@ -5,6 +5,9 @@ import cc.fascinated.common.EnumUtils;
import cc.fascinated.exception.impl.BadRequestException;
import cc.fascinated.exception.impl.ResourceNotFoundException;
import cc.fascinated.model.cache.CachedMinecraftServer;
import cc.fascinated.model.dns.DNSRecord;
import cc.fascinated.model.dns.impl.ARecord;
import cc.fascinated.model.dns.impl.SRVRecord;
import cc.fascinated.model.server.JavaMinecraftServer;
import cc.fascinated.model.server.MinecraftServer;
import cc.fascinated.repository.MinecraftServerCacheRepository;
@ -13,7 +16,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Optional;
@Service @Log4j2
@ -63,21 +68,32 @@ public class ServerService {
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();
List<DNSRecord> records = new ArrayList<>(); // The resolved DNS records for the server
SRVRecord srvRecord = platform == MinecraftServer.Platform.JAVA ? DNSUtils.resolveSRV(hostname) : null; // Resolve the SRV record
if (srvRecord != null) { // SRV was resolved, use the hostname and port
records.add(srvRecord); // Going to need this for later
InetSocketAddress socketAddress = srvRecord.getSocketAddress();
hostname = socketAddress.getHostName();
port = socketAddress.getPort();
}
ARecord aRecord = DNSUtils.resolveA(hostname); // Resolve the A record so we can get the IPv4 address
String ip = aRecord == null ? null : aRecord.getAddress(); // Get the IP address
if (ip != null) { // Was the IP resolved?
records.add(aRecord); // Going to need this for later
log.info("Resolved hostname: {} -> {}", hostname, ip);
}
CachedMinecraftServer server = new CachedMinecraftServer(
key,
platform.getPinger().ping(hostname, port),
platform.getPinger().ping(hostname, ip, port, records.toArray(new DNSRecord[0])),
System.currentTimeMillis()
);
// Check if the server is blocked by Mojang
if (platform == MinecraftServer.Platform.JAVA) {
((JavaMinecraftServer) server.getServer()).setMojangBanned(mojangService.isServerBlocked(hostname));
((JavaMinecraftServer) server.getServer()).setMojangBlocked(mojangService.isServerBlocked(hostname));
}
log.info("Found server: {}:{}", hostname, port);

View File

@ -1,5 +1,6 @@
package cc.fascinated.service.pinger;
import cc.fascinated.model.dns.DNSRecord;
import cc.fascinated.model.server.MinecraftServer;
/**
@ -7,5 +8,5 @@ import cc.fascinated.model.server.MinecraftServer;
* @param <T> the type of server to ping
*/
public interface MinecraftServerPinger<T extends MinecraftServer> {
T ping(String hostname, int port);
T ping(String hostname, String ip, int port, DNSRecord[] records);
}

View File

@ -1,17 +1,19 @@
package cc.fascinated.service.pinger.impl;
import cc.fascinated.common.DNSUtils;
import cc.fascinated.common.packet.impl.bedrock.BedrockPacketUnconnectedPing;
import cc.fascinated.common.packet.impl.bedrock.BedrockPacketUnconnectedPong;
import cc.fascinated.exception.impl.BadRequestException;
import cc.fascinated.exception.impl.ResourceNotFoundException;
import cc.fascinated.model.dns.DNSRecord;
import cc.fascinated.model.server.BedrockMinecraftServer;
import cc.fascinated.service.pinger.MinecraftServerPinger;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import java.io.IOException;
import java.net.*;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
/**
* The {@link MinecraftServerPinger} for pinging
@ -31,12 +33,7 @@ public final class BedrockMinecraftServerPinger implements MinecraftServerPinger
* @return the server that was pinged
*/
@Override
public BedrockMinecraftServer ping(@NonNull String hostname, int port) {
InetAddress inetAddress = DNSUtils.resolveA(hostname); // Resolve the hostname to an IP address
String ip = inetAddress == null ? null : inetAddress.getHostAddress(); // Get the IP address
if (ip != null) { // Was the IP resolved?
log.info("Resolved hostname: {} -> {}", hostname, ip);
}
public BedrockMinecraftServer ping(String hostname, String ip, int port, DNSRecord[] records) {
log.info("Pinging {}:{}...", hostname, port);
long before = System.currentTimeMillis(); // Timestamp before pinging
@ -58,7 +55,7 @@ public final class BedrockMinecraftServerPinger implements MinecraftServerPinger
if (response == null) { // No pong response
throw new ResourceNotFoundException("Server didn't respond to ping");
}
return BedrockMinecraftServer.create(hostname, ip, port, response); // Return the server
return BedrockMinecraftServer.create(hostname, ip, port, records, response); // Return the server
} catch (IOException ex) {
if (ex instanceof UnknownHostException) {
throw new BadRequestException("Unknown hostname: %s".formatted(hostname));

View File

@ -1,13 +1,13 @@
package cc.fascinated.service.pinger.impl;
import cc.fascinated.Main;
import cc.fascinated.common.DNSUtils;
import cc.fascinated.common.JavaMinecraftVersion;
import cc.fascinated.common.ServerUtils;
import cc.fascinated.common.packet.impl.java.JavaPacketHandshakingInSetProtocol;
import cc.fascinated.common.packet.impl.java.JavaPacketStatusInStart;
import cc.fascinated.exception.impl.BadRequestException;
import cc.fascinated.exception.impl.ResourceNotFoundException;
import cc.fascinated.model.dns.DNSRecord;
import cc.fascinated.model.server.JavaMinecraftServer;
import cc.fascinated.model.token.JavaServerStatusToken;
import cc.fascinated.service.pinger.MinecraftServerPinger;
@ -26,12 +26,7 @@ public final class JavaMinecraftServerPinger implements MinecraftServerPinger<Ja
private static final int TIMEOUT = 1500; // The timeout for the socket
@Override
public JavaMinecraftServer ping(String hostname, int port) {
InetAddress inetAddress = DNSUtils.resolveA(hostname); // Resolve the hostname to an IP address
String ip = inetAddress == null ? null : inetAddress.getHostAddress(); // Get the IP address
if (ip != null) { // Was the IP resolved?
log.info("Resolved hostname: {} -> {}", hostname, ip);
}
public JavaMinecraftServer ping(String hostname, String ip, int port, DNSRecord[] records) {
log.info("Pinging {}:{}...", hostname, port);
// Open a socket connection to the server
@ -49,7 +44,7 @@ public final class JavaMinecraftServerPinger implements MinecraftServerPinger<Ja
JavaPacketStatusInStart packetStatusInStart = new JavaPacketStatusInStart();
packetStatusInStart.process(inputStream, outputStream);
JavaServerStatusToken token = Main.GSON.fromJson(packetStatusInStart.getResponse(), JavaServerStatusToken.class);
return JavaMinecraftServer.create(hostname, ip, port, token);
return JavaMinecraftServer.create(hostname, ip, port, records, token);
}
} catch (IOException ex) {
if (ex instanceof UnknownHostException) {