Skip to content

Commit

Permalink
Merge branch 'master' into feat/switch-to-sql
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Nov 17, 2023
2 parents 449eb8e + 4677153 commit a6a25fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -102,7 +103,7 @@ private fun interceptPlayerConnection(
return

if (con.hasBegunConnecting) {
con.kick(KickReason.idInUse)
con.kick(KickReason.idInUse, silent = true)
return
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -199,15 +200,15 @@ 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
}

// CHECK: Duplicate connections
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
}
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit a6a25fc

Please sign in to comment.