#!/bin/bash # 使用方法: curl -fsSL https://git.xykqyy.com/ljp/hostSync/raw/branch/main/install.sh | bash set -e # 默认配置 VERSION="latest" INSTALL_DIR="$HOME/.hostsync/bin" NO_INIT=false PORTABLE=false FORCE=false # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' MAGENTA='\033[0;35m' NC='\033[0m' # No Color # 输出函数 info() { echo -e "${CYAN}$1${NC}"; } success() { echo -e "${GREEN}$1${NC}"; } warning() { echo -e "${YELLOW}$1${NC}"; } error() { echo -e "${RED}$1${NC}"; } # 检查并卸载已有服务 remove_existing_service() { info "🔍 检查现有安装..." # 检查 PATH 中的 hostsync local existing_path="" if command -v hostsync >/dev/null 2>&1; then existing_path=$(which hostsync) elif [[ -f "$HOME/.hostsync/bin/hostsync" ]]; then existing_path="$HOME/.hostsync/bin/hostsync" elif [[ -f "/usr/local/bin/hostsync" ]]; then existing_path="/usr/local/bin/hostsync" fi if [[ -n "$existing_path" ]]; then info "📦 发现现有安装: $existing_path" # 尝试停止并卸载服务 info "⏹️ 停止现有服务..." "$existing_path" service stop >/dev/null 2>&1 || true info "🗑️ 卸载现有服务..." "$existing_path" service uninstall >/dev/null 2>&1 || true success "✅ 已清理现有服务" # 等待一下确保服务完全停止 sleep 2 else info "💡 未发现现有安装" fi } # 检查root权限 check_root() { if [[ $EUID -ne 0 ]]; then warning "⚠️ 未检测到 root 权限,将跳过服务安装" info "💡 如需安装服务,请稍后以 root 身份运行: hostsync service install" NO_INIT=true return fi } # 检测操作系统和架构 detect_platform() { OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) case $OS in linux*) OS="linux" ;; darwin*) OS="darwin" ;; *) error "❌ 不支持的操作系统: $OS" exit 1 ;; esac case $ARCH in x86_64 | amd64) ARCH="amd64" ;; i*86 | x86) ARCH="386" ;; aarch64 | arm64) ARCH="arm64" ;; armv7* | armv6*) ARCH="armv7" ;; *) error "❌ 不支持的架构: $ARCH" exit 1 ;; esac info "🏗️ 检测到平台: $OS-$ARCH" } # 获取最新版本 get_latest_version() { if [[ "$VERSION" == "latest" ]]; then info "🔍 获取最新版本信息..." if command -v curl >/dev/null 2>&1; then VERSION=$(curl -fsSL "https://git.xykqyy.com/api/v1/repos/ljp/hostSync/releases/latest" | grep '"tag_name"' | sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/') elif command -v wget >/dev/null 2>&1; then VERSION=$(wget -qO- "https://git.xykqyy.com/api/v1/repos/ljp/hostSync/releases/latest" | grep '"tag_name"' | sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/') else warning "⚠️ 无法获取最新版本,使用 v1.0.0" VERSION="v1.0.0" fi fi if [[ -z "$VERSION" ]]; then warning "⚠️ 无法获取版本信息,使用 v1.0.0" VERSION="v1.0.0" fi info "📦 安装版本: $VERSION" # 处理版本号格式 - 分离tag版本和文件版本 TAG_VERSION="$VERSION" FILE_VERSION="$VERSION" # 确保tag版本有v前缀 if [[ ! "$TAG_VERSION" =~ ^v ]]; then TAG_VERSION="v$TAG_VERSION" fi # 确保文件版本没有v前缀 if [[ "$FILE_VERSION" =~ ^v ]]; then FILE_VERSION="${FILE_VERSION:1}" # 去掉 'v' 前缀 fi if [[ ! "$FILE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then error "❌ 无效的版本号格式: $FILE_VERSION" exit 1 fi } # 确定安装目录 setup_install_dir() { if [[ -z "$INSTALL_DIR" ]]; then if [[ "$PORTABLE" == true ]]; then INSTALL_DIR="./hostsync" else INSTALL_DIR="$HOME/.hostsync/bin" fi fi info "📂 安装目录: $INSTALL_DIR" # 创建安装目录 if [[ ! -d "$INSTALL_DIR" ]]; then mkdir -p "$INSTALL_DIR" || { error "❌ 创建目录失败: $INSTALL_DIR" exit 1 } success "✅ 创建安装目录" fi } # 下载文件 download_file() { local url="$1" local output="$2" info "📥 下载: $(basename "$output")" info "🌐 地址: $url" # 尝试使用 curl if command -v curl >/dev/null 2>&1; then if curl -fsSL "$url" -o "$output"; then success "✅ 下载完成" return 0 fi fi # 尝试使用 wget if command -v wget >/dev/null 2>&1; then if wget -q "$url" -O "$output"; then success "✅ 下载完成" return 0 fi fi error "❌ 下载失败,请检查网络连接" return 1 } # 设置环境变量 setup_environment() { if [[ "$PORTABLE" == true ]]; then return 0 fi info "🔧 配置环境变量..." # 确定 shell 配置文件 local shell_rc="" case "$SHELL" in */zsh) shell_rc="$HOME/.zshrc" ;; */bash) shell_rc="$HOME/.bashrc" ;; */fish) shell_rc="$HOME/.config/fish/config.fish" ;; *) shell_rc="$HOME/.profile" ;; esac # 添加到 PATH local path_line="export PATH=\"$INSTALL_DIR:\$PATH\"" if [[ "$SHELL" == */fish ]]; then path_line="set -gx PATH $INSTALL_DIR \$PATH" fi if [[ -f "$shell_rc" ]] && grep -q "$INSTALL_DIR" "$shell_rc"; then info "💡 PATH 已包含安装目录" else echo "" >>"$shell_rc" echo "# HostSync" >>"$shell_rc" echo "$path_line" >>"$shell_rc" success "✅ 已添加到 PATH ($shell_rc)" warning "⚠️ 请重新加载终端或运行: source $shell_rc" fi } # 主安装函数 install_hostsync() { cat <<'EOF' 🚀 HostSync 快速安装脚本 =============================== 强大的 Hosts 文件管理工具 功能特点: - 🎯 分块管理 Hosts 配置 - 🌐 智能 DNS 解析 - ⏰ 定时自动更新 - 🔧 后台服务运行 EOF # 检查依赖 if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then error "❌ 需要 curl 或 wget 来下载文件" exit 1 fi # 检查并卸载已有服务 remove_existing_service # 检查权限 check_root # 检测平台 detect_platform # 获取版本 get_latest_version # 设置安装目录 setup_install_dir # 构建下载 URL local filename="hostsync-$FILE_VERSION-$OS-$ARCH" local download_url="https://git.xykqyy.com/ljp/hostSync/releases/download/$TAG_VERSION/$filename" local output_file="$INSTALL_DIR/hostsync" # 下载文件 if ! download_file "$download_url" "$output_file"; then error "❌ 下载失败,请检查网络连接或手动下载" info "🌐 手动下载地址: https://git.xykqyy.com/ljp/hostSync/releases" exit 1 fi # 设置执行权限 chmod +x "$output_file" || { error "❌ 设置执行权限失败" exit 1 } success "✅ HostSync 下载完成!" # 配置环境 setup_environment # 运行初始化 if [[ "$NO_INIT" != true ]]; then info "🚀 开始初始化 HostSync..." if "$output_file" init; then success "✅ 初始化完成!" else warning "⚠️ 初始化可能遇到问题" info "💡 你可以稍后手动运行: sudo hostsync init" fi fi # 显示完成信息 success " 🎉 HostSync 安装完成! ===================== 📁 安装位置: $output_file 📖 配置目录: $HOME/.hostsync/ 🚀 快速开始:" info " hostsync list # 查看配置" info " hostsync add test example.com # 添加域名" cat <<'EOF' hostsync update # 更新解析 hostsync service status # 查看服务状态 📚 更多帮助: hostsync help # 查看帮助 hostsync version # 查看版本 🌐 项目地址: https://git.xykqyy.com/ljp/hostSync EOF } # 错误处理 trap 'error "❌ 安装过程中发生错误"; exit 1' ERR # 执行安装 install_hostsync