From bbe50792572f1b63c3b312c70c72eb778e3b39b2 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 4 Jun 2021 16:20:38 +0200 Subject: [PATCH] convert more classes to kotlin --- .../network/connection/StatusConnection.kt | 5 +- .../de/bixilon/minosoft/util/DNSUtil.java | 59 ------------------- .../java/de/bixilon/minosoft/util/DNSUtil.kt | 58 ++++++++++++++++++ .../util/MinosoftCommandLineArguments.java | 1 + .../bixilon/minosoft/util/ServerAddress.java | 53 ----------------- .../de/bixilon/minosoft/util/ServerAddress.kt | 25 ++++++++ 6 files changed, 86 insertions(+), 115 deletions(-) delete mode 100644 src/main/java/de/bixilon/minosoft/util/DNSUtil.java create mode 100644 src/main/java/de/bixilon/minosoft/util/DNSUtil.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/ServerAddress.java create mode 100644 src/main/java/de/bixilon/minosoft/util/ServerAddress.kt diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/connection/StatusConnection.kt b/src/main/java/de/bixilon/minosoft/protocol/network/connection/StatusConnection.kt index 768153a0d..57838996f 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/connection/StatusConnection.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/network/connection/StatusConnection.kt @@ -35,7 +35,6 @@ import de.bixilon.minosoft.util.ServerAddress import de.bixilon.minosoft.util.logging.Log import de.bixilon.minosoft.util.logging.LogLevels import de.bixilon.minosoft.util.logging.LogMessageType -import java.util.* class StatusConnection( val address: String, @@ -46,7 +45,7 @@ class StatusConnection( lateinit var realAddress: ServerAddress private set - private var addresses: LinkedList? = null + private var addresses: List? = null var serverVersion: Version? = null @@ -56,7 +55,7 @@ class StatusConnection( if (this.addresses == null) { this.addresses = DNSUtil.getServerAddresses(address) - this.realAddress = this.addresses!!.first + this.realAddress = this.addresses!!.first() } } diff --git a/src/main/java/de/bixilon/minosoft/util/DNSUtil.java b/src/main/java/de/bixilon/minosoft/util/DNSUtil.java deleted file mode 100644 index 1e1effecb..000000000 --- a/src/main/java/de/bixilon/minosoft/util/DNSUtil.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.util; - -import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition; -import org.xbill.DNS.Record; -import org.xbill.DNS.*; - -import java.util.LinkedList; -import java.util.Locale; - -public final class DNSUtil { - - public static LinkedList getServerAddresses(String hostname) throws TextParseException { - ServerAddress fallbackAddress = getServerAddress(hostname); - LinkedList ret = new LinkedList<>(); - if (hostname.contains(":")) { - // port provided, skip srv check - ret.add(fallbackAddress); - return ret; - } - String query = "_minecraft._tcp." + hostname; - Record[] records = new Lookup(query, Type.SRV).run(); - if (records == null) { - ret.add(fallbackAddress); - return ret; - } - for (Record record : records) { - SRVRecord srvRecord = (SRVRecord) record; - ret.add(new ServerAddress(srvRecord.getTarget().toString(true), srvRecord.getPort())); - } - ret.add(fallbackAddress); - return ret; - } - - private static ServerAddress getServerAddress(String hostname) { - String[] splitHostname = hostname.split(":", 2); - if (splitHostname.length == 1) { - return new ServerAddress(splitHostname[0], ProtocolDefinition.DEFAULT_PORT); - } - return new ServerAddress(splitHostname[0], Integer.parseInt(splitHostname[1])); - } - - public static String correctHostName(String hostname) { - // replaces invalid chars to avoid copy and paste issues (like spaces, ...) - return hostname.replaceAll("\\s+|((https|http):/{2})+|/", "").toLowerCase(Locale.ROOT); - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/DNSUtil.kt b/src/main/java/de/bixilon/minosoft/util/DNSUtil.kt new file mode 100644 index 000000000..db8647db7 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/util/DNSUtil.kt @@ -0,0 +1,58 @@ +/* + * Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.util + +import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition +import org.xbill.DNS.Lookup +import org.xbill.DNS.SRVRecord +import org.xbill.DNS.Type +import java.util.* + +object DNSUtil { + fun getServerAddresses(hostname: String): List { + val fallbackAddress = getServerAddress(hostname) + val addresses: MutableList = mutableListOf() + if (hostname.contains(":")) { + // port provided, skip srv check + addresses.add(fallbackAddress) + return addresses + } + val query = "_minecraft._tcp.$hostname" + val records = Lookup(query, Type.SRV).run() + if (records == null) { + addresses.add(fallbackAddress) + return addresses + } + for (record in records) { + check(record is SRVRecord) + addresses.add(ServerAddress(record.target.toString(true), record.port)) + } + addresses.add(fallbackAddress) + return addresses + } + + private fun getServerAddress(hostname: String): ServerAddress { + val splitHostname = hostname.split(":", limit = 2).toTypedArray() + return if (splitHostname.size == 1) { + ServerAddress(splitHostname[0], ProtocolDefinition.DEFAULT_PORT) + } else { + ServerAddress(splitHostname[0], splitHostname[1].toInt()) + } + } + + @JvmStatic + fun correctHostName(hostname: String): String { + // replaces invalid chars to avoid copy and paste issues (like spaces, ...) + return hostname.replace("\\s+|((https|http):/{2})+|/".toRegex(), "").lowercase(Locale.ROOT) + } +} diff --git a/src/main/java/de/bixilon/minosoft/util/MinosoftCommandLineArguments.java b/src/main/java/de/bixilon/minosoft/util/MinosoftCommandLineArguments.java index 491cb8f42..6a19ec1a1 100644 --- a/src/main/java/de/bixilon/minosoft/util/MinosoftCommandLineArguments.java +++ b/src/main/java/de/bixilon/minosoft/util/MinosoftCommandLineArguments.java @@ -21,6 +21,7 @@ import org.apache.commons.cli.*; import javax.annotation.Nullable; import java.util.HashMap; +@Deprecated(forRemoval = true) public class MinosoftCommandLineArguments { private static final HashMap OPTION_HASH_MAP = new HashMap<>(); private static final Options OPTIONS = new Options(); diff --git a/src/main/java/de/bixilon/minosoft/util/ServerAddress.java b/src/main/java/de/bixilon/minosoft/util/ServerAddress.java deleted file mode 100644 index 4657b7c9d..000000000 --- a/src/main/java/de/bixilon/minosoft/util/ServerAddress.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.util; - -import java.util.Objects; - -public class ServerAddress { - private final String hostname; - private final int port; - - public ServerAddress(String hostname, int port) { - this.hostname = hostname; - this.port = port; - } - - @Override - public int hashCode() { - return Objects.hash(this.hostname, this.port); - } - - @Override - public boolean equals(Object obj) { - if (super.equals(obj)) { - return true; - } - ServerAddress their = (ServerAddress) obj; - return this.hostname.equals(their.getHostname()) && this.port == their.getPort(); - } - - @Override - public String toString() { - return getHostname() + ":" + getPort(); - } - - public String getHostname() { - return this.hostname; - } - - public int getPort() { - return this.port; - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/ServerAddress.kt b/src/main/java/de/bixilon/minosoft/util/ServerAddress.kt new file mode 100644 index 000000000..867c9dad9 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/util/ServerAddress.kt @@ -0,0 +1,25 @@ +/* + * Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.util + +import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition + +data class ServerAddress( + val hostname: String, + val port: Int = ProtocolDefinition.DEFAULT_PORT, +) { + + override fun toString(): String { + return "$hostname:$port" + } +}