Hooking TCP client connections into HTTP response

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Hooking TCP client connections into HTTP response

pledbrook
Hi,

I'm playing around with Ratpack as a very basic authenticating HTTP proxy. Based on previous help, I got something working for HTTP. But HTTPS is a whole different kettle of fish.

The idea is that the HTTP client sends a plain HTTP CONNECT request, which the proxy server should use to set up a plain TCP connection to the target server. I've looked through the Ratpack API docs but it seems to be focused on HTTP. So I'm assuming that I need to use native Netty to open a TCP client connection. In that case, does anyone have an idea of how to open the client connection and stream data from the initial HTTP request to the TCP tunnel, and stream the data from the TCP tunnel to the HTTP response?

The HttpClient class seems to have nice `requestStream()` and `forwardTo()` methods, so I guess I may have a look at the source code for those. I don't really want to go too far down the rabbit hole for this though.

Thanks,

Peter
rus
Reply | Threaded
Open this post in threaded view
|

Re: Hooking TCP client connections into HTTP response

rus
Without doing lots of work yourself you have to use netty's ProxyHandler I believe. This is a netty level channel handler that deals with all the connect stuff.

I tried a while back to integrate this but I had keep alive issues and don't think I could quite get it working.

I think you might have more luck working with netty direct. This test case shows the support they've added https://github.com/netty/netty/blob/add7efc2d4f818bfb02cef7a34c701827e83ddf7/handler-proxy/src/test/java/io/netty/handler/proxy/ProxyHandlerTest.java#L284

Once you have something working with netty it might be easier to add the support. I'd be very interested on where you get with this and will help where I can
rus
Reply | Threaded
Open this post in threaded view
|

Re: Hooking TCP client connections into HTTP response

rus
Sorry for the confusion but my previous reply was actually about using our http client via a proxy and not about using ratpack as a proxy.  I have done work on both and mixed the two up.

I'll see if I can find my previous work.
rus
Reply | Threaded
Open this post in threaded view
|

Re: Hooking TCP client connections into HTTP response

rus
This doesn't work but is where I got to https://gist.github.com/rhart/459d25a303087262e6c4
Reply | Threaded
Open this post in threaded view
|

Re: Hooking TCP client connections into HTTP response

pledbrook
In reply to this post by rus
I give up on this. I'm lost within the mechanics of Netty and Ratpack. And I've discovered through Betamax that there is already a Netty-based HTTP proxy: https://github.com/adamfisk/LittleProxy

One thing that may be worth looking into is a set of client classes that make it easier to integrate between Ratpack and raw Netty. DefaultHttpClient does this, but the code is not for the faint of heart.
Reply | Threaded
Open this post in threaded view
|

Re: Hooking TCP client connections into HTTP response

pledbrook
In reply to this post by rus
I didn't see this before posting the previous reply. It certainly looks a lot less code than I have! I added a comment direct to the gist about the CONNECT response. That may help. Thanks for your responses btw.
rus
Reply | Threaded
Open this post in threaded view
|

Re: Hooking TCP client connections into HTTP response

rus
Np.  

I've updated the gist with something that was blocking sending the responses from the target server back to the client.  I'll try your suggestion too.

It feels very close but using a purpose built proxy is probably better.  However, I do have a personal mission to get this working :)