#!/bin/bash
current=`date "+%Y-%m-%d %H:%M:%S"`
timeStamp=`date -d "$current" +%s`
currentTimeStamp=$((timeStamp*1000+`date "+%N"`/1000000))
ips=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:")
if [ ! -n "$ips" ]; then
        text=$currentTimeStamp
else
        text=$ips
fi
openssl req -x509 -days 7300 -sha256 -nodes -subj "/C=CN/ST=Shanghai/L=Shanghai/O=PengZhun/CN=$text" -newkey rsa:2048 -keyout /www/server/pure-ftpd/etc/pure-ftpd.pem -out /www/server/pure-ftpd/etc/pure-ftpd.pem
if [ ! -e "/www/server/pure-ftpd/etc/pure-ftpd.pem" ]; then
    echo "Error Not OpenSSL!"
    kill -9 $$
fi
sed -i "s@^# MaxClientsPerIP.*@MaxClientsPerIP                        1000@" /www/server/pure-ftpd/etc/pure-ftpd.conf
sed -i "s@^# TLS.*@TLS                        1@" /www/server/pure-ftpd/etc/pure-ftpd.conf
sed -i "s@^# PassivePortRange.*@PassivePortRange                        28000 28999@" /www/server/pure-ftpd/etc/pure-ftpd.conf
sed -i "s@^MaxClientsPerIP.*@MaxClientsPerIP                        1000@" /www/server/pure-ftpd/etc/pure-ftpd.conf
sed -i "s@^TLS.*@&\nCertFile                   /www/server/pure-ftpd/etc/pure-ftpd.pem@" /www/server/pure-ftpd/etc/pure-ftpd.conf
sed -i "s@^TLS.*@TLS                        1@" /www/server/pure-ftpd/etc/pure-ftpd.conf
sed -i "s@^PassivePortRange.*@PassivePortRange                        28000 28999@" /www/server/pure-ftpd/etc/pure-ftpd.conf
if [ -z "$(grep '28000:28999' /etc/sysconfig/iptables)" ]; then
    iptables -I INPUT 6 -p tcp -m state --state NEW -m tcp --dport 28000:28999 -j ACCEPT
    service iptables save
fi
service pure-ftpd restart
echo $text
