#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export LANG=zh_CN.UTF-8

#全局变量
declare Blue="\033[36m"
declare Green="\033[32m"
declare Red="\033[31m"
declare Font="\033[0m"
declare Success="[${Green}  OK  ${Font}]"
declare Load="       loading..."
declare Info="${Green}[信息]${Font} "
declare Error="${Red}[错误]${Font}"
declare Tip="${Blue}[注意]${Font}"
declare XT=""
declare HX=""
declare G=""
declare W=""
declare centos=""
declare IP=""
declare tram=""

#检测系统类型和版本
function check_sys() {
    if [[ -f /etc/redhat-release ]]; then
        centos=$(cat /etc/redhat-release)
        XT=$(echo "$centos" | grep -Eos '\b[0-9]+\S*\b' | cut -d'.' -f1)
        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 /proc/version | grep -q -E -i "debian"; then
        release="Debian"
    elif cat /proc/version | grep -q -E -i "ubuntu"; then
        release="Ubuntu"
    else
        echo -e "${Error}无法检测到当前操作系统类型。脚本仅支持CentOS系统！"
        exit 1
    fi

    HX=$(grep "core id" /proc/cpuinfo | wc -l)
    G=$(awk '/MemTotal/{printf "%1.f\n", $2/1024/1024}' /proc/meminfo)
    W=$(getconf LONG_BIT)
}

#权限检查
function judge() {
    if [[ $EUID -ne 0 ]]; then
        echo -e "${Error}当前不是ROOT账号，建议更换ROOT账号使用。"
        exit 1
    else
        echo -e "${Tip}ROOT账号权限检查通过，祝你使用愉快！"
        sleep 1s
    fi
}

#安装wget或curl
function install_network_tools() {
    if [[ ${release} == "CentOS" ]]; then
        echo -e "${Info}正在安装必要的网络工具 ${Load}"
        yum install -y curl wget >/dev/null 2>&1
        if [[ $? -eq 0 ]]; then
            echo -e "${Tip}curl和wget安装成功！   ${Success}"
        else
            echo -e "${Tip}curl和wget安装失败！请检查网络连接。"
            exit 1
        fi
    else
        echo -e "${Tip}该脚本仅支持CentOS系统，不支持其他系统"
        exit 1
    fi
}

#更换yum源为阿里云源
function install_yum() {
    echo -e "${Info}正在更换yum源为阿里云源 ${Load}"
    install_network_tools

    if [[ ${XT} -ge 5 && ${XT} -le 8 ]]; then
        curl -s "http://mirrors.aliyun.com/repo/Centos-${XT}.repo" -o /etc/yum.repos.d/CentOS-Base.repo
        if [[ $? -eq 0 ]]; then
            echo -e "${Tip}已更换为阿里云CentOS ${XT} yum源   ${Success}"
        else
            echo -e "${Tip}更换阿里云CentOS ${XT} yum源失败！请检查网络连接。"
            exit 1
        fi
    else
        echo -e "${Tip}不支持的CentOS版本！"
        exit 1
    fi

    echo -e "${Info}正在更换EPEL源为阿里云源 ${Load}"
    cd /etc/yum.repos.d/
    yum install epel-release -y >/dev/null 2>&1
    if [[ $? -eq 0 ]]; then
        wget -O "epel-${XT}.repo" "http://mirrors.aliyun.com/repo/epel-${XT}.repo" >/dev/null 2>&1
        if [[ $? -eq 0 ]]; then
            echo -e "${Tip}EPEL源更换成功！   ${Success}"
        else
            echo -e "${Tip}更换EPEL源失败！请检查网络连接。"
            exit 1
        fi
    else
        echo -e "${Tip}EPEL源安装失败！请检查网络连接。"
        exit 1
    fi

    echo -e "${Info}正在清除旧yum源缓存 ${Load}"
    yum clean all >/dev/null 2>&1
    if [[ $? -eq 0 ]]; then
        echo -e "${Tip}清除旧yum源缓存成功！   ${Success}"
    else
        echo -e "${Tip}清除旧yum源缓存失败！"
    fi

    echo -e "${Info}正在重建yum缓存 ${Load}"
    yum makecache >/dev/null 2>&1
    if [[ $? -eq 0 ]]; then
        echo -e "${Tip}重建yum缓存成功！   ${Success}"
    else
        echo -e "${Tip}重建yum缓存失败！"
    fi

    sleep 1s
    echo -e "${Tip}脚本执行完毕   ${Success}"
}

