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")