Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit b88c0d9

Browse files
committed
feat(proxy): add support for dynamic velocity secret management
1 parent f921fa0 commit b88c0d9

File tree

25 files changed

+173
-38
lines changed

25 files changed

+173
-38
lines changed

surf-cloud-api/surf-cloud-api-common/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import dev.slne.surf.surfapi.gradle.util.slneReleases
2-
import jdk.tools.jlink.resources.plugins
32

43
plugins {
54
id("dev.slne.surf.surfapi.gradle.core")

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/NettyClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ interface NettyClient {
1515
val serverCategory: String
1616
val serverName: String
1717

18+
val velocitySecret: ByteArray
19+
1820
val connection: Connection
1921

2022
/**

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/packet/packet-extension.kt

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import dev.slne.surf.cloud.api.common.meta.SurfNettyPacket
55
import dev.slne.surf.cloud.api.common.netty.network.codec.StreamCodec
66
import dev.slne.surf.cloud.api.common.netty.network.codec.StreamDecoder
77
import dev.slne.surf.cloud.api.common.netty.network.codec.StreamMemberEncoder
8-
import dev.slne.surf.cloud.api.common.netty.network.codec.kotlinx.SurfCloudBufSerializer
98
import dev.slne.surf.cloud.api.common.netty.protocol.buffer.SurfByteBuf
109
import dev.slne.surf.cloud.api.common.util.mutableObject2ObjectMapOf
1110
import io.netty.buffer.ByteBuf
1211
import kotlinx.serialization.InternalSerializationApi
13-
import kotlinx.serialization.KSerializer
1412
import kotlinx.serialization.serializer
1513
import kotlinx.serialization.serializerOrNull
1614
import kotlin.reflect.KClass
@@ -68,17 +66,8 @@ private val codecCache = mutableObject2ObjectMapOf<KClass<out NettyPacket>, Stre
6866

6967

7068
@OptIn(InternalSerializationApi::class)
71-
fun <P : Any> KClass<out P>.createCodec(): StreamCodec<SurfByteBuf, P> {
72-
val serializer = serializer()
73-
return object : StreamCodec<SurfByteBuf, P> {
74-
override fun decode(buf: SurfByteBuf): P {
75-
return SurfCloudBufSerializer.serializer.decodeFromBuf(buf, serializer)
76-
}
77-
78-
override fun encode(buf: SurfByteBuf, value: P) {
79-
SurfCloudBufSerializer.serializer.encodeToBuf(buf, serializer as KSerializer<P>, value)
80-
}
81-
}
69+
fun <P : Any> KClass<P>.createCodec(): StreamCodec<SurfByteBuf, P> {
70+
return SurfByteBuf.streamCodecFromKotlin(serializer())
8271
}
8372

8473
/**
@@ -96,19 +85,7 @@ fun <B : ByteBuf, V : NettyPacket> KClass<out V>.findPacketCodec(): StreamCodec<
9685

9786
val serializer = serializerOrNull()
9887
if (serializer != null) {
99-
return object : StreamCodec<B, V> {
100-
override fun decode(buf: B): V {
101-
return SurfCloudBufSerializer.serializer.decodeFromBuf(buf, serializer)
102-
}
103-
104-
override fun encode(buf: B, value: V) {
105-
SurfCloudBufSerializer.serializer.encodeToBuf(
106-
buf,
107-
serializer as KSerializer<V>,
108-
value
109-
)
110-
}
111-
}
88+
return SurfByteBuf.streamCodecFromKotlin(serializer)
11289
}
11390

11491
val properties =

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/protocol/buffer/SurfByteBuf.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,18 @@ open class SurfByteBuf(source: ByteBuf) : WrappedByteBuf(source) {
9797

9898
private val GSON = Gson()
9999

100-
fun <T> streamCodecFromKotlin(serializer: KSerializer<T>): StreamCodec<SurfByteBuf, T> {
100+
fun <B: ByteBuf, T> streamCodecFromKotlin(serializer: KSerializer<T>): StreamCodec<B, T> {
101101
return SerializerCodec(serializer)
102102
}
103103

104-
private class SerializerCodec<T>(private val serializer: KSerializer<T>) :
105-
StreamCodec<SurfByteBuf, T> {
106-
override fun decode(buf: SurfByteBuf): T {
104+
private class SerializerCodec<B: ByteBuf, T>(private val serializer: KSerializer<T>) :
105+
StreamCodec<B, T> {
106+
override fun decode(buf: B): T {
107107
return SurfCloudBufSerializer.serializer.decodeFromBuf(buf, serializer)
108108
}
109109

110110
override fun encode(
111-
buf: SurfByteBuf,
111+
buf: B,
112112
value: T
113113
) {
114114
SurfCloudBufSerializer.serializer.encodeToBuf(buf, serializer, value)

surf-cloud-bukkit/src/main/kotlin/dev/slne/surf/cloud/bukkit/netty/network/BukkitSpecificPacketListenerExtension.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import dev.slne.surf.cloud.core.client.server.ClientCloudServerImpl
1313
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.RegistrationInfo
1414
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.ServerboundTransferPlayerPacketResponse
1515
import dev.slne.surf.surfapi.bukkit.api.extensions.server
16+
import dev.slne.surf.surfapi.bukkit.api.nms.NmsUseWithCaution
17+
import dev.slne.surf.surfapi.bukkit.api.nms.bridges.nmsCommonBridge
1618
import dev.slne.surf.surfapi.bukkit.api.util.dispatcher
1719
import kotlinx.coroutines.future.await
1820
import net.kyori.adventure.text.Component
1921
import org.bukkit.Bukkit
2022
import java.net.InetAddress
2123
import java.net.InetSocketAddress
24+
import java.nio.charset.StandardCharsets
2225
import java.util.*
2326
import kotlin.io.path.Path
2427
import kotlin.io.path.inputStream
@@ -100,6 +103,12 @@ class BukkitSpecificPacketListenerExtension : PlatformSpecificPacketListenerExte
100103
return player.teleportAsync(targetPlayer.location).await()
101104
}
102105

106+
@OptIn(NmsUseWithCaution::class)
107+
override fun setVelocitySecret(secret: ByteArray) {
108+
nmsCommonBridge.setVelocityEnabled(true)
109+
nmsCommonBridge.setVelocitySecret(secret.toString(StandardCharsets.UTF_8))
110+
}
111+
103112
override fun triggerShutdown() {
104113
plugin.launch(plugin.globalRegionDispatcher) {
105114
Bukkit.shutdown()

surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/ClientNettyClientImpl.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class ClientNettyClientImpl(
6161
log.atInfo().log(it)
6262
}
6363

64+
override var velocitySecret = ByteArray(0)
65+
6466
/**
6567
* Bootstraps the client. Setup the connection protocol until the PreRunning state.
6668
*/
@@ -72,6 +74,7 @@ class ClientNettyClientImpl(
7274

7375

7476
suspend fun stop() {
77+
velocitySecret.fill(0)
7578
doShutdown()
7679
}
7780

surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/ClientRunningPacketListenerImpl.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import dev.slne.surf.cloud.core.client.util.getOrLoadUser
1919
import dev.slne.surf.cloud.core.client.util.luckperms
2020
import dev.slne.surf.cloud.core.common.coroutines.PacketHandlerScope
2121
import dev.slne.surf.cloud.core.common.netty.network.ConnectionImpl
22+
import dev.slne.surf.cloud.core.common.netty.network.protocol.common.ClientboundSetVelocitySecretPacket
2223
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.*
2324
import dev.slne.surf.cloud.core.common.netty.registry.listener.NettyListenerRegistry
2425
import dev.slne.surf.cloud.core.common.player.playerManagerImpl
@@ -342,6 +343,17 @@ class ClientRunningPacketListenerImpl(
342343
}
343344
}
344345

346+
override fun handleSetVelocitySecret(packet: ClientboundSetVelocitySecretPacket) {
347+
try {
348+
client.velocitySecret = packet.secret
349+
platformExtension.setVelocitySecret(packet.secret)
350+
} catch (e: Throwable) {
351+
log.atWarning()
352+
.withCause(e)
353+
.log("Failed to set velocity secret for packet $packet")
354+
}
355+
}
356+
345357
override fun handlePacket(packet: NettyPacket) {
346358
val listeners = NettyListenerRegistry.getListeners(packet.javaClass) ?: return
347359
if (listeners.isEmpty()) return

surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/ClientSynchronizingPacketListenerImpl.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dev.slne.surf.cloud.core.client.sync.SyncRegistryImpl
1111
import dev.slne.surf.cloud.core.common.coroutines.BeforeStartTaskScope
1212
import dev.slne.surf.cloud.core.common.coroutines.PacketHandlerScope
1313
import dev.slne.surf.cloud.core.common.netty.network.ConnectionImpl
14+
import dev.slne.surf.cloud.core.common.netty.network.protocol.common.ClientboundSetVelocitySecretPacket
1415
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.ClientboundBatchUpdateServer
1516
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.RunningProtocols
1617
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.SyncSetDeltaPacket
@@ -113,6 +114,17 @@ class ClientSynchronizingPacketListenerImpl(
113114
}
114115
}
115116

117+
override fun handleSetVelocitySecret(packet: ClientboundSetVelocitySecretPacket) {
118+
try {
119+
client.velocitySecret = packet.secret
120+
platformExtension.setVelocitySecret(packet.secret)
121+
} catch (e: Exception) {
122+
log.atWarning()
123+
.withCause(e)
124+
.log("Failed to set velocity secret for packet $packet")
125+
}
126+
}
127+
116128
override fun handlePacket(packet: NettyPacket) {
117129
val listeners = NettyListenerRegistry.getListeners(packet.javaClass) ?: return
118130
if (listeners.isEmpty()) return

surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/PlatformSpecificPacketListenerExtension.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ interface PlatformSpecificPacketListenerExtension {
3838

3939
suspend fun teleportPlayerToPlayer(uuid: UUID, target: UUID): Boolean
4040

41+
fun setVelocitySecret(secret: ByteArray)
42+
4143
fun triggerShutdown()
4244

4345
fun restart()

surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/player/ClientCloudPlayerImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import dev.slne.surf.cloud.core.common.player.CommonCloudPlayerImpl
2323
import dev.slne.surf.cloud.core.common.player.ppdc.PersistentPlayerDataContainerImpl
2424
import dev.slne.surf.cloud.core.common.util.hasPermissionPlattform
2525
import dev.slne.surf.surfapi.core.api.messages.adventure.getPointer
26+
import dev.slne.surf.surfapi.core.api.nbt.fast
2627
import net.kyori.adventure.audience.Audience
2728
import net.kyori.adventure.audience.MessageType
2829
import net.kyori.adventure.bossbar.BossBar
@@ -120,7 +121,7 @@ abstract class ClientCloudPlayerImpl<PlatformPlayer : Audience>(uuid: UUID, name
120121
val response = ServerboundRequestPlayerPersistentDataContainer(uuid).fireAndAwaitOrThrow()
121122

122123
val nbt = response.nbt
123-
val container = PersistentPlayerDataContainerImpl(nbt)
124+
val container = PersistentPlayerDataContainerImpl(nbt.fast(true))
124125
val result = container.block()
125126

126127
ServerboundPlayerPersistentDataContainerUpdatePacket(

0 commit comments

Comments
 (0)