#修改DNS配置
function install_dns() {
    echo -e "${Info}正在更换DNS为：223.5.5.5 8.8.8.8 ${Load}"
    cat >/etc/resolv.conf <<EOF
options timeout:1 attempts:1 rotate
nameserver 223.5.5.5
nameserver 8.8.8.8
EOF

    if [[ $? -eq 0 ]]; then
        echo -e "${Tip}DNS修改成功！   ${Success}"
    else
        echo -e "${Tip}DNS修改失败！"
    fi
    sleep 1s
    echo -e "${Tip}脚本执行完毕   ${Success}"
}

#优化SSH卡顿慢问题
function install_ssh() {
    echo -e "${Info}正在优化SSH卡顿慢问题 ${Load}"
    install_network_tools

    case "${XT}" in
        6)
            cat >/etc/ssh/sshd_config <<EOF
# \$OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

Protocol 2
SyslogFacility AUTHPRIV
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication no
GSSAPICleanupCredentials yes
UsePAM yes
AcceptEnv LANG LC_*
X11Forwarding yes
UseDNS no
Subsystem sftp /usr/libexec/openssh/sftp-server
EOF
            ;;
        7)
            cat >/etc/ssh/sshd_config <<EOF
# \$OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

Protocol 2
SyslogFacility AUTHPRIV
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication no
GSSAPICleanupCredentials no
UsePAM yes
AcceptEnv LANG LC_*
X11Forwarding yes
UseDNS no
Subsystem sftp /usr/libexec/openssh/sftp-server
EOF
            ;;
        *)
            echo -e "${Tip}不支持的系统版本，无法优化SSH！"
            ;;
    esac

    if [[ -f /etc/ssh/sshd_config ]]; then
        systemctl restart sshd.service
        if [[ $? -eq 0 ]]; then
            echo -e "${Tip}SSH服务已重启，配置生效！   ${Success}"
        else
            echo -e "${Error}重启SSH服务失败！请手动检查。"
        fi
    fi
    sleep 1s
    echo -e "${Tip}当前${centos} 系统已优化SSH完成！   ${Success}"
    echo -e "${Tip}脚本执行完毕   ${Success}"
}

#一键执行以上所有功能
function install_quan() {
    install_yum
    install_dns
    install_ssh
}

#主菜单
function main() {
    check_sys
    export IP=$(curl -s myip.ipip.net | awk -F ' ' '{print $2}' | awk -F '：' '{print $2}')
    export tram=$(free -m | awk '/Mem/{print $2}')

    clear
    echo -e "
******************************************************
*             CentOS一键优化脚本                     *
********************安装服务端***********************
* ${Green}0.[Font} 退出脚本                                     *
* ${Green}1.[Font} 一键更换阿里云yum源/EPEL源                     *
* ${Green}2.[Font} 一键修改DNS(223.5.5.5 8.8.8.8)             *
* ${Green}3.[Font} 一键优化SSH链接卡顿/慢                         *
* ${Green}4.[Font} 一键执行以上所有功能                          *
* ${Green}注意[Font} 建议输入4使用一键脚本执行优化                   *
******************************************************
*       咕咕云 GGY.NET - 安全、稳定、可信赖的产品与服务       *
******************************************************
中央处理器: ${Green} ${HX}核 ${Font}  操作系统: ${Green} ${centos} ${W}位 ${Font}
系统内存:   ${Green} ${G}G/${tram}M ${Font}      本机IP: ${Green} ${IP} ${Font}"

    read -p "请输入相对应的序列号数字:" num
    case "${num}" in
        0)
            clear
            echo -e "感谢使用！欢迎下次再来！"
            exit 0
            ;;
        1)
            clear
            install_yum
            ;;
        2)
            clear
            install_dns
            ;;
        3)
            clear
            install_ssh
            ;;
        4)
            clear
            install_quan
            ;;
        *)
            echo -e "${Tip}无效的输入，请输入0到4之间的数字。"
            ;;
    esac
}

main