96 lines
4.0 KiB
PowerShell
96 lines
4.0 KiB
PowerShell
# Git 网络连接问题修复脚本
|
||
# 用途:配置 Git 代理或切换到 SSH
|
||
|
||
param(
|
||
[string]$ProxyHost = "127.0.0.1",
|
||
[int]$ProxyPort = 7890,
|
||
[switch]$UseSSH,
|
||
[switch]$RemoveProxy
|
||
)
|
||
|
||
Write-Host "=== Git 网络连接问题修复 ===" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
|
||
if ($RemoveProxy) {
|
||
Write-Host "移除 Git 代理配置..." -ForegroundColor Yellow
|
||
git config --global --unset http.proxy 2>$null
|
||
git config --global --unset https.proxy 2>$null
|
||
git config --global --unset http.https://github.com.proxy 2>$null
|
||
git config --global --unset https.https://github.com.proxy 2>$null
|
||
Write-Host "✓ 代理配置已移除" -ForegroundColor Green
|
||
exit 0
|
||
}
|
||
|
||
if ($UseSSH) {
|
||
Write-Host "切换到 SSH 连接..." -ForegroundColor Yellow
|
||
Write-Host ""
|
||
|
||
# 检查 SSH 密钥是否存在
|
||
$sshKeyPath = "$env:USERPROFILE\.ssh\id_ed25519.pub"
|
||
if (-not (Test-Path $sshKeyPath)) {
|
||
Write-Host "未找到 SSH 密钥,正在生成..." -ForegroundColor Yellow
|
||
ssh-keygen -t ed25519 -C "bujie9527@github.com" -f "$env:USERPROFILE\.ssh\id_ed25519" -N '""'
|
||
Write-Host "✓ SSH 密钥已生成" -ForegroundColor Green
|
||
}
|
||
|
||
# 显示公钥
|
||
Write-Host ""
|
||
Write-Host "SSH 公钥内容(需要添加到 GitHub):" -ForegroundColor Cyan
|
||
Write-Host "---"
|
||
Get-Content $sshKeyPath
|
||
Write-Host "---"
|
||
Write-Host ""
|
||
Write-Host "请执行以下步骤:" -ForegroundColor Yellow
|
||
Write-Host "1. 复制上面的公钥内容" -ForegroundColor Gray
|
||
Write-Host "2. 访问: https://github.com/settings/keys" -ForegroundColor Gray
|
||
Write-Host "3. 点击 'New SSH key'" -ForegroundColor Gray
|
||
Write-Host "4. 粘贴公钥并保存" -ForegroundColor Gray
|
||
Write-Host ""
|
||
|
||
$continue = Read-Host "已添加 SSH 密钥到 GitHub? (y/n)"
|
||
if ($continue -eq "y" -or $continue -eq "Y") {
|
||
# 测试 SSH 连接
|
||
Write-Host "测试 SSH 连接..." -ForegroundColor Yellow
|
||
$testResult = ssh -T git@github.com 2>&1
|
||
if ($testResult -match "successfully authenticated") {
|
||
Write-Host "✓ SSH 连接成功" -ForegroundColor Green
|
||
|
||
# 更改远程 URL
|
||
Write-Host "更改远程仓库 URL 为 SSH..." -ForegroundColor Yellow
|
||
git remote set-url origin git@github.com:bujie9527/wecom-ai-assistant.git
|
||
Write-Host "✓ 远程 URL 已更改为 SSH" -ForegroundColor Green
|
||
Write-Host ""
|
||
Write-Host "现在可以尝试推送:" -ForegroundColor Cyan
|
||
Write-Host " git push -u origin main" -ForegroundColor Gray
|
||
} else {
|
||
Write-Host "✗ SSH 连接失败,请检查 SSH 密钥是否正确添加到 GitHub" -ForegroundColor Red
|
||
}
|
||
}
|
||
} else {
|
||
Write-Host "配置 Git HTTP/HTTPS 代理..." -ForegroundColor Yellow
|
||
Write-Host ""
|
||
Write-Host "代理地址: http://${ProxyHost}:${ProxyPort}" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
|
||
# 仅针对 GitHub 设置代理
|
||
git config --global http.https://github.com.proxy "http://${ProxyHost}:${ProxyPort}"
|
||
git config --global https.https://github.com.proxy "http://${ProxyHost}:${ProxyPort}"
|
||
|
||
Write-Host "✓ 代理已配置" -ForegroundColor Green
|
||
Write-Host ""
|
||
Write-Host "验证配置:" -ForegroundColor Yellow
|
||
git config --global --get http.https://github.com.proxy
|
||
git config --global --get https.https://github.com.proxy
|
||
Write-Host ""
|
||
Write-Host "现在可以尝试推送:" -ForegroundColor Cyan
|
||
Write-Host " git push -u origin main" -ForegroundColor Gray
|
||
Write-Host ""
|
||
Write-Host "如果代理端口不同,请使用:" -ForegroundColor Yellow
|
||
Write-Host " .\scripts\fix-git-network.ps1 -ProxyPort 你的端口号" -ForegroundColor Gray
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "其他选项:" -ForegroundColor Cyan
|
||
Write-Host " - 切换到 SSH: .\scripts\fix-git-network.ps1 -UseSSH" -ForegroundColor Gray
|
||
Write-Host " - 移除代理: .\scripts\fix-git-network.ps1 -RemoveProxy" -ForegroundColor Gray
|