#!/bin/sh # 将安装路径修改为 /data/nezha,以保证在 Hugging Face Spaces 中具有写权限 NZ_BASE_PATH="/data/nezha" NZ_DASHBOARD_PATH="${NZ_BASE_PATH}/dashboard" red='\033[0;31m' green='\033[0;32m' yellow='\033[0;33m' plain='\033[0m' err() { printf "${red}%s${plain}\n" "$*" >&2 } warn() { printf "${red}%s${plain}\n" "$*" } success() { printf "${green}%s${plain}\n" "$*" } info() { printf "${yellow}%s${plain}\n" "$*" } println() { printf "$*\n" } mustn() { set -- "$@" if ! "$@" >/dev/null 2>&1; then err "运行 '$*' 失败。" exit 1 fi } deps_check() { deps="curl wget unzip grep" for dep in $deps; do if ! command -v "$dep" >/dev/null 2>&1; then err "未找到依赖 $dep,请先安装。" exit 1 fi done } geo_check() { api_list="https://blog.cloudflare.com/cdn-cgi/trace https://developers.cloudflare.com/cdn-cgi/trace" ua="Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0" for url in $api_list; do text="$(curl -A "$ua" -m 10 -s "$url")" endpoint="$(echo "$text" | sed -n 's/.*h=\([^ ]*\).*/\1/p')" if echo "$text" | grep -qw 'CN'; then isCN=true break elif echo "$url" | grep -q "$endpoint"; then break fi done } env_check() { uname=$(uname -m) case "$uname" in amd64|x86_64) os_arch="amd64" ;; i386|i686) os_arch="386" ;; aarch64|arm64) os_arch="arm64" ;; *arm*) os_arch="arm" ;; s390x) os_arch="s390x" ;; riscv64) os_arch="riscv64" ;; *) err "未知架构:$uname" exit 1 ;; esac } init() { deps_check env_check ## 检测是否为国内IP if [ -z "$CN" ]; then geo_check if [ -n "$isCN" ]; then CN=true GITHUB_RAW_URL="gitee.com/naibahq/scripts/raw/main" GITHUB_URL="gitee.com" Docker_IMG="registry.cn-shanghai.aliyuncs.com\/naibahq\/nezha" else GITHUB_RAW_URL="raw.githubusercontent.com/nezhahq/scripts/main" GITHUB_URL="github.com" Docker_IMG="ghcr.io\/nezhahq\/nezha" fi fi IS_DOCKER_NEZHA=0 } install() { echo "> 安装" # 创建哪吒监控目录 mkdir -p "$NZ_DASHBOARD_PATH" modify_config restart_and_update } modify_config() { echo "> 修改配置" # 下载配置文件 echo "正在下载配置文件" wget -t 2 -T 60 -O /tmp/nezha-config.yaml "https://${GITHUB_RAW_URL}/extras/config.yaml" # 设置配置参数,从环境变量读取,如果没有则使用默认值 nz_site_title=${NZ_SITE_TITLE:-"哪吒监控"} nz_port=${NZ_PORT:-"7860"} # Hugging Face Spaces 默认使用 7860 端口 nz_hostport=${NZ_HOSTPORT:-"example.com:443"} nz_tls=${NZ_TLS:-"true"} nz_lang=${NZ_LANG:-"zh_CN"} sed -i "s/nz_port/${nz_port}/g" /tmp/nezha-config.yaml sed -i "s/nz_language/${nz_lang}/" /tmp/nezha-config.yaml sed -i "s/nz_site_title/${nz_site_title}/" /tmp/nezha-config.yaml sed -i "s/nz_hostport/${nz_hostport}/" /tmp/nezha-config.yaml sed -i "s/nz_tls/${nz_tls}/" /tmp/nezha-config.yaml mkdir -p "$NZ_DASHBOARD_PATH/data" mv -f /tmp/nezha-config.yaml "${NZ_DASHBOARD_PATH}/data/config.yaml" # 确保数据目录具有写权限 chmod -R 777 "${NZ_DASHBOARD_PATH}/data" success "Dashboard 配置修改成功" } restart_and_update() { echo "> 重启并更新" restart_and_update_standalone success "哪吒监控更新成功" } restart_and_update_standalone() { _version=$(curl -m 10 -sL "https://api.github.com/repos/nezhahq/nezha/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g') if [ -z "$_version" ]; then err "获取 Dashboard 版本号失败,请检查网络连接" exit 1 else echo "当前最新版本为: ${_version}" fi if [ -z "$CN" ]; then NZ_DASHBOARD_URL="https://${GITHUB_URL}/nezhahq/nezha/releases/download/${_version}/dashboard-linux-${os_arch}.zip" else NZ_DASHBOARD_URL="https://${GITHUB_URL}/naibahq/nezha/releases/download/${_version}/dashboard-linux-${os_arch}.zip" fi wget -qO "$NZ_DASHBOARD_PATH/app.zip" "$NZ_DASHBOARD_URL" && \ unzip -qq -o "$NZ_DASHBOARD_PATH/app.zip" -d "$NZ_DASHBOARD_PATH" && \ mv "$NZ_DASHBOARD_PATH/dashboard-linux-$os_arch" "$NZ_DASHBOARD_PATH/app" && \ rm "$NZ_DASHBOARD_PATH/app.zip" chmod +x "$NZ_DASHBOARD_PATH/app" } init install