核心功能: - 支持渠道级透传设置 (修复 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>
197 lines
6.1 KiB
Go
197 lines
6.1 KiB
Go
package claude
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"one-api/dto"
|
|
"one-api/relay/channel"
|
|
relaycommon "one-api/relay/common"
|
|
"one-api/setting/model_setting"
|
|
"one-api/types"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
RequestModeCompletion = 1
|
|
RequestModeMessage = 2
|
|
)
|
|
|
|
type Adaptor struct {
|
|
RequestMode int
|
|
}
|
|
|
|
func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
|
|
return request, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
|
if strings.HasPrefix(info.UpstreamModelName, "claude-2") || strings.HasPrefix(info.UpstreamModelName, "claude-instant") {
|
|
a.RequestMode = RequestModeCompletion
|
|
} else {
|
|
a.RequestMode = RequestModeMessage
|
|
}
|
|
}
|
|
|
|
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
|
if a.RequestMode == RequestModeMessage {
|
|
return fmt.Sprintf("%s/v1/messages", info.ChannelBaseUrl), nil
|
|
} else {
|
|
return fmt.Sprintf("%s/v1/complete", info.ChannelBaseUrl), nil
|
|
}
|
|
}
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
|
if model_setting.GetGlobalSettings().PassThroughRequestEnabled || info.ChannelSetting.PassThroughBodyEnabled {
|
|
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 {
|
|
// 非穿透模式:使用通用设置
|
|
channel.SetupApiRequestHeader(info, c, req)
|
|
}
|
|
|
|
// 无论哪种模式都需要设置正确的API密钥
|
|
req.Set("x-api-key", info.ApiKey)
|
|
|
|
if !model_setting.GetGlobalSettings().PassThroughRequestEnabled && !info.ChannelSetting.PassThroughBodyEnabled {
|
|
// 非穿透模式才强制设置这些头
|
|
anthropicVersion := c.Request.Header.Get("anthropic-version")
|
|
if anthropicVersion == "" {
|
|
anthropicVersion = "2023-06-01"
|
|
}
|
|
req.Set("anthropic-version", anthropicVersion)
|
|
model_setting.GetClaudeSettings().WriteHeaders(info.OriginModelName, req)
|
|
}
|
|
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")
|
|
}
|
|
if a.RequestMode == RequestModeCompletion {
|
|
return RequestOpenAI2ClaudeComplete(*request), nil
|
|
} else {
|
|
return RequestOpenAI2ClaudeMessage(c, *request)
|
|
}
|
|
}
|
|
|
|
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
|
// TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
|
|
return channel.DoApiRequest(a, c, info, requestBody)
|
|
}
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
|
if info.IsStream {
|
|
return ClaudeStreamHandler(c, resp, info, a.RequestMode)
|
|
} else {
|
|
return ClaudeHandler(c, resp, info, a.RequestMode)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
return ModelList
|
|
}
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
return ChannelName
|
|
}
|