#/bin/bash
#关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
#setenforce 0 #临时关闭selinux

#修改最大进程数和最大打开文件数
cat >> /etc/security/limits.conf << EOF 
*       soft    nofile  65536
*       hard    nofile  1024000
*       soft    nproc   65536
*       hard    nproc   unlimited
*       soft    memlock unlimited
*       hard    memlock unlimited
EOF

#内核优化
cat >> /etc/sysctl.conf << EOF
net.netfilter.nf_conntrack_max = 2500000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
fs.file-max=1025500
fs.nr_open=1025500
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_max_tw_buckets = 1000000
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 2
net.core.netdev_max_backlog = 32768
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 32768
net.core.rmem_default = 8388608
net.core.wmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
vm.max_map_count = 1025500

net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6 = 1
vm.swappiness = 0
EOF

# 启动防火墙，并设置开机启动
systemctl start firewalld > /dev/null 2>&1 && systemctl enable firewalld > /dev/null 2>&1

# 创建目录
mkdir -p /root/software/superbot/{java_app/{config,lib,logs},shell,tmp}

# 安装插件
# 定义安装函数
install_if_missing() {
    local pkg_name=$1
    echo "检查软件包 $pkg_name 是否安装..."
    if dnf list installed "$pkg_name" &> /dev/null; then
        echo "$pkg_name 已安装"
    else
        echo "正在安装 $pkg_name..."
        dnf install -y "$pkg_name"
        # 验证安装
        if dnf list installed "$pkg_name" &> /dev/null; then
            echo "$pkg_name 安装成功"
        else
            echo "Error: $pkg_name 安装失败" >&2
            exit 1
        fi
    fi
}

# 安装所需软件包
install_if_missing "bc"
install_if_missing "jq"
install_if_missing "dhcp-client"
install_if_missing "wget"
install_if_missing "curl"
install_if_missing "java-17-openjdk"

# wget下载所需要的脚本
wget http://183.239.210.44:8088/init.sh -O /root/software/init.sh
wget http://183.239.210.44:8088/shell/create_bridge.sh -O /root/software/superbot/shell/create_bridge.sh
wget http://183.239.210.44:8088/shell/init.sh -O /root/software/superbot/shell/init.sh
wget http://183.239.210.44:8088/shell/netns.sh -O /root/software/superbot/shell/netns.sh
wget http://183.239.210.44:8088/shell/run.sh -O /root/software/superbot/shell/run.sh
wget http://183.239.210.44:8088/shell/show.sh -O /root/software/superbot/shell/show.sh
wget http://183.239.210.44:8088/shell/stop.sh -O /root/software/superbot/shell/stop.sh
wget http://183.239.210.44:8088/java_app/config/application-prod.yml -O /root/software/superbot/java_app/config/application-prod.yml
wget http://183.239.210.44:8088/java_app/config/application.yml -O /root/software/superbot/java_app/config/application.yml
wget http://183.239.210.44:8088/java_app/config/logback-spring.xml -O /root/software/superbot/java_app/config/logback-spring.xml
wget http://183.239.210.44:8088/java_app/lib/lw-superbot-client-1.0.0.jar -O /root/software/superbot/java_app/lib/lw-superbot-client-1.0.0.jar
wget http://183.239.210.44:8088/java_app/run.sh -O /root/software/superbot/java_app/run.sh