144 lines
3.9 KiB
Bash
Executable File
144 lines
3.9 KiB
Bash
Executable File
#!/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 "$@"
|