#!/bin/bash

SYS_VERSION=`rpm -q --queryformat '%{VERSION}' centos-release`
openssl req -new -x509 -subj "/C=WS/ST=Foo/L=Bar/O=kickass/CN=*" -days 3650 -nodes -out /etc/stunnel/stunnel.pem -keyout /etc/stunnel/stunnel.pem
chmod 600 /etc/stunnel/stunnel.pem

function write_config_header
{
	CONFIG="/etc/stunnel/stunnel-$1.conf";
	>$CONFIG;	
	echo "setuid = root" >> $CONFIG;
	echo "setgid = wheel" >> $CONFIG;
	echo "client = no" >> $CONFIG;
	echo "socket = l:TCP_NODELAY=1" >> $CONFIG;
	echo "socket = r:TCP_NODELAY=1" >> $CONFIG;
	echo "cert = /etc/stunnel/stunnel.pem" >> $CONFIG;
	echo "pid = /var/run/stunnel/stunnel-$1.pid" >> $CONFIG;
	echo "debug = 0" >> $CONFIG;
	echo -e >> $CONFIG;
if [ $SYS_VERSION -eq 6 ]; then
	CONFIG2="/etc/monit.d/monit-stunnel-$1";
        >$CONFIG2;
        echo "check process stunnel-$1 with pidfile /var/run/stunnel/stunnel-$1.pid" >> $CONFIG2;
        echo 'start program = "/etc/init.d/stunnel start"' >> $CONFIG2;
        echo 'stop program  = "/etc/init.d/stunnel stop"' >> $CONFIG2;
        echo "if failed host $2 port 9998 type tcp then restart" >> $CONFIG2;
        echo "if cpu > 70% for 2 cycles then alert" >> $CONFIG2;
        echo "if cpu > 85% for 5 cycles then restart" >> $CONFIG2;
        echo "if totalmem > 300.0 MB for 5 cycles then restart" >> $CONFIG2;
        echo "if children > 250 then restart" >> $CONFIG2;
elif [ $SYS_VERSION -eq 7 ]; then
	CONFIG2="/etc/monit.d/monit-stunnel-$1";
        >$CONFIG2;
        echo "check process stunnel-$1 with pidfile /var/run/stunnel/stunnel-$1.pid" >> $CONFIG2;
        echo 'start program = "/usr/bin/systemctl start stunnel"' >> $CONFIG2;
        echo 'stop program  = "/usr/bin/systemctl stop stunnel"' >> $CONFIG2;
        echo "if failed host $2 port 9998 type tcp then restart" >> $CONFIG2;
        echo "if cpu > 70% for 2 cycles then alert" >> $CONFIG2;
        echo "if cpu > 85% for 5 cycles then restart" >> $CONFIG2;
        echo "if totalmem > 300.0 MB for 5 cycles then restart" >> $CONFIG2;
        echo "if children > 250 then restart" >> $CONFIG2;
fi
}

IPLIST=`ip addr show | egrep -v '\blo|inet6' | egrep ':[0-9]+$' | awk '{print $2}' | sed -e 's/\/.*//g' | sort -r`
CONFIGID=0
#SYS_VERSION=6
c=0

for ip in $IPLIST; do
	
	if [ $SYS_VERSION -eq 6 ]; then
		
		CFID=$((c/64))
		echo "CFID: $CFID"
		CFLAG=$((c%64))
		echo "CFLA: $CFLAG"
		
		CONFIG="/etc/stunnel/stunnel-$CFID.conf";
		echo "CONFIG: $CONFIG"

		if [ $((c%64)) -eq 0 ]; then
                        echo "inside lets write header"
                        #write_config_header $CFID $ip;
                fi

	elif [ $SYS_VERSION -eq 7 ]; then

		CFID=$c
		echo "CFID: $CFID"
                CFLAG=$c
		echo "CFLA: $CFLAG"
		
		CONFIG="/etc/stunnel/stunnel-$CFID.conf";
		echo "CONFIG: $CONFIG"
		
		 if [ $CONFIGID -eq 0 ]; then
                        echo "inside lets write header"
                        write_config_header $CFID $ip;
		fi
	fi

	echo -e "[ss5-$CONFIGID]\naccept=$ip:9998\nconnect=$ip:9999\n" >> $CONFIG;
	echo -e "[squid-$CONFIGID]\naccept=$ip:3127\nconnect=$ip:3128\n" >> $CONFIG;
	
	CONF_NAME="$(echo $ip | awk -F "." '{print $3"_"$4}')"
	echo "CONF_NAME: $CONF_NAME"
	if [[ $(grep -cE "tcp" /etc/openvpn/$CONF_NAME.conf) -ne 0 ]]; then
		found_port=`grep -hE "port" /etc/openvpn/$CONF_NAME.conf | awk '{ print $2 }'`
        	echo "we found $found_port IP with: $ip"
		if [ $found_port == "443" ]; then
			echo -e "[openvpn-$CONFIGID]\naccept=$ip:4443\nconnect=$ip:443\n" >> $CONFIG;
		else
			echo -e "[openvpn-$CONFIGID]\naccept=$ip:443\nconnect=$ip:$found_port\n" >> $CONFIG;
		fi
	fi

	CONFIGID=$((CONFIGID+1))
	echo "CONFIGID: $CONFIGID"
	
	if [ $SYS_VERSION -eq 6 ]; then
		c=$((c+1))
	#elif [ $SYS_VERSION -eq 7 ]; then

	fi	
	echo -e "c: $c \n"
done
	
