#!/bin/bash

# It works by using netcat and a named pipe, see:
# http://www.stearns.org/doc/nc-intro.v0.80.html

if [ $# -ne 2 ]; then
    cat <<-EOF
	Usage: $0 <srvr-port> <proxy-port>

	This script provides a simple proxy server which records
	in- and out-bound traffic for a local server.
	It is useful for debugging protocols, i.e. if you which to know
	what *exactly* is happening on the wire.
	The traffic is appended to the files in.log and out.log:


	+--------+    request     +---------+     in.log      +--------+
	|        |  ----------->  |         |  ------------>  |        |
	| Client |                |  Proxy  |                 | Server |
	|        |  <-----------  |         |  <------------  |        |
	+--------+    response    +---------+     out.log     +--------+


	The program terminates when the connection is closed.

	Example, log traffic of SSH connection:
	\$ $0 22 8022 &
	\$ ssh -p 8022 localhost

	EOF
else
    mkfifo back.pipe
    
    netcat -l -p $2 <back.pipe | tee -a in.log | \
	netcat localhost $1 | tee -a out.log >back.pipe
    
    rm back.pipe
fi
