luispater commited on
Commit
dfd5ee8
·
unverified ·
1 Parent(s): cd1220f

refactor(headers): centralize header logic using `EnsureHeader` utility

Browse files

- Introduced `EnsureHeader` in `internal/misc/header_utils.go` to streamline header setting across executors.
- Updated Codex, Claude, and Gemini executors to utilize `EnsureHeader` for consistent header application.
- Incorporated Gin context headers (if available) into request header manipulation for better integration.

internal/misc/header_utils.go ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package misc
2
+
3
+ import (
4
+ "net/http"
5
+ "strings"
6
+ )
7
+
8
+ func EnsureHeader(target http.Header, source http.Header, key, defaultValue string) {
9
+ if target == nil {
10
+ return
11
+ }
12
+ if source != nil {
13
+ if val := strings.TrimSpace(source.Get(key)); val != "" {
14
+ target.Set(key, val)
15
+ return
16
+ }
17
+ }
18
+ if strings.TrimSpace(target.Get(key)) != "" {
19
+ return
20
+ }
21
+ if val := strings.TrimSpace(defaultValue); val != "" {
22
+ target.Set(key, val)
23
+ }
24
+ }
internal/runtime/executor/claude_executor.go CHANGED
@@ -18,6 +18,8 @@ import (
18
  sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
19
  log "github.com/sirupsen/logrus"
20
  "github.com/tidwall/sjson"
 
 
21
  )
22
 
23
  // ClaudeExecutor is a stateless executor for Anthropic Claude over the messages API.
