# Simple ruby proxy server?

**URL:** <https://rubytalk.org/t/simple-ruby-proxy-server/44295>\
**Category:** ruby-talk\
**Created:** [6 February 2008 01:39 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295 "2008-02-06T01:39:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dt\_Town](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@Dt\_Town](https://rubytalk.org/u/Dt_Town)\
**Post date:** [6 February 2008 01:39 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/1 "2008-02-06T01:39:12Z")

</div>

I am trying to right at application which will simply record the headers  
of all get requests made from my browser.

It would be ideal to just listen in on port 80, but something tells me  
that wont work because of security risks. So I was wondering if there is  
a way to do this through a proxy. I am relatively new to ruby, and I  
havent been able to find anything in the rdoc.

Any help is greatly appreciated. Thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![dusty](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dusty](https://rubytalk.org/u/dusty)\
**Post date:** [6 February 2008 02:35 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/2 "2008-02-06T02:35:03Z")

</div>

Well, this will work to record the headers. You'll need to install  
the mongrel gem.

# gem install mongrel

# headerlog.rb  
require 'rubygems'  
require 'mongrel'  
require 'logger'

class HeaderHandler \< Mongrel::HttpHandler

&nbsp;&nbsp;@@logger = Logger.new('headers.log')

&nbsp;&nbsp;def process(request,response)  
&nbsp;&nbsp;&nbsp;&nbsp;response.start(200) do |head, out|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head["Content-Type"] = "text/plain"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@logger.info request.params  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

server = Mongrel::HttpServer.new("127.0.0.1", "2222")  
server.register("/", HeaderHandler.new)  
server.register("/favicon.ico", Mongrel::Error404Handler.new(""))  
server.run.join

--- Then run it

# ruby headerlog.rb

Its set to listen on port 2222 and will record all the headers to a  
file named headers.log. You could change that port to whatever you  
want. I see no security implications of listening on port 80.

The only problem would be if you have something else listening on port  
80. Are you trying to put something in front of an existing  
application to capture the headers, then have it proxy the request to  
something else? In that case, what kind of application is it? You  
might be able to do that in the app itself. Or, perhaps turn on some  
sort of debug logging in apache or whatever server you are using. I  
might have some other ideas if this is more complex than the simple  
example I listed above.

Hope that is helpful.

---

<div class="post-metadata">

**Author:** ![Dt\_Town](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@Dt\_Town](https://rubytalk.org/u/Dt_Town)\
**Post date:** [6 February 2008 05:41 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/3 "2008-02-06T05:41:23Z")

</div>

thanks for your help dusty. I haven't gotten a chance to run it yet (I  
wont till tomorrow morning).

My approach thus far is to create a proxy server, and have IE or Firefox  
route its requests through there. With that in mind I have 3 questions:

1) Is there another (doesn't necessarily have to be better) approach  
that you would suggest?  
2) If I set IE7 to proxy its requests to port 2222, does the ruby script  
you provided return the requested data (i.e. does the web page still  
load in the browser)  
3) If I set the port to 80 in the script above, will it behave as a  
"listener" and not interfere with the request from the browser (my guess  
is no on this one).

