refactor(network): rework network module, use new bytecodec module and introduce proper packet events

This commit is contained in:
Fiereu committed 2026-05-30 10:13:12 +02:00
1 parent 8048827121
commit d27a54f360
340 files changed
+7462 -62719

No files matched your search

@@ -0,0 +1,75 @@
package de.fiereu.openmmo.common.auth
import java.nio.ByteBuffer
import java.security.MessageDigest
import java.time.Clock
import java.time.Instant
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
/** Opaque token issued by the login server and validated by the game server. */
data class SessionToken(
val userId: Long,
val issuedAt: Instant,
val bytes: ByteArray,
) {
override fun equals(other: Any?): Boolean =
other is SessionToken &&
userId == other.userId &&
issuedAt == other.issuedAt &&
bytes.contentEquals(other.bytes)
override fun hashCode(): Int =
(userId.hashCode() * 31 + issuedAt.hashCode()) * 31 + bytes.contentHashCode()
}
class SessionTokenIssuer(secret: ByteArray, private val clock: Clock = Clock.systemUTC()) {
init {
require(secret.isNotEmpty()) { "session token secret must not be empty" }
}
private val secretKey = SecretKeySpec(secret, MAC_ALGORITHM)
fun issue(userId: Long): SessionToken {
val issuedAt = clock.instant()
val bytes = build(userId, issuedAt, secretKey)
return SessionToken(userId, issuedAt, bytes)
}
}
class SessionTokenVerifier(secret: ByteArray) {
init {
require(secret.isNotEmpty()) { "session token secret must not be empty" }
}
private val secretKey = SecretKeySpec(secret, MAC_ALGORITHM)
fun verify(bytes: ByteArray): SessionToken? {
if (bytes.size != TOKEN_SIZE) return null
val buf = ByteBuffer.wrap(bytes)
val userId = buf.long
val epochSeconds = buf.long
val issuedAt = Instant.ofEpochSecond(epochSeconds)
val expected = build(userId, issuedAt, secretKey)
if (!MessageDigest.isEqual(bytes, expected)) return null
return SessionToken(userId, issuedAt, bytes)
}
}
private const val MAC_ALGORITHM = "HmacSHA256"
private const val PREFIX_SIZE = 16
private const val MAC_SIZE = 16
private const val TOKEN_SIZE = PREFIX_SIZE + MAC_SIZE
private fun build(userId: Long, issuedAt: Instant, key: SecretKeySpec): ByteArray {
val out = ByteArray(TOKEN_SIZE)
val prefix = ByteBuffer.wrap(out, 0, PREFIX_SIZE)
prefix.putLong(userId)
prefix.putLong(issuedAt.epochSecond)
val mac = Mac.getInstance(MAC_ALGORITHM)
mac.init(key)
mac.update(out, 0, PREFIX_SIZE)
val tag = mac.doFinal()
System.arraycopy(tag, 0, out, PREFIX_SIZE, MAC_SIZE)
return out
}
@@ -0,0 +1,38 @@
package de.fiereu.openmmo.common.io
import java.io.InputStream
import java.security.KeyFactory
import java.security.interfaces.ECPrivateKey
import java.security.interfaces.ECPublicKey
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
import java.util.Base64
object PemKeyLoader {
fun loadEcPublic(stream: InputStream): ECPublicKey {
val der = readPem(stream)
val spec = X509EncodedKeySpec(der)
return KeyFactory.getInstance("EC").generatePublic(spec) as ECPublicKey
}
fun loadEcPrivate(stream: InputStream): ECPrivateKey {
val der = readPem(stream)
val spec = PKCS8EncodedKeySpec(der)
return KeyFactory.getInstance("EC").generatePrivate(spec) as ECPrivateKey
}
private fun readPem(stream: InputStream): ByteArray {
val text = stream.bufferedReader(Charsets.US_ASCII).use { it.readText() }
val body =
text
.lineSequence()
.map(String::trim)
.filter { it.isNotEmpty() && !it.startsWith("-----") }
.joinToString("")
return Base64.getDecoder().decode(body)
}
}
fun resource(name: String): InputStream =
PemKeyLoader::class.java.classLoader.getResourceAsStream(name)
?: error("classpath resource not found: $name")
@@ -0,0 +1,48 @@
package de.fiereu.openmmo.common.auth
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import java.time.Clock
import java.time.Instant
import java.time.ZoneOffset
class SessionTokenTest :
FunSpec({
val secret = ByteArray(32) { it.toByte() }
test("issuer + verifier roundtrip recovers userId and issuedAt") {
val fixed = Clock.fixed(Instant.ofEpochSecond(1_700_000_000), ZoneOffset.UTC)
val issuer = SessionTokenIssuer(secret, fixed)
val token = issuer.issue(userId = 42L)
val verified = SessionTokenVerifier(secret).verify(token.bytes)
verified.shouldNotBeNull()
verified.userId shouldBe 42L
verified.issuedAt shouldBe Instant.ofEpochSecond(1_700_000_000)
}
test("tampered bytes fail verification") {
val token = SessionTokenIssuer(secret).issue(userId = 1L)
val mutated = token.bytes.copyOf()
mutated[0] = (mutated[0] + 1).toByte()
SessionTokenVerifier(secret).verify(mutated) shouldBe null
}
test("different secret fails verification") {
val token = SessionTokenIssuer(secret).issue(userId = 1L)
val other = ByteArray(32) { (it + 1).toByte() }
SessionTokenVerifier(other).verify(token.bytes) shouldBe null
}
test("wrong length fails verification fast") {
SessionTokenVerifier(secret).verify(ByteArray(0)) shouldBe null
SessionTokenVerifier(secret).verify(ByteArray(31)) shouldBe null
SessionTokenVerifier(secret).verify(ByteArray(33)) shouldBe null
}
test("empty secret rejected") {
shouldThrow<IllegalArgumentException> { SessionTokenIssuer(ByteArray(0)) }
shouldThrow<IllegalArgumentException> { SessionTokenVerifier(ByteArray(0)) }
}
})