feat: auto-detect system proxy for China users
- Detect via .NET DefaultWebProxy (covers Clash/V2Ray/快柠檬/system proxy) - Detect via IE registry proxy settings - Scan common local proxy ports as fallback - Clear error message + manual override instructions if no proxy found
This commit is contained in:
parent
f0cb8c7529
commit
889b485933
85
install.ps1
85
install.ps1
@ -134,12 +134,75 @@ function Render-SettingsTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
# ─── 代理自动检测 ────────────────────────────────────
|
||||
function Detect-SystemProxy {
|
||||
# 如果已手动设置了 HTTPS_PROXY,直接使用
|
||||
if ($env:HTTPS_PROXY) {
|
||||
Write-Host " [OK] 已设置 HTTPS_PROXY=$($env:HTTPS_PROXY)" -ForegroundColor Green
|
||||
return
|
||||
}
|
||||
|
||||
Write-Host " 检测系统代理..." -ForegroundColor Gray
|
||||
|
||||
# 方法1: 通过 .NET 获取系统代理 (最可靠,支持 Clash/V2Ray/快柠檬/系统代理)
|
||||
try {
|
||||
$proxyUri = [System.Net.WebRequest]::DefaultWebProxy.GetProxy("https://api.anthropic.com")
|
||||
if ($proxyUri -and $proxyUri.Authority -ne "api.anthropic.com") {
|
||||
$proxyUrl = "http://$($proxyUri.Authority)"
|
||||
$env:HTTPS_PROXY = $proxyUrl
|
||||
$env:HTTP_PROXY = $proxyUrl
|
||||
Write-Host " [OK] 检测到系统代理: $proxyUrl" -ForegroundColor Green
|
||||
return
|
||||
}
|
||||
} catch {}
|
||||
|
||||
# 方法2: 读取注册表 IE 代理设置
|
||||
try {
|
||||
$reg = Get-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -ErrorAction SilentlyContinue
|
||||
if ($reg.ProxyEnable -eq 1 -and $reg.ProxyServer) {
|
||||
$proxy = $reg.ProxyServer
|
||||
if ($proxy -notmatch '^http') { $proxy = "http://$proxy" }
|
||||
$env:HTTPS_PROXY = $proxy
|
||||
$env:HTTP_PROXY = $proxy
|
||||
Write-Host " [OK] 检测到 IE 代理: $proxy" -ForegroundColor Green
|
||||
return
|
||||
}
|
||||
} catch {}
|
||||
|
||||
# 方法3: 扫描常见代理端口
|
||||
$commonPorts = @(7890, 7891, 7893, 10792, 10793, 10808, 10809, 1080, 1087, 8080, 8118)
|
||||
foreach ($port in $commonPorts) {
|
||||
try {
|
||||
$tcp = New-Object System.Net.Sockets.TcpClient
|
||||
$tcp.Connect("127.0.0.1", $port)
|
||||
$tcp.Close()
|
||||
$env:HTTPS_PROXY = "http://127.0.0.1:$port"
|
||||
$env:HTTP_PROXY = "http://127.0.0.1:$port"
|
||||
Write-Host " [OK] 检测到本地代理端口: $port" -ForegroundColor Green
|
||||
return
|
||||
} catch {}
|
||||
}
|
||||
|
||||
# 未找到代理
|
||||
Write-Host " [!!] 未检测到代理/VPN" -ForegroundColor Red
|
||||
Write-Host ""
|
||||
Write-Host " Claude Code 需要代理才能在国内使用 (启动时检查 api.anthropic.com)" -ForegroundColor Yellow
|
||||
Write-Host " 请先启动代理软件 (Clash/V2Ray/快柠檬等),然后重新运行本脚本" -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
Write-Host " 如已有代理,可手动指定:" -ForegroundColor Gray
|
||||
Write-Host " `$env:HTTPS_PROXY = 'http://127.0.0.1:端口号'" -ForegroundColor Gray
|
||||
Write-Host " pwsh -ExecutionPolicy Bypass -File install.ps1" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
$continue = Read-Host " 无代理继续? (y/n,无代理大概率启动失败)"
|
||||
if ($continue -ne 'y') { exit 1 }
|
||||
}
|
||||
|
||||
# ─── 主流程 ──────────────────────────────────────────
|
||||
|
||||
Write-Banner
|
||||
|
||||
# 步骤 0: 前置检查
|
||||
Write-Host "[0/5] 前置检查..." -ForegroundColor White
|
||||
Write-Host "[0/7] 前置检查..." -ForegroundColor White
|
||||
$checks = @(
|
||||
@{ Name = "Claude Code"; OK = (Test-Command "claude") }
|
||||
@{ Name = "Node.js"; OK = (Test-Command "node") }
|
||||
@ -159,13 +222,17 @@ if (-not (Test-Command "node")) {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 步骤 0.5: 代理检测 (国内必须)
|
||||
Write-Host "`n[0.5/7] 代理检测..." -ForegroundColor White
|
||||
Detect-SystemProxy
|
||||
|
||||
# 步骤 1: 解密凭证 (进程级环境变量,不写磁盘)
|
||||
Write-Host "`n[1/6] 解密凭证..." -ForegroundColor White
|
||||
Write-Host "`n[1/7] 解密凭证..." -ForegroundColor White
|
||||
Decrypt-Secrets
|
||||
|
||||
# 步骤 2: 克隆/更新仓库
|
||||
if (-not $StartOnly) {
|
||||
Write-Host "`n[2/6] 同步 Bookworm 配置..." -ForegroundColor White
|
||||
Write-Host "`n[2/7] 同步 Bookworm 配置..." -ForegroundColor White
|
||||
|
||||
if (Test-Path $ClaudeTarget) {
|
||||
$isGit = Test-Path (Join-Path $ClaudeTarget ".git")
|
||||
@ -236,13 +303,13 @@ if (-not $StartOnly) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Host "`n[2/6] StartOnly 模式,跳过同步" -ForegroundColor Gray
|
||||
Write-Host "`n[2/7] StartOnly 模式,跳过同步" -ForegroundColor Gray
|
||||
}
|
||||
|
||||
# 步骤 3: 完整性校验
|
||||
$integrityFile = Join-Path $ClaudeTarget "integrity.sha256"
|
||||
if (Test-Path $integrityFile) {
|
||||
Write-Host "`n[3/6] 完整性校验..." -ForegroundColor White
|
||||
Write-Host "`n[4/7] 完整性校验..." -ForegroundColor White
|
||||
$failures = @()
|
||||
Get-Content $integrityFile | ForEach-Object {
|
||||
if ($_ -match '^([a-f0-9]{64})\s+(.+)$') {
|
||||
@ -266,15 +333,15 @@ if (Test-Path $integrityFile) {
|
||||
Write-Host " [OK] 所有文件完整性校验通过" -ForegroundColor Green
|
||||
}
|
||||
} else {
|
||||
Write-Host "`n[3/6] 跳过完整性校验 (无 integrity.sha256)" -ForegroundColor Gray
|
||||
Write-Host "`n[4/7] 跳过完整性校验 (无 integrity.sha256)" -ForegroundColor Gray
|
||||
}
|
||||
|
||||
# 步骤 4: 渲染 settings.json
|
||||
Write-Host "`n[4/6] 渲染配置模板..." -ForegroundColor White
|
||||
Write-Host "`n[5/7] 渲染配置模板..." -ForegroundColor White
|
||||
Render-SettingsTemplate
|
||||
|
||||
# 步骤 5: 确保必要目录存在
|
||||
Write-Host "`n[5/6] 初始化本地目录..." -ForegroundColor White
|
||||
Write-Host "`n[6/7] 初始化本地目录..." -ForegroundColor White
|
||||
$localDirs = @("debug", "sessions", "cache", "backups", "telemetry", "shell-snapshots", "projects", "memory")
|
||||
foreach ($d in $localDirs) {
|
||||
$dirPath = Join-Path $ClaudeTarget $d
|
||||
@ -297,7 +364,7 @@ if ($nodeCheck -eq $ClaudeTarget) {
|
||||
}
|
||||
|
||||
# 步骤 6: 启动 Claude Code
|
||||
Write-Host "`n[6/6] 启动 Claude Code..." -ForegroundColor White
|
||||
Write-Host "`n[7/7] 启动 Claude Code..." -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host " ╔══════════════════════════════════════════╗" -ForegroundColor Green
|
||||
Write-Host " ║ Bookworm 就绪! 正在启动 Claude Code... ║" -ForegroundColor Green
|
||||
|
||||
Loading…
Reference in New Issue
Block a user