#!/bin/bash

PIDDIR=/var/run
UCIBIN=/usr/sbin/uci
OVPN_MODE=client
IPCALC=/usr/bin/ipcalc.sh
BRCTL_BIN=/sbin/brctl
lan_net_address=$(uci get "lan_links.link[1].ip4_setting.ip4_addrs.ip4_addr[1]") > /dev/null
lan2_net_address=$(uci get "lan_links.link[2].ip4_setting.ip4_addrs.ip4_addr[1]") > /dev/null
lan_address=$(echo $lan_net_address|cut -d '/' -f1)
lan_prefix=$(echo $lan_net_address|cut -d '/' -f2)
lan2_address=$(echo $lan2_net_address|cut -d '/' -f1)
lan2_prefix=$(echo $lan2_net_address|cut -d '/' -f2)
interface=$dev


ovpn_up() {
        if [ "$interface" = tap0 ];then
                env >> /tmp/ovpn
                set >> /tmp/ovpn

                vpn_netmask=$ifconfig_netmask
                vpn_network=$(echo "$($IPCALC $ifconfig_local $vpn_netmask)")
                lan_network=$(echo "$($IPCALC $lan_address $lan_prefix)")

                if [ "$vpn_network" = "$lan_network" ];then
                        [ -d /sys/class/net/br_lan/brif/${interface} ] || $BRCTL_BIN addif br_lan $interface
                        ifconfig $interface 0.0.0.0 promisc up
                else
                        [ ! -d /sys/class/net/br_lan/brif/${interface} ] || $BRCTL_BIN delif br_lan $interface
                fi

                OPENVPN_local_server_ip_wo_port=$(echo $OPENVPN_local_server_ip|cut -d ":" -f1)

                #eurogard GW-Hack
                #writing to /tmp not possible from this script. Therefore we need to write to /app/eurogard/ovpn and the service from Niklas will read this file and copy it to /tmp/fw.d
                iptables -t nat -A postrouting_rule ! -s $lan_net_address -o br_lan -j SNAT --to $lan_address
                echo "iptables -t nat -A postrouting_rule ! -s $lan_net_address -o br_lan -j SNAT --to $lan_address" > /app/eurogard/iptables-ovpn1
                chmod u+x /app/eurogard/iptables-ovpn1

                if [[ -z "$OPENVPN_rc_count" ]]; then
                        rc_count=0
                else
                   rc_count=$(echo $OPENVPN_rc_count)
                fi
                if [ $rc_count -gt 0 ]; then
                        for i in $(seq $rc_count)
                        do
                                dest_ip=$(echo $OPENVPN_rc_dest_ip|cut -d ";" -f$i) > /dev/null
                                dest_port=$(echo $OPENVPN_rc_dest_port|cut -d ";" -f$i) > /dev/null
                                dest_proto=$(echo $OPENVPN_rc_dest_proto|cut -d ";" -f$i) > /dev/null
                                vpn_port=$(echo $OPENVPN_rc_vpn_port|cut -d ";" -f$i) > /dev/null
                                iptables -t nat -A PREROUTING -i tap+ -p $dest_proto -m $dest_proto --dport $vpn_port -j DNAT --to-destination $dest_ip:$dest_port
                                echo "iptables -t nat -A PREROUTING -i tap+ -p $dest_proto -m $dest_proto --dport $vpn_port -j DNAT --to-destination $dest_ip:$dest_port" >> /app/eurogard/iptables-ovpn1
                        done
                fi
        # Ausführung von scripts via OVPN-Variable
                if [[ -z "$OPENVPN_custom_update_script" ]]; then
                        nop=0
                else
                        bash -c "${OPENVPN_custom_update_script}"
                        if $(echo ${OPENVPN_custom_update_script}|grep iptables); then
                                echo ${OPENVPN_custom_update_script} >> /app/eurogard/iptables-ovpn1
                        fi
                fi
                if [[ -z "$OPENVPN_custom_update_script2" ]]; then
                        nop=0
                else
                        bash -c "${OPENVPN_custom_update_script2}"
                        if $(echo ${OPENVPN_custom_update_script2}|grep iptables); then
                                echo ${OPENVPN_custom_update_script2} >> /app/eurogard/iptables-ovpn1
                        fi
                fi
                if [[ -z "$OPENVPN_custom_update_script3" ]]; then
                        nop=0
                else
                        bash -c "${OPENVPN_custom_update_script3}"
                        if $(echo ${OPENVPN_custom_update_script3}|grep iptables); then
                                echo ${OPENVPN_custom_update_script3} >> /app/eurogard/iptables-ovpn1
                        fi
                fi



        fi

        if [ "$interface" = tap1 ];then
                env >> /tmp/ovpn
                set >> /tmp/ovpn

                vpn_netmask=$ifconfig_netmask
                vpn_network=$(echo "$($IPCALC $ifconfig_local $vpn_netmask)")
                lan2_network=$(echo "$($IPCALC $lan2_address $lan2_prefix)")

                if [ "$vpn_network" = "$lan2_network" ];then
                        [ -d /sys/class/net/br_lan2/brif/${interface} ] || $BRCTL_BIN addif br_lan2 $interface
                        ifconfig $interface 0.0.0.0 promisc up
                else
                        [ ! -d /sys/class/net/br_lan2/brif/${interface} ] || $BRCTL_BIN delif br_lan2 $interface
                fi

                OPENVPN_local_server_ip_wo_port=$(echo $OPENVPN_local_server_ip|cut -d ":" -f1)

                #eurogard GW-Hack
                #writing to /tmp not possible from this script. Therefore we need to write to /app/eurogard/ovpn and the service from Niklas will read this file and copy it to /tmp/fw.d
                echo "iptables -t nat -A postrouting_rule ! -s $lan2_net_address -o br_lan2 -j SNAT --to $lan2_address" > /app/eurogard/iptables-ovpn2
                iptables -t nat -A postrouting_rule ! -s $lan2_net_address -o br_lan2 -j SNAT --to $lan2_address
                chmod u+x /app/eurogard/iptables-ovpn2

                if [[ -z "$OPENVPN_rc_count" ]]; then
                        rc_count=0
                else
                   rc_count=$(echo $OPENVPN_rc_count)
                fi
                if [ $rc_count -gt 0 ]; then
                        for i in $(seq $rc_count)
                        do
                                dest_ip=$(echo $OPENVPN_rc_dest_ip|cut -d ";" -f$i) > /dev/null
                                dest_port=$(echo $OPENVPN_rc_dest_port|cut -d ";" -f$i) > /dev/null
                                dest_proto=$(echo $OPENVPN_rc_dest_proto|cut -d ";" -f$i) > /dev/null
                                vpn_port=$(echo $OPENVPN_rc_vpn_port|cut -d ";" -f$i) > /dev/null
                                iptables -t nat -A PREROUTING -i tap+ -p $dest_proto -m $dest_proto --dport $vpn_port -j DNAT --to-destination $dest_ip:$dest_port
                                echo "iptables -t nat -A PREROUTING -i tap+ -p $dest_proto -m $dest_proto --dport $vpn_port -j DNAT --to-destination $dest_ip:$dest_port" >> /app/eurogard/iptables-ovpn2
                        done
                fi
        # Ausführung von scripts via OVPN-Variable
                if [[ -z "$OPENVPN_custom_update_script" ]]; then
                        nop=0
                else
                        bash -c "${OPENVPN_custom_update_script}"
                        if $(echo ${OPENVPN_custom_update_script}|grep iptables); then
                                echo ${OPENVPN_custom_update_script} >> /app/eurogard/iptables-ovpn2
                        fi
                fi
                if [[ -z "$OPENVPN_custom_update_script2" ]]; then
                        nop=0
                else
                        bash -c "${OPENVPN_custom_update_script2}"
                        if $(echo ${OPENVPN_custom_update_script2}|grep iptables); then
                                echo ${OPENVPN_custom_update_script2} >> /app/eurogard/iptables-ovpn2
                        fi
                fi
                if [[ -z "$OPENVPN_custom_update_script3" ]]; then
                        nop=0
                else
                        bash -c "${OPENVPN_custom_update_script3}"
                        if $(echo ${OPENVPN_custom_update_script3}|grep iptables); then
                                echo ${OPENVPN_custom_update_script3} >> /app/eurogard/iptables-ovpn2
                        fi
                fi



        fi
}

case "$script_type" in
        up) ovpn_up;;
esac
