Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I can easily use Netcat (or, Socat) to capture traffic between my browser and a specific host:port.

But for Linux, does there exist any command-line counterpart of a Squid-like HTTP proxy that I can use to capture traffic between my HTTP client (either browser or command-line program) and any arbitrary host:port?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Both Perl and Python (and probably Ruby as well) have simple kits that you can use to quickly build simple HTTP proxies.

In Perl, use HTTP::Proxy. Here's the 3-line example from the documentation. Add filters to filter, log or rewrite requests or responses; see the documentation for examples.

use HTTP::Proxy;
my $proxy = HTTP::Proxy->new( port => 3128 );
$proxy->start;

In Python, use SimpleHTTPServer. Here's some sample code lightly adapted from effbot. Adapt the do_GET method (or others) to filter, log or rewrite requests or responses.

import SocketServer
import SimpleHTTPServer
import urllib
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.copyfile(urllib.urlopen(self.path), self.wfile)
httpd = SocketServer.ForkingTCPServer(('', 3128), Proxy)
httpd.serve_forever()
share|improve this answer
    
I believe this is exactly the kind of solution I was looking for. Thanks. –  Harry May 18 '12 at 4:05

This may not be the best solution, but if you use any proxy then it will have a specific host:port so the netcat solution with still work, albeit you'll have to pick apart the proxy meta-data to make sense of it.

The easiest way to do this might be to use any random anonymization proxy out there and just channel all the traffic through netcat. (I.e., set your browser proxy to localhost:port and then forward the data to the real proxy.)

If you want to have a local proxy then a SOCKS5 proxy with ssh -D <port> localhost is probably your easiest option. Obviously, you need to tell your browser to use a "socks" proxy rather than an "http" proxy.

So, something like this (assuming your local machine accepts incoming ssh connections):

ssh -fN -D 8000 localhost
nc -l 8080 | tee capturefile | nc localhost 8000

Naturally, that'll only work for one browser connection attempt, and then exit, and I have not attempted to forward the return data to the browser, so you'll need your full netcat solution.

share|improve this answer
    
Thanks. Do you have any name suggestions for anonymization proxies for Linux? I would prefer ones that are not as heavyweight as Squid (in terms of size, memory, startup speed), are startable by a non-root user, etc. –  Harry May 17 '12 at 14:52
    
Also, I didn't follow your SOCKS5 proxy example. Could you please explain what is going on with the 2 invocations above? I, e.g. know that ssh -fN -D ... starts a SOCKS5 proxy on localhost port 8000, but what does nc -l ... do? And, to which port should I make my browser point to: 8000 or 8080? –  Harry May 17 '12 at 15:37
2  
@harry for an anomyzation proxy have a look at privoxy –  Ulrich Dangel May 17 '12 at 17:17
1  
When I say "anonymization proxy" I don't mean something you set up yourself: I mean an existing proxy out there on the net. @UlrichDangel suggests privoxy and I'm sure there are others too. –  ams May 18 '12 at 8:04
    
The nc commands accept all traffic from port 8080 and forward it to 8000. I did not provide a full netcat solution because your post suggested you already knew how to do that part. –  ams May 18 '12 at 8:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.