Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
ab3e44e4
Commit
ab3e44e4
authored
Mar 30, 2026
by
shaw
Browse files
fix: 适配X-Claude-Code-Session-Id头
parent
b6527523
Changes
2
Hide whitespace changes
Inline
Side-by-side
backend/internal/service/gateway_service.go
View file @
ab3e44e4
...
@@ -369,6 +369,8 @@ var allowedHeaders = map[string]bool{
...
@@ -369,6 +369,8 @@ var allowedHeaders = map[string]bool{
"user-agent": true,
"user-agent": true,
"content-type": true,
"content-type": true,
"accept-encoding": true,
"accept-encoding": true,
"x-claude-code-session-id": true,
"x-client-request-id": true,
}
}
// GatewayCache 定义网关服务的缓存操作接口。
// GatewayCache 定义网关服务的缓存操作接口。
...
@@ -5756,6 +5758,15 @@ func (s *GatewayService) buildUpstreamRequest(ctx context.Context, c *gin.Contex
...
@@ -5756,6 +5758,15 @@ func (s *GatewayService) buildUpstreamRequest(ctx context.Context, c *gin.Contex
}
}
}
}
// 同步 X-Claude-Code-Session-Id 头:取 body 中已处理的 metadata.user_id 的 session_id 覆盖
if sessionHeader := getHeaderRaw(req.Header, "X-Claude-Code-Session-Id"); sessionHeader != "" {
if uid := gjson.GetBytes(body, "metadata.user_id").String(); uid != "" {
if parsed := ParseMetadataUserID(uid); parsed != nil {
setHeaderRaw(req.Header, "X-Claude-Code-Session-Id", parsed.SessionID)
}
}
}
// === DEBUG: 打印上游转发请求(headers + body 摘要),与 CLIENT_ORIGINAL 对比 ===
// === DEBUG: 打印上游转发请求(headers + body 摘要),与 CLIENT_ORIGINAL 对比 ===
s.debugLogGatewaySnapshot("UPSTREAM_FORWARD", req.Header, body, map[string]string{
s.debugLogGatewaySnapshot("UPSTREAM_FORWARD", req.Header, body, map[string]string{
"url": req.URL.String(),
"url": req.URL.String(),
...
@@ -8475,6 +8486,15 @@ func (s *GatewayService) buildCountTokensRequest(ctx context.Context, c *gin.Con
...
@@ -8475,6 +8486,15 @@ func (s *GatewayService) buildCountTokensRequest(ctx context.Context, c *gin.Con
}
}
}
}
// 同步 X-Claude-Code-Session-Id 头:取 body 中已处理的 metadata.user_id 的 session_id 覆盖
if sessionHeader := getHeaderRaw(req.Header, "X-Claude-Code-Session-Id"); sessionHeader != "" {
if uid := gjson.GetBytes(body, "metadata.user_id").String(); uid != "" {
if parsed := ParseMetadataUserID(uid); parsed != nil {
setHeaderRaw(req.Header, "X-Claude-Code-Session-Id", parsed.SessionID)
}
}
}
if c != nil && tokenType == "oauth" {
if c != nil && tokenType == "oauth" {
c.Set(claudeMimicDebugInfoKey, buildClaudeMimicDebugLine(req, body, account, tokenType, mimicClaudeCode))
c.Set(claudeMimicDebugInfoKey, buildClaudeMimicDebugLine(req, body, account, tokenType, mimicClaudeCode))
}
}
...
...
backend/internal/service/header_util.go
View file @
ab3e44e4
...
@@ -36,6 +36,11 @@ var headerWireCasing = map[string]string{
...
@@ -36,6 +36,11 @@ var headerWireCasing = map[string]string{
"sec-fetch-mode"
:
"sec-fetch-mode"
,
"sec-fetch-mode"
:
"sec-fetch-mode"
,
"accept-encoding"
:
"accept-encoding"
,
"accept-encoding"
:
"accept-encoding"
,
"authorization"
:
"authorization"
,
"authorization"
:
"authorization"
,
// Claude Code 2.1.87+ 新增 header
"x-claude-code-session-id"
:
"X-Claude-Code-Session-Id"
,
"x-client-request-id"
:
"x-client-request-id"
,
"content-length"
:
"content-length"
,
}
}
// headerWireOrder 定义真实 Claude CLI 发送 header 的顺序(基于抓包)。
// headerWireOrder 定义真实 Claude CLI 发送 header 的顺序(基于抓包)。
...
@@ -55,11 +60,14 @@ var headerWireOrder = []string{
...
@@ -55,11 +60,14 @@ var headerWireOrder = []string{
"authorization"
,
"authorization"
,
"x-app"
,
"x-app"
,
"User-Agent"
,
"User-Agent"
,
"X-Claude-Code-Session-Id"
,
"content-type"
,
"content-type"
,
"anthropic-beta"
,
"anthropic-beta"
,
"x-client-request-id"
,
"accept-language"
,
"accept-language"
,
"sec-fetch-mode"
,
"sec-fetch-mode"
,
"accept-encoding"
,
"accept-encoding"
,
"content-length"
,
"x-stainless-helper-method"
,
"x-stainless-helper-method"
,
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment