diff --git a/pom.xml b/pom.xml index 0d8dfef1..8954e267 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ unknown - 4.0.0.CR3 + 4.0.0.CR6 UTF-8 diff --git a/proxy/src/main/java/net/md_5/bungee/netty/CipherBase.java b/proxy/src/main/java/net/md_5/bungee/netty/CipherBase.java index f5999cdc..0c12c18c 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/CipherBase.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/CipherBase.java @@ -1,6 +1,7 @@ package net.md_5.bungee.netty; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; import javax.crypto.Cipher; import javax.crypto.ShortBufferException; import lombok.AccessLevel; @@ -31,7 +32,7 @@ public class CipherBase } } - protected void cipher(ByteBuf in, ByteBuf out) throws ShortBufferException + private byte[] bufToByte(ByteBuf in) { byte[] heapIn = heapInLocal.get(); int readableBytes = in.readableBytes(); @@ -41,6 +42,24 @@ public class CipherBase heapInLocal.set( heapIn ); } in.readBytes( heapIn, 0, readableBytes ); + return heapIn; + } + + protected ByteBuf cipher(ChannelHandlerContext ctx, ByteBuf in) throws ShortBufferException + { + int readableBytes = in.readableBytes(); + byte[] heapIn = bufToByte( in ); + + ByteBuf heapOut = ctx.alloc().heapBuffer( cipher.getOutputSize( readableBytes ) ); + heapOut.writerIndex( cipher.update( heapIn, 0, readableBytes, heapOut.array(), heapOut.arrayOffset() )); + + return heapOut; + } + + protected void cipher(ByteBuf in, ByteBuf out) throws ShortBufferException + { + int readableBytes = in.readableBytes(); + byte[] heapIn = bufToByte( in ); byte[] heapOut = heapOutLocal.get(); int outputSize = cipher.getOutputSize( readableBytes ); diff --git a/proxy/src/main/java/net/md_5/bungee/netty/CipherDecoder.java b/proxy/src/main/java/net/md_5/bungee/netty/CipherDecoder.java index cc4055b3..91fba349 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/CipherDecoder.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/CipherDecoder.java @@ -2,10 +2,11 @@ package net.md_5.bungee.netty; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToByteDecoder; +import io.netty.channel.MessageList; +import io.netty.handler.codec.MessageToMessageDecoder; import javax.crypto.Cipher; -public class CipherDecoder extends ByteToByteDecoder +public class CipherDecoder extends MessageToMessageDecoder { private final CipherBase cipher; @@ -16,8 +17,8 @@ public class CipherDecoder extends ByteToByteDecoder } @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception + protected void decode(ChannelHandlerContext ctx, ByteBuf msg, MessageList out) throws Exception { - cipher.cipher( in, out ); + out.add( cipher.cipher( ctx, msg ) ); } } diff --git a/proxy/src/main/java/net/md_5/bungee/netty/CipherEncoder.java b/proxy/src/main/java/net/md_5/bungee/netty/CipherEncoder.java index 6d5232e3..7e5c0ec6 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/CipherEncoder.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/CipherEncoder.java @@ -2,10 +2,10 @@ package net.md_5.bungee.netty; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToByteEncoder; +import io.netty.handler.codec.MessageToByteEncoder; import javax.crypto.Cipher; -public class CipherEncoder extends ByteToByteEncoder +public class CipherEncoder extends MessageToByteEncoder { private final CipherBase cipher; diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index c321044c..c3ed29a8 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -2,7 +2,8 @@ package net.md_5.bungee.netty; import com.google.common.base.Preconditions; import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundMessageHandlerAdapter; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.MessageList; import io.netty.handler.timeout.ReadTimeoutException; import java.io.IOException; import java.util.logging.Level; @@ -16,7 +17,7 @@ import net.md_5.bungee.connection.PingHandler; * channels to maintain simple states, and only call the required, adapted * methods when the channel is connected. */ -public class HandlerBoss extends ChannelInboundMessageHandlerAdapter +public class HandlerBoss extends ChannelInboundHandlerAdapter { private ChannelWrapper channel; @@ -58,27 +59,30 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter } @Override - public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception + public void messageReceived(ChannelHandlerContext ctx, MessageList msgs) throws Exception { - if ( handler != null && ctx.channel().isActive() ) + for ( Object msg : msgs ) { - if ( msg instanceof PacketWrapper ) + if ( handler != null && ctx.channel().isActive() ) { - boolean sendPacket = true; - try + if ( msg instanceof PacketWrapper ) { - ( (PacketWrapper) msg ).packet.handle( handler ); - } catch ( CancelSendSignal ex ) + boolean sendPacket = true; + try + { + ( (PacketWrapper) msg ).packet.handle( handler ); + } catch ( CancelSendSignal ex ) + { + sendPacket = false; + } + if ( sendPacket ) + { + handler.handle( ( (PacketWrapper) msg ).buf ); + } + } else { - sendPacket = false; + handler.handle( (byte[]) msg ); } - if ( sendPacket ) - { - handler.handle( ( (PacketWrapper) msg ).buf ); - } - } else - { - handler.handle( (byte[]) msg ); } } } diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java b/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java index 1ac7106d..6c4ef785 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java @@ -1,8 +1,8 @@ package net.md_5.bungee.netty; import io.netty.buffer.ByteBuf; -import io.netty.buffer.MessageBuf; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.MessageList; import io.netty.handler.codec.ReplayingDecoder; import lombok.AllArgsConstructor; import lombok.Getter; @@ -28,7 +28,7 @@ public class PacketDecoder extends ReplayingDecoder private Protocol protocol; @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, MessageBuf out) throws Exception + protected void decode(ChannelHandlerContext ctx, ByteBuf in, MessageList out) throws Exception { // While we have enough data while ( true )