luispater commited on
Commit
41556b2
·
unverified ·
1 Parent(s): 2df5ea6

feat(runtime): introduce `newProxyAwareHTTPClient` for enhanced proxy handling

Browse files

- Added `newProxyAwareHTTPClient` to centralize proxy configuration with priority on `auth.ProxyURL` and `cfg.ProxyURL`.
- Integrated enhanced proxy support across executors for HTTP, HTTPS, and SOCKS5 protocols.
- Refactored redundant HTTP client initialization to use `newProxyAwareHTTPClient` for consistent behavior.

internal/runtime/executor/claude_executor.go CHANGED
@@ -61,10 +61,7 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
61
  }
62
  applyClaudeHeaders(httpReq, apiKey, false)
63
 
64
- httpClient := &http.Client{}
65
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
66
- httpClient.Transport = rt
67
- }
68
  resp, err := httpClient.Do(httpReq)
69
  if err != nil {
70
  return cliproxyexecutor.Response{}, err
@@ -130,10 +127,7 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
130
  }
131
  applyClaudeHeaders(httpReq, apiKey, true)
132
 
133
- httpClient := &http.Client{Timeout: 0}
134
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
135
- httpClient.Transport = rt
136
- }
137
  resp, err := httpClient.Do(httpReq)
138
  if err != nil {
139
  return nil, err
@@ -196,10 +190,7 @@ func (e *ClaudeExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut
196
  }
197
  applyClaudeHeaders(httpReq, apiKey, false)
198
 
199
- httpClient := &http.Client{}
200
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
201
- httpClient.Transport = rt
202
- }
203
  resp, err := httpClient.Do(httpReq)
204
  if err != nil {
205
  return cliproxyexecutor.Response{}, err
 
61
  }
62
  applyClaudeHeaders(httpReq, apiKey, false)
63
 
64
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
65
  resp, err := httpClient.Do(httpReq)
66
  if err != nil {
67
  return cliproxyexecutor.Response{}, err
 
127
  }
128
  applyClaudeHeaders(httpReq, apiKey, true)
129
 
130
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
131
  resp, err := httpClient.Do(httpReq)
132
  if err != nil {
133
  return nil, err
 
190
  }
191
  applyClaudeHeaders(httpReq, apiKey, false)
192
 
193
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
194
  resp, err := httpClient.Do(httpReq)
195
  if err != nil {
196
  return cliproxyexecutor.Response{}, err
internal/runtime/executor/codex_executor.go CHANGED
@@ -85,10 +85,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
85
  }
86
  applyCodexHeaders(httpReq, auth, apiKey)
87
 
88
- httpClient := &http.Client{}
89
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
90
- httpClient.Transport = rt
91
- }
92
  resp, err := httpClient.Do(httpReq)
93
  if err != nil {
94
  return cliproxyexecutor.Response{}, err
@@ -172,10 +169,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
172
  }
173
  applyCodexHeaders(httpReq, auth, apiKey)
174
 
175
- httpClient := &http.Client{Timeout: 0}
176
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
177
- httpClient.Transport = rt
178
- }
179
  resp, err := httpClient.Do(httpReq)
180
  if err != nil {
181
  return nil, err
 
85
  }
86
  applyCodexHeaders(httpReq, auth, apiKey)
87
 
88
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
89
  resp, err := httpClient.Do(httpReq)
90
  if err != nil {
91
  return cliproxyexecutor.Response{}, err
 
169
  }
170
  applyCodexHeaders(httpReq, auth, apiKey)
171
 
172
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
173
  resp, err := httpClient.Do(httpReq)
