-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
226 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
58 changes: 58 additions & 0 deletions
58
...java/io/github/danthe1st/httpsintercept/handler/raw/RawForwardIncomingRequestHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.github.danthe1st.httpsintercept.handler.raw; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Handler for requests that need to be forwarded to the requested server. | ||
* This class accepts raw SSL/TLS requests that should be forwarded without decoding/re-encoding. | ||
* It sends a request to the target server which is then processed by {@link RawForwardedOutgoingRequestHandler}. | ||
*/ | ||
public final class RawForwardIncomingRequestHandler extends ChannelInboundHandlerAdapter { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RawForwardIncomingRequestHandler.class); | ||
|
||
private final String hostname; | ||
private final Bootstrap clientBootstrapTemplate; | ||
private Channel outChannel = null; | ||
|
||
public RawForwardIncomingRequestHandler(String hostname, Bootstrap clientBootstrapTemplate) { | ||
this.hostname = hostname; | ||
this.clientBootstrapTemplate = clientBootstrapTemplate; | ||
} | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws InterruptedException { | ||
if(outChannel == null){ | ||
Bootstrap actualClientBootstrap = clientBootstrapTemplate.clone() | ||
.handler(new RawForwardedOutgoingRequestHandler(ctx)); | ||
try{ | ||
outChannel = actualClientBootstrap.connect(hostname, 443) | ||
.sync() | ||
.channel(); | ||
}catch(RuntimeException e){ | ||
// this can happen due to internet connection issues or something on the remove side | ||
// cannot do much more than aborting the connection | ||
// because this is before a TLS connection is established (when the client hello packet is read) | ||
LOG.error("An exception occured trying to establish a raw connection for forwarding", e); | ||
ctx.channel().close(); | ||
return; | ||
} | ||
} | ||
|
||
outChannel.writeAndFlush(msg).sync(); | ||
} | ||
|
||
@Override | ||
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { | ||
super.channelUnregistered(ctx); | ||
if(outChannel != null){ | ||
outChannel.close(); | ||
} | ||
ctx.channel().close(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...va/io/github/danthe1st/httpsintercept/handler/raw/RawForwardedOutgoingRequestHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.danthe1st.httpsintercept.handler.raw; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.channel.socket.SocketChannel; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Handler for requests that are forwarded to the requested server without decoding/re-encoding. | ||
* @see RawForwardIncomingRequestHandler | ||
*/ | ||
public final class RawForwardedOutgoingRequestHandler extends ChannelInitializer<SocketChannel> { | ||
private static final Logger LOG = LoggerFactory.getLogger(RawForwardedOutgoingRequestHandler.class); | ||
|
||
private final ChannelHandlerContext originalClientContext; | ||
|
||
public RawForwardedOutgoingRequestHandler(ChannelHandlerContext originalClientContext) { | ||
this.originalClientContext = originalClientContext; | ||
} | ||
|
||
@Override | ||
protected void initChannel(SocketChannel ch) throws Exception { | ||
ChannelPipeline p = ch.pipeline(); | ||
p.addLast(new ResponseHandler()); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
LOG.error("An exception occured while forwarding a request", cause); | ||
originalClientContext.channel().close(); | ||
ctx.channel().close(); | ||
} | ||
|
||
@Override | ||
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { | ||
super.channelUnregistered(ctx); | ||
LOG.info("channel unregistered"); | ||
} | ||
|
||
private final class ResponseHandler extends ChannelInboundHandlerAdapter { | ||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
LOG.debug("read: {}", msg); | ||
originalClientContext.writeAndFlush(msg); | ||
} | ||
|
||
@Override | ||
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { | ||
LOG.info("channel unregistered"); | ||
originalClientContext.channel().close(); | ||
ctx.channel().close(); | ||
super.channelUnregistered(ctx); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
LOG.error("An exception occured while forwarding a raw request", cause); | ||
originalClientContext.channel().close(); | ||
ctx.channel().close(); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/io/github/danthe1st/httpsintercept/handler/sni/CustomSniHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.github.danthe1st.httpsintercept.handler.sni; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import io.github.danthe1st.httpsintercept.handler.raw.RawForwardIncomingRequestHandler; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.handler.ssl.SniHandler; | ||
import io.netty.handler.ssl.SslContext; | ||
import io.netty.util.Mapping; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class CustomSniHandler extends SniHandler { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CustomSniHandler.class); | ||
|
||
private final Bootstrap clientBootstrapTemplate; | ||
|
||
private final Set<String> ignoredHosts; | ||
|
||
public CustomSniHandler(Mapping<? super String, ? extends SslContext> mapping, Bootstrap clientBootstrapTemplate) throws IOException { | ||
super(mapping); | ||
this.clientBootstrapTemplate = clientBootstrapTemplate; | ||
|
||
ignoredHosts = getIgnoredHosts(); | ||
} | ||
|
||
private Set<String> getIgnoredHosts() throws IOException { | ||
Path ignoredHostsPath = Path.of("ignoredHosts.txt"); | ||
if(!Files.exists(ignoredHostsPath)){ | ||
Files.createFile(ignoredHostsPath); | ||
return Collections.emptySet(); | ||
} | ||
try(Stream<String> ignoredHostStream = Files.lines(ignoredHostsPath)){ | ||
return ignoredHostStream.collect(Collectors.toSet()); | ||
} | ||
} | ||
|
||
@Override | ||
protected void replaceHandler(ChannelHandlerContext channelHandlerContext, String hostname, SslContext sslContext) throws Exception { | ||
ChannelPipeline pipeline = channelHandlerContext.pipeline(); | ||
if(ignoredHosts.contains(hostname)){ | ||
LOG.debug("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)); | ||
}else{ | ||
super.replaceHandler(channelHandlerContext, hostname, sslContext); | ||
} | ||
} | ||
} |
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