实现Claude渠道请求穿透功能
- 修改 relay/channel/claude/adaptor.go 支持请求头穿透 - 穿透模式下保持原始请求头完整性,仅替换必要的API密钥 - 非穿透模式保持原有逻辑不变 - 新增 docker-compose-custom.yml 用于自定义版本部署 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
34
docker-compose-custom.yml
Normal file
34
docker-compose-custom.yml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
version: '3.4'
|
||||||
|
|
||||||
|
services:
|
||||||
|
new-api-custom:
|
||||||
|
image: new-api-custom:latest
|
||||||
|
container_name: new-api-custom
|
||||||
|
restart: always
|
||||||
|
command: --log-dir /app/logs
|
||||||
|
ports:
|
||||||
|
- "3099:3000" # 使用3099端口
|
||||||
|
volumes:
|
||||||
|
- ./data:/data # 共享相同数据目录
|
||||||
|
- ./logs-custom:/app/logs # 使用独立日志目录
|
||||||
|
environment:
|
||||||
|
- SQL_DSN=root:123456@tcp(mysql:3306)/new-api # 连接现有的mysql容器
|
||||||
|
- REDIS_CONN_STRING=redis://redis # 连接现有的redis容器
|
||||||
|
- TZ=Asia/Shanghai
|
||||||
|
- ERROR_LOG_ENABLED=true
|
||||||
|
# - STREAMING_TIMEOUT=120
|
||||||
|
# - SESSION_SECRET=random_string # 如果需要多机部署
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "wget -q -O - http://localhost:3000/api/status | grep -o '\"success\":\\s*true' | awk -F: '{print $$2}'"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
networks:
|
||||||
|
- new-api_default # 连接到现有的网络,复用现有的 redis 和 mysql
|
||||||
|
|
||||||
|
# 不定义 redis 和 mysql 服务,直接使用现有运行的容器
|
||||||
|
# 只需要连接到相同的网络即可访问
|
||||||
|
|
||||||
|
networks:
|
||||||
|
new-api_default:
|
||||||
|
external: true # 使用现有的网络
|
||||||
@@ -55,14 +55,34 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
||||||
channel.SetupApiRequestHeader(info, c, req)
|
if model_setting.GetGlobalSettings().PassThroughRequestEnabled {
|
||||||
req.Set("x-api-key", info.ApiKey)
|
// 穿透模式:直接复制原始请求头,但跳过系统级头信息
|
||||||
anthropicVersion := c.Request.Header.Get("anthropic-version")
|
for key, values := range c.Request.Header {
|
||||||
if anthropicVersion == "" {
|
keyLower := strings.ToLower(key)
|
||||||
anthropicVersion = "2023-06-01"
|
if keyLower == "host" || keyLower == "content-length" || keyLower == "connection" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, value := range values {
|
||||||
|
req.Add(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 非穿透模式:使用通用设置
|
||||||
|
channel.SetupApiRequestHeader(info, c, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 无论哪种模式都需要设置正确的API密钥
|
||||||
|
req.Set("x-api-key", info.ApiKey)
|
||||||
|
|
||||||
|
if !model_setting.GetGlobalSettings().PassThroughRequestEnabled {
|
||||||
|
// 非穿透模式才强制设置这些头
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
req.Set("anthropic-version", anthropicVersion)
|
|
||||||
model_setting.GetClaudeSettings().WriteHeaders(info.OriginModelName, req)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user