add server pinger
Some checks failed
ci / deploy (push) Failing after 1m3s

This commit is contained in:
Lee
2024-04-10 07:43:38 +01:00
parent 25c69e11e1
commit fcb8ef0357
40 changed files with 1751 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package cc.fascinated.common;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.xbill.DNS.Record;
import org.xbill.DNS.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
/**
* @author Braydon
*/
@UtilityClass
public final class DNSUtils {
private static final String SRV_QUERY_PREFIX = "_minecraft._tcp.%s";
/**
* Resolve the hostname to an {@link InetSocketAddress}.
*
* @param hostname the hostname to resolve
* @return the resolved {@link InetSocketAddress}
*/
@SneakyThrows
public static InetSocketAddress resolveSRV(@NonNull String hostname) {
Record[] records = new Lookup(SRV_QUERY_PREFIX.formatted(hostname), Type.SRV).run(); // Resolve SRV records
if (records == null) { // No records exist
return null;
}
String host = null;
int port = -1;
for (Record record : records) {
SRVRecord srv = (SRVRecord) record;
host = srv.getTarget().toString().replaceFirst("\\.$", "");
port = srv.getPort();
}
return host == null ? null : new InetSocketAddress(host, port);
}
/**
* Resolve the hostname to an {@link InetAddress}.
*
* @param hostname the hostname to resolve
* @return the resolved {@link InetAddress}
*/
@SneakyThrows
public static InetAddress resolveA(@NonNull String hostname) {
Record[] records = new Lookup(hostname, Type.A).run(); // Resolve A records
if (records == null) { // No records exist
return null;
}
InetAddress address = null;
for (Record record : records) {
address = ((ARecord) record).getAddress();
}
return address;
}
}