# General purpose TCP proxy?

**URL:** <https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677>\
**Category:** Ruby\
**Created:** [September 28, 2010, 12:48am UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677 "2010-09-28T00:48:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 28, 2010, 12:48am UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/1 "2010-09-28T00:48:08Z")

</div>

Hi,

I’m trying to figure out if there’s a gem or software available in Ruby  
which allows me to act as a general purpose in-between proxy between a  
server and multiple clients?

Basically:

– –   
-   
-   
etc.

It’s not an HTTP server, pure TCP with it’s own (documented) protocol.  
My idea is to be able to act in-between and put certain restrictions on  
clients that the server itself is not capable of.

Connections can be long-running, hours or days. I’m not planning on high  
traffic (although real traffic requirements are not known to me yet),  
but the number of clients will max around 15 or 20, not yet decided.

So, before I start into action on my own I was wandering if there’s  
something out there?

thanks for pointer,

- Markus

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 28, 2010, 1:17am UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/2 "2010-09-28T01:17:28Z")

</div>

Afternoon Markus,

On Mon, Sep 27, 2010 at 3:47 PM, Markus F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> I’m trying to figure out if there’s a gem or software available in Ruby  
> which allows me to act as a general purpose in-between proxy between a  
> server and multiple clients?

Transparent proxy - [GitHub - mojombo/proxymachine: A simple TCP routing proxy built on EventMachine that lets you configure the routing logic in Ruby.](http://github.com/mojombo/proxymachine/)

Transparent or Intercepting - [GitHub - igrigorik/em-proxy: EventMachine Proxy DSL for writing high-performance transparent / intercepting proxies in Ruby](http://github.com/igrigorik/em-proxy)

John

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 28, 2010, 8:53am UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/3 "2010-09-28T08:53:53Z")

</div>

Hi!

On 28.09.2010 01:16, John W Higgins wrote:

> Transparent or Intercepting - [GitHub - igrigorik/em-proxy: EventMachine Proxy DSL for writing high-performance transparent / intercepting proxies in Ruby](http://github.com/igrigorik/em-proxy)

This one looks very promising based on the slides and the documentation.

However I’ve instantly ran into troubles outlined here:  
[Issues · igrigorik/em-proxy · GitHub](http://github.com/igrigorik/em-proxy/issues#issue/3) . Any idea?

Another thing I’m not sure: when I look at the simply forwarding example  
in the README.rdoc, I see that I basically have hook methods (on\_data,  
on\_response, etc.) onto which I can attach my own code.

What I would need is a per client/connection logic. I.e. a client  
connects, I inspect and forward the traffic. I also my intercept some  
requests from the client and send back my own stuff. Ultimately I have a  
state per client.

From the examples it’s unclear to me how would I be able to identify the  
different clients through the whole connection process?

thanks,

- Markus

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 28, 2010, 11:00am UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/4 "2010-09-28T11:00:58Z")

</div>

This will probably (be hackable to) do what you want:  
[http://github.com/cjheath/loggingproxy](http://github.com/cjheath/loggingproxy)

Clifford H., Data Constellation. [http://dataconstellation.com](http://dataconstellation.com)  
Agile Information Management and Design

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 28, 2010, 12:53pm UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/5 "2010-09-28T12:53:25Z")

</div>

Whats wrong with haproxy ?

Works for raw TCP sockets.

On Tue, Sep 28, 2010 at 2:30 PM, Clifford H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This will probably (be hackable to) do what you want:  
> [http://github.com/cjheath/loggingproxy](http://github.com/cjheath/loggingproxy)
> 
> Clifford H., Data Constellation. [http://dataconstellation.com](http://dataconstellation.com)  
> Agile Information Management and Design

–  
Let them talk of their oriental summer climes of everlasting  
conservatories; give me the privilege of making my own summer with my  
own coals.

[http://gnufied.org](http://gnufied.org)

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 28, 2010, 11:43pm UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/6 "2010-09-28T23:43:23Z")

</div>

On 28.09.2010 12:44, hemant wrote:

> Whats wrong with haproxy ?
> 
> Works for raw TCP sockets.

I don’t know … does it allow my to have per Client connection logic and  
traffic interception, scripted, custom answers and rejection? Ideally in  
Ruby.

- Markus

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 28, 2010, 12:54pm UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/7 "2010-09-28T12:54:36Z")

</div>

On Tue, Sep 28, 2010 at 4:14 PM, hemant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Whats wrong with haproxy ?
> 
> Works for raw TCP sockets.

And sorry for top posting.

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 29, 2010, 12:04am UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/8 "2010-09-29T00:04:27Z")

</div>

Don’t apologize, the anti-top posting nazis need to learn TIMTOWTDI some  
day

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [September 29, 2010, 5:25am UTC](https://www.ruby-forum.com/t/general-purpose-tcp-proxy/195677/9 "2010-09-29T05:25:48Z")

</div>

On Mon, Sep 27, 2010 at 11:51 PM, Markus F.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Hi!
> 
> On 28.09.2010 01:16, John W Higgins wrote:
> 
> > Transparent or Intercepting - [GitHub - igrigorik/em-proxy: EventMachine Proxy DSL for writing high-performance transparent / intercepting proxies in Ruby](http://github.com/igrigorik/em-proxy)
> 
> This one looks very promising based on the slides and the documentation.
> 
> However I’ve instantly ran into troubles outlined here:  
> [Issues · igrigorik/em-proxy · GitHub](http://github.com/igrigorik/em-proxy/issues#issue/3) . Any idea?

I would chalk it up to probably just a mistake in the sample code - yes,  
it’s not the best first impression but it does happen. It may very well  
be  
conn.unbind instead of unbind but I’m not certain (unbind is a method of  
the  
connection object).

> Another thing I’m not sure: when I look at the simply forwarding example  
> in the README.rdoc, I see that I basically have hook methods (on\_data,  
> on\_response, etc.) onto which I can attach my own code.
> 
> What I would need is a per client/connection logic. I.e. a client  
> connects, I inspect and forward the traffic. I also my intercept some  
> requests from the client and send back my own stuff. Ultimately I have a  
> state per client.

Try looking at the smtp\_whitelist example. It shows intercepting a  
request -  
looking at it and sending back a response without the proxy actually  
passing  
it on to the backend server. If you return nil from the on\_data call  
then  
nothing is forwarded along.

You do have a state - the “conn” object, which EventMachine creates per  
connection and passes into the outer block that then calls into the  
on\_data,  
on\_response et al. blocks.

My guess for your case would be that you would want to extend the  
connection  
class (see connection.rb) to hold more information that would be  
available  
to you during the lifetime of the connection.

If nothing else this is around 200 or so lines of code that gives a nice  
starting point to anything more custom that you believe you would  
need/want.  
I would be rather impressed if something else gave you more flexibility  
than  
this and lived in the ruby world. You may find very well that a few  
helper  
methods would help you along the way or perhaps the opposite in that you  
don’t want the blocks but rather to work in a custom connection object  
that  
eliminates the block calls. But I do assure you that this is the right  
road  
to be on…

John
