dev_minimal_login (#7)
### Summary Implement minimal login flow without any authentication. Implement basic game server module with JoingGamePacket implemented. ### Type of Change New feature (non-breaking change which adds functionality) ### Affected Modules - [x] common/ - [ ] protocol/ - [ ] protocol.tls/ - [x] protocol.login/ - [ ] server/ - [x] server.login/ - [ ] keys/ - [ ] patcher/ - [ ] buildSrc/ ### Additional Notes _No response_ Reviewed-on: https://forge.fiereu.de/OpenMMO/OpenMMO/pulls/7 Co-authored-by: fiereu <contact@fiereu.de> Co-committed-by: fiereu <contact@fiereu.de>
This commit is contained in:
1 parent
9fabe28642
commit
a9bb88b246
33 files changed
+904
-84
No files matched your search
@@ -41,8 +41,10 @@ body:
|
|||||||
- label: "protocol/"
|
- label: "protocol/"
|
||||||
- label: "protocol.tls/"
|
- label: "protocol.tls/"
|
||||||
- label: "protocol.login/"
|
- label: "protocol.login/"
|
||||||
|
- label: "protocol.game/"
|
||||||
- label: "server/"
|
- label: "server/"
|
||||||
- label: "server.login/"
|
- label: "server.login/"
|
||||||
|
- label: "server.game/"
|
||||||
- label: "keys/"
|
- label: "keys/"
|
||||||
- label: "patcher/"
|
- label: "patcher/"
|
||||||
- label: "buildSrc/"
|
- label: "buildSrc/"
|
||||||
|
|||||||
@@ -21,3 +21,13 @@ fun loadEnvFile(fileName: String) {
|
|||||||
throw IllegalArgumentException("Env file '$fileName' does not exist.")
|
throw IllegalArgumentException("Env file '$fileName' does not exist.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasks.register("runAll") {
|
||||||
|
group = "application"
|
||||||
|
description = "Runs all servers and the patcher"
|
||||||
|
listOf(
|
||||||
|
":server.login:run",
|
||||||
|
":server.game:run",
|
||||||
|
":patcher:run"
|
||||||
|
).forEach { dependsOn(it) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package de.fiereu.openmmo.common.enums
|
||||||
|
|
||||||
|
enum class Arch {
|
||||||
|
X86,
|
||||||
|
ARM,
|
||||||
|
RISCV,
|
||||||
|
LOONGARCH;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun from(ordinal: Int): Arch {
|
||||||
|
return entries.firstOrNull { it.ordinal == ordinal } ?: throw IllegalArgumentException("Unknown arch: $ordinal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package de.fiereu.openmmo.common.enums
|
||||||
|
|
||||||
|
enum class Bitness {
|
||||||
|
_32,
|
||||||
|
_64,
|
||||||
|
_128;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun from(ordinal: Int): Bitness {
|
||||||
|
return entries.firstOrNull { it.ordinal == ordinal } ?: throw IllegalArgumentException("Unknown bitness: $ordinal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package de.fiereu.openmmo.common.enums
|
||||||
|
|
||||||
|
enum class Platform {
|
||||||
|
WINDOWS,
|
||||||
|
LINUX,
|
||||||
|
MACOS,
|
||||||
|
ANDROID,
|
||||||
|
IOS,
|
||||||
|
UNKNOWN;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun from(value: Int): Platform {
|
||||||
|
return when (value) {
|
||||||
|
0x00 -> WINDOWS
|
||||||
|
0x01 -> LINUX
|
||||||
|
0x02 -> MACOS
|
||||||
|
0x03 -> IOS
|
||||||
|
0x04 -> ANDROID
|
||||||
|
else -> UNKNOWN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,3 +9,13 @@ services:
|
|||||||
POSTGRES_PASSWORD: $LOGIN_DB_PASSWORD
|
POSTGRES_PASSWORD: $LOGIN_DB_PASSWORD
|
||||||
ports:
|
ports:
|
||||||
- $LOGIN_DB_PORT:5432
|
- $LOGIN_DB_PORT:5432
|
||||||
|
game-db:
|
||||||
|
image: postgres:latest
|
||||||
|
container_name: login-db
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: $GAME_DB_NAME
|
||||||
|
POSTGRES_USER: $GAME_DB_USER
|
||||||
|
POSTGRES_PASSWORD: $GAME_DB_PASSWORD
|
||||||
|
ports:
|
||||||
|
- $GAME_DB_PORT:5432
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
plugins {
|
||||||
|
id("buildsrc.convention.kotlin-jvm")
|
||||||
|
id("buildsrc.convention.spotless")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api(project(":protocol"))
|
||||||
|
api(project(":common"))
|
||||||
|
|
||||||
|
testImplementation(libs.bundles.kotest)
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package de.fiereu.openmmo.protocols.game
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.protocols.Protocol
|
||||||
|
import de.fiereu.openmmo.protocols.incomingPacket
|
||||||
|
import de.fiereu.openmmo.protocols.outgoingPacket
|
||||||
|
import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket
|
||||||
|
import de.fiereu.openmmo.protocols.game.packets.JoinPacketDeserializer
|
||||||
|
import de.fiereu.openmmo.protocols.game.packets.JoinPacketSerializer
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Game Protocol implementation. This protocol handles in-game communication after the TLS handshake is
|
||||||
|
* complete.
|
||||||
|
*/
|
||||||
|
abstract class GameProtocol : Protocol() {
|
||||||
|
override val async: Boolean
|
||||||
|
get() = true
|
||||||
|
}
|
||||||
|
|
||||||
|
class GameServerProtocol : GameProtocol() {
|
||||||
|
init {
|
||||||
|
incomingPacket(0x01u, JoinPacketDeserializer())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class GameClientProtocol : GameProtocol() {
|
||||||
|
init {
|
||||||
|
outgoingPacket(0x01u, JoinPacketSerializer(), JoinGamePacket::class)
|
||||||
|
}
|
||||||
|
}
|
||||||
+177
@@ -0,0 +1,177 @@
|
|||||||
|
package de.fiereu.openmmo.protocols.game.packets
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.common.enums.Arch
|
||||||
|
import de.fiereu.openmmo.common.enums.Bitness
|
||||||
|
import de.fiereu.openmmo.common.enums.Platform
|
||||||
|
import de.fiereu.openmmo.protocols.PacketDeserializer
|
||||||
|
import de.fiereu.openmmo.protocols.PacketSerializer
|
||||||
|
import de.fiereu.openmmo.protocols.readUtf16LE
|
||||||
|
import de.fiereu.openmmo.protocols.writeUtf16LE
|
||||||
|
import io.netty.buffer.ByteBuf
|
||||||
|
|
||||||
|
data class RomInfo(
|
||||||
|
val code: String,
|
||||||
|
val id: Byte,
|
||||||
|
val type: Byte
|
||||||
|
)
|
||||||
|
|
||||||
|
abstract class GameAuthenticationData
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used when a player reconnects to the game server after a disconnection.
|
||||||
|
* This data typically includes the player's unique identifier and a session key
|
||||||
|
* that was issued by the login server to re-establish the session.
|
||||||
|
*/
|
||||||
|
data class ReconnectAuthData(
|
||||||
|
val playerId: Long,
|
||||||
|
val sessionKey: ByteArray
|
||||||
|
) : GameAuthenticationData()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used when a player is joining the game server for the first time after logging in.
|
||||||
|
* This data typically includes the user's unique identifier and a session key
|
||||||
|
* that was issued by the login server to authenticate the session.
|
||||||
|
*/
|
||||||
|
data class NewAuthData(
|
||||||
|
val userId: Int,
|
||||||
|
val sessionKey: ByteArray,
|
||||||
|
) : GameAuthenticationData()
|
||||||
|
|
||||||
|
data class JoinGamePacket(
|
||||||
|
val authData: GameAuthenticationData,
|
||||||
|
val mac: ByteArray,
|
||||||
|
val clientRevision: Int,
|
||||||
|
val installationRevision: Int,
|
||||||
|
val currentChatLanguage: Byte,
|
||||||
|
val chatLanguages: Short,
|
||||||
|
val matchmakingLanguages: Short,
|
||||||
|
val romMask: Byte,
|
||||||
|
val roms: List<RomInfo>,
|
||||||
|
val clientInfo: Map<Byte, String>,
|
||||||
|
val platform: Platform,
|
||||||
|
val arch: Arch,
|
||||||
|
val bitness: Bitness,
|
||||||
|
val unk1: ByteArray
|
||||||
|
)
|
||||||
|
|
||||||
|
class JoinPacketSerializer : PacketSerializer<JoinGamePacket> {
|
||||||
|
override fun serialize(packet: JoinGamePacket, buffer: ByteBuf) {
|
||||||
|
when (packet.authData) {
|
||||||
|
is NewAuthData -> {
|
||||||
|
buffer.writeByte(0x00)
|
||||||
|
buffer.writeIntLE(packet.authData.userId)
|
||||||
|
buffer.writeByte(packet.authData.sessionKey.size)
|
||||||
|
buffer.writeBytes(packet.authData.sessionKey)
|
||||||
|
}
|
||||||
|
is ReconnectAuthData -> {
|
||||||
|
buffer.writeByte(0x01)
|
||||||
|
buffer.writeLongLE(packet.authData.playerId)
|
||||||
|
buffer.writeByte(packet.authData.sessionKey.size)
|
||||||
|
buffer.writeBytes(packet.authData.sessionKey)
|
||||||
|
}
|
||||||
|
else -> throw IllegalArgumentException("Unsupported authentication data type: ${packet.authData::class.simpleName}")
|
||||||
|
}
|
||||||
|
buffer.writeBytes(packet.mac)
|
||||||
|
buffer.writeIntLE(packet.clientRevision)
|
||||||
|
buffer.writeIntLE(packet.installationRevision)
|
||||||
|
buffer.writeByte(packet.currentChatLanguage.toInt())
|
||||||
|
buffer.writeShortLE(packet.chatLanguages.toInt())
|
||||||
|
buffer.writeShortLE(packet.matchmakingLanguages.toInt())
|
||||||
|
buffer.writeByte(packet.romMask.toInt())
|
||||||
|
|
||||||
|
buffer.writeByte(packet.roms.size)
|
||||||
|
for (rom in packet.roms) {
|
||||||
|
buffer.writeUtf16LE(rom.code)
|
||||||
|
buffer.writeByte(rom.id.toInt())
|
||||||
|
buffer.writeByte(rom.type.toInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.writeByte(packet.clientInfo.size)
|
||||||
|
for ((key, value) in packet.clientInfo) {
|
||||||
|
buffer.writeByte(key.toInt())
|
||||||
|
buffer.writeUtf16LE(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.writeByte(packet.platform.ordinal)
|
||||||
|
buffer.writeByte(packet.arch.ordinal)
|
||||||
|
buffer.writeByte(packet.bitness.ordinal)
|
||||||
|
|
||||||
|
buffer.writeByte(packet.unk1.size)
|
||||||
|
buffer.writeBytes(packet.unk1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JoinPacketDeserializer : PacketDeserializer<JoinGamePacket> {
|
||||||
|
override fun deserialize(buffer: ByteBuf): JoinGamePacket {
|
||||||
|
val authOption = buffer.readByte()
|
||||||
|
val authenticationData = when (authOption) {
|
||||||
|
0x00.toByte() -> {
|
||||||
|
val userId = buffer.readIntLE()
|
||||||
|
val sessionKeyLength = buffer.readUnsignedByte().toInt()
|
||||||
|
val sessionKey = ByteArray(sessionKeyLength)
|
||||||
|
buffer.readBytes(sessionKey)
|
||||||
|
NewAuthData(userId, sessionKey)
|
||||||
|
}
|
||||||
|
0x01.toByte() -> {
|
||||||
|
val playerId = buffer.readLongLE()
|
||||||
|
val sessionKeyLength = buffer.readUnsignedByte().toInt()
|
||||||
|
val sessionKey = ByteArray(sessionKeyLength)
|
||||||
|
buffer.readBytes(sessionKey)
|
||||||
|
ReconnectAuthData(playerId, sessionKey)
|
||||||
|
}
|
||||||
|
else -> throw IllegalArgumentException("Unsupported authentication option: $authOption")
|
||||||
|
}
|
||||||
|
|
||||||
|
val mac = ByteArray(6)
|
||||||
|
buffer.readBytes(mac)
|
||||||
|
|
||||||
|
val clientRevision = buffer.readIntLE()
|
||||||
|
val installationRevision = buffer.readIntLE()
|
||||||
|
val currentChatLanguage = buffer.readByte()
|
||||||
|
val chatLanguages = buffer.readShortLE()
|
||||||
|
val ignoreLanguages = buffer.readShortLE()
|
||||||
|
val romMask = buffer.readByte()
|
||||||
|
|
||||||
|
val romCount = buffer.readUnsignedByte().toInt()
|
||||||
|
val roms = mutableListOf<RomInfo>()
|
||||||
|
for (i in 0 until romCount) {
|
||||||
|
val code = buffer.readUtf16LE()
|
||||||
|
val id = buffer.readByte()
|
||||||
|
val type = buffer.readByte()
|
||||||
|
roms.add(RomInfo(code, id, type))
|
||||||
|
}
|
||||||
|
|
||||||
|
val clientInfoCount = buffer.readUnsignedByte().toInt()
|
||||||
|
val clientInfo = mutableMapOf<Byte, String>()
|
||||||
|
for (i in 0 until clientInfoCount) {
|
||||||
|
val key = buffer.readByte()
|
||||||
|
val value = buffer.readUtf16LE()
|
||||||
|
clientInfo[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
val platform = Platform.from(buffer.readByte().toInt())
|
||||||
|
val arch = Arch.from(buffer.readByte().toInt())
|
||||||
|
val bitness = Bitness.from(buffer.readByte().toInt())
|
||||||
|
|
||||||
|
val unk1 = buffer.readByte().toInt()
|
||||||
|
val unk2 = ByteArray(unk1)
|
||||||
|
buffer.readBytes(unk2)
|
||||||
|
|
||||||
|
return JoinGamePacket(
|
||||||
|
authenticationData,
|
||||||
|
mac,
|
||||||
|
clientRevision,
|
||||||
|
installationRevision,
|
||||||
|
currentChatLanguage,
|
||||||
|
chatLanguages,
|
||||||
|
ignoreLanguages,
|
||||||
|
romMask,
|
||||||
|
roms,
|
||||||
|
clientInfo,
|
||||||
|
platform,
|
||||||
|
arch,
|
||||||
|
bitness,
|
||||||
|
unk2
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,34 +54,34 @@ abstract class LoginProtocol() : Protocol() {
|
|||||||
|
|
||||||
class LoginServerProtocol() : LoginProtocol() {
|
class LoginServerProtocol() : LoginProtocol() {
|
||||||
init {
|
init {
|
||||||
outgoingPacket(0x01u, LoginResponsePacket::class, LoginResponsePacketSerializer())
|
outgoingPacket(0x01u, LoginResponsePacketSerializer(), LoginResponsePacket::class)
|
||||||
incomingPacket(0x02u, RequestGameServerListPacketDeserializer())
|
incomingPacket(0x02u, RequestGameServerListPacketDeserializer())
|
||||||
incomingPacket(0x03u, JoinGameServerPacketDeserializer())
|
incomingPacket(0x03u, JoinGameServerPacketDeserializer())
|
||||||
outgoingPacket(0x03u, GameServerNodesPacket::class, GameServerNodesPacketSerializer())
|
outgoingPacket(0x03u, GameServerNodesPacketSerializer(), GameServerNodesPacket::class)
|
||||||
incomingPacket(0x04u, ToSConfirmationPacketDeserializer())
|
incomingPacket(0x04u, ToSConfirmationPacketDeserializer())
|
||||||
outgoingPacket(0x05u, LoginKickPacket::class, LoginKickPacketSerializer())
|
outgoingPacket(0x05u, LoginKickPacketSerializer(), LoginKickPacket::class)
|
||||||
outgoingPacket(0x07u, SentCredentialsPacket::class, SentCredentialsPacketSerializer())
|
outgoingPacket(0x07u, SentCredentialsPacketSerializer(), SentCredentialsPacket::class)
|
||||||
outgoingPacket(0x08u, MfaChallengePacket::class, MfaChallengePacketSerializer())
|
outgoingPacket(0x08u, MfaChallengePacketSerializer(), MfaChallengePacket::class)
|
||||||
incomingPacket(0x08u, MfaResponsePacketDeserializer())
|
incomingPacket(0x08u, MfaResponsePacketDeserializer())
|
||||||
incomingPacket(0x11u, LoginRequestPacketDeserializer())
|
incomingPacket(0x11u, LoginRequestPacketDeserializer())
|
||||||
outgoingPacket(0x14u, ToSPacket::class, ToSPacketSerializer())
|
outgoingPacket(0x14u, ToSPacketSerializer(), ToSPacket::class)
|
||||||
outgoingPacket(0x22u, GameServerListPacket::class, GameServerListPacketSerializer())
|
outgoingPacket(0x22u, GameServerListPacketSerializer(), GameServerListPacket::class)
|
||||||
outgoingPacket(0x26u, ExistingSessionPacket::class, SentExistingSessionPacketSerializer())
|
outgoingPacket(0x26u, SentExistingSessionPacketSerializer(), ExistingSessionPacket::class)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LoginClientProtocol() : LoginProtocol() {
|
class LoginClientProtocol() : LoginProtocol() {
|
||||||
init {
|
init {
|
||||||
incomingPacket(0x01u, LoginResponsePacketDeserializer())
|
incomingPacket(0x01u, LoginResponsePacketDeserializer())
|
||||||
outgoingPacket(0x02u, RequestGameServerListPacket::class, RequestGameServerListPacketSerializer())
|
outgoingPacket(0x02u, RequestGameServerListPacketSerializer(), RequestGameServerListPacket::class)
|
||||||
outgoingPacket(0x03u, JoinGameServerPacket::class, JoinGameServerPacketSerializer())
|
outgoingPacket(0x03u, JoinGameServerPacketSerializer(), JoinGameServerPacket::class)
|
||||||
incomingPacket(0x03u, GameServerNodesPacketDeserializer())
|
incomingPacket(0x03u, GameServerNodesPacketDeserializer())
|
||||||
outgoingPacket(0x04u, ToSConfirmationPacket::class, ToSConfirmationPacketSerializer())
|
outgoingPacket(0x04u, ToSConfirmationPacketSerializer(), ToSConfirmationPacket::class)
|
||||||
incomingPacket(0x05u, LoginKickPacketDeserializer())
|
incomingPacket(0x05u, LoginKickPacketDeserializer())
|
||||||
incomingPacket(0x07u, SentCredentialsPacketDeserializer())
|
incomingPacket(0x07u, SentCredentialsPacketDeserializer())
|
||||||
incomingPacket(0x08u, MfaChallengePacketDeserializer())
|
incomingPacket(0x08u, MfaChallengePacketDeserializer())
|
||||||
outgoingPacket(0x08u, MfaResponsePacket::class, MfaResponsePacketSerializer())
|
outgoingPacket(0x08u, MfaResponsePacketSerializer(), MfaResponsePacket::class)
|
||||||
outgoingPacket(0x11u, LoginRequestPacket::class, LoginRequestPacketSerializer())
|
outgoingPacket(0x11u, LoginRequestPacketSerializer(), LoginRequestPacket::class)
|
||||||
incomingPacket(0x14u, ToSPacketDeserializer())
|
incomingPacket(0x14u, ToSPacketDeserializer())
|
||||||
incomingPacket(0x22u, GameServerListPacketDeserializer())
|
incomingPacket(0x22u, GameServerListPacketDeserializer())
|
||||||
incomingPacket(0x26u, SentExistingSessionPacketDeserializer())
|
incomingPacket(0x26u, SentExistingSessionPacketDeserializer())
|
||||||
|
|||||||
+49
-47
@@ -12,22 +12,33 @@ import de.fiereu.openmmo.protocols.writeIpLE
|
|||||||
import de.fiereu.openmmo.protocols.writeUtf16LE
|
import de.fiereu.openmmo.protocols.writeUtf16LE
|
||||||
import io.netty.buffer.ByteBuf
|
import io.netty.buffer.ByteBuf
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nodes are address entries for game servers that the client can connect to.
|
||||||
|
* The nodes are selected by a weighted random algorithm on the client side.
|
||||||
|
* The weight is a value between 0 and 255, where 0 means the node
|
||||||
|
* will never be selected and 255 means the node will always be selected.
|
||||||
|
* Typically, nodes are assigned weights based on their capacity or priority.
|
||||||
|
*/
|
||||||
data class GameServerNode(
|
data class GameServerNode(
|
||||||
val id: UByte,
|
|
||||||
val iPv4Address: IPv4Address,
|
val iPv4Address: IPv4Address,
|
||||||
val iPv6Address: IPv6Address,
|
val iPv6Address: IPv6Address,
|
||||||
val port: UShort = 7777u
|
val port: UShort = 7777u,
|
||||||
|
val weight: UByte
|
||||||
|
)
|
||||||
|
|
||||||
|
data class GameServerData(
|
||||||
|
val gameServerId: UByte,
|
||||||
|
val userId: Int,
|
||||||
|
val sessionToken: ByteArray,
|
||||||
|
val localAddress: IPAddress,
|
||||||
|
val localHostname: String,
|
||||||
|
val port: UShort,
|
||||||
)
|
)
|
||||||
|
|
||||||
data class GameServerNodesPacket(
|
data class GameServerNodesPacket(
|
||||||
val loginState: LoginState,
|
val loginState: LoginState,
|
||||||
val gameServerId: UByte,
|
val gameServerData: GameServerData? = null,
|
||||||
val userId: Int,
|
val nodes: List<GameServerNode> = emptyList()
|
||||||
val sessionToken: ByteArray,
|
|
||||||
val localAddress: IPAddress = IPAddress.of("127.0.0.1"),
|
|
||||||
val localHostname: String = "localhost",
|
|
||||||
val port: UShort = 7777u,
|
|
||||||
val nodes: List<GameServerNode>
|
|
||||||
)
|
)
|
||||||
|
|
||||||
class GameServerNodesPacketSerializer : PacketSerializer<GameServerNodesPacket> {
|
class GameServerNodesPacketSerializer : PacketSerializer<GameServerNodesPacket> {
|
||||||
@@ -37,48 +48,35 @@ class GameServerNodesPacketSerializer : PacketSerializer<GameServerNodesPacket>
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.writeIntLE(packet.userId)
|
val gsd = requireNotNull(packet.gameServerData) { "GameServerData must be provided for AUTHED state" }
|
||||||
buffer.writeByte(packet.sessionToken.size)
|
|
||||||
buffer.writeBytes(packet.sessionToken)
|
|
||||||
|
|
||||||
buffer.writeByte(packet.gameServerId.toInt())
|
buffer.writeIntLE(gsd.userId)
|
||||||
|
buffer.writeByte(gsd.sessionToken.size)
|
||||||
|
buffer.writeBytes(gsd.sessionToken)
|
||||||
|
|
||||||
val localAddressBytes = packet.localAddress.toLittleEndianArray()
|
buffer.writeByte(gsd.gameServerId.toInt())
|
||||||
|
|
||||||
|
val localAddressBytes = gsd.localAddress.toBigEndianArray()
|
||||||
buffer.writeByte(localAddressBytes.size)
|
buffer.writeByte(localAddressBytes.size)
|
||||||
buffer.writeBytes(localAddressBytes)
|
buffer.writeBytes(localAddressBytes)
|
||||||
buffer.writeUtf16LE(packet.localHostname)
|
buffer.writeUtf16LE(gsd.localHostname)
|
||||||
|
|
||||||
buffer.writeShortLE(packet.port.toInt())
|
buffer.writeIntLE(gsd.port.toInt())
|
||||||
|
|
||||||
buffer.writeByte(packet.nodes.size)
|
buffer.writeByte(packet.nodes.size)
|
||||||
for (node in packet.nodes) {
|
for ((index, node) in packet.nodes.withIndex()) {
|
||||||
buffer.writeByte(0) // padding
|
buffer.writeByte(index)
|
||||||
|
|
||||||
buffer.writeIpLE(node.iPv4Address)
|
buffer.writeIpLE(node.iPv4Address)
|
||||||
buffer.writeIpLE(node.iPv6Address)
|
buffer.writeIpLE(node.iPv6Address)
|
||||||
|
|
||||||
buffer.writeShortLE(node.port.toInt())
|
buffer.writeShortLE(node.port.toInt())
|
||||||
buffer.writeByte(node.id.toInt())
|
buffer.writeByte(node.weight.toInt())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GameServerNodesPacketDeserializer : PacketDeserializer<GameServerNodesPacketDeserializer.GameServerNodesPacket> {
|
class GameServerNodesPacketDeserializer : PacketDeserializer<GameServerNodesPacket> {
|
||||||
|
|
||||||
open class GameServerNodesPacket(
|
|
||||||
open val loginState: LoginState
|
|
||||||
)
|
|
||||||
|
|
||||||
data class AuthedGameServerNodesPacket(
|
|
||||||
override val loginState: LoginState,
|
|
||||||
val gameServerId: UByte,
|
|
||||||
val userId: Int,
|
|
||||||
val sessionToken: ByteArray,
|
|
||||||
val localAddress: IPAddress,
|
|
||||||
val localHostname: String,
|
|
||||||
val port: UShort,
|
|
||||||
val nodes: List<GameServerNode>
|
|
||||||
) : GameServerNodesPacket(loginState)
|
|
||||||
|
|
||||||
override fun deserialize(buffer: ByteBuf): GameServerNodesPacket {
|
override fun deserialize(buffer: ByteBuf): GameServerNodesPacket {
|
||||||
val stateId = buffer.readUnsignedByte().toInt()
|
val stateId = buffer.readUnsignedByte().toInt()
|
||||||
@@ -95,10 +93,12 @@ class GameServerNodesPacketDeserializer : PacketDeserializer<GameServerNodesPack
|
|||||||
|
|
||||||
val gameServerId = buffer.readUnsignedByte().toUByte()
|
val gameServerId = buffer.readUnsignedByte().toUByte()
|
||||||
|
|
||||||
val localAddress = buffer.readIpLE()
|
val localAddressBuffer = ByteArray(buffer.readUnsignedByte().toInt())
|
||||||
|
buffer.readBytes(localAddressBuffer)
|
||||||
|
val localAddress = IPAddress.of(localAddressBuffer)
|
||||||
val localHostname = buffer.readUtf16LE()
|
val localHostname = buffer.readUtf16LE()
|
||||||
|
|
||||||
val port = buffer.readUnsignedShortLE().toUShort()
|
val port = buffer.readUnsignedIntLE().toUShort()
|
||||||
|
|
||||||
val nodeCount = buffer.readUnsignedByte().toInt()
|
val nodeCount = buffer.readUnsignedByte().toInt()
|
||||||
val nodes = mutableListOf<GameServerNode>()
|
val nodes = mutableListOf<GameServerNode>()
|
||||||
@@ -109,19 +109,21 @@ class GameServerNodesPacketDeserializer : PacketDeserializer<GameServerNodesPack
|
|||||||
val iPv6Address = buffer.readIpLE() as IPv6Address
|
val iPv6Address = buffer.readIpLE() as IPv6Address
|
||||||
|
|
||||||
val nodePort = buffer.readUnsignedShortLE().toUShort()
|
val nodePort = buffer.readUnsignedShortLE().toUShort()
|
||||||
val nodeId = buffer.readUnsignedByte().toUByte()
|
val weight = buffer.readUnsignedByte().toUByte()
|
||||||
|
|
||||||
nodes.add(GameServerNode(nodeId, iPv4Address, iPv6Address, nodePort))
|
nodes.add(GameServerNode(iPv4Address, iPv6Address, nodePort, weight))
|
||||||
}
|
}
|
||||||
|
|
||||||
return AuthedGameServerNodesPacket(
|
return GameServerNodesPacket(
|
||||||
loginState,
|
LoginState.AUTHED,
|
||||||
gameServerId,
|
GameServerData(
|
||||||
userId,
|
gameServerId,
|
||||||
sessionToken,
|
userId,
|
||||||
localAddress,
|
sessionToken,
|
||||||
localHostname,
|
localAddress,
|
||||||
port,
|
localHostname,
|
||||||
|
port
|
||||||
|
),
|
||||||
nodes
|
nodes
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
package de.fiereu.openmmo.protocols.tls.packets.builders
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.GameServer
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.GameServerListPacket
|
||||||
|
|
||||||
|
class GameServerListPacketBuilder {
|
||||||
|
private val servers = mutableListOf<GameServer>()
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun create(): GameServerListPacketBuilder = GameServerListPacketBuilder()
|
||||||
|
|
||||||
|
fun withServers(servers: List<GameServer>): GameServerListPacket {
|
||||||
|
return GameServerListPacket(servers)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun empty(): GameServerListPacket = GameServerListPacket(emptyList())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addServer(
|
||||||
|
id: UByte,
|
||||||
|
name: String,
|
||||||
|
currentPlayers: UShort = 0u,
|
||||||
|
maxPlayers: UShort = 0u,
|
||||||
|
joinable: Boolean = true
|
||||||
|
): GameServerListPacketBuilder {
|
||||||
|
servers.add(GameServer(id, name, currentPlayers, maxPlayers, joinable))
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addServer(server: GameServer): GameServerListPacketBuilder {
|
||||||
|
servers.add(server)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun build(): GameServerListPacket = GameServerListPacket(servers.toList())
|
||||||
|
}
|
||||||
+68
@@ -0,0 +1,68 @@
|
|||||||
|
package de.fiereu.openmmo.protocols.tls.packets.builders
|
||||||
|
|
||||||
|
import com.github.maltalex.ineter.base.IPAddress
|
||||||
|
import de.fiereu.openmmo.common.enums.LoginState
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.GameServerData
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.GameServerNode
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.GameServerNodesPacket
|
||||||
|
|
||||||
|
class GameServerNodesPacketBuilder {
|
||||||
|
companion object {
|
||||||
|
fun forState(loginState: LoginState): GameServerNodesPacket {
|
||||||
|
require(loginState != LoginState.AUTHED) { "Use forAuthedUser() for AUTHED state" }
|
||||||
|
return GameServerNodesPacket(loginState)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun forAuthedUser(
|
||||||
|
userId: Int,
|
||||||
|
sessionToken: ByteArray,
|
||||||
|
gameServerId: UByte
|
||||||
|
): AuthedPacketBuilder {
|
||||||
|
return AuthedPacketBuilder(userId, sessionToken, gameServerId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AuthedPacketBuilder internal constructor(
|
||||||
|
private val userId: Int,
|
||||||
|
private val sessionToken: ByteArray,
|
||||||
|
private val gameServerId: UByte
|
||||||
|
) {
|
||||||
|
private var localAddress: IPAddress? = null
|
||||||
|
private var localHostname: String? = null
|
||||||
|
private var port: UShort? = null
|
||||||
|
private var nodes: List<GameServerNode> = emptyList()
|
||||||
|
|
||||||
|
fun withLocalEndpoint(
|
||||||
|
address: IPAddress,
|
||||||
|
hostname: String,
|
||||||
|
port: UShort
|
||||||
|
): AuthedPacketBuilder {
|
||||||
|
this.localAddress = address
|
||||||
|
this.localHostname = hostname
|
||||||
|
this.port = port
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun withNodes(nodes: List<GameServerNode>): AuthedPacketBuilder {
|
||||||
|
this.nodes = nodes
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun build(): GameServerNodesPacket {
|
||||||
|
requireNotNull(localAddress) { "Local address must be set for AUTHED packets" }
|
||||||
|
requireNotNull(localHostname) { "Local hostname must be set for AUTHED packets" }
|
||||||
|
requireNotNull(port) { "Port must be set for AUTHED packets" }
|
||||||
|
|
||||||
|
val gameServerData = GameServerData(
|
||||||
|
gameServerId,
|
||||||
|
userId,
|
||||||
|
sessionToken,
|
||||||
|
localAddress!!,
|
||||||
|
localHostname!!,
|
||||||
|
port!!
|
||||||
|
)
|
||||||
|
|
||||||
|
return GameServerNodesPacket(LoginState.AUTHED, gameServerData, nodes)
|
||||||
|
}
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package de.fiereu.openmmo.protocols.tls.packets.builders
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.common.enums.LoginState
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.LoginResponsePacket
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
class LoginResponsePacketBuilder {
|
||||||
|
companion object {
|
||||||
|
fun withState(state: LoginState): LoginResponsePacket {
|
||||||
|
require(state != LoginState.RATE_LIMITED && state != LoginState.RATE_LIMITED_2FA) {
|
||||||
|
"Use withRateLimit() for rate limited states"
|
||||||
|
}
|
||||||
|
return LoginResponsePacket(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun withRateLimit(state: LoginState, ratelimitEnd: LocalDateTime): LoginResponsePacket {
|
||||||
|
require(state == LoginState.RATE_LIMITED || state == LoginState.RATE_LIMITED_2FA) {
|
||||||
|
"Rate limit end time can only be used with RATE_LIMITED states"
|
||||||
|
}
|
||||||
|
return LoginResponsePacket(state, ratelimitEnd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,15 +30,16 @@ class TlsServerProtocol(serverKeyPair: Pair<ECPrivateKey, ECPublicKey>) : TlsPro
|
|||||||
init {
|
init {
|
||||||
incomingPacket(0x00u, ClientHelloPacketDeserializer())
|
incomingPacket(0x00u, ClientHelloPacketDeserializer())
|
||||||
outgoingPacket(
|
outgoingPacket(
|
||||||
0x01u, ServerHelloPacket::class, ServerHelloPacketSerializer(serverKeyPair.first))
|
0x01u, ServerHelloPacketSerializer(serverKeyPair.first), ServerHelloPacket::class
|
||||||
|
)
|
||||||
incomingPacket(0x02u, ClientReadyPacketDeserializer())
|
incomingPacket(0x02u, ClientReadyPacketDeserializer())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TlsClientProtocol() : TlsProtocol() {
|
class TlsClientProtocol() : TlsProtocol() {
|
||||||
init {
|
init {
|
||||||
outgoingPacket(0x00u, ClientHelloPacket::class, ClientHelloPacketSerializer())
|
outgoingPacket(0x00u, ClientHelloPacketSerializer(), ClientHelloPacket::class)
|
||||||
incomingPacket(0x01u, ServerHelloPacketDeserializer())
|
incomingPacket(0x01u, ServerHelloPacketDeserializer())
|
||||||
outgoingPacket(0x02u, ClientReadyPacket::class, ClientReadyPacketSerializer())
|
outgoingPacket(0x02u, ClientReadyPacketSerializer(), ClientReadyPacket::class)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ dependencies {
|
|||||||
|
|
||||||
implementation(libs.slf4j)
|
implementation(libs.slf4j)
|
||||||
implementation(libs.kotlin.logging)
|
implementation(libs.kotlin.logging)
|
||||||
|
implementation(libs.kotlin.reflect)
|
||||||
|
|
||||||
testImplementation(libs.bundles.kotest)
|
testImplementation(libs.bundles.kotest)
|
||||||
}
|
}
|
||||||
@@ -47,12 +47,21 @@ fun ByteBuf.readIpLE(): IPAddress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun ByteBuf.writeIpLE(ipAddress: IPAddress) {
|
fun ByteBuf.writeIpLE(ipAddress: IPAddress) {
|
||||||
if (ipAddress is IPv4Address) {
|
when (ipAddress) {
|
||||||
writeByte(4)
|
is IPv4Address -> {
|
||||||
writeIntLE(ipAddress.toInt())
|
writeByte(4)
|
||||||
} else {
|
writeIntLE(ipAddress.toInt())
|
||||||
writeByte(6)
|
}
|
||||||
writeBytes(ipAddress.toArray())
|
|
||||||
|
is IPv6Address -> {
|
||||||
|
writeByte(6)
|
||||||
|
writeLongLE(ipAddress.upper)
|
||||||
|
writeLongLE(ipAddress.lower)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
throw IllegalArgumentException("Unsupported IP address type: ${ipAddress::class}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package de.fiereu.openmmo.protocols
|
|||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.netty.buffer.ByteBuf
|
import io.netty.buffer.ByteBuf
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
import kotlin.reflect.jvm.jvmErasure
|
||||||
|
|
||||||
/** Value class wrapping a UByte to represent an opcode. */
|
/** Value class wrapping a UByte to represent an opcode. */
|
||||||
@JvmInline
|
@JvmInline
|
||||||
@@ -49,9 +50,7 @@ abstract class Protocol {
|
|||||||
|
|
||||||
abstract val async: Boolean
|
abstract val async: Boolean
|
||||||
|
|
||||||
fun getOpcode(dataClass: KClass<*>): Opcode? {
|
fun getOpcode(dataClass: KClass<*>): Opcode? = dataClassToOpcode[dataClass]
|
||||||
return dataClassToOpcode[dataClass]
|
|
||||||
}
|
|
||||||
|
|
||||||
fun <T : Any> registerIncoming(opcode: Opcode, deserializer: PacketDeserializer<T>) {
|
fun <T : Any> registerIncoming(opcode: Opcode, deserializer: PacketDeserializer<T>) {
|
||||||
log.debug {
|
log.debug {
|
||||||
@@ -63,13 +62,17 @@ abstract class Protocol {
|
|||||||
|
|
||||||
fun <T : Any> registerOutgoing(
|
fun <T : Any> registerOutgoing(
|
||||||
opcode: Opcode,
|
opcode: Opcode,
|
||||||
dataClass: KClass<T>,
|
|
||||||
serializer: PacketSerializer<T>,
|
serializer: PacketSerializer<T>,
|
||||||
|
dataClass: KClass<T>,
|
||||||
) {
|
) {
|
||||||
log.debug {
|
log.debug {
|
||||||
"Registering outgoing packet: opcode=$opcode, serializer=${serializer::class.simpleName}"
|
"Registering outgoing packet: opcode=$opcode, serializer=${serializer::class.simpleName}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dataClassToOpcode.containsKey(dataClass)) {
|
||||||
|
// Better to fail fast here than to have hard to debug issues later
|
||||||
|
error("Data class ${dataClass.simpleName} is already registered with an opcode!")
|
||||||
|
}
|
||||||
dataClassToOpcode[dataClass] = opcode
|
dataClassToOpcode[dataClass] = opcode
|
||||||
outgoingPacketRegistry.put(opcode, serializer)
|
outgoingPacketRegistry.put(opcode, serializer)
|
||||||
}
|
}
|
||||||
@@ -93,8 +96,8 @@ fun <T : Any> Protocol.incomingPacket(opcode: UByte, deserializer: PacketDeseria
|
|||||||
|
|
||||||
fun <T : Any> Protocol.outgoingPacket(
|
fun <T : Any> Protocol.outgoingPacket(
|
||||||
opcode: UByte,
|
opcode: UByte,
|
||||||
dataClass: KClass<T>,
|
serializer: PacketSerializer<T>,
|
||||||
serializer: PacketSerializer<T>
|
dataClass: KClass<T>
|
||||||
) {
|
) {
|
||||||
this.registerOutgoing(Opcode(opcode), dataClass, serializer)
|
this.registerOutgoing(Opcode(opcode), serializer, dataClass)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
src/main/resources/game.*.pem
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
# Multi-stage build for OpenMMO Game Server
|
||||||
|
FROM azul/zulu-openjdk:24-latest AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy gradle files first for better caching
|
||||||
|
COPY ./gradle gradle/
|
||||||
|
COPY ./gradlew gradlew.bat gradle.properties settings.gradle.kts gradle.env ./
|
||||||
|
COPY ./buildSrc buildSrc/
|
||||||
|
|
||||||
|
# remove all includ("...") from settings.gradle.kts
|
||||||
|
RUN sed -i '/include(/d' settings.gradle.kts
|
||||||
|
# manually include only the required modules
|
||||||
|
RUN echo 'include("common")' >> settings.gradle.kts
|
||||||
|
RUN echo 'include("keys")' >> settings.gradle.kts
|
||||||
|
RUN echo 'include("protocol")' >> settings.gradle.kts
|
||||||
|
RUN echo 'include("protocol.tls")' >> settings.gradle.kts
|
||||||
|
RUN echo 'include("protocol.game")' >> settings.gradle.kts
|
||||||
|
RUN echo 'include("server")' >> settings.gradle.kts
|
||||||
|
RUN echo 'include("server.game")' >> settings.gradle.kts
|
||||||
|
|
||||||
|
# Copy all module build files
|
||||||
|
COPY ./build.gradle.kts ./
|
||||||
|
COPY ./common/build.gradle.kts common/
|
||||||
|
COPY ./keys/build.gradle.kts keys/
|
||||||
|
COPY ./protocol/build.gradle.kts protocol/
|
||||||
|
COPY ./protocol.tls/build.gradle.kts protocol.tls/
|
||||||
|
COPY ./protocol.game/build.gradle.kts protocol.game/
|
||||||
|
COPY ./server/build.gradle.kts server/
|
||||||
|
COPY ./server.game/build.gradle.kts server.game/
|
||||||
|
|
||||||
|
# Download dependencies
|
||||||
|
RUN ./gradlew dependencies --no-daemon
|
||||||
|
|
||||||
|
# Copy source code
|
||||||
|
COPY ./common/src common/src/
|
||||||
|
COPY ./protocol/src protocol/src/
|
||||||
|
COPY ./protocol.tls/src protocol.tls/src/
|
||||||
|
COPY ./protocol.game/src protocol.game/src/
|
||||||
|
COPY ./server/src server/src/
|
||||||
|
COPY ./server.game/src server.game/src/
|
||||||
|
|
||||||
|
# Build the application
|
||||||
|
RUN ./gradlew :server.game:distTar --no-daemon
|
||||||
|
|
||||||
|
# Extract the distribution
|
||||||
|
RUN cd server.game/build/distributions && \
|
||||||
|
tar -xf server.game-*.tar && \
|
||||||
|
mv server.game-*/ server.game/
|
||||||
|
|
||||||
|
# Runtime stage
|
||||||
|
FROM azul/zulu-openjdk:24-jre-latest
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy the extracted distribution from builder
|
||||||
|
COPY --from=builder /app/server.game/build/distributions/server.game/ .
|
||||||
|
|
||||||
|
EXPOSE 2106
|
||||||
|
|
||||||
|
ENTRYPOINT ["./bin/server.game"]
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# Server.Game Module
|
||||||
|
|
||||||
|
The server.game module implements the game server for OpenMMO.
|
||||||
|
|
||||||
|
## Development
|
||||||
|
- Run with: `./gradlew :server.game:run`
|
||||||
|
- Database setup: `./gradlew cleanMigrateAndGenerate`
|
||||||
|
- Code generation: `./gradlew jooqCodegen`
|
||||||
|
- Docker build: `docker build -t openmmo-game server.game/`
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
import org.flywaydb.gradle.task.AbstractFlywayTask
|
||||||
|
|
||||||
|
buildscript { dependencies { classpath(libs.flyway.postgresql) } }
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
application
|
||||||
|
distribution
|
||||||
|
id("buildsrc.convention.kotlin-jvm")
|
||||||
|
id("buildsrc.convention.spotless")
|
||||||
|
id("buildsrc.common.keys")
|
||||||
|
alias(libs.plugins.kotlin.serialization)
|
||||||
|
alias(libs.plugins.jooq)
|
||||||
|
alias(libs.plugins.flyway)
|
||||||
|
}
|
||||||
|
|
||||||
|
group = "de.fiereu.openmmo"
|
||||||
|
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(project(":server"))
|
||||||
|
implementation(project(":protocol.game"))
|
||||||
|
implementation(libs.logback)
|
||||||
|
implementation(libs.kotlinx.coroutines)
|
||||||
|
|
||||||
|
testImplementation(libs.bundles.kotest)
|
||||||
|
testImplementation(libs.kotlinx.coroutines.test)
|
||||||
|
testImplementation(libs.mockk)
|
||||||
|
}
|
||||||
|
|
||||||
|
application { mainClass.set("de.fiereu.openmmo.server.game.GameServerKt") }
|
||||||
|
|
||||||
|
|
||||||
|
fun env(key: String): Any {
|
||||||
|
val value =
|
||||||
|
property(key)
|
||||||
|
?: System.getenv(key)
|
||||||
|
?: throw IllegalArgumentException(
|
||||||
|
"Environment variable or system property '$key' is not set.")
|
||||||
|
fun interpolate(value: String): Any {
|
||||||
|
val PATTERN_WORD = Regex("\\$([a-zA-Z0-9_]+)")
|
||||||
|
val PATTERN_MULTI = Regex("\\$\\{([a-zA-Z0-9_]+)\\}")
|
||||||
|
return (PATTERN_WORD.findAll(value) + PATTERN_MULTI.findAll(value)).distinct().fold(value) {
|
||||||
|
acc,
|
||||||
|
match ->
|
||||||
|
val envName = match.groupValues[1]
|
||||||
|
val envValue = System.getProperty(envName) ?: System.getProperty(envName)
|
||||||
|
if (envValue == null) {
|
||||||
|
throw IllegalArgumentException(
|
||||||
|
"Environment variable or system property '$envName' is not set.")
|
||||||
|
}
|
||||||
|
acc.replace(match.value, envValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return if (value is String && (value.contains('$') || value.contains('{'))) {
|
||||||
|
interpolate(value)
|
||||||
|
} else value
|
||||||
|
}
|
||||||
|
|
||||||
|
val dbPort = env("openmmo.db.game.port") as String
|
||||||
|
val dbName = env("openmmo.db.game.name") as String
|
||||||
|
val dbUser = env("openmmo.db.game.user") as String
|
||||||
|
val dbPassword = env("openmmo.db.game.password") as String
|
||||||
|
|
||||||
|
jooq {
|
||||||
|
configuration {
|
||||||
|
jdbc {
|
||||||
|
driver = "org.postgresql.Driver"
|
||||||
|
url = "jdbc:postgresql://localhost:$dbPort/$dbName"
|
||||||
|
user = dbUser
|
||||||
|
password = dbPassword
|
||||||
|
}
|
||||||
|
generator {
|
||||||
|
database {
|
||||||
|
name = "org.jooq.meta.postgres.PostgresDatabase"
|
||||||
|
includes = ".*"
|
||||||
|
excludes =
|
||||||
|
"""
|
||||||
|
flyway_schema_history |
|
||||||
|
pgp_armor_headers
|
||||||
|
"""
|
||||||
|
inputSchema = "public"
|
||||||
|
}
|
||||||
|
|
||||||
|
generate { name = "org.jooq.codegen.KotlinGenerator" }
|
||||||
|
target {
|
||||||
|
packageName = "de.fiereu.openmmo.server.game.jooq"
|
||||||
|
directory = "src/main/jooq"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flyway {
|
||||||
|
driver = "org.postgresql.Driver"
|
||||||
|
url = "jdbc:postgresql://localhost:$dbPort/$dbName"
|
||||||
|
user = dbUser
|
||||||
|
password = dbPassword
|
||||||
|
schemas = arrayOf("public")
|
||||||
|
locations = arrayOf("filesystem:src/main/resources/db/migration")
|
||||||
|
cleanDisabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fuck this shit uhh we are not gonna update our gradle plugin bc all our enterprise customers are
|
||||||
|
// stuck on Java 1.6 and gradle 5 anyway
|
||||||
|
tasks.withType<AbstractFlywayTask>().configureEach {
|
||||||
|
notCompatibleWithConfigurationCache("https://github.com/flyway/flyway/issues/3550")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.register("cleanMigrateAndGenerate") {
|
||||||
|
group = "openmmo"
|
||||||
|
description = "Cleans the database, migrates it and generates the jOOQ classes."
|
||||||
|
dependsOn("flywayClean", "flywayMigrate", "jooqCodegen")
|
||||||
|
}
|
||||||
|
|
||||||
|
spotless { kotlin { targetExclude("src/main/jooq/**") } }
|
||||||
|
|
||||||
|
// If someone knows a better way then this please please please help me :(
|
||||||
|
listOf("classes", "processResources", "spotlessKotlin", "spotlessKotlinGradle", "spotlessMisc").forEach { taskName ->
|
||||||
|
tasks.named(taskName) {
|
||||||
|
listOf("copyPublicKeyGame", "copyPrivateKeyGame")
|
||||||
|
.forEach { dependsOn(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package de.fiereu.openmmo.server.game
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.protocols.game.GameServerProtocol
|
||||||
|
import de.fiereu.openmmo.server.ServerBuilder
|
||||||
|
import de.fiereu.openmmo.server.config.ServerConfig
|
||||||
|
import de.fiereu.openmmo.server.config.TlsConfig
|
||||||
|
import de.fiereu.openmmo.server.game.protocol.game.GameProtocolHandler
|
||||||
|
import de.fiereu.openmmo.server.io.resource
|
||||||
|
import de.fiereu.openmmo.server.keys.KeyLoader
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val serverConfig = ServerConfig(port = 7777)
|
||||||
|
val tlsConfig = TlsConfig(checksumSize = 2)
|
||||||
|
val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||||
|
val server =
|
||||||
|
ServerBuilder.create()
|
||||||
|
.withCoroutineScope(coroutineScope)
|
||||||
|
.withConfig(serverConfig)
|
||||||
|
.withTlsConfig(tlsConfig)
|
||||||
|
.withPublicKey(KeyLoader.loadPemECPublicKey(resource("game.public.pem")))
|
||||||
|
.withPrivateKey(KeyLoader.loadPemECPrivateKey(resource("game.private.pem")))
|
||||||
|
.withChannelHandlerProvider { GameProtocolHandler(GameServerProtocol(), coroutineScope) }
|
||||||
|
.build()
|
||||||
|
server.start()
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package de.fiereu.openmmo.server.game.protocol.game
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.protocols.Protocol
|
||||||
|
import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket
|
||||||
|
import de.fiereu.openmmo.server.netty.handlers.ProtocolHandler
|
||||||
|
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||||
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
|
import io.netty.channel.ChannelHandlerContext
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
private val log = KotlinLogging.logger {}
|
||||||
|
|
||||||
|
class GameProtocolHandler(
|
||||||
|
protocol: Protocol,
|
||||||
|
private val coroutineScope: CoroutineScope,
|
||||||
|
) : ProtocolHandler(protocol) {
|
||||||
|
|
||||||
|
override fun onActive(ctx: ChannelHandlerContext) {
|
||||||
|
log.info { "Client ${ctx.channel().remoteAddress()} connected to game server." }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("unchecked_cast") // Even here i personally check every cast. i live in ur jvm >:(
|
||||||
|
override fun onPacketReceived(event: PacketEvent<*>) {
|
||||||
|
coroutineScope.launch {
|
||||||
|
when (event.packet) {
|
||||||
|
is JoinGamePacket -> onJoinGame(event as PacketEvent<JoinGamePacket>)
|
||||||
|
else -> log.warn { "Unhandled game packet type: ${event.packet::class.simpleName}" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onJoinGame(event: PacketEvent<JoinGamePacket>) {
|
||||||
|
log.info { "Player joined the game." }
|
||||||
|
log.debug { "This is what the Game knows about you:\n${event.packet.clientInfo.values.joinToString("\n")}" }
|
||||||
|
// TODO handle player joining the game
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
server.port = 2106
|
||||||
Whitespace-only changes.
@@ -0,0 +1,11 @@
|
|||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="TRACE">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
||||||
@@ -38,11 +38,6 @@ dependencies {
|
|||||||
|
|
||||||
application { mainClass.set("de.fiereu.openmmo.server.login.LoginServerKt") }
|
application { mainClass.set("de.fiereu.openmmo.server.login.LoginServerKt") }
|
||||||
|
|
||||||
tasks.classes.get().apply {
|
|
||||||
dependsOn("copyKeysGame")
|
|
||||||
tasks.processResources.get().mustRunAfter("copyKeysGame")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun env(key: String): Any {
|
fun env(key: String): Any {
|
||||||
val value =
|
val value =
|
||||||
property(key)
|
property(key)
|
||||||
@@ -128,9 +123,10 @@ tasks.register("cleanMigrateAndGenerate") {
|
|||||||
spotless { kotlin { targetExclude("src/main/jooq/**") } }
|
spotless { kotlin { targetExclude("src/main/jooq/**") } }
|
||||||
|
|
||||||
// If someone knows a better way then this please please please help me :(
|
// If someone knows a better way then this please please please help me :(
|
||||||
listOf("spotlessKotlinGradle", "spotlessMisc").forEach { taskName ->
|
listOf("classes", "processResources", "spotlessKotlin", "spotlessKotlinGradle", "spotlessMisc").forEach { taskName ->
|
||||||
tasks.named(taskName) {
|
tasks.named(taskName) {
|
||||||
dependsOn("copyPublicKeyGame")
|
listOf("copyPublicKeyGame", "copyPrivateKeyGame")
|
||||||
dependsOn("copyPrivateKeyGame")
|
.forEach { dependsOn(it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
+48
@@ -1,7 +1,19 @@
|
|||||||
package de.fiereu.openmmo.server.login.protocol.login
|
package de.fiereu.openmmo.server.login.protocol.login
|
||||||
|
|
||||||
|
import com.github.maltalex.ineter.base.IPAddress
|
||||||
|
import com.github.maltalex.ineter.base.IPv4Address
|
||||||
|
import com.github.maltalex.ineter.base.IPv6Address
|
||||||
|
import de.fiereu.openmmo.common.enums.LoginState
|
||||||
import de.fiereu.openmmo.protocols.Protocol
|
import de.fiereu.openmmo.protocols.Protocol
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.GameServer
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.GameServerNode
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.JoinGameServerPacket
|
||||||
import de.fiereu.openmmo.protocols.tls.packets.LoginRequestPacket
|
import de.fiereu.openmmo.protocols.tls.packets.LoginRequestPacket
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.RequestGameServerListPacket
|
||||||
|
import de.fiereu.openmmo.server.login.protocol.login.ext.respondTo
|
||||||
|
import de.fiereu.openmmo.server.login.protocol.login.ext.respondForAuthedUser
|
||||||
|
import de.fiereu.openmmo.server.login.protocol.login.ext.respondWithState
|
||||||
|
import de.fiereu.openmmo.server.login.protocol.login.ext.respondWithServers
|
||||||
import de.fiereu.openmmo.server.netty.handlers.ProtocolHandler
|
import de.fiereu.openmmo.server.netty.handlers.ProtocolHandler
|
||||||
import de.fiereu.openmmo.server.protocol.PacketEvent
|
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
@@ -25,6 +37,8 @@ class LoginProtocolHandler(
|
|||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
when (event.packet) {
|
when (event.packet) {
|
||||||
is LoginRequestPacket -> onLoginRequest(event as PacketEvent<LoginRequestPacket>)
|
is LoginRequestPacket -> onLoginRequest(event as PacketEvent<LoginRequestPacket>)
|
||||||
|
is RequestGameServerListPacket -> onGameServerListRequest(event as PacketEvent<RequestGameServerListPacket>)
|
||||||
|
is JoinGameServerPacket -> onServerSelection(event as PacketEvent<JoinGameServerPacket>)
|
||||||
else -> log.warn { "Unhandled login packet type: ${event.packet::class.simpleName}" }
|
else -> log.warn { "Unhandled login packet type: ${event.packet::class.simpleName}" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,5 +46,39 @@ class LoginProtocolHandler(
|
|||||||
|
|
||||||
fun onLoginRequest(event: PacketEvent<LoginRequestPacket>) {
|
fun onLoginRequest(event: PacketEvent<LoginRequestPacket>) {
|
||||||
log.info { "Received login request for user '${event.packet.username}'" }
|
log.info { "Received login request for user '${event.packet.username}'" }
|
||||||
|
// TODO proper authentication
|
||||||
|
event.respondWithState(LoginState.AUTHED)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onGameServerListRequest(event: PacketEvent<RequestGameServerListPacket>) {
|
||||||
|
log.info { "Received game server list request" }
|
||||||
|
// TODO replace hardcoded server list with actual server list from database or config
|
||||||
|
event.respondWithServers(listOf(
|
||||||
|
GameServer(0x00u, "OpenMMO", 0u, 1u, true)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onServerSelection(event: PacketEvent<JoinGameServerPacket>) {
|
||||||
|
log.info { "Received server selection for server id ${event.packet.gameServerId}" }
|
||||||
|
// TODO proper server node selection handling (depends on onGameServerListRequest implementation)
|
||||||
|
// Example refuse connection: event.respondWithState(LoginState.INVALID_PASSWORD)
|
||||||
|
event.respondForAuthedUser(
|
||||||
|
userId = 1,
|
||||||
|
sessionToken = ByteArray(16) { 0x42 }
|
||||||
|
)
|
||||||
|
.withLocalEndpoint(
|
||||||
|
address = IPAddress.of("127.0.0.1"),
|
||||||
|
hostname = "localhost",
|
||||||
|
port = 7777u
|
||||||
|
)
|
||||||
|
.withNodes(listOf(
|
||||||
|
GameServerNode(
|
||||||
|
iPv4Address = IPv4Address.of("127.0.0.1"),
|
||||||
|
iPv6Address = IPv6Address.of("::1"),
|
||||||
|
port = 7777u,
|
||||||
|
weight = 0x01u // Always select this node (only one node available in this example)
|
||||||
|
)
|
||||||
|
))
|
||||||
|
.respondTo(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
package de.fiereu.openmmo.server.login.protocol.login.ext
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.common.enums.LoginState
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.JoinGameServerPacket
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.builders.AuthedPacketBuilder
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.builders.GameServerNodesPacketBuilder
|
||||||
|
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||||
|
import de.fiereu.openmmo.server.protocol.respond
|
||||||
|
|
||||||
|
fun PacketEvent<JoinGameServerPacket>.respondWithErrorState(state: LoginState) {
|
||||||
|
this.respond(GameServerNodesPacketBuilder.forState(state))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun PacketEvent<JoinGameServerPacket>.respondForAuthedUser(
|
||||||
|
userId: Int,
|
||||||
|
sessionToken: ByteArray
|
||||||
|
): AuthedPacketBuilder {
|
||||||
|
return GameServerNodesPacketBuilder.forAuthedUser(
|
||||||
|
userId = userId,
|
||||||
|
sessionToken = sessionToken,
|
||||||
|
gameServerId = this.packet.gameServerId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun AuthedPacketBuilder.respondTo(event: PacketEvent<*>) {
|
||||||
|
event.respond(this.build())
|
||||||
|
}
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
package de.fiereu.openmmo.server.login.protocol.login.ext
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.common.enums.LoginState
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.LoginRequestPacket
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.builders.LoginResponsePacketBuilder
|
||||||
|
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||||
|
import de.fiereu.openmmo.server.protocol.respond
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
fun PacketEvent<LoginRequestPacket>.respondWithState(state: LoginState) {
|
||||||
|
this.respond(LoginResponsePacketBuilder.withState(state))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun PacketEvent<LoginRequestPacket>.respondWithRateLimit(
|
||||||
|
state: LoginState,
|
||||||
|
ratelimitEnd: LocalDateTime
|
||||||
|
) {
|
||||||
|
this.respond(LoginResponsePacketBuilder.withRateLimit(state, ratelimitEnd))
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package de.fiereu.openmmo.server.login.protocol.login.ext
|
||||||
|
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.GameServer
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.RequestGameServerListPacket
|
||||||
|
import de.fiereu.openmmo.protocols.tls.packets.builders.GameServerListPacketBuilder
|
||||||
|
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||||
|
import de.fiereu.openmmo.server.protocol.respond
|
||||||
|
|
||||||
|
fun PacketEvent<RequestGameServerListPacket>.respondWithServers(servers: List<GameServer>) {
|
||||||
|
this.respond(GameServerListPacketBuilder.withServers(servers))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun PacketEvent<RequestGameServerListPacket>.respondWithEmptyList() {
|
||||||
|
this.respond(GameServerListPacketBuilder.empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun PacketEvent<RequestGameServerListPacket>.respondWithServerList(): GameServerListPacketBuilder {
|
||||||
|
return GameServerListPacketBuilder.create()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun GameServerListPacketBuilder.respondTo(event: PacketEvent<*>) {
|
||||||
|
event.respond(this.build())
|
||||||
|
}
|
||||||
@@ -17,5 +17,7 @@ include(":common")
|
|||||||
include(":protocol")
|
include(":protocol")
|
||||||
include(":protocol.tls")
|
include(":protocol.tls")
|
||||||
include(":protocol.login")
|
include(":protocol.login")
|
||||||
|
include(":protocol.game")
|
||||||
include(":server")
|
include(":server")
|
||||||
include(":server.login")
|
include(":server.login")
|
||||||
|
include(":server.game")
|
||||||
Reference in new issue
Block a user