#!/bin/bash

dst=$1
localport=$2
dstport=$3
tunnelpath=$4
fifo=`uuidgen | cut -d'-' -f5`
fifofile=`echo "${fifo}.fifo"`
pidpath=`echo "${tunnelpath}/${fifo}.pid"`

(cd $tunnelpath; mkfifo $fifofile; nc -l $localport 0<$fifofile | nc $dst $dstport 1>$fifofile) &
sleep 1
pid=`ps axw | grep "nc -l ${localport}" | grep -v grep | awk '{print $1}'`

echo ${pid} > ${pidpath}
