From 4677153397bcf52b5db1ccce1bdf6212bd308fdf Mon Sep 17 00:00:00 2001 From: phinner <62483793+phinner@users.noreply.github.com> Date: Fri, 17 Nov 2023 12:04:01 +0100 Subject: [PATCH] fix: Set gatekeeper logging to silent, to avoid DDOS by log spamming --- .../mindustry/misc/PlayerExtensions.kt | 26 ++++++++++++++++++- .../mindustry/security/GatekeeperListener.kt | 22 +++++++++------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/misc/PlayerExtensions.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/misc/PlayerExtensions.kt index 41450068..87a5e671 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/misc/PlayerExtensions.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/misc/PlayerExtensions.kt @@ -25,10 +25,12 @@ import com.xpdustry.imperium.common.misc.toInetAddress import com.xpdustry.imperium.common.security.Identity import java.time.Instant import kotlin.time.Duration +import kotlin.time.Duration.Companion.minutes import mindustry.Vars import mindustry.gen.Call import mindustry.gen.Player import mindustry.net.NetConnection +import mindustry.net.Packets.KickReason import org.slf4j.event.Level val Player.identity: Identity.Mindustry @@ -39,7 +41,24 @@ val Player.joinTime: Instant fun Player.showInfoMessage(message: String) = Call.infoMessage(con, message) +fun NetConnection.kick(reason: KickReason, silent: Boolean = false) { + val duration = + if (reason == KickReason.kick || reason == KickReason.banned || reason == KickReason.vote) + 30.minutes + else Duration.ZERO + kick(reason.name, reason, duration, silent) +} + fun NetConnection.kick(reason: String, duration: Duration, silent: Boolean = false) { + kick(reason, null, duration, silent) +} + +private fun NetConnection.kick( + reason: String, + kick: KickReason?, + duration: Duration, + silent: Boolean +) { if (kicked) return logger @@ -50,7 +69,12 @@ fun NetConnection.kick(reason: String, duration: Duration, silent: Boolean = fal Vars.netServer.admins.handleKicked(uuid, address, duration.inWholeMilliseconds) } - Call.kick(this, reason) + if (kick == null) { + Call.kick(this, reason) + } else { + Call.kick(this, kick) + } + // STEAM: Will break if the connection closes now close() diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/GatekeeperListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/GatekeeperListener.kt index 23052ac5..f9cdea45 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/GatekeeperListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/GatekeeperListener.kt @@ -37,6 +37,7 @@ import com.xpdustry.imperium.mindustry.misc.runMindustryThread import fr.xpdustry.distributor.api.util.Priority import java.io.ByteArrayOutputStream import java.io.DataOutputStream +import kotlin.time.Duration import kotlinx.coroutines.launch import mindustry.Vars import mindustry.core.Version @@ -102,7 +103,7 @@ private fun interceptPlayerConnection( return if (con.hasBegunConnecting) { - con.kick(KickReason.idInUse) + con.kick(KickReason.idInUse, silent = true) return } @@ -115,7 +116,7 @@ private fun interceptPlayerConnection( con.mobile = packet.mobile if (packet.uuid == null || packet.usid == null) { - con.kick(KickReason.idInUse) + con.kick(KickReason.idInUse, silent = true) return } @@ -125,7 +126,7 @@ private fun interceptPlayerConnection( } if (Time.millis() < Vars.netServer.admins.getKickTime(packet.uuid, con.address)) { - con.kick(KickReason.recentKick) + con.kick(KickReason.recentKick, silent = true) return } @@ -153,7 +154,7 @@ private fun interceptPlayerConnection( .append("> ") .append(mods.toString("\n> ")) } - con.kick(result.toString(), 0) + con.kick(result.toString(), Duration.ZERO, silent = true) return } @@ -168,7 +169,7 @@ private fun interceptPlayerConnection( "&lcDo &lywhitelist-add {}&lc to whitelist the player &lb'{}'", packet.uuid, packet.name) - con.kick(KickReason.whitelist) + con.kick(KickReason.whitelist, silent = true) return } @@ -190,7 +191,7 @@ private fun interceptPlayerConnection( .trim() .equals(packet.name.stripMindustryColors().trim(), ignoreCase = true) }) { - con.kick(KickReason.nameInUse) + con.kick(KickReason.nameInUse, silent = true) return } @@ -199,7 +200,7 @@ private fun interceptPlayerConnection( player.uuid() == packet.uuid || player.usid() == packet.usid }) { con.uuid = packet.uuid - con.kick(KickReason.idInUse) + con.kick(KickReason.idInUse, silent = true) return } @@ -207,7 +208,7 @@ private fun interceptPlayerConnection( for (otherCon in Vars.net.connections) { if (otherCon !== con && packet.uuid == otherCon.uuid) { con.uuid = packet.uuid - con.kick(KickReason.idInUse) + con.kick(KickReason.idInUse, silent = true) return } } @@ -216,7 +217,7 @@ private fun interceptPlayerConnection( // CHECK: Empty name if (packet.name.trim().stripMindustryColors().isBlank()) { - con.kick(KickReason.nameEmpty) + con.kick(KickReason.nameEmpty, silent = true) return } @@ -229,7 +230,8 @@ private fun interceptPlayerConnection( if (packet.version != Version.build && Version.build != -1 && packet.version != -1) { con.kick( if (packet.version > Version.build) KickReason.serverOutdated - else KickReason.clientOutdated) + else KickReason.clientOutdated, + silent = true) return }