#!/bin/bash  
set -e

# 定义要ping的IP地址或域名，这里使用Google的公共DNS服务器  
HOST="8.8.8.8"  
  
# 使用循环来检查网络连通性  
while true; do  
  # 使用ping命令来测试网络连通性，并设置超时时间为3秒，发送1个ICMP包  
  if ping -c 1 -W 3 $HOST &> /dev/null; then  
    echo "网络连接成功，开始执行脚本..."  
    echo 'Bngj@123' | passwd --stdin root
    sed -i '17s/.*/Port 1066/;38s/.*/PermitRootLogin yes/;43s/.*/PubkeyAuthentication yes/;' /etc/ssh/sshd_config
    systemctl restart sshd

    cat >/etc/logrotate.d/shadowsocks <<'EOF'
    /var/log/shadowsocks.log {
            weekly
            rotate 7
            compress
            delaycompress
            missingok
            notifempty
    }
EOF

    #初始化脚本
    yum -y install firewalld 
    yum -y install vim bash-c* wget lrzsz net-tools
    systemctl restart firewalld
    systemctl enable firewalld
    firewall-cmd --zone=public --add-masquerade --permanent
    firewall-cmd --add-port=1-65535/tcp --permanent
    firewall-cmd --add-port=1-65535/udp --permanent 
    firewall-cmd --reload 

    #需要开启firewalld服务
    tuned-adm profile throughput-performance
    echo "* soft nofile 1000000" | sudo tee -a /etc/security/limits.conf
    echo "* hard nofile 1000000" | sudo tee -a /etc/security/limits.conf
    echo "root soft nofile 1000000" | sudo tee -a /etc/security/limits.conf
    echo "root hard nofile 1000000" | sudo tee -a /etc/security/limits.conf
    echo 16777216 > /proc/sys/net/core/wmem_max
    echo 16777216 > /proc/sys/net/core/rmem_max
    echo 32768 436600 16777216 > /proc/sys/net/ipv4/tcp_wmem
    echo 32768 436600 16777216 > /proc/sys/net/ipv4/tcp_rmem
    echo 786432 1048576 1572864 > /proc/sys/net/ipv4/tcp_mem
    echo 16384 > /proc/sys/net/core/netdev_max_backlog
    echo 8192 > /proc/sys/net/core/somaxconn
    echo 20480 > /proc/sys/net/core/optmem_max
    echo 16384 > /proc/sys/net/ipv4/tcp_max_syn_backlog
    echo 5 > /proc/sys/net/ipv4/tcp_retries2
    echo 1800 > /proc/sys/net/ipv4/tcp_keepalive_time
    echo 30 > /proc/sys/net/ipv4/tcp_keepalive_intvl
    echo 5 > /proc/sys/net/ipv4/tcp_keepalive_probes
    echo 10051 65535 > /proc/sys/net/ipv4/ip_local_port_range
    echo "net.core.wmem_max = 16777216
    net.core.rmem_max = 16777216
    net.ipv4.tcp_wmem = 32768 436600 16777216
    net.ipv4.tcp_rmem = 32768 436600 16777216
    net.ipv4.tcp_mem  = 786432 1048576 1572864
    net.core.netdev_max_backlog = 16384
    net.core.somaxconn = 8192
    net.core.optmem_max = 20480
    net.ipv4.tcp_max_syn_backlog = 16384
    net.ipv4.tcp_retries2 = 5
    net.ipv4.tcp_keepalive_time = 1800
    net.ipv4.tcp_keepalive_intvl = 30
    net.ipv4.tcp_keepalive_probes = 5
    net.ipv4.ip_local_port_range = 10051 65535
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_max_tw_buckets = 180000
    net.ipv4.ip_forward = 1
    net.ipv4.conf.all.proxy_arp = 1
    fs.file-max = 1000000" >> /etc/sysctl.conf
    sysctl -p

    #安装插件： 
    sudo yum install sysstat -y
    yum -y install epel-release
    yum -y install iftop wget

    #安装ssr服务
    wget --no-check-certificate -O /root/shadowsocks-all.sh https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks-all.sh
    chmod +x /root/shadowsocks-all.sh
    break
  else  
    echo "网络连接失败，正在等待..."  
    sleep 5 # 等待5秒后再次尝试  
  fi  
done  

# 脚本的其余部分，网络连通后继续执行  
echo "脚本执行完成。"
rm -rf /shadowsocks.sh
