feat(claude): 实现智能请求头管理系统

核心功能:
- 支持渠道级透传设置 (修复 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 <noreply@anthropic.com>
This commit is contained in:
huangzhenpc
2025-08-26 16:24:51 +08:00
parent a8b11912df
commit 38e17d621d
2 changed files with 68 additions and 10 deletions

View File

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