Introduction:
Lately I’ve had to create a pure bidirectional TCP Proxy for a project. For this there are lots of alternatives like haproxy, nginx, cat and socat and others. Because of the simplicity of the command I decided to use socat but will also show the command for cat as well.
The NCAT method:
The following command will us a pipe to transport the data in both directions. Only one client can be connected at one time.
cd /var/tmp
mkfifo fifo &>/dev/null
/bin/nc -l -p $frontend_port -s $frontend_addr <fifo | /bin/nc $backend_addr $backend_port >fifo
The SOCAT method(Best!):
Note: this method runs the command in a screen session but doesn’t need to if the process is only temporarily needed to be run.
/usr/bin/screen -d -m /usr/bin/socat -d -d -lmlocal2 \
TCP4-LISTEN:$frontend_port,bind=$frontend_addr,reuseaddr,fork,su=daemon \
TCP4:$backend_addr:$backend_port,bind=$backend_iface_addr