Thanks for your help, I appreciate it very much.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![dusty](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dusty](https://rubytalk.org/u/dusty)\
**Post date:** [6 February 2008 20:45 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/4 "2008-02-06T20:45:00Z")

</div>

I had a feeling it was more than just capturing the headers.

Do you happen to be running apache on the webserver? You can log all  
of it and not worry about a proxy. Here are two ways.

1. mod\_dumpio

# Put this in your apache config  
DumpIOInput On  
LogLevel debug

2. mod\_log\_forensic

# Put this in your apache config  
ForensicLog /some/path/to/a/logfile.log

This would simply capture the traffic on the web server itself.

---

<div class="post-metadata">

**Author:** ![dusty](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dusty](https://rubytalk.org/u/dusty)\
**Post date:** [6 February 2008 20:49 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/5 "2008-02-06T20:49:55Z")

</div>

BTW - if you are just looking to do this on your computer so you can  
see what is going on while you hit remote sites, then I'd just use  
ethereal (or wireshark, I believe its called now). You can see the  
whole packet, probably the easiest way.

Although, I'm sure someone on this list has written a ruby proxy  
server before, I haven't. Sounds interesting though, I might take a  
stab at it if I can find some spare time.

---

<div class="post-metadata">

**Author:** ![GOTO\_Kentaro](https://avatars.discourse-cdn.com/v4/letter/g/ebca7d/32.png) [@GOTO\_Kentaro](https://rubytalk.org/u/GOTO_Kentaro)\
**Post date:** [7 February 2008 01:41 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/6 "2008-02-07T01:41:18Z")

</div>

[HTTP Proxy instruction? - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/139859) may help you.

Gotoken

> **···**
>
> On Wed, Feb 6, 2008 at 2:41 PM, Dt Town \<dtown22@gmail.com\> wrote:
> 
> > thanks for your help dusty. I haven't gotten a chance to run it yet (I  
> > wont till tomorrow morning).
> > 
> > My approach thus far is to create a proxy server, and have IE or Firefox  
> > route its requests through there. With that in mind I have 3 questions:
> > 
> > 1) Is there another (doesn't necessarily have to be better) approach  
> > that you would suggest?  
> > 2) If I set IE7 to proxy its requests to port 2222, does the ruby script  
> > you provided return the requested data (i.e. does the web page still  
> > load in the browser)  
> > 3) If I set the port to 80 in the script above, will it behave as a  
> > "listener" and not interfere with the request from the browser (my guess  
> > is no on this one).
> > 
> > Thanks for your help, I appreciate it very much.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Francis\_Cianfrocca](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@Francis\_Cianfrocca](https://rubytalk.org/u/Francis_Cianfrocca)\
**Post date:** [6 February 2008 20:57 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/7 "2008-02-06T20:57:13Z")

</div>

There was some discussion of HTTP proxies on the Ruby/Eventmachine mailing  
list a few weeks back, so you might search through that archive. It sounds  
like the OP wants to write a reverse proxy, which isn't hard to do at all  
using EventMachine. The thing that made the subject more interesting,  
however, was the requirement that the proxy be transparent, so that the  
proxied traffic appears to be coming from the original peer's source IP  
address. That takes kernel support and is not available on all platforms.

> **···**
>
> On Feb 6, 2008 3:49 PM, dusty \<dusty.doris@gmail.com\> wrote:
> 
> > BTW - if you are just looking to do this on your computer so you can  
> > see what is going on while you hit remote sites, then I'd just use  
> > ethereal (or wireshark, I believe its called now). You can see the  
> > whole packet, probably the easiest way.
> > 
> > Although, I'm sure someone on this list has written a ruby proxy  
> > server before, I haven't. Sounds interesting though, I might take a  
> > stab at it if I can find some spare time.

---

<div class="post-metadata">

**Author:** ![Dt\_Town](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@Dt\_Town](https://rubytalk.org/u/Dt_Town)\
**Post date:** [6 February 2008 21:31 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/8 "2008-02-06T21:31:54Z")

</div>

thanks for the info guys. I have looked into wireshark, and I have been  
back and forth on whether to do a packet level implementation (i.e. run  
wireshark command line, and then parse the data), or do a http level  
implementation (i.e. the proxy). The thing I dont like about packet  
level is the sheer number of packets that I would have to sift through.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![dusty](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dusty](https://rubytalk.org/u/dusty)\
**Post date:** [7 February 2008 03:50 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/9 "2008-02-07T03:50:04Z")

</div>

Nice!

---

<div class="post-metadata">

**Author:** ![Dt\_Town](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@Dt\_Town](https://rubytalk.org/u/Dt_Town)\
**Post date:** [7 February 2008 07:39 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/10 "2008-02-07T07:39:38Z")

</div>

GOTO Kentaro wrote:

> [HTTP Proxy instruction? - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/139859) may help you.
> 
> Gotoken

Thanks, that looks very helpful, I will give it a shot tomorrow.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Dt\_Town](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@Dt\_Town](https://rubytalk.org/u/Dt_Town)\
**Post date:** [7 February 2008 19:42 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/11 "2008-02-07T19:42:14Z")

</div>

Hi Gotoken,

At first I thought it was the answer to my problems, but it seems that  
not all pages will load with it. i think pages which are heavily ajax  
dependent are the ones that dont load. I will try and tinker some more  
with it.

GOTO Kentaro wrote:

> **···**
>
> > [HTTP Proxy instruction? - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/139859) may help you.
> > 
> > Gotoken
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![dusty](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dusty](https://rubytalk.org/u/dusty)\
**Post date:** [6 February 2008 21:04 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/12 "2008-02-06T21:04:58Z")

</div>

Thanks for the heads up, I'll check it out. Been playing with event  
machine a little bit recently, its nice!

---

<div class="post-metadata">

**Author:** ![GOTO\_Kentaro](https://avatars.discourse-cdn.com/v4/letter/g/ebca7d/32.png) [@GOTO\_Kentaro](https://rubytalk.org/u/GOTO_Kentaro)\
**Post date:** [9 February 2008 13:31 UTC](https://rubytalk.org/t/simple-ruby-proxy-server/44295/13 "2008-02-09T13:31:17Z")

</div>

Hi,

WEBrick::HTTPProxyServer is not fast and maybe too slow in your situation.  
Could you report again when you find a simple way to reproduce the problem?  
I'll try to help you trouble shooting then.

Thanks,

Gotoken

> **···**
>
> On Fri, Feb 8, 2008 at 4:42 AM, an an \<dtown22@gmail.com\> wrote:
> 
> > Hi Gotoken,
> > 
> > At first I thought it was the answer to my problems, but it seems that  
> > not all pages will load with it. i think pages which are heavily ajax  
> > dependent are the ones that dont load. I will try and tinker some more  
> > with it.
> > 
> > GOTO Kentaro wrote:  
> > \> [HTTP Proxy instruction? - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/139859) may help you.  
> > \>  
> > \> Gotoken
> > 
> > --
> > 
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
