Use MessageToMessageDecoder, cancel when preServerboundCheck fail

This commit is contained in:
creeper123123321 2019-11-13 14:41:43 -03:00
parent cda0152a4b
commit 05f4106489
4 changed files with 16 additions and 14 deletions

View File

@ -27,7 +27,7 @@ package com.github.creeper123123321.viafabric.handler.clientside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException;
@ -36,7 +36,7 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.util.List;
public class VRDecodeHandler extends ByteToMessageDecoder {
public class VRDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
private UserConnection user;
public VRDecodeHandler(UserConnection user) {
@ -47,7 +47,7 @@ public class VRDecodeHandler extends ByteToMessageDecoder {
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
CommonTransformer.preClientbound(user);
if (!CommonTransformer.willTransformPacket(user)) {
out.add(msg.readRetainedSlice(msg.readableBytes()));
out.add(msg.retain());
return;
}
ByteBuf draft = msg.alloc().buffer().writeBytes(msg);

View File

@ -44,10 +44,10 @@ public class VREncodeHandler extends MessageToMessageEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if (CommonTransformer.preServerboundCheck(user)) {
throw CancelException.CACHED; // Theoretically a server with m2m decoder would hold this packet, but we'll just cancel for now and this is used for kicking packet spamming
throw CancelException.CACHED; // M2ME expects at least one message
}
if (!CommonTransformer.willTransformPacket(user)) {
out.add(msg.readRetainedSlice(msg.readableBytes()));
out.add(msg.retain());
return;
}
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);

View File

@ -27,7 +27,7 @@ package com.github.creeper123123321.viafabric.handler.serverside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException;
@ -36,7 +36,7 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.util.List;
public class FabricDecodeHandler extends ByteToMessageDecoder {
public class FabricDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
private final UserConnection user;
public FabricDecodeHandler(UserConnection user) {
@ -44,13 +44,15 @@ public class FabricDecodeHandler extends ByteToMessageDecoder {
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (CommonTransformer.preServerboundCheck(user)) return;
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if (CommonTransformer.preServerboundCheck(user)) {
throw CancelException.CACHED;
}
if (!CommonTransformer.willTransformPacket(user)) {
out.add(in.readRetainedSlice(in.readableBytes()));
out.add(msg.retain());
return;
}
ByteBuf draft = ctx.alloc().buffer().writeBytes(in);
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
try {
CommonTransformer.transformServerbound(draft, user, ignored -> CancelException.CACHED);
out.add(draft.retain());

View File

@ -42,13 +42,13 @@ public class FabricEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
}
@Override
protected void encode(final ChannelHandlerContext ctx, ByteBuf in, final List<Object> out) throws Exception {
protected void encode(final ChannelHandlerContext ctx, ByteBuf msg, final List<Object> out) throws Exception {
CommonTransformer.preClientbound(user);
if (!CommonTransformer.willTransformPacket(user)) {
out.add(in.readRetainedSlice(in.readableBytes()));
out.add(msg.readRetainedSlice(msg.readableBytes()));
return;
}
ByteBuf draft = ctx.alloc().buffer().writeBytes(in);
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
try {
CommonTransformer.transformClientbound(draft, user, ignored -> CancelException.CACHED);
out.add(draft.retain());