-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from KyoriPowered/feat/ingame-motd
feat: add initial server list ping implementation
- Loading branch information
Showing
8 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
/run/ | ||
/*.iml | ||
/kotlin-js-store/ | ||
/.kotlin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
...in/kotlin/net/kyori/adventure/webui/jvm/minimessage/preview/ServerStatusPreviewManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package net.kyori.adventure.webui.jvm.minimessage.preview | ||
|
||
import io.github.reactivecircus.cache4k.Cache | ||
import io.ktor.network.selector.SelectorManager | ||
import io.ktor.network.sockets.aSocket | ||
import io.ktor.network.sockets.openReadChannel | ||
import io.ktor.network.sockets.openWriteChannel | ||
import io.ktor.server.application.Application | ||
import io.ktor.utils.io.ByteReadChannel | ||
import io.ktor.utils.io.ByteWriteChannel | ||
import io.ktor.utils.io.close | ||
import io.ktor.utils.io.writeByte | ||
import io.ktor.utils.io.writeFully | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.ensureActive | ||
import kotlinx.coroutines.job | ||
import kotlinx.coroutines.launch | ||
import net.kyori.adventure.text.minimessage.MiniMessage | ||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer | ||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer | ||
import okhttp3.internal.and | ||
import org.slf4j.LoggerFactory | ||
import java.io.ByteArrayOutputStream | ||
import java.io.DataOutputStream | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.time.Duration.Companion.hours | ||
|
||
/** Manager class for previewing server status. */ | ||
public class ServerStatusPreviewManager( | ||
application: Application, | ||
) : CoroutineScope { | ||
|
||
private val logger = LoggerFactory.getLogger(ServerStatusPreviewManager::class.java) | ||
private val managerJob = SupervisorJob(application.coroutineContext.job) | ||
override val coroutineContext: CoroutineContext = application.coroutineContext + managerJob | ||
|
||
private val motdPreviews = Cache.Builder<String, String>().expireAfterAccess(1.hours).build() | ||
private val kickPreviews = Cache.Builder<String, String>().expireAfterAccess(1.hours).build() | ||
|
||
init { | ||
launch { | ||
// Initialise the socket. | ||
val serverSocket = aSocket(SelectorManager(Dispatchers.IO)).tcp().bind("0.0.0.0", 25565) | ||
logger.info("Listening for pings at ${serverSocket.localAddress}") | ||
|
||
while (true) { | ||
// Ensure we are active so that the socket is properly closed when the application ends. | ||
ensureActive() | ||
|
||
val socket = serverSocket.accept() | ||
logger.debug("Accepted socket connection from {}", socket.remoteAddress) | ||
|
||
launch { | ||
try { | ||
val receiveChannel = socket.openReadChannel() | ||
val sendChannel = socket.openWriteChannel(autoFlush = true) | ||
|
||
// handshake | ||
val handshakePacket = receiveChannel.readMcPacket() | ||
val protocolVersion = handshakePacket.readVarInt() | ||
val serverAddress = handshakePacket.readUtf8String() | ||
val serverPort = handshakePacket.readShort() | ||
val nextState = handshakePacket.readVarInt() | ||
|
||
if (nextState != 1) { | ||
// send kick | ||
sendChannel.writeMcPacket(0) { | ||
it.writeString( | ||
GsonComponentSerializer.gson() | ||
.serialize(MiniMessage.miniMessage().deserialize(lookupKickMessage(serverAddress))) | ||
) | ||
} | ||
} else { | ||
// send status response | ||
sendChannel.writeMcPacket(0) { | ||
it.writeString( | ||
"""{ | ||
"version": { | ||
"name": "${ | ||
LegacyComponentSerializer.legacySection() | ||
.serialize(MiniMessage.miniMessage().deserialize("<rainbow>MiniMessage")) | ||
}", | ||
"protocol": 1 | ||
}, | ||
"players": { | ||
"max": 0, | ||
"online": 0 | ||
}, | ||
"description": ${ | ||
GsonComponentSerializer.gson().serialize( | ||
MiniMessage.miniMessage().deserialize(lookupMotd(serverAddress)) | ||
) | ||
} | ||
}""".trimIndent() | ||
) | ||
} | ||
} | ||
|
||
sendChannel.close() | ||
} catch (e: Exception) { | ||
logger.error("An unknown error occurred whilst responding to a ping from ${socket.remoteAddress}", e) | ||
} | ||
|
||
socket.close() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun lookupKickMessage(serverAddress: String): String { | ||
return kickPreviews.get(serverAddress.split("\\.")[0]) ?: "<red>You cant join here!" | ||
} | ||
|
||
private fun lookupMotd(serverAddress: String): String { | ||
return motdPreviews.get(serverAddress.split("\\.")[0]) ?: "<rainbow>MiniMessage is cool!" | ||
} | ||
|
||
public fun initializeKickPreview(input: String): String { | ||
val key = generateRandomString() | ||
kickPreviews.put(key, input) | ||
return "$key.webui.advntr.dev" | ||
} | ||
|
||
public fun initializeMotdPreview(input: String): String { | ||
val key = generateRandomString() | ||
motdPreviews.put(key, input) | ||
return "$key.webui.advntr.dev" | ||
} | ||
|
||
private fun generateRandomString(length: Int = 8): String { | ||
val allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | ||
return (1..length) | ||
.map { allowedChars.random() } | ||
.joinToString("") | ||
} | ||
|
||
private suspend fun ByteWriteChannel.writeMcPacket(packetId: Int, consumer: (packet: DataOutputStream) -> Unit) { | ||
val stream = ByteArrayOutputStream() | ||
val packet = DataOutputStream(stream) | ||
|
||
consumer.invoke(packet) | ||
|
||
val data = stream.toByteArray() | ||
writeVarInt(data.size + 1) | ||
writeVarInt(packetId) | ||
writeFully(data) | ||
} | ||
|
||
private fun DataOutputStream.writeString(string: String) { | ||
val bytes = string.toByteArray(Charsets.UTF_8) | ||
writeVarInt(bytes.size) | ||
write(bytes) | ||
} | ||
|
||
private fun DataOutputStream.writeVarInt(int: Int) { | ||
var value = int | ||
while (true) { | ||
if ((value and 0x7F.inv()) == 0) { | ||
writeByte(value) | ||
return | ||
} | ||
|
||
writeByte((value and 0x7F) or 0x80) | ||
|
||
value = value ushr 7 | ||
} | ||
} | ||
|
||
private suspend fun ByteWriteChannel.writeVarInt(int: Int) { | ||
var value = int | ||
while (true) { | ||
if ((value and 0x7F.inv()) == 0) { | ||
writeByte(value) | ||
return | ||
} | ||
|
||
writeByte((value and 0x7F) or 0x80) | ||
|
||
value = value ushr 7 | ||
} | ||
} | ||
|
||
private suspend fun ByteReadChannel.readMcPacket(): ByteReadChannel { | ||
val length = readVarInt() | ||
val packetId = readVarInt() | ||
val data = ByteArray(length) | ||
readFully(data, 0, length) | ||
return ByteReadChannel(data) | ||
} | ||
|
||
private suspend fun ByteReadChannel.readVarInt(): Int { | ||
var value = 0 | ||
var position = 0 | ||
var currentByte: Byte | ||
|
||
while (true) { | ||
currentByte = readByte() | ||
value = value or ((currentByte and 0x7F) shl position) | ||
|
||
if ((currentByte and 0x80) == 0) break | ||
|
||
position += 7 | ||
|
||
if (position >= 32) throw RuntimeException("VarInt is too big") | ||
} | ||
|
||
return value | ||
} | ||
|
||
private suspend fun ByteReadChannel.readUtf8String(): String { | ||
val length = readVarInt() | ||
val data = ByteArray(length) | ||
readFully(data, 0, length) | ||
return String(data) | ||
} | ||
} |