Created
August 9, 2015 15:21
-
-
Save sriki77/3e22b40846f1f3e05a6b to your computer and use it in GitHub Desktop.
SSL Unification + Netty + Proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SSLUnificationHandler extends ChannelInboundByteHandlerAdapter { | |
static final String NAME = "HttpSsLUnificationHandler"; | |
protected void addSSLHandler(ChannelPipeline pipeline) throws Exception { | |
//SSL Engine init.... | |
pipeline.addAfter(NAME, "sslHandler", new SslHandler(sslEngine)); | |
} | |
@Override | |
protected void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception { | |
if (isConnectRequest(in)) { | |
updateNextInboundBuffer(ctx, in.readBytes(in.readableBytes())); | |
return; | |
} | |
ChannelPipeline pipeline = ctx.pipeline(); | |
if (SslHandler.isEncrypted(in)) { | |
addSSLHandler(pipeline); | |
} | |
ByteBuf byteBuf = in.readBytes(in.readableBytes()); | |
pipeline.remove(this); | |
updateNextInboundBuffer(ctx, byteBuf); | |
} | |
private void updateNextInboundBuffer(ChannelHandlerContext ctx, ByteBuf byteBuf) { | |
ctx.nextInboundByteBuffer().writeBytes(byteBuf); | |
ctx.fireInboundBufferUpdated(); | |
} | |
private boolean isConnectRequest(ByteBuf in) { | |
final int firstChar = in.getUnsignedByte(in.readerIndex()); | |
final int secondChar = in.getUnsignedByte(in.readerIndex() + 1); | |
return firstChar == 'C' && secondChar == 'O'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment