Skip to content

Commit

Permalink
Change pipeline injection for paper
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzaurora committed May 7, 2023
1 parent 1b067f6 commit 2154359
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package co.pvphub.blossom.backend
import co.pvphub.blossom.BlossomManager
import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.MessageToByteEncoder
import org.bukkit.plugin.java.JavaPlugin
import io.netty.channel.ChannelOutboundHandlerAdapter
import io.netty.channel.ChannelPromise
import io.netty.channel.SimpleChannelInboundHandler
import io.papermc.paper.network.ChannelInitializeListenerHolder.addListener
import net.kyori.adventure.key.Key
import net.minecraft.network.protocol.Packet
import org.bukkit.plugin.java.JavaPlugin
import java.net.InetSocketAddress
import java.net.SocketAddress

class BlossomBackend : JavaPlugin() {

Expand All @@ -18,16 +20,26 @@ class BlossomBackend : JavaPlugin() {
// disabled until further discussionconfig.getString("token")!!
)
addListener(Key.key("blossom", "guard")) {
it.pipeline().addAfter("packet_handler", "blossom_guard", object : MessageToByteEncoder<Packet<*>>() {
override fun encode(ctx: ChannelHandlerContext, msg: Packet<*>?, out: ByteBuf?) {
it.pipeline().addFirst("blossom_guard", object : SimpleChannelInboundHandler<Any>() {
var isBlacklisted = false

override fun channelActive(ctx: ChannelHandlerContext) {
super.channelActive(ctx)
val remoteAddress = ctx.channel().remoteAddress()
if(remoteAddress is InetSocketAddress) {
println(manager.bush.isIpBlacklisted(remoteAddress.address.hostAddress))
if(manager.bush.isIpBlacklisted(remoteAddress.address.hostAddress)) {
it.disconnect()
isBlacklisted = true
ctx.disconnect()
} else {
ctx.pipeline().remove("blossom_guard")
}
}
}

override fun channelRead0(ctx: ChannelHandlerContext?, msg: Any?) {
if(!isBlacklisted) super.channelRead(ctx, msg)
}

})
}
Expand Down

0 comments on commit 2154359

Please sign in to comment.