#!/bin/bash
#!/bin/sh

PIDS=`ps -ef | grep YouMiIpServer | grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
	#kill -9 $PIDS
    echo -e "\033[31m有米IP搭建端已在运行中,PID:$PIDS \033[0m"
    echo -e "\033[44;37m运行信息:\033[0m"
    echo "状态：`ps -aux| awk '$2~/^'$PIDS'$/{print $8}'`"
    echo "路径：`ps -aux| awk '$2~/^'$PIDS'$/{print $11}'`"
    echo "用户: `ps -aux| awk '$2~/^'$PIDS'$/{print $1}'`"
    echo "CPU占用：`ps -aux| awk '$2~/^'$PIDS'$/{print $3}'`%"
    echo "内存占用：`ps -aux| awk '$2~/^'$PIDS'$/{print $4}'`%"
    echo "运行时间：`ps -aux| awk '$2~/^'$PIDS'$/{print $10}'`"
    tail -n 20 /root/ProxyServer/YoumiIp.log   
    echo -e "\033[45;37m系统信息:\033[0m"
    echo -e "CPU：`cat /proc/cpuinfo | grep "model name" | uniq | awk -F ":" '{print $2}'` `cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l`U`cat /proc/cpuinfo | grep "cpu cores" | uniq | awk -F ":" '{print $2}'`C/`cat /proc/cpuinfo | grep "processor" | wc -l`T"
    memory_k=`cat /proc/meminfo | head -1 | awk -F ":" '{print $2}' | awk '{print $1}'`
    swap_k=`cat /proc/meminfo | grep "SwapTotal:" | awk -F ":" '{print $2}' | awk '{print $1}'`
    echo -e "内存：`awk 'BEGIN{printf "%.2f\n",'$memory_k'/'1048576'}'`GB(`awk 'BEGIN{printf "%.2f\n",'$swap_k'/'1048576'}'`)"
    echo -e "TCP连接总数："$(ss -s | awk '$1=="TCP"{print $2}')
    echo -e "UDP连接总数："$(ss -s | awk '$1=="UDP"{print $2}')
    echo -e "\033[32m`netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'`\033[0m"
    exit 2
fi
os_number=`getconf LONG_BIT`
if [ $os_number == 64 ];then
    SYSTEM_RECOGNIZE="64"
elif [ $os_number == 32 ];then
    SYSTEM_RECOGNIZE="32"
fi
wget http://down.lhip.cc/ProxyServerLinux${SYSTEM_RECOGNIZE}.zip
mkdir -p /root/ProxyServer/tmp
if cat  /etc/*release|grep -E "centos|redhat" > /dev/null 2&>1
then
    os_release="centos"
    which unzip
    if [ $? == 1 ];then
        yum install -y unzip && mv ProxyServerLinux${SYSTEM_RECOGNIZE}.zip /root/ProxyServer/tmp/ && unzip /root/ProxyServer/tmp/*.zip -d /root/ProxyServer/tmp/
    else
        mv ProxyServerLinux${SYSTEM_RECOGNIZE}.zip /root/ProxyServer/tmp/ && unzip /root/ProxyServer/tmp/*.zip -d /root/ProxyServer/tmp/
    fi
else
    os_release="ubuntu"
    which unzip
    if [ $? == 1 ];then
        apt-get install unzip && mv ProxyServerLinux${SYSTEM_RECOGNIZE}.zip /root/ProxyServer/tmp/ && unzip /root/ProxyServer/*.zip -d /root/ProxyServer/tmp/
    else
        mv ProxyServerLinux${SYSTEM_RECOGNIZE}.zip /root/ProxyServer/tmp/ && unzip /root/ProxyServer/tmp/*.zip -d /root/ProxyServer/tmp/
    fi
fi
mv /root/ProxyServer/tmp/YouMiIpServer /root/ProxyServer/YouMiIpServer
if [ ! -f /root/ProxyServer/YouMiIp.json ];then
    mv /root/ProxyServer/tmp/YouMiIp.json /root/ProxyServer/YouMiIp.json
    if [ -n "$1" ] ;then
        sed -i "s/xydsoft/$1/g" /root/ProxyServer/YouMiIp.json
    else
        sed -i "s/xydsoft//g" /root/ProxyServer/YouMiIp.json
    fi
    if [ -n "$2" ] ;then
        sed -i "s/3999/$2/g" /root/ProxyServer/YouMiIp.json
    fi
    if [ -n "$3" ] ;then
        sed -i "s/3998/$3/g" /root/ProxyServer/YouMiIp.json
    fi
    if [ -n "$4" ] ;then
        sed -i "s/3997/$4/g" /root/ProxyServer/YouMiIp.json
    fi
    if [ -n "$5" ] ;then
        sed -i "s/3996/$5/g" /root/ProxyServer/YouMiIp.json
    fi
fi
if [ ! -f /root/ProxyServer/YouMiIpUser.json ];then
    mv /root/ProxyServer/tmp/YouMiIpUser.json /root/ProxyServer/YouMiIpUser.json
fi
if [ ! -f /root/ProxyServer/YouMiIpAddrList.json ];then
     mv /root/ProxyServer/tmp/YouMiIpAddrList.json /root/ProxyServer/YouMiIpAddrList.json
fi
rm -rf /root/ProxyServer/tmp
chmod 777 /root/ProxyServer/YouMiIpServer
if grep YouMiIpServer /etc/rc.local
then
    chmod 777 /etc/rc.local
    echo "设置开机启动完成."
else
    systemctl stop firewalld
    systemctl disable firewalld
    echo "cd /root/ProxyServer/ &&  nohup /root/ProxyServer/YouMiIpServer > /root/ProxyServer/YoumiIp.log 2>&1 &" >> /etc/rc.local
    if grep YouMiIpServer /etc/rc.local
    then
        chmod 777 /etc/rc.local
        echo "设置开机启动完成."
    else
        echo "设置开机启动失败!"
    fi
fi
cd /root/ProxyServer/ && nohup /root/ProxyServer/YouMiIpServer > /root/ProxyServer/YoumiIp.log 2>&1 &
sleep 3
tail -n 20 /root/ProxyServer/YoumiIp.log