From d647807ee6e96fd940e7d00682e4962e4610ce9c Mon Sep 17 00:00:00 2001 From: yoyo Date: Wed, 3 Dec 2025 17:42:24 +0800 Subject: [PATCH] =?UTF-8?q?go=E4=BE=9D=E8=B5=96=E9=95=9C=E5=83=8F=E6=8D=A2?= =?UTF-8?q?=E5=9B=BD=E5=86=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.sh | 79 +++++++++++++++++++++++++++++++++++++++++++++++------- run.sh | 38 ++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 11 deletions(-) diff --git a/install.sh b/install.sh index 71941e1..f3c7f4b 100755 --- a/install.sh +++ b/install.sh @@ -432,8 +432,18 @@ install_go_from_official() { echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile > /dev/null fi - # 设置当前会话的 PATH + # 配置 Go 代理(使用国内镜像) + if ! grep -q "GOPROXY" /etc/profile 2>/dev/null; then + echo '' | sudo tee -a /etc/profile > /dev/null + echo '# Go 代理配置(使用国内镜像加速)' | sudo tee -a /etc/profile > /dev/null + echo 'export GOPROXY=https://goproxy.cn,direct' | sudo tee -a /etc/profile > /dev/null + echo 'export GOSUMDB=sum.golang.google.cn' | sudo tee -a /etc/profile > /dev/null + fi + + # 设置当前会话的 PATH 和 GOPROXY export PATH=$PATH:/usr/local/go/bin + export GOPROXY=https://goproxy.cn,direct + export GOSUMDB=sum.golang.google.cn # 验证安装 if command -v go > /dev/null 2>&1; then @@ -489,6 +499,19 @@ install_go() { if [ "$install_success" = true ] && command -v go > /dev/null 2>&1; then GO_VERSION=$(go version 2>/dev/null | head -1) echo -e "${GREEN}✓ Go 安装完成: ${GO_VERSION}${NC}" + + # 配置 Go 代理(使用国内镜像) + if ! grep -q "GOPROXY" /etc/profile 2>/dev/null; then + echo '' | sudo tee -a /etc/profile > /dev/null + echo '# Go 代理配置(使用国内镜像加速)' | sudo tee -a /etc/profile > /dev/null + echo 'export GOPROXY=https://goproxy.cn,direct' | sudo tee -a /etc/profile > /dev/null + echo 'export GOSUMDB=sum.golang.google.cn' | sudo tee -a /etc/profile > /dev/null + fi + + # 设置当前会话的 GOPROXY + export GOPROXY=https://goproxy.cn,direct + export GOSUMDB=sum.golang.google.cn + return 0 fi @@ -649,17 +672,55 @@ build_from_source() { sudo git config --global --add safe.directory "$SOURCE_DIR" 2>/dev/null || true git config --global --add safe.directory "$SOURCE_DIR" 2>/dev/null || true - # 下载依赖(使用 sudo 以 root 用户执行,确保 PATH 包含 Go) + # 配置 Go 代理(使用国内镜像) + echo -e "${BLUE}配置 Go 代理(使用国内镜像)...${NC}" + # Go 代理镜像列表(按优先级排序) + GO_PROXY_MIRRORS=( + "https://goproxy.cn,direct" + "https://goproxy.io,direct" + "https://mirrors.aliyun.com/go-proxy/,direct" + "https://proxy.golang.org,direct" + ) + + # 测试并选择最快的 Go 代理 + GO_PROXY="" + for proxy in "${GO_PROXY_MIRRORS[@]}"; do + proxy_url=$(echo "$proxy" | cut -d',' -f1) + echo -n " 测试 ${proxy_url}... " + if curl -sf --connect-timeout 2 "${proxy_url}" > /dev/null 2>&1; then + GO_PROXY="$proxy" + echo -e "${GREEN}可用${NC}" + break + else + echo -e "${RED}不可用${NC}" + fi + done + + # 如果所有镜像都不可用,使用默认配置 + if [ -z "$GO_PROXY" ]; then + GO_PROXY="https://proxy.golang.org,direct" + echo -e "${YELLOW}⚠ 所有国内镜像不可用,使用默认代理${NC}" + else + echo -e "${GREEN}✓ 使用 Go 代理: ${GO_PROXY}${NC}" + fi + + # 下载依赖(使用 sudo 以 root 用户执行,确保 PATH 包含 Go,并设置 GOPROXY) echo -e "${BLUE}下载 Go 依赖...${NC}" - # 构建包含 Go PATH 的环境变量 + # 构建包含 Go PATH 和 GOPROXY 的环境变量 GO_PATH_ENV="PATH=\$PATH:/usr/local/go/bin" if [ -d "/usr/local/go/bin" ]; then GO_PATH_ENV="PATH=/usr/local/go/bin:\$PATH" fi - if ! sudo bash -c "cd '$SOURCE_DIR' && $GO_PATH_ENV && go mod download" 2>&1; then - echo -e "${RED}下载依赖失败${NC}" - show_build_alternatives - exit 1 + GO_ENV="$GO_PATH_ENV GOPROXY=${GO_PROXY} GOSUMDB=sum.golang.google.cn" + if ! sudo bash -c "cd '$SOURCE_DIR' && $GO_ENV && go mod download" 2>&1; then + echo -e "${YELLOW}⚠ 使用国内镜像下载失败,尝试使用官方源...${NC}" + # 如果失败,尝试使用官方源 + GO_ENV="$GO_PATH_ENV GOPROXY=https://proxy.golang.org,direct" + if ! sudo bash -c "cd '$SOURCE_DIR' && $GO_ENV && go mod download" 2>&1; then + echo -e "${RED}下载依赖失败${NC}" + show_build_alternatives + exit 1 + fi fi # 编译到临时文件(在用户有权限的目录),然后移动到目标位置 @@ -667,8 +728,8 @@ build_from_source() { TEMP_BINARY=$(mktemp) BINARY_PATH="$SOURCE_DIR/agent" - # 使用 sudo 以 root 用户编译,直接输出到目标位置,确保 PATH 包含 Go - if sudo bash -c "cd '$SOURCE_DIR' && $GO_PATH_ENV && GOOS=linux GOARCH=${ARCH} CGO_ENABLED=0 go build -buildvcs=false -ldflags='-w -s' -o '$BINARY_PATH' ./cmd/agent" 2>&1; then + # 使用 sudo 以 root 用户编译,直接输出到目标位置,确保 PATH 包含 Go,并设置 GOPROXY + if sudo bash -c "cd '$SOURCE_DIR' && $GO_ENV && GOOS=linux GOARCH=${ARCH} CGO_ENABLED=0 go build -buildvcs=false -ldflags='-w -s' -o '$BINARY_PATH' ./cmd/agent" 2>&1; then if [ -f "$BINARY_PATH" ] && [ -s "$BINARY_PATH" ]; then sudo chmod +x "$BINARY_PATH" echo -e "${GREEN}✓ 编译成功${NC}" diff --git a/run.sh b/run.sh index ff10ea1..f538e7e 100755 --- a/run.sh +++ b/run.sh @@ -68,10 +68,44 @@ update_and_build() { exit 1 fi + # 配置 Go 代理(使用国内镜像) + if [ -z "$GOPROXY" ]; then + # 测试并选择最快的 Go 代理 + GO_PROXY_MIRRORS=( + "https://goproxy.cn,direct" + "https://goproxy.io,direct" + "https://mirrors.aliyun.com/go-proxy/,direct" + ) + + for proxy in "${GO_PROXY_MIRRORS[@]}"; do + proxy_url=$(echo "$proxy" | cut -d',' -f1) + if curl -sf --connect-timeout 2 "${proxy_url}" > /dev/null 2>&1; then + export GOPROXY="$proxy" + export GOSUMDB=sum.golang.google.cn + break + fi + done + + # 如果所有镜像都不可用,使用默认配置 + if [ -z "$GOPROXY" ]; then + export GOPROXY="https://proxy.golang.org,direct" + export GOSUMDB=sum.golang.google.cn + fi + fi + # 更新依赖 - echo -e "${BLUE}更新 Go 依赖...${NC}" + echo -e "${BLUE}更新 Go 依赖(使用代理: ${GOPROXY})...${NC}" if ! go mod download 2>&1; then - echo -e "${YELLOW}警告: 依赖更新失败,尝试继续编译${NC}" + echo -e "${YELLOW}警告: 依赖更新失败,尝试使用官方源...${NC}" + # 如果失败,尝试使用官方源 + OLD_GOPROXY="$GOPROXY" + export GOPROXY="https://proxy.golang.org,direct" + if ! go mod download 2>&1; then + export GOPROXY="$OLD_GOPROXY" + echo -e "${YELLOW}警告: 依赖更新失败,尝试继续编译${NC}" + else + echo -e "${GREEN}✓ 依赖更新完成${NC}" + fi else echo -e "${GREEN}✓ 依赖更新完成${NC}" fi