#!/bin/sh
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

UA_Browser="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36";
UA_Dalvik="Dalvik/2.1.0 (Linux; U; Android 9; ALP-AL00 Build/HUAWEIALP-AL00)";
Disney_Auth="grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&latitude=0&longitude=0&platform=browser&subject_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJiNDAzMjU0NS0yYmE2LTRiZGMtOGFlOS04ZWI3YTY2NzBjMTIiLCJhdWQiOiJ1cm46YmFtdGVjaDpzZXJ2aWNlOnRva2VuIiwibmJmIjoxNjIyNjM3OTE2LCJpc3MiOiJ1cm46YmFtdGVjaDpzZXJ2aWNlOmRldmljZSIsImV4cCI6MjQ4NjYzNzkxNiwiaWF0IjoxNjIyNjM3OTE2LCJqdGkiOiI0ZDUzMTIxMS0zMDJmLTQyNDctOWQ0ZC1lNDQ3MTFmMzNlZjkifQ.g-QUcXNzMJ8DwC9JqZbbkYUSKkB1p4JGW77OON5IwNUcTGTNRLyVIiR8mO6HFyShovsR38HRQGVa51b15iAmXg&subject_token_type=urn%3Abamtech%3Aparams%3Aoauth%3Atoken-type%3Adevice"
Disney_Header="authorization: Bearer ZGlzbmV5JmJyb3dzZXImMS4wLjA.Cu56AgSfBTDag5NiRA81oLHkDZfu5L3CKadnefEAY84"
Font_Black="\033[30m";
Font_Red="\033[31m";
Font_Green="\033[32m";
Font_Yellow="\033[33m";
Font_Blue="\033[34m";
Font_Purple="\033[35m";
Font_SkyBlue="\033[36m";
Font_White="\033[37m";
Font_Suffix="\033[0m";
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[错误]${Font_color_suffix}"
Tip="${Green_font_prefix}[注意]${Font_color_suffix}"
sh_ver="1.0.0"



rh(){
    wget --no-check-certificate -q https://hyo.sh/nhfetch && bash nhfetch install
    sleep 3
}

changemirrors(){
    bash <(curl -sSL https://dl.hyo.sh/files/sh/ChangeMirrors.sh)
}

CheckIPAddr()
{
echo $1|grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$" > /dev/null;
#IP地址必须为全数字
        if [ $? -ne 0 ]
        then
                return 1
        fi
        ipaddr=$1
        a=`echo $ipaddr|awk -F . '{print $1}'`  #以"."分隔，取出每个列的值
        b=`echo $ipaddr|awk -F . '{print $2}'`
        c=`echo $ipaddr|awk -F . '{print $3}'`
        d=`echo $ipaddr|awk -F . '{print $4}'`
        for num in $a $b $c $d
        do
                if [ $num -gt 255 ] || [ $num -lt 0 ]    #每个数值必须在0-255之间
                then
                        return 1
                fi
        done
                return 0
}
check_sys(){
    if [[ -f /etc/redhat-release ]]; then
        release="centos"
    elif cat /etc/issue | grep -q -E -i "debian"; then
        release="debian"
    elif cat /etc/issue | grep -q -E -i "ubuntu"; then
        release="ubuntu"
    elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
        release="centos"
    elif cat /proc/version | grep -q -E -i "debian"; then
        release="debian"
    elif cat /proc/version | grep -q -E -i "ubuntu"; then
        release="ubuntu"
    elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
        release="centos"
    fi
    #bit=`uname -m`
}


install_besttrace(){
    clear
    echo -e "${Info}安装besttrace"
    wget --no-check-certificate -O /usr/bin/besttrace https://dl.hyo.sh/files/sh/besttrace
    chmod +x /usr/bin/besttrace
}

install_soga(){
    clear
    echo -e "${Info}安装Soga后端"
    if [[ ${release} == "centos" ]]; then
                wget --no-check-certificate https://hyo.sh/docker-compose.yml
                docker pull vaxilu/soga:latest
            else
                wget --no-check-certificate https://hyo.sh/docker-compose.yml
                docker pull vaxilu/soga:latest
            fi
    echo -e "${Info} 完成Soga后端安装"
    
}

youhua(){
clear
        echo -e "${Info}优化系统中"
echo -e "${Info}安装htpdate中"
if [[ ${release} == "centos" ]]; then
                yum install unzip gcc gcc-c++ -y
                wget --no-check-certificate https://hyo.sh/ntp.zip
                unzip ntp.zip
                cd htpdate-master
                make && make install
                mv /etc/localtime /etc/localtime.bak  
                ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime   
            else
                apt-get install htpdate -y
                mv /etc/localtime /etc/localtime.bak  
                ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
            fi

echo -e "${Info}安装Irqbalance中"
if [[ ${release} == "centos" ]]; then
                yum install irqbalance -y
            else
                apt-get install irqbalance -y
            fi

echo -e "${Info}開啟Irqbalance中"
systemctl restart irqbalance.service
systemctl enable irqbalance.service
echo -e "${Info}開啟Irqbalance完成"

echo -e "${Info}安装BC中"
if [[ ${release} == "centos" ]]; then
                yum install bc -y
            else
                apt install bc -y
            fi

echo -e "${Info}配置BC中"
rfc=4096
cc=$(grep -c processor /proc/cpuinfo)
rsfe=$(echo $cc*$rfc | bc)
rsfe4=$(echo $rsfe*4 | bc)
sysctl -w net.core.rps_sock_flow_entries=$rsfe
echo -e "${Info}配置BC完成"

echo -e "${Info}同步时间中"
timedatectl set-timezone Asia/Shanghai
htpdate -s www.baidu.com
hwclock -w

echo -e "${Info}配置日志中"
cat > /etc/systemd/journald.conf <<EOF
[Journal]
SystemMaxUse=384M
SystemMaxFileSize=128M
ForwardToSyslog=no
EOF

cat > /etc/security/limits.conf << EOF
root soft nofile 512000
root hard nofile 512000
root soft nproc 512000
root hard nproc 512000
EOF

#sed -i '/kernel.pid_max/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/vm.nr_hugepages/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.optmem_max/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.all.route_localnet/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.ip_forward/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.all.forwarding/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.default.forwarding/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.all.forwarding/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.default.forwarding/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.lo.forwarding/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.all.disable_ipv6/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.default.disable_ipv6/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.lo.disable_ipv6/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.all.accept_ra/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.default.accept_ra/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.default.accept_ra/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.netdev_max_backlog/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.netdev_budget/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.netdev_budget_usecs/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/fs.file-max /d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.rmem_max/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.wmem_max/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.rmem_default/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.wmem_default/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.somaxconn/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.icmp_echo_ignore_all/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.icmp_echo_ignore_broadcasts/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.icmp_ignore_bogus_error_responses/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.all.accept_redirects/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.default.accept_redirects/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.all.secure_redirects/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.default.secure_redirects/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.all.send_redirects/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.default.send_redirects/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.default.rp_filter/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.all.rp_filter/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_keepalive_time/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_keepalive_intvl/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_keepalive_probes/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_synack_retries/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_syncookies/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_rfc1337/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_timestamps/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_tw_reuse/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_tw_recycle/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_fin_timeout/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.ip_local_port_range/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_max_tw_buckets/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_fastopen/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_rmem/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_wmem/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.udp_rmem_min/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.udp_wmem_min/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_mtu_probing/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.all.arp_ignore /d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.default.arp_ignore/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.all.arp_announce/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.conf.default.arp_announce/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_autocorking/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_slow_start_after_idle/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_max_syn_backlog/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.default_qdisc/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_congestion_control/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_notsent_lowat/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_no_metrics_save/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_ecn/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.tcp_ecn_fallback/d' /etc/sysctl.d/99-sysctl.conf
##sed -i '/net.ipv4.tcp_frto/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.all.accept_redirects/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.conf.default.accept_redirects/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/vm.swappiness/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.ip_unprivileged_port_start/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/vm.overcommit_memory/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.neigh.default.gc_thresh3/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.neigh.default.gc_thresh2/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv4.neigh.default.gc_thresh1/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.neigh.default.gc_thresh3/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.neigh.default.gc_thresh2/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.ipv6.neigh.default.gc_thresh1/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.netfilter.nf_conntrack_max/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.nf_conntrack_max/d' /etc/sysctl.d/99-sysctl.conf
#sed -i '/net.core.rps_sock_flow_entries/d' /etc/sysctl.d/99-sysctl.conf


cat > '/etc/sysctl.d/99-sysctl.conf' << EOF
#!!! Do not change these settings unless you know what you are doing !!!
vm.swappiness = 10
fs.file-max = 1000000
net.ipv4.ip_forward = 1
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864
net.ipv4.tcp_congestion_control = bbr
net.ipv4.tcp_mtu_probing = 1
net.core.default_qdisc = fq
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_no_metrics_save = 1
net.ipv4.tcp_thin_linear_timeouts = 1
net.ipv4.tcp_fastopen = 3
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
kernel.printk = 0

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1


EOF

sysctl --system
echo madvise > /sys/kernel/mm/transparent_hugepage/enabled

sed -i '/DefaultTimeoutStartSec/d' /etc/systemd/system.conf
sed -i '/DefaultTimeoutStopSec/d' /etc/systemd/system.conf
sed -i '/DefaultRestartSec/d' /etc/systemd/system.conf
sed -i '/DefaultLimitCORE/d' /etc/systemd/system.conf
sed -i '/DefaultLimitNOFILE/d' /etc/systemd/system.conf
sed -i '/DefaultLimitNPROC/d' /etc/systemd/system.conf

cat > '/etc/systemd/system.conf' << EOF
[Manager]
#DefaultTimeoutStartSec=90s
DefaultTimeoutStopSec=30s
#DefaultRestartSec=100ms
DefaultLimitCORE=infinity
DefaultLimitNOFILE=65535
DefaultLimitNPROC=65535
EOF

sed -i '/soft nofile/d' /etc/security/limits.conf
sed -i '/hard nofile/d' /etc/security/limits.conf
sed -i '/soft nproc/d' /etc/security/limits.conf
sed -i '/hard nproc/d' /etc/security/limits.conf
cat > '/etc/security/limits.conf' << EOF
* soft nofile 512000
* hard nofile 512000
* soft nproc 512000
* hard nproc 512000
EOF

if grep -q "ulimit" /etc/profile
then
    :
else
sed -i '/ulimit -SHn/d' /etc/profile
sed -i '/ulimit -SHn/d' /etc/profile
echo "ulimit -SHn 65535" >> /etc/profile
echo "ulimit -SHu 65535" >> /etc/profile
fi
if grep -q "pam_limits.so" /etc/pam.d/common-session
then
    :
else
sed -i '/required pam_limits.so/d' /etc/pam.d/common-session
echo "session required pam_limits.so" >> /etc/pam.d/common-session
fi
echo -e "$安装Haveged中"
if [[ ${release} == "centos" ]]; then
            yum -y install epel-release
            yum install rng-tools haveged -y
            echo 'EXTRAOPTIONS="--rng-device /dev/urandom"' >/etc/sysconfig/rngd 
            service rngd restart 
            systemctl enable haveged
            systemctl restart haveged 
            systemctl enable rng-tools 
        else
            apt install rng-tools haveged -y
            systemctl enable haveged
            systemctl restart haveged 
            systemctl enable rng-tools 
        fi
echo -e "${Green_font_prefix}安裝Haveged完成${Font_color_suffix} "
systemctl daemon-reload
sysctl -p
clear
echo -e "${Info}系统优化完毕！"
sleep 2s
}

installbbr(){
    echo -e "$安装bbr中"
    bash <(curl -Ls hyo.sh/tools.sh)
}






essential()
{
clear
echo -e "${Green_font_prefix}安装必備組件${Font_color_suffix} "
if [[ ${release} == "centos" ]]; then
            yum update -y
            yum install -y yum-utils
            yum install wget -y
            echo -e "${Green_font_prefix}安裝Wget完成${Font_color_suffix} "
            yum install curl -y
            echo -e "${Green_font_prefix}安裝Curl完成${Font_color_suffix} "
                #yum install ntp -y
                #echo -e "${Green_font_prefix}安裝Ntp完成${Font_color_suffix} "
                #yum install nload -y
                #echo -e "${Green_font_prefix}安裝Nload完成${Font_color_suffix} "
                #yum install screen -y
                #echo -e "${Green_font_prefix}安裝Screen完成${Font_color_suffix} "
                yum install iperf3 -y
                echo -e "${Green_font_prefix}安裝Iperf3完成${Font_color_suffix} "
                yum install lsof -y
                echo -e "${Green_font_prefix}安裝Lsof完成${Font_color_suffix} "
                yum install ufw -y
                echo -e "${Green_font_prefix}安裝UFW完成${Font_color_suffix} "
                #systemctl enable ntp
                #echo -e "${Green_font_prefix}自啟動NTP完成${Font_color_suffix} "
                #systemctl start ntp
                #echo -e "${Green_font_prefix}開啟NTP完成${Font_color_suffix} "
                systemctl disable ufw
                systemctl disable firewalld
                systemctl stop ufw
                systemctl stop firewalld
                echo -e "${Green_font_prefix}關閉UFW完成${Font_color_suffix} "

            else
                apt-get update --allow-releaseinfo-change
                apt install sudo -y
                apt install wget -y
                echo -e "${Green_font_prefix}安裝Wget完成${Font_color_suffix} "
                apt install curl -y
                echo -e "${Green_font_prefix}安裝Curl完成${Font_color_suffix} "
                #apt install ntp -y
                #echo -e "${Green_font_prefix}安裝Ntp完成${Font_color_suffix} "
                apt install nload -y
                echo -e "${Green_font_prefix}安裝Nload完成${Font_color_suffix} "
                apt install screen -y
                echo -e "${Green_font_prefix}安裝Screen完成${Font_color_suffix} "
                apt install iperf3 -y
                echo -e "${Green_font_prefix}安裝Iperf3完成${Font_color_suffix} "
                apt install lsof -y
                echo -e "${Green_font_prefix}安裝Lsof完成${Font_color_suffix} "
                apt install ufw -y
                echo -e "${Green_font_prefix}安裝UFW完成${Font_color_suffix} "
                #systemctl enable ntp
                #echo -e "${Green_font_prefix}自啟動NTP完成${Font_color_suffix} "
                #systemctl start ntp
                #echo -e "${Green_font_prefix}開啟NTP完成${Font_color_suffix} "
                systemctl disable ufw
                systemctl stop ufw
                echo -e "${Green_font_prefix}關閉UFW完成${Font_color_suffix} "
            fi
                

echo -e "${Green_font_prefix}安裝全部必備組件完成${Font_color_suffix} "
}


install_docker(){
    echo -e "${Green_font_prefix}安装Docker中${Font_color_suffix} "
                    if [[ ${release} == "centos" ]]; then
                        #yum install -y yum-utils
                        #yum-config-manager \
                        #--add-repo \
                        #https://download.docker.com/linux/centos/docker-ce.repo
                        #yum install docker-ce docker-ce-cli containerd.io -y
                        wget -qO- get.docker.com | bash
                        sleep 3s
                        systemctl start docker
                        systemctl enable docker
                        sleep 3s
                        curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
                        sleep 3s
                        chmod +x /usr/local/bin/docker-compose
                    else
                        #sudo apt update -y
                        #sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg2 -y
                        #curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
                        #sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
                        #sudo apt update -y
                        #sudo apt install docker-ce -y
                        wget -qO- get.docker.com | bash
                        sleep 3s
                        systemctl start docker
                        systemctl enable docker
                        sleep 3s
                        curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
                        sleep 3s
                        chmod +x /usr/local/bin/docker-compose
            fi
echo -e "${Green_font_prefix}安裝Docker完成${Font_color_suffix} "
}






return_menu(){
    echo -e "${Green_font_prefix}3秒後返回腳本首頁${Font_color_suffix} "
    sleep 3
    clear
    start_menu
}

start_menu()
{
clear
check_sys
echo && echo -e " ${Red_font_prefix}Hyo的一键脚本${Font_color_suffix}
  
 ${Green_font_prefix}1.${Font_color_suffix}  安装 Soga 后端
 ———————————————————————————————————————————————
 ${Green_font_prefix}11.${Font_color_suffix} 优化系统
 ${Green_font_prefix}12.${Font_color_suffix} 安装 BBR
 ${Green_font_prefix}13.${Font_color_suffix} 安装 docker
 ${Green_font_prefix}14.${Font_color_suffix} 安裝必備組件
 ${Green_font_prefix}15.${Font_color_suffix} 安裝besttrace
 ———————————————————————————————————————————————" &&

check_status
    get_system_info
    echo -e " 系统及内核: ${Font_color_suffix}$opsy ($lbit Bit) $virtual${PLAIN} $kern${PLAIN}${Font_color_suffix}"
    if [[ ${kernel_status} == "noinstall" ]]; then
        echo -e " 当前状态: ${Green_font_prefix}未安装${Font_color_suffix} 加速内核 ${Red_font_prefix}请先安装内核${Font_color_suffix}"
    else
        echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix} ${_font_prefix}${kernel_status}${Font_color_suffix} 加速内核 , ${Green_font_prefix}${run_status}${Font_color_suffix}"
        
    fi
    echo -e " 当前拥塞控制算法为: ${Green_font_prefix}${net_congestion_control}${Font_color_suffix} 当前队列算法为: ${Green_font_prefix}${net_qdisc}${Font_color_suffix} "
    
read -e -p " 请输入数字:" num
case "$num" in
    1)
    install_soga
    ;;
    11)
    youhua
    ;;
    12)
    installbbr
    ;;
    13)
    install_docker
    ;;
    14)
    essential
    ;;
    15)
    install_besttrace
    ;;
    *)
    echo -e "${Green_font_prefix}请输入正确数字${Font_color_suffix}"
    return_menu
    ;;
esac
}






check_sys(){
    if [[ -f /etc/redhat-release ]]; then
        release="centos"
    elif cat /etc/issue | grep -q -E -i "debian"; then
        release="debian"
    elif cat /etc/issue | grep -q -E -i "ubuntu"; then
        release="ubuntu"
    elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
        release="centos"
    elif cat /proc/version | grep -q -E -i "debian"; then
        release="debian"
    elif cat /proc/version | grep -q -E -i "ubuntu"; then
        release="ubuntu"
    elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
        release="centos"
    fi

#from https://github.com/oooldking
get_opsy() {
    [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
    [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
    [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}
get_system_info() {
    cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
    cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
    freq=$( awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
    corescache=$( awk -F: '/cache size/ {cache=$2} END {print cache}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
    tram=$( free -m | awk '/Mem/ {print $2}' )
    uram=$( free -m | awk '/Mem/ {print $3}' )
    bram=$( free -m | awk '/Mem/ {print $6}' )
    swap=$( free -m | awk '/Swap/ {print $2}' )
    uswap=$( free -m | awk '/Swap/ {print $3}' )
    up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days %d hour %d min\n",a,b,c)}' /proc/uptime )
    load=$( w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
    opsy=$( get_opsy )
    arch=$( uname -m )
    lbit=$( getconf LONG_BIT )
    kern=$( uname -r )

    # disk_size1=$( LANG=C df -hPl | grep -wvE '\-|none|tmpfs|overlay|shm|udev|devtmpfs|by-uuid|chroot|Filesystem' | awk '{print $2}' )
    # disk_size2=$( LANG=C df -hPl | grep -wvE '\-|none|tmpfs|overlay|shm|udev|devtmpfs|by-uuid|chroot|Filesystem' | awk '{print $3}' )
    # disk_total_size=$( calc_disk ${disk_size1[@]} )
    # disk_used_size=$( calc_disk ${disk_size2[@]} )

    tcpctrl=$( sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}' )

    virt_check
}
virt_check(){
    # if hash ifconfig 2>/dev/null; then
        # eth=$(ifconfig)
    # fi

    virtualx=$(dmesg) 2>/dev/null

    if  [ $(which dmidecode) ]; then
        sys_manu=$(dmidecode -s system-manufacturer) 2>/dev/null
        sys_product=$(dmidecode -s system-product-name) 2>/dev/null
        sys_ver=$(dmidecode -s system-version) 2>/dev/null
    else
        sys_manu=""
        sys_product=""
        sys_ver=""
    fi
    
    if grep docker /proc/1/cgroup -qa; then
        virtual="Docker"
    elif grep lxc /proc/1/cgroup -qa; then
        virtual="Lxc"
    elif grep -qa container=lxc /proc/1/environ; then
        virtual="Lxc"
    elif [[ -f /proc/user_beancounters ]]; then
        virtual="OpenVZ"
    elif [[ "$virtualx" == *kvm-clock* ]]; then
        virtual="KVM"
    elif [[ "$cname" == *KVM* ]]; then
        virtual="KVM"
    elif [[ "$cname" == *QEMU* ]]; then
        virtual="KVM"
    elif [[ "$virtualx" == *"VMware Virtual Platform"* ]]; then
        virtual="VMware"
    elif [[ "$virtualx" == *"Parallels Software International"* ]]; then
        virtual="Parallels"
    elif [[ "$virtualx" == *VirtualBox* ]]; then
        virtual="VirtualBox"
    elif [[ -e /proc/xen ]]; then
        virtual="Xen"
    elif [[ "$sys_manu" == *"Microsoft Corporation"* ]]; then
        if [[ "$sys_product" == *"Virtual Machine"* ]]; then
            if [[ "$sys_ver" == *"7.0"* || "$sys_ver" == *"Hyper-V" ]]; then
                virtual="Hyper-V"
            else
                virtual="Microsoft Virtual Machine"
            fi
        fi
    else
        virtual="Dedicated母鸡"
    fi
}

    
}

check_version(){
    if [[ -s /etc/redhat-release ]]; then
        version=`grep -oE  "[0-9.]+" /etc/redhat-release | cut -d . -f 1`
    else
        version=`grep -oE  "[0-9.]+" /etc/issue | cut -d . -f 1`
    fi
    bit=`uname -m`
    if [[ ${bit} = "x86_64" ]]; then
        bit="x64"
    else
        bit="x32"
    fi
}




        
    check_status(){
    kernel_version=`uname -r | awk -F "-" '{print $1}'`
    kernel_version_full=`uname -r`
    net_congestion_control=`cat /proc/sys/net/ipv4/tcp_congestion_control | awk '{print $1}'`
    net_qdisc=`cat /proc/sys/net/core/default_qdisc | awk '{print $1}'`
    kernel_version_r=`uname -r | awk '{print $1}'`
    # if [[ ${kernel_version_full} = "4.14.182-bbrplus" || ${kernel_version_full} = "4.14.168-bbrplus" || ${kernel_version_full} = "4.14.98-bbrplus" || ${kernel_version_full} = "4.14.129-bbrplus" || ${kernel_version_full} = "4.14.160-bbrplus" || ${kernel_version_full} = "4.14.166-bbrplus" || ${kernel_version_full} = "4.14.161-bbrplus" ]]; then
    if [[ ${kernel_version_full} == *bbrplus* ]]; then
        kernel_status="BBRplus"
    # elif [[ ${kernel_version} = "3.10.0" || ${kernel_version} = "3.16.0" || ${kernel_version} = "3.2.0" || ${kernel_version} = "4.4.0" || ${kernel_version} = "3.13.0"  || ${kernel_version} = "2.6.32" || ${kernel_version} = "4.9.0" || ${kernel_version} = "4.11.2" || ${kernel_version} = "4.15.0" ]]; then
        # kernel_status="Lotserver" 
    elif [[ ${kernel_version_full} == *4.9.0-4* || ${kernel_version_full} == *4.15.0-30* || ${kernel_version_full} == *4.8.0-36* || ${kernel_version_full} == *3.16.0-77* || ${kernel_version_full} == *3.16.0-4* || ${kernel_version_full} == *3.2.0-4* || ${kernel_version_full} == *4.11.2-1* || ${kernel_version_full} == *2.6.32-504* || ${kernel_version_full} == *4.4.0-47* || ${kernel_version_full} == *3.13.0-29 || ${kernel_version_full} == *4.4.0-47* ]]; then
        kernel_status="Lotserver"
    elif [[ `echo ${kernel_version} | awk -F'.' '{print $1}'` == "4" ]] && [[ `echo ${kernel_version} | awk -F'.' '{print $2}'` -ge 9 ]] || [[ `echo ${kernel_version} | awk -F'.' '{print $1}'` == "5" ]]; then
        kernel_status="BBR"
    else 
        kernel_status="noinstall"
    fi
    

    if [[ ${kernel_status} == "BBR" ]]; then
        run_status=`cat /proc/sys/net/ipv4/tcp_congestion_control | awk '{print $1}'`
        if [[ ${run_status} == "bbr" ]]; then
            run_status=`cat /proc/sys/net/ipv4/tcp_congestion_control | awk '{print $1}'`
            if [[ ${run_status} == "bbr" ]]; then
                run_status="BBR启动成功"
            else 
                run_status="BBR启动失败"
            fi
        elif [[ ${run_status} == "bbr2" ]]; then
            run_status=`cat /proc/sys/net/ipv4/tcp_congestion_control | awk '{print $1}'`
            if [[ ${run_status} == "bbr2" ]]; then
                run_status="BBR2启动成功"
            else 
                run_status="BBR2启动失败"
            fi  
        elif [[ ${run_status} == "tsunami" ]]; then
            run_status=`lsmod | grep "tsunami" | awk '{print $1}'`
            if [[ ${run_status} == "tcp_tsunami" ]]; then
                run_status="BBR魔改版启动成功"
            else 
                run_status="BBR魔改版启动失败"
            fi
        elif [[ ${run_status} == "nanqinlang" ]]; then
            run_status=`lsmod | grep "nanqinlang" | awk '{print $1}'`
            if [[ ${run_status} == "tcp_nanqinlang" ]]; then
                run_status="暴力BBR魔改版启动成功"
            else 
                run_status="暴力BBR魔改版启动失败"
            fi
        else 
            run_status="未安装加速模块"
        fi
        
    elif [[ ${kernel_status} == "Lotserver" ]]; then
        if [[ -e /appex/bin/lotServer.sh ]]; then
            run_status=`bash /appex/bin/lotServer.sh status | grep "LotServer" | awk  '{print $3}'`
            if [[ ${run_status} = "running!" ]]; then
                run_status="启动成功"
            else 
                run_status="启动失败"
            fi
        else 
            run_status="未安装加速"
        fi  
    elif [[ ${kernel_status} == "BBRplus" ]]; then
        run_status=`cat /proc/sys/net/ipv4/tcp_congestion_control | awk '{print $1}'`
        if [[ ${run_status} == "bbrplus" ]]; then
            run_status=`cat /proc/sys/net/ipv4/tcp_congestion_control | awk '{print $1}'`
            if [[ ${run_status} == "bbrplus" ]]; then
                run_status="BBRplus启动成功"
            else 
                run_status="BBRplus启动失败"
            fi
        elif [[ ${run_status} == "bbr" ]]; then
                run_status="BBR启动成功"    
        else 
            run_status="未安装加速模块"
        fi
    fi
}

ssh_port(){
    clear
    read -p "输入 SSH Port: " ssh_port
    read -p "输入 SSH 密码: " ssh_pwd
    echo root:${ssh_pwd}| chpasswd root
    sed -i 's/^#\?Port.*/Port '${ssh_port}'/g' /etc/ssh/sshd_config;
    sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config;
    sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config;
    systemctl restart sshd
}

checkurl(){
    url=$(curl --max-time 5 --retry 3 --retry-delay 2 --connect-timeout 2 -s --head $1 | head -n 1)
            if [[ ${url} == *200* || ${url} == *302* || ${url} == *308* ]]; then
                echo "地址正确"
            else
                echo "地址错误"
                exit 1
            fi
}


help(){
    echo "使用方法：bash  <(curl -Ls hyo.sh) [-a] [-c] [-s] [-x] [-d] [-m]"
    echo ""
    echo "  -allinone , --allinone   一键优化+后端"
    echo ""
    echo "  -a , --all              安裝所有必備文件"
    echo ""
    echo "  -b , --bbr              安裝BBR"
    echo ""
    echo "  -c , --changemirrors   更换镜像源"
    echo ""
    echo "  -s , --soga           安裝Soga后端"
    echo ""
    echo "  -d , --docker           安装Docker"
    echo ""
    echo "  -m , --menu             打開腳本主菜單"
}

check_sys
check_version
[[ ${release} != "debian" ]] && [[ ${release} != "ubuntu" ]] && [[ ${release} != "centos" ]] && echo -e "${Error} 本脚本不支持当前系统 ${release} !" && exit 1


if [[ $# = 1 ]];then
    key="$1"
    case $key in
        -allinone|--allinone)
        rh
        essential
        youhua
        install_docker
        install_soga
        ;;
        -a|--all)
        rh
        essential
        youhua
        ;;
        -b|--bbr)
        installbbr
        ;;
        -c|--changemirrors)
        changemirrors
        ;;
        -d|--docker)
        install_docker
        ;;
        -s|--soga)
        install_docker
        install_soga
        ;;
        -ssh|--ssh)
        ssh_port
        ;;
        
        help|-h|--help)
        help
        ;;
        -m|--menu)
        start_menu
        ;;
        *)
        start_menu
        ;;
    esac
else
    help
fi



