#!/bin/sh
#

flush_all() {
# Accept all traffic first to avoid ssh lockdown  via iptables firewall rules #
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

# Flush All Iptables Chains/Firewall rules #
iptables -F

# Delete all Iptables Chains #
iptables -X

# Flush all counters too #
iptables -Z
# Flush and delete all nat and  mangle #
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
#-------------------------
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
iptables -t nat -A POSTROUTING -o tun5 -j MASQUERADE
}


del_portfwd () {
DESTPORT=$1
DESTIP=$2
SRCPORT=$3
SRCIP=$4
SRCIFACE=$5

iptables -t nat -D PREROUTING -p tcp -i $SRCIFACE --dport $SRCPORT -j DNAT --to-destination $DESTIP:$DESTPORT 2> /dev/null
iptables -D FORWARD -p tcp -d $SRCIP --dport $SRCPORT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT 2> /dev/null

}

set_portfwd () {
DESTPORT=$1
DESTIP=$2
SRCPORT=$3
SRCIP=$4
SRCIFACE=$5

#del_portfwd $1 $2 $3 $4 $5

iptables -t nat -A PREROUTING -p tcp -i $SRCIFACE --dport $SRCPORT -j DNAT --to-destination $DESTIP:$DESTPORT
iptables -A FORWARD -p tcp -d $SRCIP --dport $SRCPORT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

}

exit 0 #Commnet out if activated

flush_all

set_portfwd 80 192.168.100.1 80 `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` eth0
set_portfwd 80 192.168.100.1 80 `ifconfig tun5 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` tun5
set_portfwd 102 192.168.100.1 102 `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` eth0
set_portfwd 102 192.168.100.1 102 `ifconfig tun5 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` tun5
set_portfwd 443 192.168.100.1 443 `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` eth0
set_portfwd 443 192.168.100.1 443 `ifconfig tun5 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` tun5
set_portfwd 5900 192.168.100.20 5900 `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` eth0
set_portfwd 5900 192.168.100.20 5900 `ifconfig tun5 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` tun5
