diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/client/RESTfulMCClient.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/client/RESTfulMCClient.java index d192b2a..d8f5b6e 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/client/RESTfulMCClient.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/client/RESTfulMCClient.java @@ -25,6 +25,8 @@ package cc.restfulmc.sdk.client; import cc.restfulmc.sdk.command.impl.AsyncClientCommands; import cc.restfulmc.sdk.command.impl.SyncClientCommands; +import cc.restfulmc.sdk.response.server.dns.DNSRecord; +import cc.restfulmc.sdk.serializer.DNSRecordSerializer; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import lombok.AccessLevel; @@ -39,6 +41,7 @@ import lombok.experimental.Accessors; public final class RESTfulMCClient { public static final Gson GSON = new GsonBuilder() .serializeNulls() + .registerTypeAdapter(DNSRecord.class, new DNSRecordSerializer()) .create(); /** diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/BedrockMinecraftServer.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/BedrockMinecraftServer.java index 6ef2c30..2a2e9bd 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/BedrockMinecraftServer.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/BedrockMinecraftServer.java @@ -23,6 +23,7 @@ */ package cc.restfulmc.sdk.response.server; +import cc.restfulmc.sdk.response.server.dns.DNSRecord; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; @@ -55,10 +56,10 @@ public final class BedrockMinecraftServer extends MinecraftServer { */ @NonNull private final GameMode gamemode; - public BedrockMinecraftServer(@NonNull String hostname, String ip, int port, GeoLocation geo,@NonNull Players players, - @NonNull MOTD motd, @NonNull String id, @NonNull Edition edition, @NonNull Version version, - @NonNull GameMode gamemode) { - super(hostname, ip, port, geo, players, motd); + public BedrockMinecraftServer(@NonNull String hostname, String ip, int port, DNSRecord[] records, GeoLocation geo, + @NonNull Players players, @NonNull MOTD motd, @NonNull String id, @NonNull Edition edition, + @NonNull Version version, @NonNull GameMode gamemode) { + super(hostname, ip, port, records, geo, players, motd); this.id = id; this.edition = edition; this.version = version; diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/JavaMinecraftServer.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/JavaMinecraftServer.java index 48b30bc..eb91e64 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/JavaMinecraftServer.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/JavaMinecraftServer.java @@ -23,6 +23,7 @@ */ package cc.restfulmc.sdk.response.server; +import cc.restfulmc.sdk.response.server.dns.DNSRecord; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; @@ -107,11 +108,12 @@ public class JavaMinecraftServer extends MinecraftServer { */ private final boolean mojangBanned; - public JavaMinecraftServer(@NonNull String hostname, String ip, int port, GeoLocation geo, @NonNull Players players, - @NonNull MOTD motd, @NonNull Version version, Favicon favicon, String software, Plugin[] plugins, - ModInfo modInfo, ForgeData forgeData, String world, boolean queryEnabled, boolean previewsChat, - boolean enforcesSecureChat, boolean preventsChatReports, boolean mojangBanned) { - super(hostname, ip, port, geo, players, motd); + public JavaMinecraftServer(@NonNull String hostname, String ip, int port, DNSRecord[] records, GeoLocation geo, + @NonNull Players players, @NonNull MOTD motd, @NonNull Version version, Favicon favicon, + String software, Plugin[] plugins, ModInfo modInfo, ForgeData forgeData, String world, + boolean queryEnabled, boolean previewsChat, boolean enforcesSecureChat, boolean preventsChatReports, + boolean mojangBanned) { + super(hostname, ip, port, records, geo, players, motd); this.version = version; this.favicon = favicon; this.software = software; diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/MinecraftServer.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/MinecraftServer.java index a42228d..5f0937f 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/MinecraftServer.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/MinecraftServer.java @@ -24,6 +24,7 @@ package cc.restfulmc.sdk.response.server; import cc.restfulmc.sdk.response.CacheableResponse; +import cc.restfulmc.sdk.response.server.dns.DNSRecord; import lombok.*; import java.util.UUID; @@ -52,6 +53,11 @@ public abstract class MinecraftServer extends CacheableResponse { */ @EqualsAndHashCode.Include private final int port; + /** + * The DNS records resolved for this server, null if none. + */ + private final DNSRecord[] records; + /** * The Geo location of this server, null if unknown. */ diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/DNSRecord.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/DNSRecord.java new file mode 100644 index 0000000..e8bbd92 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/DNSRecord.java @@ -0,0 +1,63 @@ +/* + * MIT License + * + * Copyright (c) 2024 Braydon (Rainnny). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package cc.restfulmc.sdk.response.server.dns; + +import cc.restfulmc.sdk.response.server.dns.impl.ARecord; +import cc.restfulmc.sdk.response.server.dns.impl.SRVRecord; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; + +/** + * A representation of a DNS record. + * + * @author Braydon + */ +@AllArgsConstructor @Getter @ToString +public abstract class DNSRecord { + /** + * The type of this record. + */ + @NonNull private final Type type; + + /** + * The TTL (Time To Live) of this record. + */ + private final long ttl; + + /** + * Types of a record. + */ + @AllArgsConstructor @Getter + public enum Type { + A(ARecord.class), + SRV(SRVRecord.class); + + /** + * The record class for this type. + */ + @NonNull private final Class extends DNSRecord> recordClass; + } +} \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/impl/ARecord.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/impl/ARecord.java new file mode 100644 index 0000000..e2d55a7 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/impl/ARecord.java @@ -0,0 +1,47 @@ +/* + * MIT License + * + * Copyright (c) 2024 Braydon (Rainnny). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package cc.restfulmc.sdk.response.server.dns.impl; + +import cc.restfulmc.sdk.response.server.dns.DNSRecord; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; + +/** + * An A record implementation. + * + * @author Braydon + */ +@Getter @ToString(callSuper = true) +public final class ARecord extends DNSRecord { + /** + * The address of this record, null if unresolved. + */ + private final String address; + + public ARecord(@NonNull Type type, long ttl, String address) { + super(type, ttl); + this.address = address; + } +} \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/impl/SRVRecord.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/impl/SRVRecord.java new file mode 100644 index 0000000..687f74b --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/server/dns/impl/SRVRecord.java @@ -0,0 +1,65 @@ +/* + * MIT License + * + * Copyright (c) 2024 Braydon (Rainnny). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package cc.restfulmc.sdk.response.server.dns.impl; + +import cc.restfulmc.sdk.response.server.dns.DNSRecord; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; + +/** + * An SRV record implementation. + * + * @author Braydon + */ +@Getter @ToString(callSuper = true) +public final class SRVRecord extends DNSRecord { + /** + * The priority of this record. + */ + private final int priority; + + /** + * The weight of this record. + */ + private final int weight; + + /** + * The port of this record. + */ + private final int port; + + /** + * The target of this record. + */ + @NonNull private final String target; + + public SRVRecord(@NonNull Type type, long ttl, int priority, int weight, int port, @NonNull String target) { + super(type, ttl); + this.priority = priority; + this.weight = weight; + this.port = port; + this.target = target; + } +} \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/serializer/DNSRecordSerializer.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/serializer/DNSRecordSerializer.java new file mode 100644 index 0000000..702b484 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/serializer/DNSRecordSerializer.java @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2024 Braydon (Rainnny). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package cc.restfulmc.sdk.serializer; + +import cc.restfulmc.sdk.client.RESTfulMCClient; +import cc.restfulmc.sdk.response.server.dns.DNSRecord; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; + +/** + * A custom deserializer for {@link DNSRecord}'s. + *
+ * This serializer will take the "type" in a DNS + * record, and convert the record to the correct type. + * E.g: A -> ARecord, SRV -> SRVRecord, etc + *
+ * + * @author Braydon + */ +public final class DNSRecordSerializer implements JsonDeserializer