Skip to content

Instantly share code, notes, and snippets.

@sriki77
Created August 9, 2015 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sriki77/3e22b40846f1f3e05a6b to your computer and use it in GitHub Desktop.
Save sriki77/3e22b40846f1f3e05a6b to your computer and use it in GitHub Desktop.
SSL Unification + Netty + Proxy
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