Using netcat (nc) as an HTTP proxy server and monitor

24,901

Solution 1

Just had the need yesterday. You can find the answer here (french) : http://www.linux-france.org/~mdecore/linux/doc/memo2/node168.html

mknod backpipe p
nc -l -p 80 < backpipe | tee -a in | nc localhost 8080 | tee -a out.html > backpipe

This listens on port 80 and redirect on port 8080. Incoming traffic will be present in the in file, outgoing traffic in the out.html file.The named pipe is needed for the connection to be bi-directional.

Solution 2

Not netcat on its own, since it would have to interpret the HTTP request and pass it on. For example, an HTTP request through a proxy starts with:

GET http://www.example.org/ HTTP/1.1

which your proxy then has to go, 'okay, I gotta connect to example.org and GET /'.

Now this could maybe be done by piping the nc output into a script which parses the HTTP req and then calls 'wget' to get the page, then slurp that back through netcat... oh heck, why?

Apache, or squid can probably do the job.

Share:
24,901
Landon Kuhn
Author by

Landon Kuhn

Updated on July 05, 2022

Comments

  • Landon Kuhn
    Landon Kuhn almost 2 years

    Is it possible to use the Unix netcat (nc) program to create a TCP proxy server and monitor? I would like all TCP traffic to be passed across the pipe, as well as sent to stdout for monitoring. Note this will be used for monitoring HTTP traffic between a hardware device and an HTTP server.