@@ -177,22 +179,28 @@ func (e *ClaudeExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (
177
  func applyClaudeHeaders(r *http.Request, apiKey string, stream bool) {
178
  r.Header.Set("Authorization", "Bearer "+apiKey)
179
  r.Header.Set("Content-Type", "application/json")
180
- r.Header.Set("Anthropic-Version", "2023-06-01")
181
- r.Header.Set("Anthropic-Dangerous-Direct-Browser-Access", "true")
182
- r.Header.Set("Anthropic-Beta", "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  r.Header.Set("Connection", "keep-alive")
184
  r.Header.Set("User-Agent", "claude-cli/1.0.83 (external, cli)")
185
  r.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
186
- r.Header.Set("X-App", "cli")
187
- r.Header.Set("X-Stainless-Helper-Method", "stream")
188
- r.Header.Set("X-Stainless-Retry-Count", "0")
189
- r.Header.Set("X-Stainless-Runtime-Version", "v24.3.0")
190
- r.Header.Set("X-Stainless-Package-Version", "0.55.1")
191
- r.Header.Set("X-Stainless-Runtime", "node")
192
- r.Header.Set("X-Stainless-Lang", "js")
193
- r.Header.Set("X-Stainless-Arch", "arm64")
194
- r.Header.Set("X-Stainless-Os", "MacOS")
195
- r.Header.Set("X-Stainless-Timeout", "60")
196
  if stream {
197
  r.Header.Set("Accept", "text/event-stream")
198
  return
 
18
  sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
19
  log "github.com/sirupsen/logrus"
20
  "github.com/tidwall/sjson"
21
+
22
+ "github.com/gin-gonic/gin"
23
  )
24
 
25
  // ClaudeExecutor is a stateless executor for Anthropic Claude over the messages API.
 
179
  func applyClaudeHeaders(r *http.Request, apiKey string, stream bool) {
180
  r.Header.Set("Authorization", "Bearer "+apiKey)
181
  r.Header.Set("Content-Type", "application/json")
182
+
183
+ var ginHeaders http.Header
184
+ if ginCtx, ok := r.Context().Value("gin").(*gin.Context); ok && ginCtx != nil && ginCtx.Request != nil {
185
+ ginHeaders = ginCtx.Request.Header
186
+ }
187
+
188
+ misc.EnsureHeader(r.Header, ginHeaders, "Anthropic-Version", "2023-06-01")
189
+ misc.EnsureHeader(r.Header, ginHeaders, "Anthropic-Dangerous-Direct-Browser-Access", "true")
190
+ misc.EnsureHeader(r.Header, ginHeaders, "Anthropic-Beta", "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14")
191
+ misc.EnsureHeader(r.Header, ginHeaders, "X-App", "cli")
192
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Helper-Method", "stream")
193
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Retry-Count", "0")
194
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Runtime-Version", "v24.3.0")
195
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Package-Version", "0.55.1")
196
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Runtime", "node")
197
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Lang", "js")
198
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Arch", "arm64")
199
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Os", "MacOS")
200
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Stainless-Timeout", "60")
201
  r.Header.Set("Connection", "keep-alive")
202
  r.Header.Set("User-Agent", "claude-cli/1.0.83 (external, cli)")
203
  r.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
 
 
 
 
 
 
 
 
 
 
204
  if stream {
205
  r.Header.Set("Accept", "text/event-stream")
206
  return
internal/runtime/executor/codex_executor.go CHANGED
@@ -11,6 +11,7 @@ import (
11
 
12
  codexauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/codex"
13
  "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
 
14
  "github.com/router-for-me/CLIProxyAPI/v6/internal/util"
15
  cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
16
  cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
@@ -18,6 +19,7 @@ import (
18
  log "github.com/sirupsen/logrus"
19
  "github.com/tidwall/sjson"
20
 
 
21
  "github.com/google/uuid"
22
  )
23
 
@@ -232,9 +234,16 @@ func (e *CodexExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*
232
  func applyCodexHeaders(r *http.Request, auth *cliproxyauth.Auth, token string) {
233
  r.Header.Set("Content-Type", "application/json")
234
  r.Header.Set("Authorization", "Bearer "+token)
235
- r.Header.Set("Version", "0.21.0")
236
- r.Header.Set("Openai-Beta", "responses=experimental")
237
- r.Header.Set("Session_id", uuid.NewString())
 
 
 
 
 
 
 
238
  r.Header.Set("Accept", "text/event-stream")
239
  r.Header.Set("Connection", "Keep-Alive")
240
 
@@ -248,9 +257,7 @@ func applyCodexHeaders(r *http.Request, auth *cliproxyauth.Auth, token string) {
248
  r.Header.Set("Originator", "codex_cli_rs")
249
  if auth != nil && auth.Metadata != nil {
250
  if accountID, ok := auth.Metadata["account_id"].(string); ok {
251
- if trimmed := strings.TrimSpace(accountID); trimmed != "" {
252
- r.Header.Set("Chatgpt-Account-Id", trimmed)
253
- }
254
  }
255
  }
256
  }
 
11
 
12
  codexauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/codex"
13
  "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
14
+ "github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
15
  "github.com/router-for-me/CLIProxyAPI/v6/internal/util"
16
  cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
17
  cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
 
19
  log "github.com/sirupsen/logrus"
20
  "github.com/tidwall/sjson"
21
 
22
+ "github.com/gin-gonic/gin"
23
  "github.com/google/uuid"
24
  )
25
 
 
234
  func applyCodexHeaders(r *http.Request, auth *cliproxyauth.Auth, token string) {
235
  r.Header.Set("Content-Type", "application/json")
236
  r.Header.Set("Authorization", "Bearer "+token)
237
+
238
+ var ginHeaders http.Header
239
+ if ginCtx, ok := r.Context().Value("gin").(*gin.Context); ok && ginCtx != nil && ginCtx.Request != nil {
240
+ ginHeaders = ginCtx.Request.Header
241
+ }
242
+
243
+ misc.EnsureHeader(r.Header, ginHeaders, "Version", "0.21.0")
244
+ misc.EnsureHeader(r.Header, ginHeaders, "Openai-Beta", "responses=experimental")
245
+ misc.EnsureHeader(r.Header, ginHeaders, "Session_id", uuid.NewString())
246
+
247
  r.Header.Set("Accept", "text/event-stream")
248
  r.Header.Set("Connection", "Keep-Alive")
249
 
 
257
  r.Header.Set("Originator", "codex_cli_rs")
258
  if auth != nil && auth.Metadata != nil {
259
  if accountID, ok := auth.Metadata["account_id"].(string); ok {
260
+ r.Header.Set("Chatgpt-Account-Id", accountID)
 
 
261
  }
262
  }
263
  }
internal/runtime/executor/gemini_cli_executor.go CHANGED
@@ -11,7 +11,9 @@ import (
11
  "strings"
12
  "time"
13
 
 
14
  "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
 
15
  cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
16
  cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
17
  sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
@@ -393,9 +395,14 @@ func stringValue(m map[string]any, key string) string {
393
 
394
  // applyGeminiCLIHeaders sets required headers for the Gemini CLI upstream.
395
  func applyGeminiCLIHeaders(r *http.Request) {
396
- r.Header.Set("User-Agent", "google-api-nodejs-client/9.15.1")
397
- r.Header.Set("X-Goog-Api-Client", "gl-node/22.17.0")
398
- r.Header.Set("Client-Metadata", geminiCLIClientMetadata())
 
 
 
 
 
399
  }
400
 
401
  // geminiCLIClientMetadata returns a compact metadata string required by upstream.
 
11
  "strings"
12
  "time"
13
 
14
+ "github.com/gin-gonic/gin"
15
  "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
16
+ "github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
17
  cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
18
  cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
19
  sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
 
395
 
396
  // applyGeminiCLIHeaders sets required headers for the Gemini CLI upstream.
397
  func applyGeminiCLIHeaders(r *http.Request) {
398
+ var ginHeaders http.Header
399
+ if ginCtx, ok := r.Context().Value("gin").(*gin.Context); ok && ginCtx != nil && ginCtx.Request != nil {
400
+ ginHeaders = ginCtx.Request.Header
401
+ }
402
+
403
+ misc.EnsureHeader(r.Header, ginHeaders, "User-Agent", "google-api-nodejs-client/9.15.1")
404
+ misc.EnsureHeader(r.Header, ginHeaders, "X-Goog-Api-Client", "gl-node/22.17.0")
405
+ misc.EnsureHeader(r.Header, ginHeaders, "Client-Metadata", geminiCLIClientMetadata())
406
  }
407
 
408
  // geminiCLIClientMetadata returns a compact metadata string required by upstream.