Fix the detection of encrypted data. See #655

This commit is contained in:
Norman Maurer 2012-10-16 13:23:18 +02:00
parent 0c82f5d2e5
commit 52d64afa2f

View File

@ -55,42 +55,37 @@ public class PortUnificationServerHandler extends FrameDecoder {
@Override @Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
// Will use the first 5 bytes to detect a protocol.
// Will use the first two bytes to detect a protocol. if (buffer.readableBytes() < 5) {
if (buffer.readableBytes() < 2) {
return null; return null;
} }
final int magic1 = buffer.getUnsignedByte(buffer.readerIndex()); if (isSsl(buffer)) {
final int magic2 = buffer.getUnsignedByte(buffer.readerIndex() + 1);
if (isSsl(magic1)) {
enableSsl(ctx); enableSsl(ctx);
} else if (isGzip(magic1, magic2)) {
enableGzip(ctx);
} else if (isHttp(magic1, magic2)) {
switchToHttp(ctx);
} else if (isFactorial(magic1)) {
switchToFactorial(ctx);
} else { } else {
// Unknown protocol; discard everything and close the connection. final int magic1 = buffer.getUnsignedByte(buffer.readerIndex());
buffer.skipBytes(buffer.readableBytes()); final int magic2 = buffer.getUnsignedByte(buffer.readerIndex() + 1);
ctx.getChannel().close(); if (isGzip(magic1, magic2)) {
return null; enableGzip(ctx);
} else if (isHttp(magic1, magic2)) {
switchToHttp(ctx);
} else if (isFactorial(magic1)) {
switchToFactorial(ctx);
} else {
// Unknown protocol; discard everything and close the connection.
buffer.skipBytes(buffer.readableBytes());
ctx.getChannel().close();
return null;
}
} }
// Forward the current read buffer as is to the new handlers. // Forward the current read buffer as is to the new handlers.
return buffer.readBytes(buffer.readableBytes()); return buffer.readBytes(buffer.readableBytes());
} }
private boolean isSsl(int magic1) { private boolean isSsl(ChannelBuffer buffer) {
if (detectSsl) { if (detectSsl) {
switch (magic1) { return SslHandler.isEncrypted(buffer);
case 20: case 21: case 22: case 23: case 255:
return true;
default:
return magic1 >= 128;
}
} }
return false; return false;
} }