Last active
September 8, 2018 10:27
-
-
Save Proximyst/184a643fb6bc9983f899fc275fc6721e to your computer and use it in GitHub Desktop.
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
package com.proximyst.ussrm.impl | |
import com.proximyst.ussrm.packet.lowlevel.Packet | |
import com.proximyst.ussrm.packet.lowlevel.PacketEncoding | |
import com.proximyst.ussrm.packet.lowlevel.data.VarInt | |
import com.proximyst.ussrm.packet.lowlevel.data.toPacket | |
import com.proximyst.ussrm.packet.lowlevel.data.toVarInt | |
import com.proximyst.ussrm.packet.lowlevel.packet.handshake.serverbound.PacketHandshakeServerboundHandshake | |
import com.proximyst.ussrm.packet.lowlevel.packet.login.clientbound.PacketLoginClientboundDisconnect | |
import com.proximyst.ussrm.packet.lowlevel.packet.status.clientbound.PacketStatusClientboundResponse | |
import io.netty.buffer.ByteBuf | |
import io.netty.buffer.Unpooled | |
import io.netty.channel.ChannelHandlerContext | |
import io.netty.channel.ChannelInboundHandlerAdapter | |
import java.util.* | |
class NettyHandler : ChannelInboundHandlerAdapter() { | |
override fun channelRead(ctx : ChannelHandlerContext, msg : Any) { | |
if (msg !is ByteBuf) return | |
val byteArray = ByteArray(msg.readableBytes()) | |
msg.readBytes(byteArray) | |
msg.resetReaderIndex() | |
println(String(byteArray)) | |
println(buildString { | |
byteArray.forEach { | |
this.append(String.format("0x%x ", it)) | |
} | |
}) | |
if (byteArray[0] == 0xFE.toByte() && byteArray.size > 1) { | |
ctx.write(0xFF.toByte()) | |
ctx.write("§1${0.toChar()}127${0.toChar()}4.2.0${0.toChar()}seb fat${0.toChar()}0${0.toChar()}1337".toPacket().encode()) | |
msg.release() | |
ctx.disconnect() | |
ctx.close() | |
return | |
} | |
val packet = PacketEncoding.UNCOMPRESSED.decode(msg) | |
fun release() { | |
packet.release() | |
msg.release() | |
ctx.flush() | |
ctx.close() | |
} | |
when ( | |
val packetId = VarInt.decode(packet)?.contained() ?: run { release(); return }) { | |
0x00 -> { | |
val handshake = PacketHandshakeServerboundHandshake.decode(packet) ?: run { release(); return } | |
if (handshake.nextState == Packet.State.LOGIN) { | |
ctx.write( | |
PacketLoginClientboundDisconnect( | |
mutableMapOf( | |
"reason" to """ | |
{ | |
"text": "login state achieved ($packetId)" | |
} | |
""".trimIndent() | |
) | |
).encode() | |
) | |
} else { | |
val toWrite = Unpooled.buffer() | |
toWrite.writeBytes(PacketStatusClientboundResponse.id.toVarInt().encode()) | |
toWrite.writeBytes( | |
PacketStatusClientboundResponse( | |
mutableMapOf( | |
"response" to """ | |
{ | |
"version": { | |
"name": "version $packetId", | |
"protocol": ${handshake.protocolVersion - 10} | |
}, | |
"players": { | |
"max": 1337, | |
"online": 420, | |
"sample": [ | |
{ | |
"name": "dab on the haters", | |
"id": "${UUID.nameUUIDFromBytes("OfflinePlayer dab on the haters".toByteArray())}" | |
} | |
] | |
}, | |
"description": { | |
"text": "fat fat fat", | |
"bold": true, | |
"color": "red" | |
} | |
} | |
""".trimIndent() | |
).also { println(it) } as MutableMap<String, Any?> | |
).encode() | |
) | |
ctx.write(toWrite.readableBytes().toVarInt().encode()) | |
ctx.write(toWrite) | |
} | |
} | |
} | |
release() | |
} | |
override fun exceptionCaught(ctx : ChannelHandlerContext, cause : Throwable) { | |
cause.printStackTrace() | |
ctx.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment