From 5611d54a470ac2555e9bfacf6a57b608c0bdd17b Mon Sep 17 00:00:00 2001 From: Houzhong Xu Date: Sat, 20 Sep 2025 17:32:21 +0000 Subject: [PATCH] Add smart proxy-aware installation system - Add smart-install.sh that auto-detects network environment - Update install-zsh-config.sh to check proxy before downloading - Update quick-install.sh to handle proxy configuration - Add proxy management section to README - Ensure oh-my-zsh and plugins download through proxy when needed - Automatic proxy configuration creation for network-restricted environments --- configuration/zsh/README.md | 35 +++++- configuration/zsh/install-zsh-config.sh | 22 ++++ configuration/zsh/quick-install.sh | 14 +++ configuration/zsh/smart-install.sh | 143 ++++++++++++++++++++++++ 4 files changed, 212 insertions(+), 2 deletions(-) create mode 100755 configuration/zsh/smart-install.sh diff --git a/configuration/zsh/README.md b/configuration/zsh/README.md index cd07ced..9f23337 100644 --- a/configuration/zsh/README.md +++ b/configuration/zsh/README.md @@ -17,7 +17,16 @@ configuration/zsh/ ## 使用方法 -### 方法一:快速安装(推荐) +### 方法一:智能安装(推荐) + +自动检测网络环境,智能选择直连或代理: + +```bash +# 智能安装(自动检测网络环境) +curl -fsSL https://ben:8d7d70f324796be650b79415303c31f567bf459b@gitea.tailnet-68f9.ts.net/ben/mgmt/raw/branch/main/configuration/zsh/smart-install.sh | bash +``` + +### 方法二:快速安装 在新 VPS 上运行: @@ -26,7 +35,7 @@ configuration/zsh/ curl -fsSL https://ben:8d7d70f324796be650b79415303c31f567bf459b@gitea.tailnet-68f9.ts.net/ben/mgmt/raw/branch/main/configuration/zsh/quick-install.sh | bash ``` -### 方法二:手动安装 +### 方法三:手动安装 1. 克隆仓库: ```bash @@ -54,6 +63,28 @@ sync-zsh-config 2. 备份当前配置 3. 部署新配置 +## 代理管理 + +如果网络环境需要代理,可以使用以下命令管理代理: + +```bash +# 代理管理命令 +proxy-on # 临时开启代理 +proxy-off # 临时关闭代理 +proxy-toggle # 切换代理状态 +proxy-enable # 永久开启代理 +proxy-disable # 永久关闭代理 +proxy-status # 查看代理状态 +proxy-test # 测试代理连接 +``` + +### 代理使用场景 + +- **临时使用**: `proxy-on` → 使用代理 → `proxy-off` +- **永久开启**: `proxy-enable` → 重启后仍然有效 +- **快速切换**: `proxy-toggle` → 一键切换状态 +- **状态检查**: `proxy-status` → 查看当前状态和IP + ## 包含的功能 ### 插件 diff --git a/configuration/zsh/install-zsh-config.sh b/configuration/zsh/install-zsh-config.sh index f312eb9..4694def 100755 --- a/configuration/zsh/install-zsh-config.sh +++ b/configuration/zsh/install-zsh-config.sh @@ -37,6 +37,27 @@ check_root() { fi } +# 设置代理(如果需要) +setup_proxy() { + log_info "检查代理设置..." + + # 检查是否已经有代理配置 + if [[ -f "/root/mgmt/configuration/proxy.env" ]]; then + log_info "发现代理配置文件,加载代理设置..." + source "/root/mgmt/configuration/proxy.env" + + # 测试代理连接 + if curl -s --connect-timeout 5 --proxy "$http_proxy" https://httpbin.org/ip >/dev/null 2>&1; then + log_success "代理连接正常,将使用代理下载" + else + log_warning "代理连接失败,将使用直连" + unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY + fi + else + log_info "未发现代理配置,将使用直连" + fi +} + # 安装依赖 install_dependencies() { log_info "安装依赖包..." @@ -244,6 +265,7 @@ main() { log_info "开始安装 ZSH 配置..." check_root + setup_proxy install_dependencies install_oh_my_zsh install_custom_plugins diff --git a/configuration/zsh/quick-install.sh b/configuration/zsh/quick-install.sh index 683a2ac..228bb75 100755 --- a/configuration/zsh/quick-install.sh +++ b/configuration/zsh/quick-install.sh @@ -47,6 +47,20 @@ else cd "$MGMT_DIR" fi +# 检查并设置代理(如果需要) +if [[ -f "$MGMT_DIR/configuration/proxy.env" ]]; then + log_info "发现代理配置,先设置代理..." + source "$MGMT_DIR/configuration/proxy.env" + + # 测试代理连接 + if curl -s --connect-timeout 5 --proxy "$http_proxy" https://httpbin.org/ip >/dev/null 2>&1; then + log_success "代理连接正常,将使用代理安装" + else + log_warning "代理连接失败,将使用直连安装" + unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY + fi +fi + # 运行安装脚本 log_info "运行 ZSH 配置安装脚本..." chmod +x "$MGMT_DIR/configuration/zsh/install-zsh-config.sh" diff --git a/configuration/zsh/smart-install.sh b/configuration/zsh/smart-install.sh new file mode 100755 index 0000000..d79539c --- /dev/null +++ b/configuration/zsh/smart-install.sh @@ -0,0 +1,143 @@ +#!/bin/bash + +# 智能安装脚本 - 自动检测网络环境并设置代理 +# 用法: curl -fsSL https://your-gitea.com/ben/mgmt/raw/branch/main/configuration/zsh/smart-install.sh | bash + +set -euo pipefail + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } +log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; } +log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; } +log_error() { echo -e "${RED}[ERROR]${NC} $1"; } + +# Gitea 仓库信息 +GITEA_URL="https://ben:8d7d70f324796be650b79415303c31f567bf459b@gitea.tailnet-68f9.ts.net/ben/mgmt.git" +MGMT_DIR="/root/mgmt" +PROXY_HOST="istoreos.tailnet-68f9.ts.net" +PROXY_PORT="1082" +PROXY_URL="http://${PROXY_HOST}:${PROXY_PORT}" + +# 检查 root 权限 +if [[ $EUID -ne 0 ]]; then + log_error "此脚本需要 root 权限运行" + exit 1 +fi + +# 检测网络环境 +detect_network() { + log_info "检测网络环境..." + + # 测试直连 GitHub + if curl -s --connect-timeout 5 https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh >/dev/null 2>&1; then + log_success "直连网络正常" + echo "direct" + return 0 + fi + + # 测试代理连接 + if curl -s --connect-timeout 5 --proxy "$PROXY_URL" https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh >/dev/null 2>&1; then + log_success "代理网络正常" + echo "proxy" + return 0 + fi + + log_error "网络连接失败" + return 1 +} + +# 设置代理环境 +setup_proxy_env() { + log_info "设置代理环境..." + + export http_proxy="$PROXY_URL" + export https_proxy="$PROXY_URL" + export HTTP_PROXY="$PROXY_URL" + export HTTPS_PROXY="$PROXY_URL" + export no_proxy="localhost,127.0.0.1,::1,.local,.tailnet-68f9.ts.net" + export NO_PROXY="localhost,127.0.0.1,::1,.local,.tailnet-68f9.ts.net" + + log_success "代理环境已设置" +} + +# 克隆或更新仓库 +clone_repository() { + log_info "获取配置仓库..." + + if [[ -d "$MGMT_DIR" ]]; then + log_info "更新现有仓库..." + cd "$MGMT_DIR" + git pull origin main + else + log_info "克隆仓库..." + git clone "$GITEA_URL" "$MGMT_DIR" + cd "$MGMT_DIR" + fi +} + +# 创建代理配置文件 +create_proxy_config() { + log_info "创建代理配置文件..." + + cat > "$MGMT_DIR/configuration/proxy.env" << EOF +# Proxy Configuration for ${PROXY_HOST}:${PROXY_PORT} +export http_proxy=${PROXY_URL} +export https_proxy=${PROXY_URL} +export HTTP_PROXY=${PROXY_URL} +export HTTPS_PROXY=${PROXY_URL} +export no_proxy=localhost,127.0.0.1,::1,.local,.tailnet-68f9.ts.net +export NO_PROXY=localhost,127.0.0.1,::1,.local,.tailnet-68f9.ts.net +export ALL_PROXY=${PROXY_URL} +export all_proxy=${PROXY_URL} +export GIT_HTTP_PROXY=${PROXY_URL} +export GIT_HTTPS_PROXY=${PROXY_URL} +export CURL_PROXY=${PROXY_URL} +export WGET_PROXY=${PROXY_URL} +EOF + + log_success "代理配置文件已创建" +} + +# 主安装流程 +main() { + log_info "开始智能安装 ZSH 配置..." + + # 检测网络环境 + local network_type=$(detect_network) + + if [[ "$network_type" == "proxy" ]]; then + # 需要代理 + setup_proxy_env + create_proxy_config + elif [[ "$network_type" == "direct" ]]; then + # 直连即可 + log_info "使用直连网络安装" + else + log_error "网络连接失败,无法继续安装" + exit 1 + fi + + # 克隆仓库 + clone_repository + + # 运行安装脚本 + log_info "运行 ZSH 配置安装脚本..." + chmod +x "$MGMT_DIR/configuration/zsh/install-zsh-config.sh" + "$MGMT_DIR/configuration/zsh/install-zsh-config.sh" + + log_success "智能安装完成!" + + if [[ "$network_type" == "proxy" ]]; then + log_info "代理配置已保存,重启后仍然有效" + log_info "使用 'proxy-status' 查看代理状态" + log_info "使用 'proxy-toggle' 切换代理状态" + fi +} + +main "$@"