The script I typically use is shown below. Essentially it has an endless loop that listens on a port, tees the data that comes in to a request log file, sends the input to a remote server, tees the response from the remote server to a response log, and then writes the data back to a named pipe that is connected to stdin on the netcat process that was listening.
#!/bin/bash function logFile { echo "$(date +%Y-%m-%d-%H-%M-%S).${1}.log" } function serveRequests { port=$1 remoteHost=$2 remotePort=$3 while true; do rm -f backpipe mkfifo backpipe cat backpipe | nc -l $port | tee -a $(logFile request) | nc $remoteHost $remotePort | tee -a $(logFile response) >backpipe done } port=${1:-12345} remoteHost=${2:-localhost} remotePort=${3:-80} serveRequests $port $remoteHost $remotePort