Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.