From 38e17d621def5e4afbdf3503e7a7a3ead3e0bb70 Mon Sep 17 00:00:00 2001 From: huangzhenpc Date: Tue, 26 Aug 2025 16:24:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(claude):=20=E5=AE=9E=E7=8E=B0=E6=99=BA?= =?UTF-8?q?=E8=83=BD=E8=AF=B7=E6=B1=82=E5=A4=B4=E7=AE=A1=E7=90=86=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 核心功能: - 支持渠道级透传设置 (修复 killcode 客户端兼容性) - 智能客户端检测 (Claude Code、killcode、其他客户端) - 动态请求头策略 (透传 vs 伪装) 客户端处理策略: - Claude Code (claude-cli/*): 完全透传原始请求头 - killcode (B2/JS + Stainless SDK): 完全透传原始请求头 - 其他客户端: 伪装成 killcode 格式绕过上游限制 技术改进: - 防止显示 Go-http-client/2.0 User-Agent - 保留重要认证头部 (anthropic-version, anthropic-beta) - 兼容全局透传和渠道透传设置 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- go.mod | 2 +- relay/channel/claude/adaptor.go | 76 +++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 1a92947e..a3caba37 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module one-api // +heroku goVersion go1.18 -go 1.23.4 +go 1.23 require ( github.com/Calcium-Ion/go-epay v0.0.4 diff --git a/relay/channel/claude/adaptor.go b/relay/channel/claude/adaptor.go index 4636691e..8031d94f 100644 --- a/relay/channel/claude/adaptor.go +++ b/relay/channel/claude/adaptor.go @@ -61,15 +61,28 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error { if model_setting.GetGlobalSettings().PassThroughRequestEnabled || info.ChannelSetting.PassThroughBodyEnabled { - // 穿透模式:直接复制原始请求头,但跳过系统级头信息 - for key, values := range c.Request.Header { - keyLower := strings.ToLower(key) - if keyLower == "host" || keyLower == "content-length" || keyLower == "connection" { - continue - } - for _, value := range values { - req.Add(key, value) + userAgent := c.Request.Header.Get("User-Agent") + + // 智能请求头策略:检测客户端类型并决定处理方式 + if isClaudeCode(userAgent) || isKillcode(userAgent) { + // Claude Code 和 killcode: 完全透传原始请求头 + for key, values := range c.Request.Header { + keyLower := strings.ToLower(key) + if keyLower == "host" || keyLower == "content-length" || keyLower == "connection" { + continue + } + for _, value := range values { + req.Add(key, value) + } } + } else { + // 其他客户端: 伪装成 killcode 格式以绕过上游限制 + setupKillcodeHeaders(c, req) + } + + // 保险:确保 User-Agent 一定存在,避免显示 Go-http-client/2.0 + if req.Get("User-Agent") == "" { + req.Set("User-Agent", "B2/JS 0.51.0") } } else { // 非穿透模式:使用通用设置 @@ -79,7 +92,7 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *rel // 无论哪种模式都需要设置正确的API密钥 req.Set("x-api-key", info.ApiKey) - if !model_setting.GetGlobalSettings().PassThroughRequestEnabled { + if !model_setting.GetGlobalSettings().PassThroughRequestEnabled && !info.ChannelSetting.PassThroughBodyEnabled { // 非穿透模式才强制设置这些头 anthropicVersion := c.Request.Header.Get("anthropic-version") if anthropicVersion == "" { @@ -91,6 +104,51 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *rel return nil } +// isClaudeCode 检测是否为 Claude Code 客户端 +func isClaudeCode(userAgent string) bool { + return strings.Contains(userAgent, "claude-cli") +} + +// isKillcode 检测是否为 killcode 客户端 +func isKillcode(userAgent string) bool { + return strings.Contains(userAgent, "B2/JS") || strings.Contains(userAgent, "X-Stainless") +} + +// setupKillcodeHeaders 设置 killcode 风格的请求头,用于伪装其他客户端 +func setupKillcodeHeaders(c *gin.Context, req *http.Header) { + // 基础头部 + req.Set("User-Agent", "B2/JS 0.51.0") + req.Set("Connection", "keep-alive") + req.Set("Accept", "application/json") + req.Set("Accept-Encoding", "br, gzip, deflate") + req.Set("Content-Type", "application/json") + + // Stainless SDK 特有头部 + req.Set("X-Stainless-Retry-Count", "0") + req.Set("X-Stainless-Timeout", "600") + req.Set("X-Stainless-Lang", "js") + req.Set("X-Stainless-Package-Version", "0.51.0") + req.Set("X-Stainless-OS", "Windows") + req.Set("X-Stainless-Arch", "x64") + req.Set("X-Stainless-Runtime", "node") + req.Set("X-Stainless-Runtime-Version", "v20.19.1") + + // 保留重要的原始头部 + if anthropicVersion := c.Request.Header.Get("anthropic-version"); anthropicVersion != "" { + req.Set("anthropic-version", anthropicVersion) + } else { + req.Set("anthropic-version", "2023-06-01") + } + + if anthropicBeta := c.Request.Header.Get("anthropic-beta"); anthropicBeta != "" { + req.Set("anthropic-beta", anthropicBeta) + } + + // 其他辅助头部 + req.Set("accept-language", "*") + req.Set("sec-fetch-mode", "cors") +} + func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) { if request == nil { return nil, errors.New("request is nil")