174
  if err != nil {
175
  return nil, err
internal/runtime/executor/gemini_cli_executor.go CHANGED
@@ -74,7 +74,7 @@ func (e *GeminiCLIExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth
74
  models = append([]string{req.Model}, models...)
75
  }
76
 
77
- httpClient := newHTTPClient(ctx, 0)
78
  respCtx := context.WithValue(ctx, "alt", opts.Alt)
79
 
80
  var lastStatus int
@@ -155,7 +155,7 @@ func (e *GeminiCLIExecutor) ExecuteStream(ctx context.Context, auth *cliproxyaut
155
  models = append([]string{req.Model}, models...)
156
  }
157
 
158
- httpClient := newHTTPClient(ctx, 0)
159
  respCtx := context.WithValue(ctx, "alt", opts.Alt)
160
 
161
  var lastStatus int
@@ -281,7 +281,7 @@ func (e *GeminiCLIExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.
281
  models = append([]string{req.Model}, models...)
282
  }
283
 
284
- httpClient := newHTTPClient(ctx, 0)
285
  respCtx := context.WithValue(ctx, "alt", opts.Alt)
286
 
287
  var lastStatus int
@@ -438,15 +438,8 @@ func updateGeminiCLITokenMetadata(auth *cliproxyauth.Auth, base map[string]any,
438
  auth.Metadata["token"] = merged
439
  }
440
 
441
- func newHTTPClient(ctx context.Context, timeout time.Duration) *http.Client {
442
- client := &http.Client{}
443
- if timeout > 0 {
444
- client.Timeout = timeout
445
- }
446
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
447
- client.Transport = rt
448
- }
449
- return client
450
  }
451
 
452
  func cloneMap(in map[string]any) map[string]any {
 
74
  models = append([]string{req.Model}, models...)
75
  }
76
 
77
+ httpClient := newHTTPClient(ctx, e.cfg, auth, 0)
78
  respCtx := context.WithValue(ctx, "alt", opts.Alt)
79
 
80
  var lastStatus int
 
155
  models = append([]string{req.Model}, models...)
156
  }
157
 
158
+ httpClient := newHTTPClient(ctx, e.cfg, auth, 0)
159
  respCtx := context.WithValue(ctx, "alt", opts.Alt)
160
 
161
  var lastStatus int
 
281
  models = append([]string{req.Model}, models...)
282
  }
283
 
284
+ httpClient := newHTTPClient(ctx, e.cfg, auth, 0)
285
  respCtx := context.WithValue(ctx, "alt", opts.Alt)
286
 
287
  var lastStatus int
 
438
  auth.Metadata["token"] = merged
439
  }
440
 
441
+ func newHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) *http.Client {
442
+ return newProxyAwareHTTPClient(ctx, cfg, auth, timeout)
 
 
 
 
 
 
 
443
  }
444
 
445
  func cloneMap(in map[string]any) map[string]any {
internal/runtime/executor/gemini_executor.go CHANGED
@@ -103,10 +103,7 @@ func (e *GeminiExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
103
  httpReq.Header.Set("Authorization", "Bearer "+bearer)
104
  }
105
 
106
- httpClient := &http.Client{}
107
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
108
- httpClient.Transport = rt
109
- }
110
  resp, err := httpClient.Do(httpReq)
111
  if err != nil {
112
  return cliproxyexecutor.Response{}, err
@@ -159,10 +156,7 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
159
  httpReq.Header.Set("Authorization", "Bearer "+bearer)
160
  }
161
 
162
- httpClient := &http.Client{Timeout: 0}
163
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
164
- httpClient.Transport = rt
165
- }
166
  resp, err := httpClient.Do(httpReq)
167
  if err != nil {
168
  return nil, err
@@ -230,10 +224,7 @@ func (e *GeminiExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut
230
  httpReq.Header.Set("Authorization", "Bearer "+bearer)
231
  }
232
 
233
- httpClient := &http.Client{}
234
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
235
- httpClient.Transport = rt
236
- }
237
  resp, err := httpClient.Do(httpReq)
238
  if err != nil {
239
  return cliproxyexecutor.Response{}, err
 
103
  httpReq.Header.Set("Authorization", "Bearer "+bearer)
104
  }
105
 
106
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
107
  resp, err := httpClient.Do(httpReq)
108
  if err != nil {
109
  return cliproxyexecutor.Response{}, err
 
156
  httpReq.Header.Set("Authorization", "Bearer "+bearer)
157
  }
158
 
159
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
160
  resp, err := httpClient.Do(httpReq)
161
  if err != nil {
162
  return nil, err
 
224
  httpReq.Header.Set("Authorization", "Bearer "+bearer)
225
  }
226
 
227
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
228
  resp, err := httpClient.Do(httpReq)
229
  if err != nil {
230
  return cliproxyexecutor.Response{}, err
internal/runtime/executor/openai_compat_executor.go CHANGED
@@ -63,10 +63,7 @@ func (e *OpenAICompatExecutor) Execute(ctx context.Context, auth *cliproxyauth.A
63
  httpReq.Header.Set("Authorization", "Bearer "+apiKey)
64
  httpReq.Header.Set("User-Agent", "cli-proxy-openai-compat")
65
 
66
- httpClient := &http.Client{}
67
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
68
- httpClient.Transport = rt
69
- }
70
  resp, err := httpClient.Do(httpReq)
71
  if err != nil {
72
  return cliproxyexecutor.Response{}, err
@@ -115,10 +112,7 @@ func (e *OpenAICompatExecutor) ExecuteStream(ctx context.Context, auth *cliproxy
115
  httpReq.Header.Set("Accept", "text/event-stream")
116
  httpReq.Header.Set("Cache-Control", "no-cache")
117
 
118
- httpClient := &http.Client{Timeout: 0}
119
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
120
- httpClient.Transport = rt
121
- }
122
  resp, err := httpClient.Do(httpReq)
123
  if err != nil {
124
  return nil, err
 
63
  httpReq.Header.Set("Authorization", "Bearer "+apiKey)
64
  httpReq.Header.Set("User-Agent", "cli-proxy-openai-compat")
65
 
66
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
67
  resp, err := httpClient.Do(httpReq)
68
  if err != nil {
69
  return cliproxyexecutor.Response{}, err
 
112
  httpReq.Header.Set("Accept", "text/event-stream")
113
  httpReq.Header.Set("Cache-Control", "no-cache")
114
 
115
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
116
  resp, err := httpClient.Do(httpReq)
117
  if err != nil {
118
  return nil, err
internal/runtime/executor/proxy_helpers.go ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package executor
2
+
3
+ import (
4
+ "context"
5
+ "net"
6
+ "net/http"
7
+ "net/url"
8
+ "strings"
9
+ "time"
10
+
11
+ "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
12
+ cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
13
+ log "github.com/sirupsen/logrus"
14
+ "golang.org/x/net/proxy"
15
+ )
16
+
17
+ // newProxyAwareHTTPClient creates an HTTP client with proper proxy configuration priority:
18
+ // 1. Use auth.ProxyURL if configured (highest priority)
19
+ // 2. Use cfg.ProxyURL if auth proxy is not configured
20
+ // 3. Use RoundTripper from context if neither are configured
21
+ //
22
+ // Parameters:
23
+ // - ctx: The context containing optional RoundTripper
24
+ // - cfg: The application configuration
25
+ // - auth: The authentication information
26
+ // - timeout: The client timeout (0 means no timeout)
27
+ //
28
+ // Returns:
29
+ // - *http.Client: An HTTP client with configured proxy or transport
30
+ func newProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) *http.Client {
31
+ httpClient := &http.Client{}
32
+ if timeout > 0 {
33
+ httpClient.Timeout = timeout
34
+ }
35
+
36
+ // Priority 1: Use auth.ProxyURL if configured
37
+ var proxyURL string
38
+ if auth != nil {
39
+ proxyURL = strings.TrimSpace(auth.ProxyURL)
40
+ }
41
+
42
+ // Priority 2: Use cfg.ProxyURL if auth proxy is not configured
43
+ if proxyURL == "" && cfg != nil {
44
+ proxyURL = strings.TrimSpace(cfg.ProxyURL)
45
+ }
46
+
47
+ // If we have a proxy URL configured, set up the transport
48
+ if proxyURL != "" {
49
+ transport := buildProxyTransport(proxyURL)
50
+ if transport != nil {
51
+ httpClient.Transport = transport
52
+ return httpClient
53
+ }
54
+ // If proxy setup failed, log and fall through to context RoundTripper
55
+ log.Debugf("failed to setup proxy from URL: %s, falling back to context transport", proxyURL)
56
+ }
57
+
58
+ // Priority 3: Use RoundTripper from context (typically from RoundTripperFor)
59
+ if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
60
+ httpClient.Transport = rt
61
+ }
62
+
63
+ return httpClient
64
+ }
65
+
66
+ // buildProxyTransport creates an HTTP transport configured for the given proxy URL.
67
+ // It supports SOCKS5, HTTP, and HTTPS proxy protocols.
68
+ //
69
+ // Parameters:
70
+ // - proxyURL: The proxy URL string (e.g., "socks5://user:pass@host:port", "http://host:port")
71
+ //
72
+ // Returns:
73
+ // - *http.Transport: A configured transport, or nil if the proxy URL is invalid
74
+ func buildProxyTransport(proxyURL string) *http.Transport {
75
+ if proxyURL == "" {
76
+ return nil
77
+ }
78
+
79
+ parsedURL, errParse := url.Parse(proxyURL)
80
+ if errParse != nil {
81
+ log.Errorf("parse proxy URL failed: %v", errParse)
82
+ return nil
83
+ }
84
+
85
+ var transport *http.Transport
86
+
87
+ // Handle different proxy schemes
88
+ if parsedURL.Scheme == "socks5" {
89
+ // Configure SOCKS5 proxy with optional authentication
90
+ username := parsedURL.User.Username()
91
+ password, _ := parsedURL.User.Password()
92
+ proxyAuth := &proxy.Auth{User: username, Password: password}
93
+ dialer, errSOCKS5 := proxy.SOCKS5("tcp", parsedURL.Host, proxyAuth, proxy.Direct)
94
+ if errSOCKS5 != nil {
95
+ log.Errorf("create SOCKS5 dialer failed: %v", errSOCKS5)
96
+ return nil
97
+ }
98
+ // Set up a custom transport using the SOCKS5 dialer
99
+ transport = &http.Transport{
100
+ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
101
+ return dialer.Dial(network, addr)
102
+ },
103
+ }
104
+ } else if parsedURL.Scheme == "http" || parsedURL.Scheme == "https" {
105
+ // Configure HTTP or HTTPS proxy
106
+ transport = &http.Transport{Proxy: http.ProxyURL(parsedURL)}
107
+ } else {
108
+ log.Errorf("unsupported proxy scheme: %s", parsedURL.Scheme)
109
+ return nil
110
+ }
111
+
112
+ return transport
113
+ }
internal/runtime/executor/qwen_executor.go CHANGED
@@ -58,10 +58,7 @@ func (e *QwenExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req
58
  }
59
  applyQwenHeaders(httpReq, token, false)
60
 
61
- httpClient := &http.Client{}
62
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
63
- httpClient.Transport = rt
64
- }
65
  resp, err := httpClient.Do(httpReq)
66
  if err != nil {
67
  return cliproxyexecutor.Response{}, err
@@ -112,10 +109,7 @@ func (e *QwenExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Aut
112
  }
113
  applyQwenHeaders(httpReq, token, true)
114
 
115
- httpClient := &http.Client{Timeout: 0}
116
- if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
117
- httpClient.Transport = rt
118
- }
119
  resp, err := httpClient.Do(httpReq)
120
  if err != nil {
121
  return nil, err
 
58
  }
59
  applyQwenHeaders(httpReq, token, false)
60
 
61
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
62
  resp, err := httpClient.Do(httpReq)
63
  if err != nil {
64
  return cliproxyexecutor.Response{}, err
 
109
  }
110
  applyQwenHeaders(httpReq, token, true)
111
 
112
+ httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
 
 
 
113
  resp, err := httpClient.Do(httpReq)
114
  if err != nil {
115
  return nil, err