-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#66 I think HTTPS is almost working. Tunnel seems to be established b…
…ut getting 400 response.
- Loading branch information
1 parent
dab5b0c
commit 3c5f9c2
Showing
10 changed files
with
186 additions
and
16 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
betamax-netty/src/main/java/co/freeside/betamax/proxy/netty/CallbackNotifier.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,8 @@ | ||
package co.freeside.betamax.proxy.netty; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
|
||
public interface CallbackNotifier { | ||
void onSuccess(ChannelHandlerContext outboundCtx); | ||
void onFailure(ChannelHandlerContext outboundCtx, Throwable cause); | ||
} |
28 changes: 28 additions & 0 deletions
28
betamax-netty/src/main/java/co/freeside/betamax/proxy/netty/DirectClientHandler.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,28 @@ | ||
package co.freeside.betamax.proxy.netty; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
|
||
public final class DirectClientHandler extends ChannelInboundHandlerAdapter { | ||
private static final String name = "DIRECT_CLIENT_HANDLER"; | ||
|
||
public static String getName() { | ||
return name; | ||
} | ||
private final CallbackNotifier cb; | ||
|
||
public DirectClientHandler(CallbackNotifier cb) { | ||
this.cb = cb; | ||
} | ||
|
||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) { | ||
ctx.pipeline().remove(this); | ||
cb.onSuccess(ctx); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) { | ||
cb.onFailure(ctx, throwable); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
betamax-netty/src/main/java/co/freeside/betamax/proxy/netty/DirectClientInitializer.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,20 @@ | ||
package co.freeside.betamax.proxy.netty; | ||
|
||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.channel.socket.SocketChannel; | ||
|
||
public final class DirectClientInitializer extends ChannelInitializer<SocketChannel> { | ||
|
||
private final CallbackNotifier callbackNotifier; | ||
|
||
public DirectClientInitializer(CallbackNotifier callbackNotifier) { | ||
this.callbackNotifier = callbackNotifier; | ||
} | ||
|
||
@Override | ||
public void initChannel(SocketChannel socketChannel) { | ||
ChannelPipeline channelPipeline = socketChannel.pipeline(); | ||
channelPipeline.addLast(DirectClientHandler.getName(), new DirectClientHandler(callbackNotifier)); | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
betamax-netty/src/main/java/co/freeside/betamax/proxy/netty/NettyHelpers.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,14 @@ | ||
package co.freeside.betamax.proxy.netty; | ||
|
||
import io.netty.buffer.*; | ||
import io.netty.channel.*; | ||
|
||
class NettyHelpers { | ||
public static void closeOnFlush(Channel ch) { | ||
if (ch.isActive()) { | ||
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); | ||
} | ||
} | ||
|
||
private NettyHelpers() {} | ||
} |
51 changes: 45 additions & 6 deletions
51
betamax-netty/src/main/java/co/freeside/betamax/proxy/netty/ProxyConnectHandler.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 |
---|---|---|
@@ -1,23 +1,62 @@ | ||
package co.freeside.betamax.proxy.netty; | ||
|
||
import java.net.*; | ||
import io.netty.bootstrap.*; | ||
import io.netty.channel.*; | ||
import io.netty.channel.socket.nio.*; | ||
import io.netty.handler.codec.http.*; | ||
import static io.netty.handler.codec.http.HttpMethod.*; | ||
import static io.netty.handler.codec.http.HttpVersion.*; | ||
|
||
public class ProxyConnectHandler extends SimpleChannelInboundHandler<HttpRequest> { | ||
|
||
private final SocketAddress proxyAddress; | ||
private final Bootstrap bootstrap = new Bootstrap(); | ||
|
||
public ProxyConnectHandler(SocketAddress proxyAddress) { | ||
this.proxyAddress = proxyAddress; | ||
} | ||
|
||
@Override | ||
public boolean acceptInboundMessage(Object message) throws Exception { | ||
System.err.printf("Evaluating %s%n", message); | ||
return super.acceptInboundMessage(message) && CONNECT.equals(((HttpRequest) message).getMethod()); | ||
return super.acceptInboundMessage(message) && isConnectRequest((HttpRequest) message); | ||
} | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext context, HttpRequest request) { | ||
System.err.println("I actually got a CONNECT. Now what?"); | ||
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.METHOD_NOT_ALLOWED); | ||
context.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); | ||
protected void channelRead0(final ChannelHandlerContext context, final HttpRequest request) { | ||
CallbackNotifier callback = new CallbackNotifier() { | ||
@Override | ||
public void onSuccess(final ChannelHandlerContext outboundContext) { | ||
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); | ||
context.channel().writeAndFlush(response) | ||
.addListener(new ChannelFutureListener() { | ||
@Override | ||
public void operationComplete(ChannelFuture channelFuture) { | ||
outboundContext.channel().pipeline().addLast(new RelayHandler(context.channel())); | ||
context.channel().pipeline().addLast(new RelayHandler(outboundContext.channel())); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onFailure(ChannelHandlerContext outboundCtx, Throwable cause) { | ||
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY); | ||
context.channel().writeAndFlush(response); | ||
NettyHelpers.closeOnFlush(context.channel()); | ||
} | ||
}; | ||
|
||
final Channel inboundChannel = context.channel(); | ||
bootstrap.group(inboundChannel.eventLoop()) | ||
.channel(NioSocketChannel.class) | ||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) | ||
.option(ChannelOption.SO_KEEPALIVE, true) | ||
.handler(new DirectClientInitializer(callback)); | ||
|
||
bootstrap.connect(proxyAddress); | ||
} | ||
|
||
private boolean isConnectRequest(HttpRequest request) { | ||
return CONNECT.equals(request.getMethod()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
betamax-netty/src/main/java/co/freeside/betamax/proxy/netty/RelayHandler.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,45 @@ | ||
package co.freeside.betamax.proxy.netty; | ||
|
||
import io.netty.buffer.*; | ||
import io.netty.channel.*; | ||
import io.netty.util.*; | ||
|
||
public final class RelayHandler extends ChannelInboundHandlerAdapter { | ||
private static final String name = "RELAY_HANDLER"; | ||
|
||
public static String getName() { | ||
return name; | ||
} | ||
|
||
private final Channel relayChannel; | ||
|
||
public RelayHandler(Channel relayChannel) { | ||
this.relayChannel = relayChannel; | ||
} | ||
|
||
@Override | ||
public void channelActive(ChannelHandlerContext context) { | ||
context.writeAndFlush(Unpooled.EMPTY_BUFFER); | ||
} | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext context, Object message) { | ||
if (relayChannel.isActive()) { | ||
relayChannel.writeAndFlush(message); | ||
} else { | ||
ReferenceCountUtil.release(message); | ||
} | ||
} | ||
|
||
@Override | ||
public void channelInactive(ChannelHandlerContext context) { | ||
NettyHelpers.closeOnFlush(relayChannel); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { | ||
cause.printStackTrace(); | ||
context.close(); | ||
} | ||
|
||
} |
10 changes: 8 additions & 2 deletions
10
...-netty/src/main/java/co/freeside/betamax/proxy/netty/TunnelingHttpChannelInitializer.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 |
---|---|---|
@@ -1,18 +1,24 @@ | ||
package co.freeside.betamax.proxy.netty; | ||
|
||
import java.net.*; | ||
import io.netty.channel.*; | ||
import io.netty.channel.socket.*; | ||
|
||
public class TunnelingHttpChannelInitializer extends HttpChannelInitializer { | ||
|
||
public TunnelingHttpChannelInitializer(int workerThreads, ChannelHandler handler) { | ||
static final String HANDLER_HTTP_CONNECT = "connector"; | ||
|
||
private final SocketAddress proxyAddress; | ||
|
||
public TunnelingHttpChannelInitializer(int workerThreads, ChannelHandler handler, SocketAddress proxyAddress) { | ||
super(workerThreads, handler); | ||
this.proxyAddress = proxyAddress; | ||
} | ||
|
||
@Override | ||
public void initChannel(SocketChannel channel) throws Exception { | ||
super.initChannel(channel); | ||
|
||
channel.pipeline().addAfter("decoder", "connector", new ProxyConnectHandler()); | ||
channel.pipeline().addAfter("betamaxHandler", HANDLER_HTTP_CONNECT, new ProxyConnectHandler(proxyAddress)); | ||
} | ||
} |
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