Skip to content

Commit

Permalink
handle case when no hostname present
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Mar 11, 2024
1 parent 51b64e4 commit 0aba5bf
Showing 1 changed file with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.netty.handler.ssl.SniHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.util.Mapping;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,30 +32,36 @@ public CustomSniHandler(Mapping<? super String, ? extends SslContext> mapping, B
}

@Override
protected void replaceHandler(ChannelHandlerContext channelHandlerContext, String hostname, SslContext sslContext) throws Exception {
protected void replaceHandler(ChannelHandlerContext channelHandlerContext, @Nullable String hostname, SslContext sslContext) throws Exception {
ChannelPipeline pipeline = channelHandlerContext.pipeline();
// TODO no hostname
if(ignoredHosts.allMatches(hostname).hasNext()){
if(hostname == null){
LOG.error("no hostname present - aborting connection");
channelHandlerContext.close();
}else if(ignoredHosts.allMatches(hostname).hasNext()){
LOG.info("skipping hostname {}", hostname);

boolean foundThis = false;

for(Iterator<Map.Entry<String, ChannelHandler>> it = pipeline.iterator(); it.hasNext();){
ChannelHandler handler = it.next().getValue();
if(foundThis){
it.remove();
}
if(handler == this){
foundThis = true;
}
}

if(!foundThis){
throw new IllegalStateException("cannot find self handler in pipeline");
}
pipeline.replace(this, "forwardNoProcess", new RawForwardIncomingRequestHandler(hostname, clientBootstrapTemplate));
replaceWithRawForward(hostname, pipeline);
}else{
super.replaceHandler(channelHandlerContext, hostname, sslContext);
}
}

private void replaceWithRawForward(String hostname, ChannelPipeline pipeline) {
boolean foundThis = false;

for(Iterator<Map.Entry<String, ChannelHandler>> it = pipeline.iterator(); it.hasNext();){
ChannelHandler handler = it.next().getValue();
if(foundThis){
it.remove();
}
if(handler == this){
foundThis = true;
}
}

if(!foundThis){
throw new IllegalStateException("cannot find self handler in pipeline");
}
pipeline.replace(this, "forwardNoProcess", new RawForwardIncomingRequestHandler(hostname, clientBootstrapTemplate));
}
}

0 comments on commit 0aba5bf

Please sign in to comment.