diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/Configuration.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/Configuration.kt index d9312096..6630e31d 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/Configuration.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/Configuration.kt @@ -1,5 +1,6 @@ package io.shulkermc.proxyagent +import io.shulkermc.proxyagent.utils.addressFromHostString import java.net.InetSocketAddress import java.util.Optional import java.util.UUID @@ -18,15 +19,7 @@ object Configuration { .getOrDefault(15) val PROXY_PREFERRED_RECONNECT_ADDRESS: Optional = getOptionalStringEnv("SHULKER_PROXY_PREFERRED_RECONNECT_ADDRESS") - .map { - val parts = it.split(":") - - if (parts.size == 2) { - InetSocketAddress(parts[0], parts[1].toInt()) - } else { - InetSocketAddress(parts[0], 25565) - } - } + .map { str -> addressFromHostString(str) } val NETWORK_ADMINS: List = getOptionalStringEnv("SHULKER_NETWORK_ADMINS") diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/filesystem/LocalFileSystemAdapter.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/filesystem/LocalFileSystemAdapter.kt index aaba9d85..20a5bd73 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/filesystem/LocalFileSystemAdapter.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/filesystem/LocalFileSystemAdapter.kt @@ -1,11 +1,11 @@ package io.shulkermc.proxyagent.adapters.filesystem +import io.shulkermc.proxyagent.utils.addressFromHostString import org.apache.commons.io.monitor.FileAlterationListenerAdaptor import org.apache.commons.io.monitor.FileAlterationMonitor import org.apache.commons.io.monitor.FileAlterationObserver import org.yaml.snakeyaml.Yaml import java.io.File -import java.net.InetSocketAddress import java.nio.file.Files import java.nio.file.Path import kotlin.io.path.exists @@ -69,16 +69,10 @@ class LocalFileSystemAdapter : FileSystemAdapter { val address = entry["address"] as String val tags = entry.getOrDefault("tags", emptyList()) as List - val addressParts = address.split(":") - name to FileSystemAdapter.ExternalServer( name, - if (addressParts.size == 2) { - InetSocketAddress(addressParts[0], addressParts[1].toInt()) - } else { - InetSocketAddress(address, 25565) - }, + addressFromHostString(address), tags.toSet(), ) } diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/kubernetes/ImplKubernetesGatewayAdapter.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/kubernetes/ImplKubernetesGatewayAdapter.kt index 83c5acd0..097d1bbf 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/kubernetes/ImplKubernetesGatewayAdapter.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/kubernetes/ImplKubernetesGatewayAdapter.kt @@ -10,6 +10,7 @@ import io.fabric8.kubernetes.client.informers.SharedIndexInformer import io.fabric8.kubernetes.client.okhttp.OkHttpClientFactory import io.shulkermc.proxyagent.Configuration import io.shulkermc.proxyagent.adapters.kubernetes.models.AgonesV1GameServer +import io.shulkermc.proxyagent.utils.addressFromHostString import java.net.InetSocketAddress import java.util.Optional import java.util.concurrent.CompletionStage @@ -107,7 +108,7 @@ class ImplKubernetesGatewayAdapter(proxyNamespace: String, proxyName: String) : private fun getExternalAddressFromService(service: Service): Optional { return if (service.spec.type == "LoadBalancer") { Optional.ofNullable(service.status.loadBalancer?.ingress?.firstOrNull()) - .map { ingress -> InetSocketAddress(ingress.ip, MINECRAFT_DEFAULT_PORT) } + .map { ingress -> addressFromHostString(ingress.ip) } } else { Optional.empty() } diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/services/ServerDirectoryService.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/services/ServerDirectoryService.kt index 79657929..f46398ff 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/services/ServerDirectoryService.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/services/ServerDirectoryService.kt @@ -5,16 +5,13 @@ import io.shulkermc.proxyagent.ShulkerProxyAgentCommon import io.shulkermc.proxyagent.adapters.filesystem.FileSystemAdapter import io.shulkermc.proxyagent.adapters.kubernetes.WatchAction import io.shulkermc.proxyagent.adapters.kubernetes.models.AgonesV1GameServer +import io.shulkermc.proxyagent.utils.addressFromHostString import java.net.InetSocketAddress import java.util.Optional class ServerDirectoryService( private val agent: ShulkerProxyAgentCommon, ) { - companion object { - private const val DEFAULT_MINECRAFT_PORT = 25565 - } - private val serversByTag = HashMap>() private val tagsByServer = HashMap>() @@ -52,9 +49,8 @@ class ServerDirectoryService( this.registerServer( minecraftServer.metadata.name, - InetSocketAddress( + addressFromHostString( "${minecraftServer.metadata.name}.${Configuration.CLUSTER_NAME}-cluster.${minecraftServer.metadata.namespace}", - DEFAULT_MINECRAFT_PORT, ), tags?.split(",")?.toSet().orEmpty(), ) diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/utils/InetSocketAddressExt.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/utils/InetSocketAddressExt.kt new file mode 100644 index 00000000..f9a9114e --- /dev/null +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/utils/InetSocketAddressExt.kt @@ -0,0 +1,16 @@ +package io.shulkermc.proxyagent.utils + +import java.net.InetSocketAddress + +fun addressFromHostString( + hostAndPort: String, + defaultPort: Int = 25565, +): InetSocketAddress { + val parts = hostAndPort.split(":") + + return if (parts.size == 2) { + InetSocketAddress(parts[0], parts[1].toInt()) + } else { + InetSocketAddress(parts[0], defaultPort) + } +}