Reenable encryption + online mode.

This commit is contained in:
md_5 2013-03-14 17:46:57 +11:00
parent d5f25b07e5
commit 58f1ab208b
2 changed files with 44 additions and 16 deletions

View File

@ -119,8 +119,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
Preconditions.checkArgument( handshake.username.length() <= 16, "Cannot have username longer than 16 characters" ); Preconditions.checkArgument( handshake.username.length() <= 16, "Cannot have username longer than 16 characters" );
this.handshake = handshake; this.handshake = handshake;
ch.write( forgeMods ); ch.write( forgeMods );
handle( (PacketCDClientStatus) null ); ch.write( request = EncryptionUtil.encryptRequest() );
//ch.write( request = EncryptionUtil.encryptRequest() );
thisState = State.ENCRYPT; thisState = State.ENCRYPT;
} }
@ -202,7 +201,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override @Override
public void handle(PacketCDClientStatus clientStatus) throws Exception public void handle(PacketCDClientStatus clientStatus) throws Exception
{ {
// Preconditions.checkState( thisState == State.LOGIN, "Not expecting LOGIN" ); Preconditions.checkState( thisState == State.LOGIN, "Not expecting LOGIN" );
UserConnection userCon = new UserConnection( (BungeeCord) bungee, ch, this, handshake, forgeLogin, loginMessages ); UserConnection userCon = new UserConnection( (BungeeCord) bungee, ch, this, handshake, forgeLogin, loginMessages );
ch.pipeline().get( HandlerBoss.class ).setHandler( new UpstreamBridge( bungee, userCon ) ); ch.pipeline().get( HandlerBoss.class ).setHandler( new UpstreamBridge( bungee, userCon ) );

View File

@ -15,6 +15,7 @@ public class CipherCodec extends ByteToByteCodec
private Cipher encrypt; private Cipher encrypt;
private Cipher decrypt; private Cipher decrypt;
private ByteBuf heapIn;
private ByteBuf heapOut; private ByteBuf heapOut;
public CipherCodec(Cipher encrypt, Cipher decrypt) public CipherCodec(Cipher encrypt, Cipher decrypt)
@ -39,17 +40,23 @@ public class CipherCodec extends ByteToByteCodec
public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception
{ {
super.freeInboundBuffer( ctx ); super.freeInboundBuffer( ctx );
if ( heapOut != null ) free();
{
heapOut.release();
heapOut = null;
}
} }
@Override @Override
public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception
{ {
super.freeOutboundBuffer( ctx ); super.freeOutboundBuffer( ctx );
free();
}
private void free()
{
if ( heapIn != null )
{
heapIn.release();
heapIn = null;
}
if ( heapOut != null ) if ( heapOut != null )
{ {
heapOut.release(); heapOut.release();
@ -59,25 +66,47 @@ public class CipherCodec extends ByteToByteCodec
private void cipher(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out, Cipher cipher) throws Exception private void cipher(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out, Cipher cipher) throws Exception
{ {
synchronized ( this ) try
{ {
// Allocate input buffer
if ( heapIn == null )
{
heapIn = ctx.alloc().heapBuffer();
}
// Allocate correct amount of space
int readableBytes = in.readableBytes();
heapIn.capacity( heapIn.writerIndex() + readableBytes );
// Read into buffer
in.readBytes( heapIn );
// Allocate output buffer
if ( heapOut == null ) if ( heapOut == null )
{ {
heapOut = ctx.alloc().heapBuffer(); heapOut = ctx.alloc().heapBuffer();
} }
int available = in.readableBytes(); // Get output size
int outputSize = cipher.getOutputSize( available ); int outputSize = cipher.getOutputSize( readableBytes );
if ( heapOut.capacity() < outputSize ) // Check we have enough space
if ( heapOut.writableBytes() < outputSize )
{ {
heapOut.capacity( outputSize ); heapOut.capacity( heapOut.writerIndex() + outputSize );
} }
int processed = cipher.update( in.array(), in.arrayOffset() + in.readerIndex(), available, heapOut.array(), heapOut.arrayOffset() + heapOut.writerIndex() );
in.readerIndex( in.readerIndex() + processed ); // Do the processing
int processed = cipher.update( heapIn.array(), heapIn.arrayOffset() + heapIn.readerIndex(), readableBytes, heapOut.array(), heapOut.arrayOffset() + heapOut.writerIndex() );
// Tell the out buffer we read some from it
heapOut.writerIndex( heapOut.writerIndex() + processed ); heapOut.writerIndex( heapOut.writerIndex() + processed );
out.writeBytes( heapOut ); out.writeBytes( heapOut );
heapOut.discardSomeReadBytes(); heapIn.clear();
heapOut.clear();
} catch ( Throwable ex )
{
// TODO: Remove this once we are stable
ex.printStackTrace();
} }
} }
} }