#!/bin/sh

# Set environment
export PATH=/bin/:/sbin:/usr/bin/:/usr/sbin:/usr/local/sbin/:/usr/local/bin/:$PATH
CURTIME=$(date +%Y%m%d%H%M%S)

# 基础设置 
func_install_plugin(){

    # 配置防火墙
    if [ ! -e /etc/iptables.rules.v4 ];then
        iptables -N RH-Firewall-1-INPUT
        iptables -A INPUT -j RH-Firewall-1-INPUT
        iptables -A RH-Firewall-1-INPUT -s 223.111.217.181/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 153.35.135.181/32  -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 180.101.141.181/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 117.158.189.111/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 61.158.133.242/32  -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 222.88.95.58/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 106.8.131.208/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 106.8.131.29/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 111.63.205.58/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 116.132.219.218/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 121.228.176.2/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 103.45.72.2/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 36.150.140.2/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 192.168.0.0/16 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -s 127.0.0.1/32 -p tcp -m tcp --dport 22 -j ACCEPT
        iptables -A RH-Firewall-1-INPUT -p tcp -m tcp --dport 22 -j DROP
        /usr/sbin/iptables-save > /etc/iptables.rules.v4
        sed -i '/^exit 0/i /usr/sbin/iptables-restore  < /etc/iptables.rules.v4' /etc/rc.local
    else
        echo ""
    fi
    
}


# frp配置
func_setup_frp(){

    /sbin/service frpc stop
    /sbin/service frpc disable
    /sbin/service frps stop
    /sbin/service frps disable
    /sbin/service frpc_fengze stop
    /sbin/service frpc_fengze disable

    
   
    wget -q http://thunder:xunlei@apt.sandai.net/basic_setup/dkj/frpc_install.sh -O /tmp/frpc_install.sh
    sh /tmp/frpc_install.sh PORT_NUMBER HOSTNAME 

cat >>/usr/local/frpc_check_alive.sh<<EOF
#!/bin/bash

proc_count=$(ps auxf | grep  '[f]rpc.ini' -c)
if [ $proc_count -eq 0 ];then
    /usr/bin/frpc -c /etc/frp/frpc.ini >/dev/null 2>&1 &    
fi
}
EOF


# 主函数
main(){

    func_setup_basic

    func_setup_frp
}



# 脚本执行入口
main
