#!/bin/bash SUDO=$(if [ $(id -u $whoami) -gt 0 ]; then echo "sudo "; fi) IFACE=$(ip route show | grep default | awk '{print $5}') INET=$(ip address show $IFACE scope global | awk '/inet / {split($2,var,"/"); print var[1]}') LOCAL_PORT=$2 REMOTE_IP=$3 REMOTE_PORT=$4 set_forward () { if [ -z "$($SUDO cat /etc/sysctl.conf | grep 'net.ipv4.ip_forward = 1')" ]; then echo "net.ipv4.ip_forward = 1" | $SUDO tee -a /etc/sysctl.conf $SUDO sysctl -p fi } forward () { set_forward $SUDO iptables -t nat -A PREROUTING -p tcp --dport $LOCAL_PORT -j DNAT --to-destination $REMOTE_IP:$REMOTE_PORT -m comment --comment "FORWARD $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -t nat -A PREROUTING -p udp --dport $LOCAL_PORT -j DNAT --to-destination $REMOTE_IP:$REMOTE_PORT -m comment --comment "FORWARD $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -t nat -A POSTROUTING -d $REMOTE_IP -p tcp --dport $REMOTE_PORT -j SNAT --to-source $INET -m comment --comment "BACKWARD $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -t nat -A POSTROUTING -d $REMOTE_IP -p udp --dport $REMOTE_PORT -j SNAT --to-source $INET -m comment --comment "BACKWARD $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -A FORWARD -p tcp -d $REMOTE_IP --dport $REMOTE_PORT -j ACCEPT -m comment --comment "UPLOAD $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -A FORWARD -p udp -d $REMOTE_IP --dport $REMOTE_PORT -j ACCEPT -m comment --comment "UPLOAD-UDP $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -A FORWARD -p tcp -s $REMOTE_IP -j ACCEPT -m comment --comment "DOWNLOAD $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -A FORWARD -p udp -s $REMOTE_IP -j ACCEPT -m comment --comment "DOWNLOAD-UDP $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables-save } monitor () { set_forward $SUDO iptables -A INPUT -p tcp -d $INET --dport $LOCAL_PORT -m comment --comment "UPLOAD $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -A INPUT -p udp -d $INET --dport $LOCAL_PORT -m comment --comment "UPLOAD-UDP $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -A INPUT -p tcp -s $REMOTE_IP -d $INET -m comment --comment "DOWNLOAD $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables -A INPUT -p udp -s $REMOTE_IP -d $INET -m comment --comment "DOWNLOAD-UDP $LOCAL_PORT->$REMOTE_IP:$REMOTE_PORT" $SUDO iptables-save } list () { COMMENT="$LOCAL_PORT->" $SUDO iptables -S | grep $COMMENT $SUDO iptables -t nat -S | grep $COMMENT } delete () { COMMENT="$LOCAL_PORT->" while [[ ! -z "$($SUDO iptables -S | grep $COMMENT)" ]] do $SUDO iptables -S | grep $COMMENT | awk -v SUDO="$SUDO" '{$1="";$COMMEND=SUDO" iptables -D "$0; system($COMMEND)}' done while [[ ! -z "$($SUDO iptables -t nat -S | grep $COMMENT)" ]] do $SUDO iptables -t nat -S | grep $COMMENT | awk -v SUDO="$SUDO" '{$1="";$COMMEND=SUDO" iptables -t nat -D "$0; system($COMMEND)}' done } if [ "$1" == "forward" ]; then forward elif [ "$1" == "monitor" ]; then monitor elif [ "$1" == "list" ]; then list elif [ "$1" == "delete" ]; then delete else echo "Unrecognized command: $1" fi