Created
February 4, 2026 01:24
-
-
Save engalar/a315d484b6595dbde1b09d118d50ca49 to your computer and use it in GitHub Desktop.
Claude Code Base URL 配置检查工具
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Claude Code Base URL 配置检查工具 | |
| # 使用方法: irm http://your-host:port/check_claude.ps1 | iex | |
| [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
| $OutputEncoding = [System.Text.Encoding]::UTF8 | |
| Write-Host "========================================" -ForegroundColor Cyan | |
| Write-Host " Claude Code Base URL 配置检查工具" -ForegroundColor Cyan | |
| Write-Host "========================================" -ForegroundColor Cyan | |
| Write-Host "" | |
| # 1. 检查环境变量 | |
| Write-Host "[1] 检查环境变量..." -ForegroundColor Yellow | |
| Write-Host "" | |
| if ($env:ANTHROPIC_BASE_URL) { | |
| Write-Host "ANTHROPIC_BASE_URL: $env:ANTHROPIC_BASE_URL" | |
| } else { | |
| Write-Host "ANTHROPIC_BASE_URL: 未设置 (使用默认值)" | |
| } | |
| if ($env:ANTHROPIC_API_KEY) { | |
| Write-Host "ANTHROPIC_API_KEY: 已设置 (隐藏)" | |
| } else { | |
| Write-Host "[警告] ANTHROPIC_API_KEY: 未设置!" -ForegroundColor Red | |
| } | |
| Write-Host "" | |
| # 2. 检查配置文件 | |
| Write-Host "[2] 检查配置文件..." -ForegroundColor Yellow | |
| Write-Host "" | |
| $claudeJson = "$env:USERPROFILE\.claude.json" | |
| $claudeSettings = "$env:USERPROFILE\.claude\settings.json" | |
| $extractedUrl = $null | |
| $authToken = $null | |
| # 检查 ~/.claude.json | |
| if (Test-Path $claudeJson) { | |
| Write-Host "找到: $claudeJson" | |
| try { | |
| $config = Get-Content $claudeJson -Raw | ConvertFrom-Json | |
| if ($config.apiBaseUrl) { | |
| Write-Host "[信息] claude.json 中检测到自定义 apiBaseUrl" -ForegroundColor Green | |
| Write-Host " apiBaseUrl: $($config.apiBaseUrl)" | |
| $extractedUrl = $config.apiBaseUrl | |
| } | |
| } catch { | |
| Write-Host "[警告] 无法解析 claude.json" -ForegroundColor Red | |
| } | |
| } else { | |
| Write-Host "未找到: $claudeJson (正常)" | |
| } | |
| Write-Host "" | |
| # 检查 ~/.claude/settings.json | |
| if (Test-Path $claudeSettings) { | |
| Write-Host "找到: $claudeSettings" | |
| try { | |
| $settings = Get-Content $claudeSettings -Raw | ConvertFrom-Json | |
| if ($settings.env.ANTHROPIC_BASE_URL) { | |
| Write-Host "[信息] settings.json env 中找到 ANTHROPIC_BASE_URL:" -ForegroundColor Green | |
| Write-Host " ANTHROPIC_BASE_URL: $($settings.env.ANTHROPIC_BASE_URL)" | |
| $extractedUrl = $settings.env.ANTHROPIC_BASE_URL | |
| } else { | |
| Write-Host "[信息] settings.json 中无 ANTHROPIC_BASE_URL" | |
| } | |
| if ($settings.env.ANTHROPIC_AUTH_TOKEN) { | |
| Write-Host "[信息] settings.json 中找到 ANTHROPIC_AUTH_TOKEN" -ForegroundColor Green | |
| $authToken = $settings.env.ANTHROPIC_AUTH_TOKEN | |
| } | |
| } catch { | |
| Write-Host "[警告] 无法解析 settings.json" -ForegroundColor Red | |
| } | |
| } else { | |
| Write-Host "未找到: $claudeSettings (正常)" | |
| } | |
| Write-Host "" | |
| # 3. 检查项目配置 | |
| Write-Host "[3] 检查项目配置..." -ForegroundColor Yellow | |
| Write-Host "" | |
| $projectConfig = ".\.claude.json" | |
| if (Test-Path $projectConfig) { | |
| Write-Host "找到: $projectConfig (项目级)" | |
| try { | |
| $projConfig = Get-Content $projectConfig -Raw | ConvertFrom-Json | |
| if ($projConfig.apiBaseUrl) { | |
| Write-Host "[信息] 项目 .claude.json 中找到 Base URL 配置" -ForegroundColor Green | |
| } | |
| } catch {} | |
| } else { | |
| Write-Host "无项目级 .claude.json (正常)" | |
| } | |
| Write-Host "" | |
| # 4. 验证 URL 格式 | |
| Write-Host "[4] 验证 URL 格式..." -ForegroundColor Yellow | |
| Write-Host "" | |
| if ($extractedUrl) { | |
| Write-Host "配置的 Base URL: $extractedUrl" | |
| $checkUrl = $extractedUrl | |
| } elseif ($env:ANTHROPIC_BASE_URL) { | |
| Write-Host "环境变量 Base URL: $env:ANTHROPIC_BASE_URL" | |
| $checkUrl = $env:ANTHROPIC_BASE_URL | |
| } else { | |
| Write-Host "使用默认值: https://api.anthropic.com" | |
| $checkUrl = "https://api.anthropic.com" | |
| } | |
| Write-Host "" | |
| Write-Host "验证中: $checkUrl" | |
| # 检查尾部斜杠 | |
| if ($checkUrl.EndsWith("/")) { | |
| Write-Host "[错误] URL 不应以斜杠结尾" -ForegroundColor Red | |
| } else { | |
| Write-Host "[通过] 无尾部斜杠" -ForegroundColor Green | |
| } | |
| # 检查 https | |
| if ($checkUrl.StartsWith("https://")) { | |
| Write-Host "[通过] 使用 HTTPS" -ForegroundColor Green | |
| } else { | |
| Write-Host "[警告] 建议使用 HTTPS" -ForegroundColor Yellow | |
| } | |
| # 检查 /v1 | |
| if ($checkUrl -match "/v1") { | |
| Write-Host "[错误] URL 不应包含 /v1 路径" -ForegroundColor Red | |
| } else { | |
| Write-Host "[通过] 无 /v1 路径" -ForegroundColor Green | |
| } | |
| Write-Host "" | |
| # 5. 测试连接 | |
| Write-Host "[5] 测试 API 连接..." -ForegroundColor Yellow | |
| Write-Host "" | |
| if (-not $authToken) { | |
| if ($env:ANTHROPIC_API_KEY) { | |
| $authToken = $env:ANTHROPIC_API_KEY | |
| } else { | |
| Write-Host "[跳过] 未找到 API key 或 auth token" -ForegroundColor Yellow | |
| $authToken = $null | |
| } | |
| } | |
| if ($authToken) { | |
| $testUrl = "$checkUrl/v1/messages" | |
| Write-Host "测试端点: $testUrl" | |
| Write-Host "" | |
| try { | |
| $headers = @{ | |
| "x-api-key" = $authToken | |
| "anthropic-version" = "2023-06-01" | |
| "content-type" = "application/json" | |
| } | |
| $response = Invoke-WebRequest -Uri $testUrl -Method Get -Headers $headers -UseBasicParsing -ErrorAction Stop | |
| $statusCode = $response.StatusCode | |
| } catch { | |
| if ($_.Exception.Response) { | |
| $statusCode = [int]$_.Exception.Response.StatusCode | |
| } else { | |
| $statusCode = 0 | |
| } | |
| } | |
| Write-Host "HTTP 状态码: $statusCode" | |
| Write-Host "" | |
| switch ($statusCode) { | |
| 200 { Write-Host "[通过] API 连接正常" -ForegroundColor Green } | |
| 401 { Write-Host "[错误] 认证失败 - API Key 或 Auth Token 无效" -ForegroundColor Red } | |
| 403 { Write-Host "[错误] 禁止访问 - 权限不足" -ForegroundColor Red } | |
| 404 { | |
| Write-Host "[错误] 端点不存在 - Base URL 可能配置错误" -ForegroundColor Red | |
| Write-Host "" | |
| Write-Host "可能的原因:" -ForegroundColor Yellow | |
| Write-Host " 1. Base URL 路径不正确" | |
| Write-Host " 2. 服务端未实现 /v1/messages 端点" | |
| Write-Host " 3. 需要不同的 API 路径格式" | |
| Write-Host "" | |
| Write-Host "建议检查:" -ForegroundColor Yellow | |
| Write-Host " - 确认服务提供商的正确 API 端点" | |
| Write-Host " - 尝试移除或修改 URL 中的 /api 部分" | |
| } | |
| 405 { Write-Host "[信息] 方法不允许 - 端点存在但不支持 GET 请求 (正常)" -ForegroundColor Green } | |
| 500 { Write-Host "[错误] 服务器内部错误" -ForegroundColor Red } | |
| 502 { Write-Host "[错误] 网关错误 - 上游服务不可用" -ForegroundColor Red } | |
| 503 { Write-Host "[错误] 服务不可用" -ForegroundColor Red } | |
| 0 { Write-Host "[错误] 无法连接 - 网络问题或 URL 无效" -ForegroundColor Red } | |
| default { Write-Host "[信息] 状态码: $statusCode" -ForegroundColor Yellow } | |
| } | |
| } | |
| Write-Host "" | |
| # 总结 | |
| Write-Host "========================================" -ForegroundColor Cyan | |
| Write-Host " 检查完成" -ForegroundColor Cyan | |
| Write-Host "========================================" -ForegroundColor Cyan | |
| Write-Host "" | |
| Write-Host "默认 URL: https://api.anthropic.com" | |
| Write-Host "当前 URL: $checkUrl" | |
| Write-Host "" | |
| Write-Host "配置文件位置:" | |
| Write-Host " 全局: $claudeJson" | |
| Write-Host " 设置: $claudeSettings" | |
| Write-Host "" | |
| # ============================================ | |
| # 内置 HTTP 服务器 (可选启动) | |
| # ============================================ | |
| function Start-ScriptServer { | |
| param( | |
| [int]$Port = 8080 | |
| ) | |
| $scriptContent = $MyInvocation.ScriptName ? (Get-Content $MyInvocation.ScriptName -Raw) : $null | |
| if (-not $scriptContent) { | |
| # 如果是通过 iex 执行的,使用当前脚本内容 | |
| $scriptContent = @' | |
| {{SCRIPT_CONTENT}} | |
| '@ | |
| } | |
| $localIP = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notmatch "Loopback" -and $_.IPAddress -notmatch "^169" } | Select-Object -First 1).IPAddress | |
| if (-not $localIP) { $localIP = "localhost" } | |
| Write-Host "" | |
| Write-Host "========================================" -ForegroundColor Magenta | |
| Write-Host " 启动脚本分发服务器" -ForegroundColor Magenta | |
| Write-Host "========================================" -ForegroundColor Magenta | |
| Write-Host "" | |
| Write-Host "其他电脑执行:" -ForegroundColor Green | |
| Write-Host " irm http://${localIP}:${Port}/check_claude.ps1 | iex" -ForegroundColor White -BackgroundColor DarkGreen | |
| Write-Host "" | |
| Write-Host "按 Ctrl+C 停止" -ForegroundColor Yellow | |
| $listener = New-Object System.Net.HttpListener | |
| $listener.Prefixes.Add("http://+:$Port/") | |
| $listener.Start() | |
| try { | |
| while ($listener.IsListening) { | |
| $context = $listener.GetContext() | |
| $buffer = [System.Text.Encoding]::UTF8.GetBytes($scriptContent) | |
| $context.Response.ContentType = "text/plain; charset=utf-8" | |
| $context.Response.ContentLength64 = $buffer.Length | |
| $context.Response.OutputStream.Write($buffer, 0, $buffer.Length) | |
| $context.Response.Close() | |
| Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $($context.Request.RemoteEndPoint.Address) 已获取脚本" -ForegroundColor Green | |
| } | |
| } finally { | |
| $listener.Stop() | |
| } | |
| } | |
| # 导出函数供后续调用 | |
| # 使用: Start-ScriptServer -Port 8080 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment