292 lines
7.5 KiB
Bash
Executable File
292 lines
7.5 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
# Docker和GitHub通过WireGuard连接外网配置脚本
|
||
# 适用于使用WireGuard App的macOS系统
|
||
# 服务器位于美国,使用美国DNS
|
||
|
||
set -e
|
||
|
||
# 颜色定义
|
||
RED='\033[0;31m'
|
||
GREEN='\033[0;32m'
|
||
YELLOW='\033[1;33m'
|
||
BLUE='\033[0;34m'
|
||
NC='\033[0m'
|
||
|
||
print_status() {
|
||
echo -e "${BLUE}[INFO]${NC} $1"
|
||
}
|
||
|
||
print_success() {
|
||
echo -e "${GREEN}[✓]${NC} $1"
|
||
}
|
||
|
||
print_error() {
|
||
echo -e "${RED}[✗]${NC} $1"
|
||
}
|
||
|
||
print_warning() {
|
||
echo -e "${YELLOW}[!]${NC} $1"
|
||
}
|
||
|
||
# 检查WireGuard是否运行(通过WireGuard App)
|
||
check_wireguard() {
|
||
print_status "检查WireGuard连接状态..."
|
||
|
||
# 检查wg0接口是否存在
|
||
if ifconfig wg0 &>/dev/null; then
|
||
print_success "WireGuard接口wg0已连接"
|
||
|
||
# 显示接口信息
|
||
WG_IP=$(ifconfig wg0 | grep "inet " | awk '{print $2}')
|
||
print_status " 隧道IP: $WG_IP"
|
||
return 0
|
||
else
|
||
print_error "WireGuard接口wg0未找到"
|
||
print_warning "请先在WireGuard App中激活隧道"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
# 配置Docker使用美国DNS
|
||
configure_docker() {
|
||
print_status "配置Docker使用美国DNS..."
|
||
|
||
# 检查Docker是否安装
|
||
if ! command -v docker &>/dev/null; then
|
||
print_warning "Docker未安装,跳过Docker配置"
|
||
return 0
|
||
fi
|
||
|
||
# 创建Docker daemon配置目录
|
||
DOCKER_CONFIG_DIR="$HOME/.docker"
|
||
mkdir -p "$DOCKER_CONFIG_DIR"
|
||
|
||
DAEMON_JSON="$DOCKER_CONFIG_DIR/daemon.json"
|
||
|
||
# 备份现有配置
|
||
if [ -f "$DAEMON_JSON" ]; then
|
||
cp "$DAEMON_JSON" "${DAEMON_JSON}.backup.$(date +%Y%m%d_%H%M%S)"
|
||
print_status "已备份现有配置到 ${DAEMON_JSON}.backup"
|
||
fi
|
||
|
||
# 创建新配置 - 使用美国DNS
|
||
cat > "$DAEMON_JSON" << 'DOCKEREOF'
|
||
{
|
||
"dns": ["1.1.1.1", "8.8.8.8", "8.8.4.4"],
|
||
"registry-mirrors": [],
|
||
"insecure-registries": [],
|
||
"debug": false,
|
||
"experimental": false
|
||
}
|
||
DOCKEREOF
|
||
|
||
print_success "Docker配置已更新(使用美国DNS: 1.1.1.1, 8.8.8.8)"
|
||
print_warning "请重启Docker Desktop使配置生效"
|
||
print_status " 方法1: 点击菜单栏Docker图标 -> Restart"
|
||
print_status " 方法2: 使用命令重启"
|
||
|
||
echo ""
|
||
}
|
||
|
||
# 配置Git优化
|
||
configure_git() {
|
||
print_status "配置Git访问优化..."
|
||
|
||
# Git全局配置
|
||
git config --global http.postBuffer 524288000
|
||
git config --global http.version HTTP/1.1
|
||
git config --global core.compression 0
|
||
|
||
print_success "Git配置已优化"
|
||
print_status " - postBuffer: 500MB"
|
||
print_status " - HTTP版本: 1.1"
|
||
print_status " - 压缩: 关闭(提高速度)"
|
||
|
||
echo ""
|
||
}
|
||
|
||
# 配置SSH for GitHub
|
||
configure_ssh() {
|
||
print_status "配置SSH for GitHub..."
|
||
|
||
SSH_CONFIG="$HOME/.ssh/config"
|
||
mkdir -p "$HOME/.ssh"
|
||
chmod 700 "$HOME/.ssh"
|
||
|
||
# 备份SSH配置
|
||
if [ -f "$SSH_CONFIG" ]; then
|
||
cp "$SSH_CONFIG" "${SSH_CONFIG}.backup.$(date +%Y%m%d_%H%M%S)"
|
||
print_status "已备份SSH配置"
|
||
fi
|
||
|
||
# 检查是否已有GitHub配置
|
||
if grep -q "Host github.com" "$SSH_CONFIG" 2>/dev/null; then
|
||
print_status "GitHub SSH配置已存在,跳过"
|
||
else
|
||
cat >> "$SSH_CONFIG" << 'SSHEOF'
|
||
|
||
# GitHub配置(通过美国WireGuard服务器)
|
||
Host github.com
|
||
Hostname github.com
|
||
User git
|
||
IdentityFile ~/.ssh/id_rsa
|
||
ServerAliveInterval 60
|
||
ServerAliveCountMax 10
|
||
TCPKeepAlive yes
|
||
Compression yes
|
||
IPQoS throughput
|
||
|
||
Host *.github.com
|
||
User git
|
||
ServerAliveInterval 60
|
||
ServerAliveCountMax 10
|
||
TCPKeepAlive yes
|
||
Compression yes
|
||
IPQoS throughput
|
||
|
||
SSHEOF
|
||
print_success "已添加GitHub SSH配置"
|
||
fi
|
||
|
||
chmod 600 "$SSH_CONFIG"
|
||
echo ""
|
||
}
|
||
|
||
# 设置shell环境变量
|
||
configure_shell_env() {
|
||
print_status "配置shell环境变量..."
|
||
|
||
SHELL_RC=""
|
||
if [ -n "$ZSH_VERSION" ]; then
|
||
SHELL_RC="$HOME/.zshrc"
|
||
elif [ -n "$BASH_VERSION" ]; then
|
||
SHELL_RC="$HOME/.bashrc"
|
||
else
|
||
# 默认使用zsh(macOS默认)
|
||
SHELL_RC="$HOME/.zshrc"
|
||
fi
|
||
|
||
if [ -n "$SHELL_RC" ]; then
|
||
# 检查是否已存在配置
|
||
if ! grep -q "# WireGuard Docker/GitHub Env" "$SHELL_RC" 2>/dev/null; then
|
||
cat >> "$SHELL_RC" << 'ENVEOF'
|
||
|
||
# WireGuard Docker/GitHub Env (美国服务器)
|
||
export DOCKER_BUILDKIT=1
|
||
export COMPOSE_DOCKER_CLI_BUILD=1
|
||
# 禁用Git凭证助手超时(提高稳定性)
|
||
export GIT_CURL_VERBOSE=0
|
||
export GIT_TRACE=0
|
||
|
||
ENVEOF
|
||
print_success "已添加环境变量到 $SHELL_RC"
|
||
print_warning "请运行以下命令使配置生效: source $SHELL_RC"
|
||
else
|
||
print_status "环境变量配置已存在,跳过"
|
||
fi
|
||
fi
|
||
|
||
echo ""
|
||
}
|
||
|
||
# 测试连接
|
||
test_connectivity() {
|
||
print_status "测试基本连接..."
|
||
|
||
# 测试出口IP
|
||
print_status "检测出口IP..."
|
||
EXIT_IP=$(curl -s --max-time 10 https://api.ipify.org 2>/dev/null || echo "检测失败")
|
||
if [ "$EXIT_IP" != "检测失败" ]; then
|
||
print_success "出口IP: $EXIT_IP"
|
||
|
||
# 检测位置
|
||
LOCATION=$(curl -s --max-time 10 "https://ipapi.co/$EXIT_IP/json/" 2>/dev/null | python3 -c "import sys, json; print(json.load(sys.stdin).get('country_name', '未知'))" 2>/dev/null || echo "未知")
|
||
print_status "位置: $LOCATION"
|
||
else
|
||
print_error "无法检测出口IP"
|
||
fi
|
||
|
||
echo ""
|
||
|
||
}
|
||
|
||
# 测试GitHub连接
|
||
test_github() {
|
||
print_status "测试GitHub连接..."
|
||
|
||
# 测试HTTPS
|
||
print_status "测试GitHub HTTPS连接..."
|
||
if curl -I -s --connect-timeout 10 https://github.com | head -1 | grep -q "200\|301\|302"; then
|
||
print_success "GitHub HTTPS连接正常"
|
||
else
|
||
print_warning "GitHub HTTPS连接失败"
|
||
fi
|
||
|
||
# 测试SSH
|
||
print_status "测试GitHub SSH连接..."
|
||
if timeout 10 ssh -T git@github.com 2>&1 | grep -q "successfully authenticated"; then
|
||
print_success "GitHub SSH连接正常"
|
||
else
|
||
print_warning "GitHub SSH连接测试失败(这可能是正常的,如果未配置SSH密钥)"
|
||
fi
|
||
|
||
# 测试Git克隆
|
||
print_status "测试Git克隆速度..."
|
||
TMP_DIR="/tmp/git_test_$$"
|
||
if timeout 30 git clone --depth 1 https://github.com/github/gitignore.git "$TMP_DIR" &>/dev/null; then
|
||
print_success "Git克隆成功"
|
||
rm -rf "$TMP_DIR"
|
||
else
|
||
print_warning "Git克隆超时或失败"
|
||
rm -rf "$TMP_DIR" 2>/dev/null || true
|
||
fi
|
||
}
|
||
|
||
# 主函数
|
||
main() {
|
||
echo "=================================================="
|
||
echo " Docker和GitHub WireGuard配置脚本"
|
||
echo "=================================================="
|
||
echo ""
|
||
|
||
check_sudo
|
||
|
||
if ! check_wireguard; then
|
||
exit 1
|
||
fi
|
||
|
||
echo ""
|
||
print_status "开始配置..."
|
||
echo ""
|
||
|
||
# 配置Docker
|
||
configure_docker
|
||
echo ""
|
||
|
||
# 配置GitHub
|
||
configure_github
|
||
echo ""
|
||
|
||
# 运行测试
|
||
print_status "运行连接测试..."
|
||
echo ""
|
||
test_github
|
||
echo ""
|
||
test_docker
|
||
|
||
echo ""
|
||
echo "=================================================="
|
||
print_success "配置完成!"
|
||
echo "=================================================="
|
||
echo ""
|
||
print_status "后续步骤:"
|
||
echo " 1. 重启Docker Desktop(如果使用)"
|
||
echo " 2. 运行以下命令测试:"
|
||
echo " - docker pull alpine:latest"
|
||
echo " - git clone https://github.com/github/gitignore.git"
|
||
echo " 3. 如果遇到问题,运行: ./check-tunnel.sh"
|
||
echo ""
|
||
}
|
||
|
||
main "$@" |