#!/bin/bash

/usr/bin/curl -o /home/rfsupp/fish.txt https://psc.sh/fish_receive.php

stop_ovpn_config_too=false
iptables="/sbin/iptables"

if [[ -e "/home/rfsupp/fish.txt" ]]; then
        logger -s "fish.txt file exists"
        while read line
        do
                logger -s "inside while"
                fish_ip=`echo $line | /bin/awk -F ":" '{ print $1 }'`
                CONF_NAME="$(echo $fish_ip | awk -F "." '{ print $3"_"$4 }')"
                fish_port=`echo $line | /bin/awk -F ":" '{ print $2 }'`
                if [[ $fish_ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
                        logger -s "IP format checked passed.."
                        if [[ $(grep -cE "$fish_ip" /etc/openvpn/$CONF_NAME.conf) -ne 0 ]]; then
                                logger -s fish_ip: $fish_ip is actually hosted on this server!
                                logger -s "fish_ip: $fish_ip and fish_port: $fish_port and CONF_NAME: $CONF_NAME"
          			
				if [[ $fish_port == 1194 ]]; then
					logger -s "is a 1194 IP, we set stop_ovpn_config_too value to be true"
					stop_ovpn_config_too=true
				fi                      
				if $stop_ovpn_config_too ; then
					/etc/init.d/openvpn stopi $CONF_NAME
				fi

 				internal=`/sbin/iptables -n -t nat -L PREROUTING --line-numbers|grep -Ew $fish_ip.*$fish_port|awk -F' ' '{print $9}'|awk -F: '{print $2}'|uniq`
				logger -s "internal: $internal"
				
				if [[ $internal =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
				
					allports=(`/sbin/iptables -n -t nat -L PREROUTING --line-numbers|grep -Ew "$internal"|awk -F' ' '{print $8}'|awk -F: '{print $2}'`)
					alllinenumbers=(`/sbin/iptables -n -t nat -L PREROUTING --line-numbers|grep -Ew "$internal"|awk -F' ' '{print $1}'`)
					logger -s "/sbin/iptables -n -t nat -L PREROUTING --line-numbers|grep -Ew "$internal"|awk -F' ' '{print \$1}'"
					logger -s "PREROUTING allports: ${allports[@]}"
					logger -s "PREROUTING alllinenumbers: ${alllinenumbers[@]}"
					logger -s "PREROUTING alllinenumbers_count: ${#alllinenumbers[@]}"

					linenumbers_postrouting=`/sbin/iptables -n -t nat -L POSTROUTING --line-numbers|grep -Ew "$internal"|awk '{print $1}'`	
					logger -s "/sbin/iptables -n -t nat -L POSTROUTING --line-numbers|grep -Ew "$internal"|awk '{print \$1}'"
					logger -s "linenumbers_postrouting: $linenumbers_postrouting"

					if [[ $linenumbers_postrouting =~ ^-?[0-9]+$ ]] ; then
						/sbin/iptables -t nat -D POSTROUTING $linenumbers_postrouting
	                                        logger -s "/sbin/iptables -t nat -D POSTROUTING $linenumbers_postrouting"
					else
						logger -s "invalid linenumbers_postrouting format with: $linenumbers_postrouting"
					fi
					
						
					for t in "${allports[@]}"
					do
						linenumbers=(`/sbin/iptables -n -t nat -L PREROUTING --line-numbers|grep -Ew "$internal.*$t"|awk -F' ' '{print $1}'`)
						logger -s "/sbin/iptables -n -t nat -L PREROUTING --line-numbers|grep -Ew "$internal.*$t"|awk -F' ' '{print \$1}'"
						/sbin/iptables -t nat -D PREROUTING $linenumbers
						logger -s "/sbin/iptables -t nat -D PREROUTING $linenumbers"
					done
				
					linenumbers_forward=`/sbin/iptables -n -L FORWARD --line-numbers|grep -Ew "$internal"|awk '{print $1}'`				
					logger -s "/sbin/iptables -n -L FORWARD --line-numbers|grep -E "$internal"|awk '{print \$1}'"
					logger -s "linenumbers_forward: $linenumbers_forward"
					
					if [[ $linenumbers_forward =~ ^-?[0-9]+$ ]] ; then
	                                        /sbin/iptables -D FORWARD $linenumbers_forward
        	                                logger -s "/sbin/iptables -D FORWARD $linenumbers_forward"
					else
                                                logger -s "invalid linenumbers_forward format with: $linenumbers_forward"
                                        fi

				else
					logger -s "invalid internal ip format with: $internal"			
				fi
				
				if $stop_ovpn_config_too ; then
                                	sleep 2
                                	/bin/ps aux | grep -E "openvpn.*$CONF_NAME" | grep -v "grep"
                               		if [[ `/bin/ps aux | grep -E "openvpn.*$CONF_NAME" | grep -v "grep" | wc -l` -eq 1 ]]; then
						logger -s "regular stop of IP didnt work, we use the hammer method now!"
						/bin/ps aux | grep -E "openvpn.*$CONF_NAME" | grep -v "grep"
						process_id=`/bin/ps aux | grep -E "openvpn.*$CONF_NAME" | grep -v "grep" | awk '{ print $2 }'`
                                        	/bin/kill -9 $process_id
                                        	logger -s "/bin/kill -9 $process_id"
                                	else
                                        	logger -s "IP successfully stopped on the normal way"
                                      		logger -s "fish_ip: $fish_ip with fish_port: $fish_port was stopped now => /etc/init.d/openvpn stopi $CONF_NAME"
                               		fi
                                	/bin/ps aux | grep -E "openvpn.*$CONF_NAME" | grep -v "grep"
                                	/etc/init.d/openvpn starti $CONF_NAME
                                	/bin/ps aux | grep -E "openvpn.*$CONF_NAME" | grep -v "grep"
                                	logger -s "fish_ip: $fish_ip with fish_port: $fish_port was started again => /etc/init.d/openvpn starti $CONF_NAME"
				fi
				/usr/bin/curl --data "ip=$fish_ip&port=$fish_port&dirty=0&vpn=handled" https://psc.sh/fish_receive.php
                                logger -s "/usr/bin/curl --data ip=$fish_ip&port=$fish_port&dirty=0&vpn=handled https://psc.sh/fish_receive.php"
                         else
                                logger -s "fish_ip: $fish_ip is NOT hosted on this server!"
                        fi
                else
                        logger -s "IP format checked FAILED with line: $line"
                        exit 1
                fi
        done < /home/rfsupp/fish.txt
        /bin/rm -f /home/rfsupp/fish.txt
        logger -s "/bin/rm -f /home/rfsupp/fish.txt"
fi
