diff --git a/.github/workflows/edgee-release.yml b/.github/workflows/edgee-release.yml index c5bf8b4d8e9..eb0d7aac3bd 100644 --- a/.github/workflows/edgee-release.yml +++ b/.github/workflows/edgee-release.yml @@ -28,6 +28,7 @@ jobs: set -euo pipefail go test \ ./cmd/server \ + ./internal/auth/claude \ ./internal/config \ ./internal/logging \ ./internal/runtime/executor \ @@ -36,7 +37,8 @@ jobs: ./sdk/api/handlers \ ./sdk/api/handlers/claude \ ./sdk/cliproxy \ - ./sdk/cliproxy/auth + ./sdk/cliproxy/auth \ + ./sdk/proxyutil go test ./internal/api -run '^TestCodexOAuthRoute' -count=1 - name: Race-test concurrency and route isolation run: >- @@ -46,6 +48,14 @@ jobs: ./internal/api/handlers/management ./internal/runtime/executor ./sdk/cliproxy/auth + - name: Race-test shared proxy connections + run: >- + go test -race -count=1 + -run 'Test(UtlsClient|InvalidProxyURL|ClientsForTheSameProxy|Connect|RetryBackoff|ReviewNotEstablishedConnIsReused|EquivalentProxySettings|NewClaudeAuth|PatchAuthFileFields|ConfigHandlersRejectInvalidProxyURL|UploadAuthFileRejectsBadProxyURL)' + ./internal/api/handlers/management + ./internal/auth/claude + ./internal/runtime/executor/helps + ./sdk/proxyutil build-windows-arm64: needs: test-gate diff --git a/Dockerfile b/Dockerfile index 8a9552988ed..54dabcde2cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ RUN go mod download COPY . . -ARG VERSION=7.2.62+edgee.7 +ARG VERSION=7.2.62+edgee.8 ARG COMMIT=none ARG BUILD_DATE=unknown diff --git a/README.md b/README.md index 9579dcf3972..ea03aab619a 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ English | [中文](README_CN.md) | [日本語](README_JA.md) A proxy server that provides OpenAI/Gemini/Claude/Codex/Grok compatible API interfaces for CLI. > [!NOTE] -> **This is the `claude-all` edgee fork.** It adds opt-in per-request routing through `codex-oauth-base-url` for selected inbound API keys, bumps the version string to `7.2.62+edgee.7`, and ships a pinned windows/arm64 release via `.github/workflows/edgee-release.yml` (upstream tag-triggered release workflows are disabled here). Edgee-routed non-streaming Codex calls return the upstream's real terminal error, or, for Claude-format requests, the partial reply for `response.incomplete`, instead of stock's 408; unmarked requests are unchanged. The closed upstream [PR #4258](https://github.com/router-for-me/CLIProxyAPI/pull/4258) contains only the older v1 change and is not the complete current patch series. +> **This is the `claude-all` edgee fork.** It adds opt-in per-request routing through `codex-oauth-base-url` for selected inbound API keys, bumps the version string to `7.2.62+edgee.8`, and ships a pinned windows/arm64 release via `.github/workflows/edgee-release.yml` (upstream tag-triggered release workflows are disabled here). Edgee-routed non-streaming Codex calls return the upstream's real terminal error, or, for Claude-format requests, the partial reply for `response.incomplete`, instead of stock's 408; unmarked requests are unchanged. Proxied requests share one connection pool per proxy URL and account (HTTP/2 for uTLS hosts), and a malformed proxy-url fails loudly at config load and on every request instead of going direct. The closed upstream [PR #4258](https://github.com/router-for-me/CLIProxyAPI/pull/4258) contains only the older v1 change and is not the complete current patch series. > -> **Rebase procedure** (adopting a newer upstream version): `git fetch upstream --tags`, branch from the new upstream tag, then cherry-pick both feature commits in order: `a0d9081c7d8dbd0095788bccc178cfe75ff2ad0f` followed by `4e23ea9598c7ec1660e83097b5ca6149cf7f24ec`. Apply the edgee.3 repair series as the complete range `git cherry-pick 24d7b959681fcfed91cfdc0a4aa9b147e94dcbc9..v7.2.62-edgee.7`; do not substitute PR #4258 for either feature commit or omit any commit in that range. Cherry-pick the release-workflow/documentation commits as needed, resolve conflicts, run the release workflow's test gate, bump the fork version string to `+edgee.N` (current: `7.2.62+edgee.7`), tag `v-edgee.N`, let `edgee-release.yml` publish, then update the minimum version, release tag, and both SHA pins in claude-all's `scripts/claude-all.ps1` (the sole pin location). +> **Rebase procedure** (adopting a newer upstream version): `git fetch upstream --tags`, branch from the new upstream tag, then cherry-pick both feature commits in order: `a0d9081c7d8dbd0095788bccc178cfe75ff2ad0f` followed by `4e23ea9598c7ec1660e83097b5ca6149cf7f24ec`. Apply the edgee.3 repair series as the complete range `git cherry-pick 24d7b959681fcfed91cfdc0a4aa9b147e94dcbc9..v7.2.62-edgee.8`; do not substitute PR #4258 for either feature commit or omit any commit in that range. Cherry-pick the release-workflow/documentation commits as needed, resolve conflicts, run the release workflow's test gate, bump the fork version string to `+edgee.N` (current: `7.2.62+edgee.8`), tag `v-edgee.N`, let `edgee-release.yml` publish, then update the minimum version, release tag, and both SHA pins in claude-all's `scripts/claude-all.ps1` (the sole pin location). > > **Un-fork / return to upstream** (once the PR lands in an official release): point claude-all's edgee-mode download back at the upstream release asset, restore the upstream version-probe regex, replace both fork SHA pins with the upstream archive/binary hashes, and archive this repository. diff --git a/cmd/server/main.go b/cmd/server/main.go index 151c90cac89..f4aea61244d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -41,7 +41,7 @@ import ( ) var ( - Version = "7.2.62+edgee.7" + Version = "7.2.62+edgee.8" Commit = "none" BuildDate = "unknown" DefaultConfigPath = "" diff --git a/cmd/server/version_test.go b/cmd/server/version_test.go index 96ba5efa36f..b9aa734c22a 100644 --- a/cmd/server/version_test.go +++ b/cmd/server/version_test.go @@ -3,7 +3,7 @@ package main import "testing" func TestEmbeddedVersion(t *testing.T) { - if Version != "7.2.62+edgee.7" { - t.Fatalf("Version = %q, want 7.2.62+edgee.7", Version) + if Version != "7.2.62+edgee.8" { + t.Fatalf("Version = %q, want 7.2.62+edgee.8", Version) } } diff --git a/docker-compose.yml b/docker-compose.yml index 160d924454f..4df66049a2a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,7 @@ services: context: . dockerfile: Dockerfile args: - VERSION: ${VERSION:-7.2.62+edgee.7} + VERSION: ${VERSION:-7.2.62+edgee.8} COMMIT: ${COMMIT:-none} BUILD_DATE: ${BUILD_DATE:-unknown} container_name: cli-proxy-api diff --git a/internal/api/handlers/management/auth_files.go b/internal/api/handlers/management/auth_files.go index ecbdf8514b9..90ad3d6217e 100644 --- a/internal/api/handlers/management/auth_files.go +++ b/internal/api/handlers/management/auth_files.go @@ -60,12 +60,13 @@ type codexOAuthService interface { } var ( - callbackForwardersMu sync.Mutex - callbackForwarders = make(map[int]*callbackForwarder) - errAuthFileMustBeJSON = errors.New("auth file must be .json") - errAuthFileNotFound = errors.New("auth file not found") - errPluginVirtualAuth = errors.New("plugin virtual auth cannot be modified directly; edit or delete the source auth file") - newCodexOAuthService = func(cfg *config.Config) codexOAuthService { return codex.NewCodexAuth(cfg) } + callbackForwardersMu sync.Mutex + callbackForwarders = make(map[int]*callbackForwarder) + errAuthFileMustBeJSON = errors.New("auth file must be .json") + errAuthFileInvalidProxy = errors.New("auth file has an invalid proxy_url") + errAuthFileNotFound = errors.New("auth file not found") + errPluginVirtualAuth = errors.New("plugin virtual auth cannot be modified directly; edit or delete the source auth file") + newCodexOAuthService = func(cfg *config.Config) codexOAuthService { return codex.NewCodexAuth(cfg) } ) func extractLastRefreshTimestamp(meta map[string]any) (time.Time, bool) { @@ -751,6 +752,10 @@ func (h *Handler) UploadAuthFile(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": "file must be .json"}) return } + if errors.Is(errUpload, errAuthFileInvalidProxy) { + c.JSON(http.StatusBadRequest, gin.H{"error": errUpload.Error()}) + return + } c.JSON(http.StatusInternalServerError, gin.H{"error": errUpload.Error()}) return } @@ -807,7 +812,11 @@ func (h *Handler) UploadAuthFile(c *gin.Context) { return } if err = h.writeAuthFile(ctx, filepath.Base(name), data); err != nil { - c.JSON(500, gin.H{"error": err.Error()}) + status := 500 + if errors.Is(err, errAuthFileInvalidProxy) { + status = 400 + } + c.JSON(status, gin.H{"error": err.Error()}) return } c.JSON(200, gin.H{"status": "ok"}) @@ -1161,6 +1170,11 @@ func (h *Handler) buildAuthFromFileData(path string, data []byte) (*coreauth.Aut if err := json.Unmarshal(data, &metadata); err != nil { return nil, fmt.Errorf("invalid auth file: %w", err) } + if value, ok := metadata["proxy_url"]; ok { + if errProxy := validateAuthFileProxyURL("proxy_url", value); errProxy != nil { + return nil, fmt.Errorf("%w: %v", errAuthFileInvalidProxy, errProxy) + } + } provider, _ := metadata["type"].(string) if provider == "" { provider = "unknown" @@ -1481,8 +1495,12 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { return } - changed := false - touchedRoots := make(map[string]struct{}, len(req)) + // Validate the whole patch before touching anything, so a rejected patch changes nothing. + type fieldPatch struct { + path string + value any + } + patches := make([]fieldPatch, 0, len(req)) for key, rawValue := range req { fieldPath := strings.TrimSpace(key) if fieldPath == "" { @@ -1494,17 +1512,30 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid field %s", fieldPath)}) return } - if targetAuth.Metadata == nil { - targetAuth.Metadata = make(map[string]any) + if rootAuthFileField(fieldPath) == "proxy_url" { + if errProxy := validateAuthFileProxyURL(fieldPath, value); errProxy != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": errProxy.Error()}) + return + } } + patches = append(patches, fieldPatch{path: fieldPath, value: value}) + } - if fieldPath == "headers" { - applyAuthFileHeadersPatch(targetAuth, value) - } else if errSet := setAuthFileMetadataValue(targetAuth.Metadata, fieldPath, value); errSet != nil { + // GetByID clones only the top level, so detach nested metadata from the live record. + targetAuth.Metadata = cloneAuthMetadata(targetAuth.Metadata) + if targetAuth.Metadata == nil { + targetAuth.Metadata = make(map[string]any) + } + changed := false + touchedRoots := make(map[string]struct{}, len(patches)) + for _, patch := range patches { + if patch.path == "headers" { + applyAuthFileHeadersPatch(targetAuth, patch.value) + } else if errSet := setAuthFileMetadataValue(targetAuth.Metadata, patch.path, patch.value); errSet != nil { c.JSON(http.StatusBadRequest, gin.H{"error": errSet.Error()}) return } - if root := rootAuthFileField(fieldPath); root != "" { + if root := rootAuthFileField(patch.path); root != "" { touchedRoots[root] = struct{}{} } changed = true @@ -1528,6 +1559,55 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "ok"}) } +// validateAuthFileProxyURL requires proxy_url to be set whole, as a string that is a usable proxy setting. +func validateAuthFileProxyURL(fieldPath string, value any) error { + if fieldPath != "proxy_url" { + return fmt.Errorf("%s: proxy_url must be set as a whole string", fieldPath) + } + if value == nil { + return nil + } + raw, isString := value.(string) + if !isString { + return fmt.Errorf("proxy_url must be a string") + } + return config.ValidateProxyURL("proxy_url", raw) +} + +func cloneAuthMetadata(metadata map[string]any) map[string]any { + if metadata == nil { + return nil + } + out := make(map[string]any, len(metadata)) + for key, value := range metadata { + out[key] = cloneAuthMetadataValue(value) + } + return out +} + +func cloneAuthMetadataValue(value any) any { + switch typed := value.(type) { + case map[string]any: + return cloneAuthMetadata(typed) + case map[string]string: + out := make(map[string]string, len(typed)) + for key, item := range typed { + out[key] = item + } + return out + case []any: + out := make([]any, len(typed)) + for i, item := range typed { + out[i] = cloneAuthMetadataValue(item) + } + return out + case []string: + return append([]string(nil), typed...) + default: + return value + } +} + func decodeAuthFileFieldValue(raw json.RawMessage) (any, error) { decoder := json.NewDecoder(bytes.NewReader(raw)) decoder.UseNumber() @@ -1645,9 +1725,9 @@ func syncAuthFileMetadataFields(auth *coreauth.Auth, touchedRoots map[string]str } } if _, ok := touchedRoots["proxy_url"]; ok { - if proxyURL, okString := auth.Metadata["proxy_url"].(string); okString { - auth.ProxyURL = strings.TrimSpace(proxyURL) - } + // A null or removed proxy_url clears the live setting, matching the saved file. + proxyURL, _ := auth.Metadata["proxy_url"].(string) + auth.ProxyURL = strings.TrimSpace(proxyURL) } if _, ok := touchedRoots["headers"]; ok { syncAuthFileHeaderAttributes(auth) diff --git a/internal/api/handlers/management/config_basic.go b/internal/api/handlers/management/config_basic.go index a0818aa8aeb..56cc794d081 100644 --- a/internal/api/handlers/management/config_basic.go +++ b/internal/api/handlers/management/config_basic.go @@ -320,7 +320,18 @@ func (h *Handler) PutRoutingStrategy(c *gin.Context) { // Proxy URL func (h *Handler) GetProxyURL(c *gin.Context) { c.JSON(200, gin.H{"proxy-url": h.cfg.ProxyURL}) } func (h *Handler) PutProxyURL(c *gin.Context) { - h.updateStringField(c, func(v string) { h.cfg.ProxyURL = v }) + var body struct { + Value *string `json:"value"` + } + if err := c.ShouldBindJSON(&body); err != nil || body.Value == nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + if rejectInvalidProxyURL(c, "proxy-url", *body.Value) { + return + } + h.cfg.ProxyURL = *body.Value + h.persist(c) } func (h *Handler) DeleteProxyURL(c *gin.Context) { h.cfg.ProxyURL = "" diff --git a/internal/api/handlers/management/config_lists.go b/internal/api/handlers/management/config_lists.go index 03476e63f8b..3e4cd4e3d34 100644 --- a/internal/api/handlers/management/config_lists.go +++ b/internal/api/handlers/management/config_lists.go @@ -139,6 +139,9 @@ func (h *Handler) PutGeminiKeys(c *gin.Context) { } arr = obj.Items } + if rejectInvalidProxyURLs(c, &config.Config{GeminiKey: arr}) { + return + } h.mu.Lock() defer h.mu.Unlock() h.cfg.GeminiKey = append([]config.GeminiKey(nil), arr...) @@ -204,6 +207,9 @@ func (h *Handler) PatchGeminiKey(c *gin.Context) { entry.BaseURL = strings.TrimSpace(*body.Value.BaseURL) } if body.Value.ProxyURL != nil { + if rejectInvalidProxyURL(c, "proxy-url", *body.Value.ProxyURL) { + return + } entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL) } if body.Value.Headers != nil { @@ -298,6 +304,9 @@ func (h *Handler) PutInteractionsKeys(c *gin.Context) { } arr = obj.Items } + if rejectInvalidProxyURLs(c, &config.Config{InteractionsKey: arr}) { + return + } h.mu.Lock() defer h.mu.Unlock() h.cfg.InteractionsKey = append([]config.GeminiKey(nil), arr...) @@ -364,6 +373,9 @@ func (h *Handler) PatchInteractionsKey(c *gin.Context) { entry.BaseURL = strings.TrimSpace(*body.Value.BaseURL) } if body.Value.ProxyURL != nil { + if rejectInvalidProxyURL(c, "proxy-url", *body.Value.ProxyURL) { + return + } entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL) } if body.Value.Headers != nil { @@ -457,6 +469,9 @@ func (h *Handler) PutClaudeKeys(c *gin.Context) { } arr = obj.Items } + if rejectInvalidProxyURLs(c, &config.Config{ClaudeKey: arr}) { + return + } for i := range arr { normalizeClaudeKey(&arr[i]) } @@ -518,6 +533,9 @@ func (h *Handler) PatchClaudeKey(c *gin.Context) { entry.BaseURL = strings.TrimSpace(*body.Value.BaseURL) } if body.Value.ProxyURL != nil { + if rejectInvalidProxyURL(c, "proxy-url", *body.Value.ProxyURL) { + return + } entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL) } if body.Value.Models != nil { @@ -612,6 +630,9 @@ func (h *Handler) PutOpenAICompat(c *gin.Context) { } arr = obj.Items } + if rejectInvalidProxyURLs(c, &config.Config{OpenAICompatibility: arr}) { + return + } filtered := make([]config.OpenAICompatibility, 0, len(arr)) for i := range arr { normalizeOpenAICompatibilityEntry(&arr[i]) @@ -690,6 +711,9 @@ func (h *Handler) PatchOpenAICompat(c *gin.Context) { entry.BaseURL = trimmed } if body.Value.APIKeyEntries != nil { + if rejectInvalidProxyURLs(c, &config.Config{OpenAICompatibility: []config.OpenAICompatibility{{APIKeyEntries: *body.Value.APIKeyEntries}}}) { + return + } entry.APIKeyEntries = append([]config.OpenAICompatibilityAPIKey(nil), (*body.Value.APIKeyEntries)...) } if body.Value.Models != nil { @@ -753,6 +777,9 @@ func (h *Handler) PutVertexCompatKeys(c *gin.Context) { } arr = obj.Items } + if rejectInvalidProxyURLs(c, &config.Config{VertexCompatAPIKey: arr}) { + return + } for i := range arr { normalizeVertexCompatKey(&arr[i]) if arr[i].APIKey == "" { @@ -833,6 +860,9 @@ func (h *Handler) PatchVertexCompatKey(c *gin.Context) { entry.BaseURL = trimmed } if body.Value.ProxyURL != nil { + if rejectInvalidProxyURL(c, "proxy-url", *body.Value.ProxyURL) { + return + } entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL) } if body.Value.Headers != nil { @@ -1106,6 +1136,9 @@ func (h *Handler) PutCodexKeys(c *gin.Context) { } arr = obj.Items } + if rejectInvalidProxyURLs(c, &config.Config{CodexKey: arr}) { + return + } // Filter out codex entries with empty base-url (treat as removed) filtered := make([]config.CodexKey, 0, len(arr)) for i := range arr { @@ -1180,6 +1213,9 @@ func (h *Handler) PatchCodexKey(c *gin.Context) { entry.BaseURL = trimmed } if body.Value.ProxyURL != nil { + if rejectInvalidProxyURL(c, "proxy-url", *body.Value.ProxyURL) { + return + } entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL) } if body.Value.Models != nil { @@ -1381,3 +1417,21 @@ func sanitizedOAuthModelAlias(entries map[string][]config.OAuthModelAlias) map[s } return cfg.OAuthModelAlias } + +// rejectInvalidProxyURLs answers 400 and returns true when any proxy-url in candidate is malformed. +func rejectInvalidProxyURLs(c *gin.Context, candidate *config.Config) bool { + if err := candidate.ValidateProxyURLs(); err != nil { + c.JSON(400, gin.H{"error": err.Error()}) + return true + } + return false +} + +// rejectInvalidProxyURL answers 400 and returns true when raw is not a usable proxy setting. +func rejectInvalidProxyURL(c *gin.Context, field, raw string) bool { + if err := config.ValidateProxyURL(field, raw); err != nil { + c.JSON(400, gin.H{"error": err.Error()}) + return true + } + return false +} diff --git a/internal/api/handlers/management/proxy_url_validation_test.go b/internal/api/handlers/management/proxy_url_validation_test.go new file mode 100644 index 00000000000..b1131f05a02 --- /dev/null +++ b/internal/api/handlers/management/proxy_url_validation_test.go @@ -0,0 +1,227 @@ +package management + +import ( + "bytes" + "context" + "mime/multipart" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + + "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" + coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" +) + +const goodProxy = "http://proxy.local:8080" + +func proxyTestConfig() *config.Config { + return &config.Config{ + GeminiKey: []config.GeminiKey{{APIKey: "k", ProxyURL: goodProxy}}, + InteractionsKey: []config.GeminiKey{{APIKey: "k", ProxyURL: goodProxy}}, + ClaudeKey: []config.ClaudeKey{{APIKey: "k", ProxyURL: goodProxy}}, + CodexKey: []config.CodexKey{{APIKey: "k", BaseURL: "https://codex.example.com", ProxyURL: goodProxy}}, + VertexCompatAPIKey: []config.VertexCompatKey{{APIKey: "k", BaseURL: "https://vertex.example.com", ProxyURL: goodProxy}}, + OpenAICompatibility: []config.OpenAICompatibility{{ + Name: "compat", + BaseURL: "https://compat.example.com", + APIKeyEntries: []config.OpenAICompatibilityAPIKey{{APIKey: "k", ProxyURL: goodProxy}}, + }}, + } +} + +func serveJSON(handler gin.HandlerFunc, method, body string) *httptest.ResponseRecorder { + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + ctx.Request = httptest.NewRequest(method, "/", strings.NewReader(body)) + ctx.Request.Header.Set("Content-Type", "application/json") + handler(ctx) + return rec +} + +// Every config handler that takes a proxy URL answers 400 for a bad one and changes neither the config nor its file. +func TestConfigHandlersRejectInvalidProxyURLWithoutChanges(t *testing.T) { + gin.SetMode(gin.TestMode) + bad := []string{"ftp://p:21", "http://:8080", "http://p:99999", "socks5://p"} + type handlerCase struct { + name string + method string + call func(*Handler) gin.HandlerFunc + body string // %s is the bad proxy URL + } + cases := []handlerCase{ + {"PutProxyURL", http.MethodPut, func(h *Handler) gin.HandlerFunc { return h.PutProxyURL }, `{"value":"%s"}`}, + {"PutGeminiKeys", http.MethodPut, func(h *Handler) gin.HandlerFunc { return h.PutGeminiKeys }, `[{"api-key":"k","proxy-url":"%s"}]`}, + {"PutInteractionsKeys", http.MethodPut, func(h *Handler) gin.HandlerFunc { return h.PutInteractionsKeys }, `[{"api-key":"k","proxy-url":"%s"}]`}, + {"PutClaudeKeys", http.MethodPut, func(h *Handler) gin.HandlerFunc { return h.PutClaudeKeys }, `[{"api-key":"k","proxy-url":"%s"}]`}, + {"PutCodexKeys", http.MethodPut, func(h *Handler) gin.HandlerFunc { return h.PutCodexKeys }, `[{"api-key":"k","base-url":"https://c.example.com","proxy-url":"%s"}]`}, + {"PutVertexCompatKeys", http.MethodPut, func(h *Handler) gin.HandlerFunc { return h.PutVertexCompatKeys }, `[{"api-key":"k","base-url":"https://v.example.com","proxy-url":"%s"}]`}, + {"PutOpenAICompat", http.MethodPut, func(h *Handler) gin.HandlerFunc { return h.PutOpenAICompat }, `[{"name":"compat","base-url":"https://c.example.com","api-key-entries":[{"api-key":"k","proxy-url":"%s"}]}]`}, + {"PatchGeminiKey", http.MethodPatch, func(h *Handler) gin.HandlerFunc { return h.PatchGeminiKey }, `{"index":0,"value":{"prefix":"changed","proxy-url":"%s"}}`}, + {"PatchInteractionsKey", http.MethodPatch, func(h *Handler) gin.HandlerFunc { return h.PatchInteractionsKey }, `{"index":0,"value":{"prefix":"changed","proxy-url":"%s"}}`}, + {"PatchClaudeKey", http.MethodPatch, func(h *Handler) gin.HandlerFunc { return h.PatchClaudeKey }, `{"index":0,"value":{"prefix":"changed","proxy-url":"%s"}}`}, + {"PatchCodexKey", http.MethodPatch, func(h *Handler) gin.HandlerFunc { return h.PatchCodexKey }, `{"index":0,"value":{"prefix":"changed","proxy-url":"%s"}}`}, + {"PatchVertexCompatKey", http.MethodPatch, func(h *Handler) gin.HandlerFunc { return h.PatchVertexCompatKey }, `{"index":0,"value":{"prefix":"changed","proxy-url":"%s"}}`}, + {"PatchOpenAICompat", http.MethodPatch, func(h *Handler) gin.HandlerFunc { return h.PatchOpenAICompat }, `{"index":0,"value":{"prefix":"changed","api-key-entries":[{"api-key":"k","proxy-url":"%s"}]}}`}, + } + for _, tc := range cases { + for _, proxyURL := range bad { + t.Run(tc.name+" "+proxyURL, func(t *testing.T) { + path := writeTestConfigFile(t) + h := &Handler{cfg: proxyTestConfig(), configFilePath: path} + fileBefore, _ := os.ReadFile(path) + + rec := serveJSON(tc.call(h), tc.method, strings.Replace(tc.body, "%s", proxyURL, 1)) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want 400; body=%s", rec.Code, rec.Body.String()) + } + if !reflect.DeepEqual(h.cfg, proxyTestConfig()) { + t.Fatal("config changed by a rejected request") + } + if fileAfter, _ := os.ReadFile(path); !bytes.Equal(fileBefore, fileAfter) { + t.Fatal("config file changed by a rejected request") + } + }) + } + } +} + +func TestPatchAuthFileFieldsRejectsBadProxyURLWithoutChanges(t *testing.T) { + t.Setenv("MANAGEMENT_PASSWORD", "") + gin.SetMode(gin.TestMode) + bodies := map[string]string{ + "bad scheme": `{"name":"a.json","prefix":"changed","nested.value":"changed","proxy_url":"ftp://p:21"}`, + "no hostname": `{"name":"a.json","nested.value":"changed","proxy_url":"http://:8080"}`, + "number": `{"name":"a.json","nested.value":"changed","proxy_url":123}`, + "bool": `{"name":"a.json","nested.value":"changed","proxy_url":false}`, + "object": `{"name":"a.json","nested.value":"changed","proxy_url":{"host":"p"}}`, + "nested write": `{"name":"a.json","nested.value":"changed","proxy_url.host":"broken"}`, + } + for name, body := range bodies { + t.Run(name, func(t *testing.T) { + manager := coreauth.NewManager(&memoryAuthStore{}, nil, nil) + record := &coreauth.Auth{ + ID: "a.json", + FileName: "a.json", + Provider: "codex", + Metadata: map[string]any{"type": "codex", "proxy_url": goodProxy, "nested": map[string]any{"value": "old"}}, + } + if _, err := manager.Register(context.Background(), record); err != nil { + t.Fatal(err) + } + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager) + + rec := serveJSON(h.PatchAuthFileFields, http.MethodPatch, body) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want 400; body=%s", rec.Code, rec.Body.String()) + } + live, _ := manager.GetByID("a.json") + if got := live.Metadata["proxy_url"]; got != goodProxy { + t.Fatalf("proxy_url = %#v, want unchanged", got) + } + if got := live.Metadata["nested"].(map[string]any)["value"]; got != "old" { + t.Fatalf("nested.value = %#v, want unchanged", got) + } + if _, ok := live.Metadata["prefix"]; ok { + t.Fatal("prefix applied by a rejected patch") + } + }) + } +} + +func TestUploadAuthFileRejectsBadProxyURL(t *testing.T) { + t.Setenv("MANAGEMENT_PASSWORD", "") + gin.SetMode(gin.TestMode) + for _, proxyField := range []string{`"ftp://p:21"`, `123`, `"socks5://p"`} { + t.Run(proxyField, func(t *testing.T) { + authDir := t.TempDir() + manager := coreauth.NewManager(nil, nil, nil) + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, manager) + + var body bytes.Buffer + writer := multipart.NewWriter(&body) + part, err := writer.CreateFormFile("file", "codex-a.json") + if err != nil { + t.Fatal(err) + } + _, _ = part.Write([]byte(`{"type":"codex","proxy_url":` + proxyField + `}`)) + _ = writer.Close() + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + ctx.Request = httptest.NewRequest(http.MethodPost, "/", &body) + ctx.Request.Header.Set("Content-Type", writer.FormDataContentType()) + + h.UploadAuthFile(ctx) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want 400; body=%s", rec.Code, rec.Body.String()) + } + if _, errStat := os.Stat(filepath.Join(authDir, "codex-a.json")); !os.IsNotExist(errStat) { + t.Fatalf("auth file written despite the bad proxy: %v", errStat) + } + if _, ok := manager.GetByID("codex-a.json"); ok { + t.Fatal("auth registered despite the bad proxy") + } + }) + } +} + +func registerPatchTarget(t *testing.T) (*coreauth.Manager, *Handler) { + t.Helper() + manager := coreauth.NewManager(&memoryAuthStore{}, nil, nil) + record := &coreauth.Auth{ + ID: "a.json", + FileName: "a.json", + Provider: "codex", + ProxyURL: goodProxy, + Metadata: map[string]any{"type": "codex", "proxy_url": goodProxy, "nested": map[string]any{"value": "old"}}, + } + if _, err := manager.Register(context.Background(), record); err != nil { + t.Fatal(err) + } + return manager, NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager) +} + +// A patch that fails partway through applying leaves the live record untouched, whatever order its fields apply in. +func TestPatchAuthFileFieldsFailingMidApplyLeavesLiveRecordUnchanged(t *testing.T) { + t.Setenv("MANAGEMENT_PASSWORD", "") + gin.SetMode(gin.TestMode) + for i := 0; i < 20; i++ { + manager, h := registerPatchTarget(t) + + rec := serveJSON(h.PatchAuthFileFields, http.MethodPatch, `{"name":"a.json","nested.value":"changed","bad..path":"x"}`) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want 400; body=%s", rec.Code, rec.Body.String()) + } + live, _ := manager.GetByID("a.json") + if got := live.Metadata["nested"].(map[string]any)["value"]; got != "old" { + t.Fatalf("run %d: nested.value = %#v, want unchanged", i, got) + } + } +} + +func TestPatchAuthFileFieldsNullProxyURLClearsTheLiveProxy(t *testing.T) { + t.Setenv("MANAGEMENT_PASSWORD", "") + gin.SetMode(gin.TestMode) + manager, h := registerPatchTarget(t) + + rec := serveJSON(h.PatchAuthFileFields, http.MethodPatch, `{"name":"a.json","proxy_url":null}`) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String()) + } + live, _ := manager.GetByID("a.json") + if live.ProxyURL != "" { + t.Fatalf("live ProxyURL = %q, want it cleared like the saved proxy_url", live.ProxyURL) + } + if got, _ := live.Metadata["proxy_url"].(string); got != "" { + t.Fatalf("saved proxy_url = %#v, want cleared", live.Metadata["proxy_url"]) + } +} diff --git a/internal/auth/claude/anthropic_auth.go b/internal/auth/claude/anthropic_auth.go index d7ca154296b..2646467d1fe 100644 --- a/internal/auth/claude/anthropic_auth.go +++ b/internal/auth/claude/anthropic_auth.go @@ -154,6 +154,12 @@ func NewClaudeAuth(cfg *config.Config) *ClaudeAuth { // NewClaudeAuthWithProxyURL creates a new Anthropic authentication service with a proxy override. // proxyURL takes precedence over cfg.ProxyURL when non-empty. func NewClaudeAuthWithProxyURL(cfg *config.Config, proxyURL string) *ClaudeAuth { + return NewClaudeAuthForAccount(cfg, proxyURL, "") +} + +// NewClaudeAuthForAccount is NewClaudeAuthWithProxyURL for the account authID, whose +// requests use that account's own shared connection. +func NewClaudeAuthForAccount(cfg *config.Config, proxyURL, authID string) *ClaudeAuth { effectiveProxyURL := strings.TrimSpace(proxyURL) var sdkCfg *config.SDKConfig if cfg != nil { @@ -171,7 +177,7 @@ func NewClaudeAuthWithProxyURL(cfg *config.Config, proxyURL string) *ClaudeAuth // Use custom HTTP client with Firefox TLS fingerprint to bypass // Cloudflare's bot detection on Anthropic domains return &ClaudeAuth{ - httpClient: NewAnthropicHttpClient(sdkCfg), + httpClient: NewAnthropicHttpClient(sdkCfg, authID), } } diff --git a/internal/auth/claude/anthropic_auth_proxy_test.go b/internal/auth/claude/anthropic_auth_proxy_test.go index 7cab9cd2f1f..269a27134b3 100644 --- a/internal/auth/claude/anthropic_auth_proxy_test.go +++ b/internal/auth/claude/anthropic_auth_proxy_test.go @@ -4,30 +4,38 @@ import ( "testing" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" - "golang.org/x/net/proxy" + "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" ) func TestNewClaudeAuthWithProxyURL_OverrideDirectTakesPrecedence(t *testing.T) { cfg := &config.Config{SDKConfig: config.SDKConfig{ProxyURL: "socks5://proxy.example.com:1080"}} auth := NewClaudeAuthWithProxyURL(cfg, "direct") - transport, ok := auth.httpClient.Transport.(*utlsRoundTripper) - if !ok || transport == nil { - t.Fatalf("expected utlsRoundTripper, got %T", auth.httpClient.Transport) - } - if transport.dialer != proxy.Direct { - t.Fatalf("expected proxy.Direct, got %T", transport.dialer) + if auth.httpClient.Transport != helps.UtlsTransport("direct") { + t.Fatalf("expected the direct utls transport, got %T", auth.httpClient.Transport) } } func TestNewClaudeAuthWithProxyURL_OverrideProxyAppliedWithoutConfig(t *testing.T) { auth := NewClaudeAuthWithProxyURL(nil, "socks5://proxy.example.com:1080") - transport, ok := auth.httpClient.Transport.(*utlsRoundTripper) - if !ok || transport == nil { - t.Fatalf("expected utlsRoundTripper, got %T", auth.httpClient.Transport) + if auth.httpClient.Transport != helps.UtlsTransport("socks5://proxy.example.com:1080") { + t.Fatalf("expected the socks5 utls transport, got %T", auth.httpClient.Transport) + } +} + +func TestNewClaudeAuthForAccount_UsesTheAccountsOwnTransport(t *testing.T) { + const proxyURL = "http://proxy.example.com:8080" + a := NewClaudeAuthForAccount(nil, proxyURL, "account-a").httpClient.Transport + b := NewClaudeAuthForAccount(nil, proxyURL, "account-b").httpClient.Transport + + if a == b { + t.Fatal("two accounts' token refreshes share one transport") + } + if a != helps.UtlsTransportForAuth(proxyURL, "account-a") { + t.Fatal("refresh client is not the account's shared transport") } - if transport.dialer == proxy.Direct { - t.Fatalf("expected proxy dialer, got %T", transport.dialer) + if a == helps.UtlsTransport(proxyURL) { + t.Fatal("refresh client uses the account-less login transport") } } diff --git a/internal/auth/claude/utls_transport.go b/internal/auth/claude/utls_transport.go index bb82e7ddecd..a3ee1e2c093 100644 --- a/internal/auth/claude/utls_transport.go +++ b/internal/auth/claude/utls_transport.go @@ -1,162 +1,21 @@ // Package claude provides authentication functionality for Anthropic's Claude API. -// This file implements a custom HTTP transport using utls to bypass TLS fingerprinting. package claude import ( "net/http" "strings" - "sync" - tls "github.com/refraction-networking/utls" + "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" - "github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil" - log "github.com/sirupsen/logrus" - "golang.org/x/net/http2" - "golang.org/x/net/proxy" ) -// utlsRoundTripper implements http.RoundTripper using utls with Chrome fingerprint -// to bypass Cloudflare's TLS fingerprinting on Anthropic domains. -type utlsRoundTripper struct { - // mu protects the connections map and pending map - mu sync.Mutex - // connections caches HTTP/2 client connections per host - connections map[string]*http2.ClientConn - // pending tracks hosts that are currently being connected to (prevents race condition) - pending map[string]*sync.Cond - // dialer is used to create network connections, supporting proxies - dialer proxy.Dialer -} - -// newUtlsRoundTripper creates a new utls-based round tripper with optional proxy support -func newUtlsRoundTripper(cfg *config.SDKConfig) *utlsRoundTripper { - var dialer proxy.Dialer = proxy.Direct - if cfg != nil { - proxyDialer, mode, errBuild := proxyutil.BuildDialer(cfg.ProxyURL) - if errBuild != nil { - log.Errorf("failed to configure proxy dialer for %q: %v", proxyutil.Redact(cfg.ProxyURL), errBuild) - } else if mode != proxyutil.ModeInherit && proxyDialer != nil { - dialer = proxyDialer - } - } - - return &utlsRoundTripper{ - connections: make(map[string]*http2.ClientConn), - pending: make(map[string]*sync.Cond), - dialer: dialer, - } -} - -// getOrCreateConnection gets an existing connection or creates a new one. -// It uses a per-host locking mechanism to prevent multiple goroutines from -// creating connections to the same host simultaneously. -func (t *utlsRoundTripper) getOrCreateConnection(host, addr string) (*http2.ClientConn, error) { - t.mu.Lock() - - // Check if connection exists and is usable - if h2Conn, ok := t.connections[host]; ok && h2Conn.CanTakeNewRequest() { - t.mu.Unlock() - return h2Conn, nil - } - - // Check if another goroutine is already creating a connection - if cond, ok := t.pending[host]; ok { - // Wait for the other goroutine to finish - cond.Wait() - // Check if connection is now available - if h2Conn, ok := t.connections[host]; ok && h2Conn.CanTakeNewRequest() { - t.mu.Unlock() - return h2Conn, nil - } - // Connection still not available, we'll create one - } - - // Mark this host as pending - cond := sync.NewCond(&t.mu) - t.pending[host] = cond - t.mu.Unlock() - - // Create connection outside the lock - h2Conn, err := t.createConnection(host, addr) - - t.mu.Lock() - defer t.mu.Unlock() - - // Remove pending marker and wake up waiting goroutines - delete(t.pending, host) - cond.Broadcast() - - if err != nil { - return nil, err - } - - // Store the new connection - t.connections[host] = h2Conn - return h2Conn, nil -} - -// createConnection creates a new HTTP/2 connection with Chrome TLS fingerprint. -// Chrome's TLS fingerprint is closer to Node.js/OpenSSL (which real Claude Code uses) -// than Firefox, reducing the mismatch between TLS layer and HTTP headers. -func (t *utlsRoundTripper) createConnection(host, addr string) (*http2.ClientConn, error) { - conn, err := t.dialer.Dial("tcp", addr) - if err != nil { - return nil, err - } - - tlsConfig := &tls.Config{ServerName: host} - tlsConn := tls.UClient(conn, tlsConfig, tls.HelloChrome_Auto) - - if err := tlsConn.Handshake(); err != nil { - conn.Close() - return nil, err - } - - tr := &http2.Transport{} - h2Conn, err := tr.NewClientConn(tlsConn) - if err != nil { - tlsConn.Close() - return nil, err - } - - return h2Conn, nil -} - -// RoundTrip implements http.RoundTripper -func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { - host := req.URL.Host - addr := host - if !strings.Contains(addr, ":") { - addr += ":443" - } - - // Get hostname without port for TLS ServerName - hostname := req.URL.Hostname() - - h2Conn, err := t.getOrCreateConnection(hostname, addr) - if err != nil { - return nil, err - } - - resp, err := h2Conn.RoundTrip(req) - if err != nil { - // Connection failed, remove it from cache - t.mu.Lock() - if cached, ok := t.connections[hostname]; ok && cached == h2Conn { - delete(t.connections, hostname) - } - t.mu.Unlock() - return nil, err - } - - return resp, nil -} - // NewAnthropicHttpClient creates an HTTP client that bypasses TLS fingerprinting -// for Anthropic domains by using utls with Chrome fingerprint. -// It accepts optional SDK configuration for proxy settings. -func NewAnthropicHttpClient(cfg *config.SDKConfig) *http.Client { - return &http.Client{ - Transport: newUtlsRoundTripper(cfg), +// for Anthropic domains by using the shared utls Chrome-fingerprint transport of authID +// ("" for flows without an account, such as login). +func NewAnthropicHttpClient(cfg *config.SDKConfig, authID string) *http.Client { + var proxyURL string + if cfg != nil { + proxyURL = strings.TrimSpace(cfg.ProxyURL) } + return &http.Client{Transport: helps.UtlsTransportForAuth(proxyURL, authID)} } diff --git a/internal/buildinfo/buildinfo.go b/internal/buildinfo/buildinfo.go index d45623a5f7c..dd98ec9d8b8 100644 --- a/internal/buildinfo/buildinfo.go +++ b/internal/buildinfo/buildinfo.go @@ -5,7 +5,7 @@ package buildinfo // Defaults cover local development builds. var ( // Version is the semantic version or git describe output of the binary. - Version = "7.2.62+edgee.7" + Version = "7.2.62+edgee.8" // Commit is the git commit SHA baked into the binary. Commit = "none" diff --git a/internal/config/config.go b/internal/config/config.go index 89db25f5004..876b732e907 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,6 +20,7 @@ import ( "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" sdkpluginstore "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginstore" + "github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil" log "github.com/sirupsen/logrus" "golang.org/x/crypto/bcrypt" "gopkg.in/yaml.v3" @@ -767,6 +768,9 @@ func LoadConfigOptional(configFile string, optional bool) (*Config, error) { if err = cfg.ValidateCodexOAuthRouteAPIKeys(); err != nil { return nil, err } + if err = cfg.ValidateProxyURLs(); err != nil { + return nil, err + } // Hash remote management key if plaintext is detected (nested) // We consider a value to be already hashed if it looks like a bcrypt hash ($2a$, $2b$, or $2y$ prefix). @@ -850,6 +854,57 @@ func LoadConfigOptional(configFile string, optional bool) (*Config, error) { return &cfg, nil } +// ValidateProxyURLs rejects any malformed proxy-url, so a typo stops startup instead of failing requests later. +func (cfg *Config) ValidateProxyURLs() error { + if cfg == nil { + return nil + } + if err := ValidateProxyURL("proxy-url", cfg.ProxyURL); err != nil { + return err + } + for i, k := range cfg.GeminiKey { + if err := ValidateProxyURL(fmt.Sprintf("gemini-api-key[%d].proxy-url", i), k.ProxyURL); err != nil { + return err + } + } + for i, k := range cfg.InteractionsKey { + if err := ValidateProxyURL(fmt.Sprintf("interactions-api-key[%d].proxy-url", i), k.ProxyURL); err != nil { + return err + } + } + for i, k := range cfg.CodexKey { + if err := ValidateProxyURL(fmt.Sprintf("codex-api-key[%d].proxy-url", i), k.ProxyURL); err != nil { + return err + } + } + for i, k := range cfg.ClaudeKey { + if err := ValidateProxyURL(fmt.Sprintf("claude-api-key[%d].proxy-url", i), k.ProxyURL); err != nil { + return err + } + } + for i, k := range cfg.VertexCompatAPIKey { + if err := ValidateProxyURL(fmt.Sprintf("vertex-api-key[%d].proxy-url", i), k.ProxyURL); err != nil { + return err + } + } + for i, compat := range cfg.OpenAICompatibility { + for j, k := range compat.APIKeyEntries { + if err := ValidateProxyURL(fmt.Sprintf("openai-compatibility[%d].api-key-entries[%d].proxy-url", i, j), k.ProxyURL); err != nil { + return err + } + } + } + return nil +} + +// ValidateProxyURL returns an error naming field when raw is set but is not a usable proxy setting. +func ValidateProxyURL(field, raw string) error { + if _, err := proxyutil.Parse(raw); err != nil { + return fmt.Errorf("%s: %w", field, err) + } + return nil +} + // NormalizeCodexOAuthBaseURL validates and normalizes the optional Codex OAuth upstream URL. func (cfg *Config) NormalizeCodexOAuthBaseURL() error { if cfg == nil || cfg.CodexOAuthBaseURL == "" { diff --git a/internal/config/parse.go b/internal/config/parse.go index 28078ee2bf9..3c384b79d24 100644 --- a/internal/config/parse.go +++ b/internal/config/parse.go @@ -45,6 +45,9 @@ func ParseConfigBytes(data []byte) (*Config, error) { if err := cfg.ValidateCodexOAuthRouteAPIKeys(); err != nil { return nil, err } + if err := cfg.ValidateProxyURLs(); err != nil { + return nil, err + } // Hash remote management key if plaintext is detected (nested), but do NOT persist. if cfg.RemoteManagement.SecretKey != "" && !looksLikeBcrypt(cfg.RemoteManagement.SecretKey) { diff --git a/internal/config/proxy_url_validation_test.go b/internal/config/proxy_url_validation_test.go new file mode 100644 index 00000000000..d11dda71fce --- /dev/null +++ b/internal/config/proxy_url_validation_test.go @@ -0,0 +1,25 @@ +package config + +import ( + "strings" + "testing" +) + +func TestParseConfigBytesRejectsMalformedProxyURLs(t *testing.T) { + t.Parallel() + for name, yaml := range map[string]string{ + "global": "proxy-url: sock5://127.0.0.1:1080\n", + "codex key": "codex-api-key:\n - api-key: k\n base-url: https://example.com\n proxy-url: \"http://\"\n", + "compat entry": "openai-compatibility:\n - name: x\n base-url: https://example.com\n" + + " api-key-entries:\n - api-key: k\n proxy-url: ftp://p:21\n", + } { + if _, err := ParseConfigBytes([]byte(yaml)); err == nil || !strings.Contains(err.Error(), "proxy-url") { + t.Fatalf("%s: err = %v, want a proxy-url error", name, err) + } + } + for _, ok := range []string{"", "direct", "none", "http://127.0.0.1:18319", "socks5h://u:p@host:1080"} { + if _, err := ParseConfigBytes([]byte("proxy-url: \"" + ok + "\"\n")); err != nil { + t.Fatalf("proxy-url %q rejected: %v", ok, err) + } + } +} diff --git a/internal/runtime/executor/claude_executor.go b/internal/runtime/executor/claude_executor.go index b3e348abb5d..53eed292688 100644 --- a/internal/runtime/executor/claude_executor.go +++ b/internal/runtime/executor/claude_executor.go @@ -824,7 +824,7 @@ func (e *ClaudeExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) ( if refreshToken == "" { return auth, nil } - svc := claudeauth.NewClaudeAuthWithProxyURL(e.cfg, auth.ProxyURL) + svc := claudeauth.NewClaudeAuthForAccount(e.cfg, auth.ProxyURL, auth.ID) td, err := svc.RefreshTokensWithRetry(ctx, refreshToken, 3) if err != nil { return nil, err diff --git a/internal/runtime/executor/codex_websockets_executor.go b/internal/runtime/executor/codex_websockets_executor.go index 7b95161f2c0..46936f07a6e 100644 --- a/internal/runtime/executor/codex_websockets_executor.go +++ b/internal/runtime/executor/codex_websockets_executor.go @@ -704,7 +704,10 @@ func (e *CodexWebsocketsExecutor) ExecuteStream(ctx context.Context, auth *clipr } func (e *CodexWebsocketsExecutor) dialCodexWebsocket(ctx context.Context, auth *cliproxyauth.Auth, wsURL string, headers http.Header) (*websocket.Conn, *http.Response, error) { - dialer := newProxyAwareWebsocketDialer(e.cfg, auth) + dialer, errDialer := newProxyAwareWebsocketDialer(e.cfg, auth) + if errDialer != nil { + return nil, nil, errDialer + } dialer.HandshakeTimeout = codexResponsesWebsocketHandshakeTO dialer.EnableCompression = true if ctx == nil { @@ -791,7 +794,7 @@ func readCodexWebsocketMessage(ctx context.Context, sess *codexWebsocketSession, } } -func newProxyAwareWebsocketDialer(cfg *config.Config, auth *cliproxyauth.Auth) *websocket.Dialer { +func newProxyAwareWebsocketDialer(cfg *config.Config, auth *cliproxyauth.Auth) (*websocket.Dialer, error) { dialer := &websocket.Dialer{ Proxy: http.ProxyFromEnvironment, HandshakeTimeout: codexResponsesWebsocketHandshakeTO, @@ -810,22 +813,21 @@ func newProxyAwareWebsocketDialer(cfg *config.Config, auth *cliproxyauth.Auth) * proxyURL = strings.TrimSpace(cfg.ProxyURL) } if proxyURL == "" { - return dialer + return dialer, nil } setting, errParse := proxyutil.Parse(proxyURL) if errParse != nil { - log.Errorf("codex websockets executor: %v", errParse) - return dialer + return nil, fmt.Errorf("websocket dialer: invalid proxy %s: %w", proxyutil.Redact(proxyURL), errParse) } switch setting.Mode { case proxyutil.ModeDirect: dialer.Proxy = nil - return dialer + return dialer, nil case proxyutil.ModeProxy: default: - return dialer + return dialer, nil } switch setting.URL.Scheme { @@ -838,8 +840,7 @@ func newProxyAwareWebsocketDialer(cfg *config.Config, auth *cliproxyauth.Auth) * } socksDialer, errSOCKS5 := proxy.SOCKS5("tcp", setting.URL.Host, proxyAuth, proxy.Direct) if errSOCKS5 != nil { - log.Errorf("codex websockets executor: create SOCKS5 dialer failed: %v", errSOCKS5) - return dialer + return nil, fmt.Errorf("websocket dialer: create SOCKS5 dialer failed: %w", errSOCKS5) } dialer.Proxy = nil dialer.NetDialContext = func(_ context.Context, network, addr string) (net.Conn, error) { @@ -848,10 +849,10 @@ func newProxyAwareWebsocketDialer(cfg *config.Config, auth *cliproxyauth.Auth) * case "http", "https": dialer.Proxy = http.ProxyURL(setting.URL) default: - log.Errorf("codex websockets executor: unsupported proxy scheme: %s", setting.URL.Scheme) + return nil, fmt.Errorf("websocket dialer: unsupported proxy scheme %q", setting.URL.Scheme) } - return dialer + return dialer, nil } func buildCodexResponsesWebsocketURL(httpURL string) (string, error) { diff --git a/internal/runtime/executor/codex_websockets_executor_test.go b/internal/runtime/executor/codex_websockets_executor_test.go index 753259a5603..f9e35289cf5 100644 --- a/internal/runtime/executor/codex_websockets_executor_test.go +++ b/internal/runtime/executor/codex_websockets_executor_test.go @@ -1059,12 +1059,25 @@ func contextWithGinHeaders(headers map[string]string) context.Context { func TestNewProxyAwareWebsocketDialerDirectDisablesProxy(t *testing.T) { t.Parallel() - dialer := newProxyAwareWebsocketDialer( + dialer, err := newProxyAwareWebsocketDialer( &config.Config{SDKConfig: sdkconfig.SDKConfig{ProxyURL: "http://global-proxy.example.com:8080"}}, &cliproxyauth.Auth{ProxyURL: "direct"}, ) + if err != nil { + t.Fatal(err) + } if dialer.Proxy != nil { t.Fatal("expected websocket proxy function to be nil for direct mode") } } + +func TestNewProxyAwareWebsocketDialerRejectsInvalidProxy(t *testing.T) { + t.Parallel() + + for _, proxyURL := range []string{"ftp://proxy.example.com:21", "http://"} { + if _, err := newProxyAwareWebsocketDialer(nil, &cliproxyauth.Auth{ProxyURL: proxyURL}); err == nil { + t.Fatalf("proxy %q: expected an error, got a dialer", proxyURL) + } + } +} diff --git a/internal/runtime/executor/helps/proxy_helpers.go b/internal/runtime/executor/helps/proxy_helpers.go index 572f87c7a1c..e8f11c1765f 100644 --- a/internal/runtime/executor/helps/proxy_helpers.go +++ b/internal/runtime/executor/helps/proxy_helpers.go @@ -2,14 +2,15 @@ package helps import ( "context" + "fmt" "net/http" "strings" + "sync" "time" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" "github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil" - log "github.com/sirupsen/logrus" ) // NewProxyAwareHTTPClient creates an HTTP client with proper proxy configuration priority: @@ -32,9 +33,10 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip } // Priority 1: Use auth.ProxyURL if configured - var proxyURL string + var proxyURL, authID string if auth != nil { proxyURL = strings.TrimSpace(auth.ProxyURL) + authID = auth.ID } // Priority 2: Use cfg.ProxyURL if auth proxy is not configured @@ -42,15 +44,9 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip proxyURL = strings.TrimSpace(cfg.ProxyURL) } - // If we have a proxy URL configured, set up the transport if proxyURL != "" { - transport := buildProxyTransport(proxyURL) - if transport != nil { - httpClient.Transport = transport - return httpClient - } - // If proxy setup failed, log and fall through to context RoundTripper - log.Debugf("failed to setup proxy from URL: %s, falling back to context transport", proxyutil.Redact(proxyURL)) + httpClient.Transport = proxyTransport(proxyURL, authID) + return httpClient } // Priority 3: Use RoundTripper from context (typically from RoundTripperFor) @@ -61,19 +57,29 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip return httpClient } -// buildProxyTransport creates an HTTP transport configured for the given proxy URL. -// It supports SOCKS5, HTTP, and HTTPS proxy protocols. -// -// Parameters: -// - proxyURL: The proxy URL string (e.g., "socks5://user:pass@host:port", "http://host:port") -// -// Returns: -// - *http.Transport: A configured transport, or nil if the proxy URL is invalid -func buildProxyTransport(proxyURL string) *http.Transport { +// One transport per proxy and account for the whole process, so connections are pooled across +// clients but never shared between accounts. Entries are never evicted; idle connections close. +var ( + sharedProxyMu sync.Mutex + sharedProxyTransports = map[transportKey]*http.Transport{} +) + +// proxyTransport returns the shared transport for proxyURL (SOCKS5, HTTP, HTTPS or "direct"). +// An invalid proxy URL yields a transport that fails every request. +func proxyTransport(proxyURL, authID string) http.RoundTripper { + key, errKey := newTransportKey(proxyURL, authID) + if errKey != nil { + return proxyutil.FailingTransport(fmt.Errorf("invalid proxy %s: %w", proxyutil.Redact(proxyURL), errKey)) + } + sharedProxyMu.Lock() + defer sharedProxyMu.Unlock() + if transport, ok := sharedProxyTransports[key]; ok { + return transport + } transport, _, errBuild := proxyutil.BuildHTTPTransport(proxyURL) if errBuild != nil { - log.Errorf("%v", errBuild) - return nil + return proxyutil.FailingTransport(fmt.Errorf("invalid proxy %s: %w", proxyutil.Redact(proxyURL), errBuild)) } + sharedProxyTransports[key] = transport return transport } diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index ad3315c6633..e74a6e3a0d6 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -2,131 +2,591 @@ package helps import ( "context" + "crypto/x509" + "errors" + "fmt" + "io" + "math/rand/v2" "net" "net/http" + "net/url" + "slices" "strings" "sync" + "sync/atomic" + "syscall" "time" tls "github.com/refraction-networking/utls" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" "github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil" - log "github.com/sirupsen/logrus" "golang.org/x/net/http2" "golang.org/x/net/proxy" ) -// utlsRoundTripper implements http.RoundTripper using utls with Chrome fingerprint -// to bypass Cloudflare's TLS fingerprinting on Anthropic domains. -type utlsRoundTripper struct { - mu sync.Mutex - connections map[string]*http2.ClientConn - pending map[string]*sync.Cond - dialer proxy.Dialer +// utlsDialTimeout bounds proxy CONNECT plus the TLS handshake for one shared connection. +const utlsDialTimeout = 30 * time.Second + +// One HTTP/2 transport per proxy and account for the whole process, so requests share +// connections instead of leaving one idle tunnel per request, and accounts never share one. +// Entries are never evicted: a removed account or replaced proxy keeps only memory, because the +// upstream closes its idle tunnel (chatgpt.com after about 6.5 minutes). +var ( + sharedUtlsMu sync.Mutex + sharedUtlsTransports = map[transportKey]*utlsRoundTripper{} + // utlsTestRootCAs is a test hook guarded by sharedUtlsMu; nil means the system roots. + utlsTestRootCAs *x509.CertPool +) + +// UtlsTransport returns the process-wide Chrome-fingerprinted HTTP/2 transport for +// proxyURL ("" dials directly) used without an account, such as login flows. +// An invalid proxy URL yields a transport that fails every request. +func UtlsTransport(proxyURL string) http.RoundTripper { + return utlsTransportFor(proxyURL, "") } -func newUtlsRoundTripper(proxyURL string) *utlsRoundTripper { - var dialer proxy.Dialer = proxy.Direct - if proxyURL != "" { - proxyDialer, mode, errBuild := proxyutil.BuildDialer(proxyURL) - if errBuild != nil { - log.Errorf("utls: failed to configure proxy dialer for %q: %v", proxyutil.Redact(proxyURL), errBuild) - } else if mode != proxyutil.ModeInherit && proxyDialer != nil { - dialer = proxyDialer - } +// UtlsTransportForAuth is UtlsTransport for one account, which never shares a connection with another. +func UtlsTransportForAuth(proxyURL, authID string) http.RoundTripper { + return utlsTransportFor(proxyURL, authID) +} + +func utlsTransportFor(proxyURL, authID string) http.RoundTripper { + rt, err := sharedUtlsTransport(proxyURL, authID) + if err != nil { + return proxyutil.FailingTransport(err) } - return &utlsRoundTripper{ - connections: make(map[string]*http2.ClientConn), - pending: make(map[string]*sync.Cond), - dialer: dialer, + return rt +} + +func sharedUtlsTransport(proxyURL, authID string) (*utlsRoundTripper, error) { + key, err := newTransportKey(proxyURL, authID) + if err != nil { + return nil, fmt.Errorf("utls: invalid proxy %s: %w", proxyutil.Redact(proxyURL), err) + } + sharedUtlsMu.Lock() + defer sharedUtlsMu.Unlock() + if rt, ok := sharedUtlsTransports[key]; ok { + return rt, nil } + dialer, err := utlsDialer(proxyURL) + if err != nil { + return nil, err + } + pool := &utlsConnPool{ + dialer: dialer, + rootCAs: utlsTestRootCAs, + conns: map[string][]*utlsPooledConn{}, + dialing: map[string]*utlsDial{}, + } + // No idle or ping timeouts: AGENTS.md forbids timeouts after a connection is established. + h2 := &http2.Transport{ConnPool: pool} + pool.h2 = h2 + rt := &utlsRoundTripper{pool: pool} + sharedUtlsTransports[key] = rt + return rt, nil } -func (t *utlsRoundTripper) getOrCreateConnection(host, addr string) (*http2.ClientConn, error) { - t.mu.Lock() +// transportKey names a shared transport: the proxy ("direct" for none, otherwise the proxy +// URL without path or case differences) and the account ("" when there is none). +type transportKey struct { + proxy string + authID string +} - if h2Conn, ok := t.connections[host]; ok && h2Conn.CanTakeNewRequest() { - t.mu.Unlock() - return h2Conn, nil +func newTransportKey(proxyURL, authID string) (transportKey, error) { + setting, err := proxyutil.Parse(proxyURL) + if err != nil { + return transportKey{}, err } + key := transportKey{proxy: "direct", authID: authID} + if setting.Mode == proxyutil.ModeProxy { + normalised := url.URL{Scheme: setting.URL.Scheme, User: setting.URL.User, Host: strings.ToLower(setting.URL.Host)} + key.proxy = normalised.String() + } + return key, nil +} + +func utlsDialer(proxyURL string) (proxy.ContextDialer, error) { + if strings.TrimSpace(proxyURL) == "" { + return proxy.Direct, nil + } + dialer, _, err := proxyutil.BuildDialer(proxyURL) + if err != nil { + return nil, fmt.Errorf("utls: invalid proxy %s: %w", proxyutil.Redact(proxyURL), err) + } + contextDialer, ok := dialer.(proxy.ContextDialer) + if !ok { + return nil, fmt.Errorf("utls: proxy %s has no cancellable dialer (%T)", proxyutil.Redact(proxyURL), dialer) + } + return contextDialer, nil +} + +// utlsRoundTripper sends requests over the shared HTTP/2 connections itself, instead of through +// http2.Transport's retry loop, so it controls which requests may be sent twice: one the server +// confirmed it never processed is retried; any other is replayed at most once, and only when +// it is safe to send again. It also closes the body when no connection was obtained and drops +// a connection that has gone silent. +type utlsRoundTripper struct { + pool *utlsConnPool +} + +// maxUnprocessedRetries bounds retries of a request the server confirmed it never processed. +const maxUnprocessedRetries = 6 - if cond, ok := t.pending[host]; ok { - cond.Wait() - if h2Conn, ok := t.connections[host]; ok && h2Conn.CanTakeNewRequest() { - t.mu.Unlock() - return h2Conn, nil +func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + if req.URL.Scheme != "https" { + closeRequestBody(req) + return nil, fmt.Errorf("utls: unsupported scheme %q", req.URL.Scheme) + } + port := req.URL.Port() + if port == "" { + port = "443" + } + addr := net.JoinHostPort(req.URL.Hostname(), port) + replayed := false + for retry := 0; ; retry++ { + attempt, body := withOnceClosedBody(req) + conn, err := t.pool.reserve(req.Context(), addr) + if err != nil { + body.Close() + return nil, err } + if h := testHooks.Load(); h != nil && h.afterReserve != nil { + h.afterReserve(req) + } + release := conn.track(req.Context()) + readAtStart := conn.read.Load() + resp, err := conn.cc.RoundTrip(attempt) + if err == nil { + if resp.Body == http.NoBody { + release() + return resp, nil + } + tracked := &utlsResponseBody{ReadCloser: resp.Body, ctx: req.Context(), pool: t.pool, conn: conn, release: release} + tracked.lastRead.Store(conn.read.Load()) + resp.Body = tracked + return resp, nil + } + release() + body.Close() + if conn.dead.Load() { + t.pool.MarkDead(conn.cc) + } + if err.Error() == errTextConnNotEstablished { + // x/net leaves a conn that died before its first stream reservable; drop it so the next request redials. + t.pool.MarkDead(conn.cc) + return nil, err + } + if req.Context().Err() != nil { + t.pool.dropIfSilent(conn, readAtStart) + return nil, err + } + switch { + case serverDidNotProcess(err) && retry < maxUnprocessedRetries: + case !replayed && mayHaveBeenProcessed(err) && safeToSendTwice(req): + replayed = true + default: + return nil, err + } + next, rewindErr := rewoundRequest(req, err) + if rewindErr != nil { + return nil, rewindErr + } + if waitErr := waitBeforeRetry(req.Context(), retryBackoff(retry)); waitErr != nil { + closeRequestBody(next) + return nil, waitErr + } + req = next } +} - cond := sync.NewCond(&t.mu) - t.pending[host] = cond - t.mu.Unlock() +// retryBackoff is the wait before the retry that follows attempt retry; tests replace it. +var retryBackoff = xnetRetryBackoff - h2Conn, err := t.createConnection(host, addr) +// xnetRetryBackoff is x/net's schedule: no wait before the first retry, then 1s, 2s, 4s... plus +// up to 10% jitter. It waits before a request is sent, never on an open stream. +func xnetRetryBackoff(retry int) time.Duration { + if retry == 0 { + return 0 + } + backoff := float64(time.Second) * float64(uint(1)<<(uint(retry)-1)) + return time.Duration(backoff + backoff*0.1*rand.Float64()) +} - t.mu.Lock() - defer t.mu.Unlock() +func waitBeforeRetry(ctx context.Context, d time.Duration) error { + if d <= 0 { + return nil + } + timer := time.NewTimer(d) + defer timer.Stop() + select { + case <-timer.C: + return nil + case <-ctx.Done(): + return ctx.Err() + } +} - delete(t.pending, host) - cond.Broadcast() +// http2 does not export these errors; they are matched by text. +const ( + errTextConnUnusable = "http2: client conn not usable" + errTextConnGotGoAway = "http2: Transport received Server's graceful shutdown GOAWAY" + errTextConnNotEstablished = "http2: client conn could not be established" + errTextConnForceClosed = "http2: client connection force closed via ClientConn.Close" + errTextFromPeer = "received from peer" +) +// serverDidNotProcess reports an error after which the server cannot have acted on the request: +// the connection was unusable before the stream opened, the stream was above the GOAWAY's last +// stream id, or the server refused the stream. +func serverDidNotProcess(err error) bool { + if text := err.Error(); text == errTextConnUnusable || text == errTextConnGotGoAway { + return true + } + var se http2.StreamError + return errors.As(err, &se) && se.Code == http2.ErrCodeRefusedStream +} + +// mayHaveBeenProcessed reports a failure that is often transient but leaves unknown whether the +// server acted on the request: an abrupt close before the response (x/net reports it to the other +// streams on that conn as a forced close), or a PROTOCOL_ERROR reset sent by the server. A +// PROTOCOL_ERROR raised locally, such as on a malformed response, means the server already +// answered, so it is not one. +func mayHaveBeenProcessed(err error) bool { + if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, syscall.ECONNRESET) || errors.Is(err, syscall.EPIPE) || err.Error() == errTextConnForceClosed { + return true + } + var se http2.StreamError + return errors.As(err, &se) && se.Code == http2.ErrCodeProtocol && se.Cause != nil && se.Cause.Error() == errTextFromPeer +} + +// safeToSendTwice reports an idempotent method, or an idempotency key as net/http allows. +func safeToSendTwice(req *http.Request) bool { + switch req.Method { + case http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodTrace: + return true + } + return req.Header.Get("Idempotency-Key") != "" || req.Header.Get("X-Idempotency-Key") != "" +} + +// rewoundRequest returns a copy of req with its body back at the start. Without GetBody it +// returns cause, the error that prompted the retry; a failing GetBody returns its own error. +func rewoundRequest(req *http.Request, cause error) (*http.Request, error) { + next := req.Clone(req.Context()) + if req.Body == nil || req.Body == http.NoBody { + return next, nil + } + if req.GetBody == nil { + return nil, cause + } + body, err := req.GetBody() if err != nil { - return nil, err + return nil, fmt.Errorf("utls: rewinding the request body after %v: %w", cause, err) } + next.Body = body + return next, nil +} - t.connections[host] = h2Conn - return h2Conn, nil +// onceClosedBody lets both http2 and the round tripper close an attempt's body, closing it once. +type onceClosedBody struct { + io.ReadCloser + once sync.Once } -func (t *utlsRoundTripper) createConnection(host, addr string) (*http2.ClientConn, error) { - conn, err := t.dialer.Dial("tcp", addr) +func (b *onceClosedBody) Close() error { + var err error + b.once.Do(func() { err = b.ReadCloser.Close() }) + return err +} + +type noopCloser struct{} + +func (noopCloser) Close() error { return nil } + +// withOnceClosedBody returns the request to send for one attempt and the closer for its body. +func withOnceClosedBody(req *http.Request) (*http.Request, io.Closer) { + if req.Body == nil || req.Body == http.NoBody { + return req, noopCloser{} + } + body := &onceClosedBody{ReadCloser: req.Body} + attempt := *req + attempt.Body = body + return &attempt, body +} + +func closeRequestBody(req *http.Request) { + if req.Body != nil { + _ = req.Body.Close() + } +} + +// utlsConnPool is an http2.ClientConnPool that dials once per address and lets +// each waiting request give up when its own context ends. +type utlsConnPool struct { + h2 *http2.Transport + dialer proxy.ContextDialer + rootCAs *x509.CertPool + mu sync.Mutex + conns map[string][]*utlsPooledConn + dialing map[string]*utlsDial +} + +// utlsPooledConn is a shared connection plus a count of the bytes it has received, which tells +// a silent (dead) tunnel apart from a slow but healthy one, and the requests still using it. +type utlsPooledConn struct { + cc *http2.ClientConn + read atomic.Uint64 + dead atomic.Bool // its read loop stopped, so it can never carry another request + mu sync.Mutex // orders reservations against retire's decision to close + live int // reserved requests whose contexts have not ended + retired bool + closeOnce sync.Once +} + +// tryReserve reserves a stream and counts one live request in a single step, so a conn retire +// has already chosen to close is never handed out and retire never closes under a new request. +func (c *utlsPooledConn) tryReserve() bool { + c.mu.Lock() + defer c.mu.Unlock() + if c.retired || !c.cc.ReserveNewRequest() { + return false + } + c.live++ + return true +} + +// track ends the request tryReserve counted when the returned release runs or its context ends. +// Closing a retired conn waits for those requests only, never for a stuck write. +func (c *utlsPooledConn) track(ctx context.Context) (release func()) { + var once sync.Once + done := func() { once.Do(c.done) } + stop := context.AfterFunc(ctx, done) + return func() { stop(); done() } +} + +func (c *utlsPooledConn) done() { + c.mu.Lock() + c.live-- + closing := c.live == 0 && c.retired + c.mu.Unlock() + if closing { + c.close() + } +} + +func (c *utlsPooledConn) retire() { + c.mu.Lock() + c.retired = true + closing := c.live == 0 + c.mu.Unlock() + if closing { + c.close() + } +} + +// close drops the socket at once; Shutdown would wait behind a writer stuck on a full send buffer. +func (c *utlsPooledConn) close() { + c.closeOnce.Do(func() { + if h := testHooks.Load(); h != nil && h.beforeRetiredClose != nil { + h.beforeRetiredClose() + } + _ = c.cc.Close() + }) +} + +// testHooks lets tests pause at the scheduling points around retirement; nil in production. +var testHooks atomic.Pointer[utlsTestHooks] + +type utlsTestHooks struct { + afterReserve func(*http.Request) + beforeRetiredClose func() +} + +type countingConn struct { + net.Conn + conn *utlsPooledConn +} + +func (c countingConn) Read(b []byte) (int, error) { + n, err := c.Conn.Read(b) + c.conn.read.Add(uint64(n)) if err != nil { - return nil, err + c.conn.dead.Store(true) } + return n, err +} - tlsConfig := &tls.Config{ServerName: host} - tlsConn := tls.UClient(conn, tlsConfig, tls.HelloChrome_Auto) +// utlsResponseBody releases its connection when the body is done. A body that ends by its +// context with nothing received on the connection since the last read retires it as silent. +type utlsResponseBody struct { + io.ReadCloser + ctx context.Context + pool *utlsConnPool + conn *utlsPooledConn + release func() + lastRead atomic.Uint64 + once sync.Once +} - if err := tlsConn.Handshake(); err != nil { - conn.Close() - return nil, err +func (b *utlsResponseBody) Read(p []byte) (int, error) { + n, err := b.ReadCloser.Read(p) + if err != nil { + b.finish(err == io.EOF) + } else { + b.lastRead.Store(b.conn.read.Load()) } + return n, err +} + +func (b *utlsResponseBody) Close() error { + err := b.ReadCloser.Close() + b.finish(false) + return err +} + +func (b *utlsResponseBody) finish(complete bool) { + b.once.Do(func() { + b.release() + if !complete && b.ctx.Err() != nil { + b.pool.dropIfSilent(b.conn, b.lastRead.Load()) + } + }) +} + +type utlsDial struct { + done chan struct{} + conn *utlsPooledConn + err error +} - tr := &http2.Transport{} - h2Conn, err := tr.NewClientConn(tlsConn) +// GetClientConn serves http2's own use of the pool; utlsRoundTripper calls reserve directly. +func (p *utlsConnPool) GetClientConn(req *http.Request, addr string) (*http2.ClientConn, error) { + conn, err := p.reserve(req.Context(), addr) if err != nil { - tlsConn.Close() return nil, err } + conn.done() // http2 tracks its own streams on this path + return conn.cc, nil +} + +// reserve returns a shared connection with a stream reserved and counted for one request, +// dialling one when none can take it. A request whose context has already ended reserves nothing. +func (p *utlsConnPool) reserve(ctx context.Context, addr string) (*utlsPooledConn, error) { + for { + if err := ctx.Err(); err != nil { + return nil, err + } + p.mu.Lock() + p.dropDeadLocked(addr) + for _, conn := range p.conns[addr] { + if conn.tryReserve() { + p.mu.Unlock() + return conn, nil + } + } + d, ok := p.dialing[addr] + if !ok { + d = &utlsDial{done: make(chan struct{})} + p.dialing[addr] = d + go p.dial(d, addr) + } + p.mu.Unlock() + + select { + case <-d.done: + case <-ctx.Done(): + return nil, ctx.Err() + } + if d.err != nil { + return nil, d.err + } + if ctx.Err() == nil && d.conn.tryReserve() { + return d.conn, nil + } + } +} - return h2Conn, nil +func (p *utlsConnPool) dropDeadLocked(addr string) { + if list := slices.DeleteFunc(p.conns[addr], func(conn *utlsPooledConn) bool { return conn.dead.Load() }); len(list) == 0 { + delete(p.conns, addr) + } else { + p.conns[addr] = list + } } -func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { - hostname := req.URL.Hostname() - port := req.URL.Port() - if port == "" { - port = "443" +func (p *utlsConnPool) MarkDead(cc *http2.ClientConn) { + p.mu.Lock() + defer p.mu.Unlock() + for addr, list := range p.conns { + if i := slices.IndexFunc(list, func(conn *utlsPooledConn) bool { return conn.cc == cc }); i >= 0 { + if list = slices.Delete(list, i, i+1); len(list) == 0 { + delete(p.conns, addr) + } else { + p.conns[addr] = list + } + return + } + } +} + +// dropIfSilent retires conn when a request on it ended with its context and the connection +// received nothing at all meanwhile: the tunnel is dead, so new requests must dial a fresh one. +// A quick cancel on a quiet but healthy connection therefore costs one redial. The conn closes +// once no request whose context is still live uses it. +func (p *utlsConnPool) dropIfSilent(conn *utlsPooledConn, readSince uint64) { + if conn.read.Load() != readSince { + return } - addr := net.JoinHostPort(hostname, port) + p.MarkDead(conn.cc) + conn.retire() +} - h2Conn, err := t.getOrCreateConnection(hostname, addr) +// dial runs detached from any one request, so a cancelled first caller does not fail the others. +func (p *utlsConnPool) dial(d *utlsDial, addr string) { + ctx, cancel := context.WithTimeout(context.Background(), utlsDialTimeout) + defer cancel() + conn, err := p.dialTLS(ctx, addr) + if err == nil { + pooled := &utlsPooledConn{} + if pooled.cc, err = p.h2.NewClientConn(countingConn{Conn: conn, conn: pooled}); err != nil { + _ = conn.Close() + } else { + d.conn = pooled + } + } if err != nil { - return nil, err + d.err = fmt.Errorf("utls: shared dial to %s failed: %w", addr, err) + } + + p.mu.Lock() + delete(p.dialing, addr) + if err == nil { + p.conns[addr] = append(p.conns[addr], d.conn) } + p.mu.Unlock() + close(d.done) +} - resp, err := h2Conn.RoundTrip(req) +func (p *utlsConnPool) dialTLS(ctx context.Context, addr string) (net.Conn, error) { + host, _, err := net.SplitHostPort(addr) if err != nil { - t.mu.Lock() - if cached, ok := t.connections[hostname]; ok && cached == h2Conn { - delete(t.connections, hostname) - } - t.mu.Unlock() return nil, err } - - return resp, nil + conn, err := p.dialer.DialContext(ctx, "tcp", addr) + if err != nil { + return nil, err + } + tlsConn := tls.UClient(conn, &tls.Config{ServerName: host, RootCAs: p.rootCAs}, tls.HelloChrome_Auto) + if err = tlsConn.HandshakeContext(ctx); err != nil { + _ = conn.Close() + return nil, err + } + if proto := tlsConn.ConnectionState().NegotiatedProtocol; proto != http2.NextProtoTLS { + _ = tlsConn.Close() + return nil, fmt.Errorf("utls: %s negotiated ALPN %q, want %q", addr, proto, http2.NextProtoTLS) + } + return tlsConn, nil } // utlsProtectedHosts contains the hosts that should use utls Chrome TLS fingerprint @@ -156,9 +616,10 @@ func (f *fallbackRoundTripper) RoundTrip(req *http.Request) (*http.Response, err // Use this for provider requests that need a Chrome-like TLS fingerprint. // Falls back to standard transport for non-HTTPS requests. func NewUtlsHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) *http.Client { - var proxyURL string + var proxyURL, authID string if auth != nil { proxyURL = strings.TrimSpace(auth.ProxyURL) + authID = auth.ID } if proxyURL == "" && cfg != nil { proxyURL = strings.TrimSpace(cfg.ProxyURL) @@ -169,12 +630,10 @@ func NewUtlsHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyau ctxRoundTripper, _ = ctx.Value("cliproxy.roundtripper").(http.RoundTripper) } - var utlsRT http.RoundTripper = newUtlsRoundTripper(proxyURL) + utlsRT := utlsTransportFor(proxyURL, authID) var standardTransport http.RoundTripper = http.DefaultTransport if proxyURL != "" { - if transport := buildProxyTransport(proxyURL); transport != nil { - standardTransport = transport - } + standardTransport = proxyTransport(proxyURL, authID) } else if ctxRoundTripper != nil { utlsRT = ctxRoundTripper standardTransport = ctxRoundTripper diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index 093ad4bef7c..ac89f3c07dd 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -1,13 +1,63 @@ package helps import ( + "bufio" + "bytes" "context" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + stdtls "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "errors" + "fmt" "io" + "math/big" + "net" "net/http" + "net/http/httptest" + "os" + "slices" "strings" + "sync" + "sync/atomic" "testing" + "time" + + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" + sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" + "golang.org/x/net/http2" + "golang.org/x/net/http2/hpack" ) +func TestMain(m *testing.M) { + // Retries skip x/net's 1s, 2s, 4s... backoff in tests; TestRetryBackoffFollowsXNet covers it. + retryBackoff = func(int) time.Duration { return 0 } + os.Exit(m.Run()) +} + +func TestRetryBackoffFollowsXNet(t *testing.T) { + if d := xnetRetryBackoff(0); d != 0 { + t.Fatalf("first retry waits %v, want no wait", d) + } + for retry, base := range map[int]time.Duration{1: time.Second, 2: 2 * time.Second, 3: 4 * time.Second, 6: 32 * time.Second} { + if d := xnetRetryBackoff(retry); d < base || d > base+base/10 { + t.Fatalf("retry %d waits %v, want %v plus up to 10%%", retry, d, base) + } + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + start := time.Now() + if err := waitBeforeRetry(ctx, time.Hour); !errors.Is(err, context.Canceled) { + t.Fatalf("err = %v, want context canceled", err) + } + if time.Since(start) > time.Second { + t.Fatal("a cancelled request kept waiting for its retry") + } +} + type utlsClientRoundTripFunc func(*http.Request) (*http.Response, error) func (f utlsClientRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { @@ -43,3 +93,1287 @@ func TestNewUtlsHTTPClientUsesContextRoundTripperForProtectedHost(t *testing.T) t.Fatal("expected context RoundTripper to handle protected host request") } } + +var ( + chatgptCertOnce sync.Once + chatgptCert stdtls.Certificate +) + +// startChatgptServer serves TLS for chatgpt.com on loopback; utls trusts it through the test hook. +func startChatgptServer(t *testing.T, h2 bool, handler http.Handler) *httptest.Server { + t.Helper() + return startChatgptServerWithIdleTimeout(t, h2, handler, 0) +} + +func startChatgptServerWithIdleTimeout(t *testing.T, h2 bool, handler http.Handler, idle time.Duration) *httptest.Server { + t.Helper() + chatgptCertOnce.Do(func() { + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + panic(err) + } + tmpl := &x509.Certificate{ + SerialNumber: big.NewInt(1), + Subject: pkix.Name{CommonName: "chatgpt.com"}, + DNSNames: []string{"chatgpt.com"}, + NotBefore: time.Now().Add(-time.Hour), + NotAfter: time.Now().Add(time.Hour), + IsCA: true, + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + } + der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key) + if err != nil { + panic(err) + } + leaf, err := x509.ParseCertificate(der) + if err != nil { + panic(err) + } + chatgptCert = stdtls.Certificate{Certificate: [][]byte{der}, PrivateKey: key, Leaf: leaf} + roots := x509.NewCertPool() + roots.AddCert(leaf) + sharedUtlsMu.Lock() + utlsTestRootCAs = roots + sharedUtlsMu.Unlock() + }) + server := httptest.NewUnstartedServer(handler) + server.EnableHTTP2 = h2 + server.Config.IdleTimeout = idle + server.TLS = &stdtls.Config{Certificates: []stdtls.Certificate{chatgptCert}} + server.StartTLS() + t.Cleanup(server.Close) + return server +} + +func chatgptURL(server *httptest.Server, path string) string { + _, port, _ := net.SplitHostPort(server.Listener.Addr().String()) + return "https://chatgpt.com:" + port + path +} + +// connectProxy is an HTTP CONNECT proxy that sends every tunnel to loopback and counts them. +type connectProxy struct { + url string + accepted atomic.Int32 + opened atomic.Int32 + open atomic.Int32 + + mu sync.Mutex + tunnels []*proxyTunnel +} + +type proxyTunnel struct { + client net.Conn + dropping atomic.Bool + stalled atomic.Bool +} + +// stallOpenTunnels stops reading from every open tunnel's client, so the client's writes back up +// until its TCP send buffer is full; resume reads again. +func (p *connectProxy) stallOpenTunnels() (resume func()) { + p.mu.Lock() + defer p.mu.Unlock() + tunnels := slices.Clone(p.tunnels) + for _, tun := range tunnels { + tun.stalled.Store(true) + } + return func() { + for _, tun := range tunnels { + tun.stalled.Store(false) + } + } +} + +// blackHoleOpenTunnels keeps every open tunnel's TCP connection up but drops its bytes. +func (p *connectProxy) blackHoleOpenTunnels() { + p.mu.Lock() + defer p.mu.Unlock() + for _, tun := range p.tunnels { + tun.dropping.Store(true) + } +} + +// closeOpenTunnels cuts every open tunnel without any HTTP/2 GOAWAY. +func (p *connectProxy) closeOpenTunnels() { + p.mu.Lock() + defer p.mu.Unlock() + for _, tun := range p.tunnels { + _ = tun.client.Close() + } + p.tunnels = nil +} + +func (tun *proxyTunnel) pipe(dst, src net.Conn) { + buf := make([]byte, 32*1024) + for { + for src == tun.client && tun.stalled.Load() { + time.Sleep(5 * time.Millisecond) + } + n, err := src.Read(buf) + if n > 0 && !tun.dropping.Load() { + if _, errWrite := dst.Write(buf[:n]); errWrite != nil { + return + } + } + if err != nil { + return + } + } +} + +func startConnectProxy(t *testing.T, silent bool) *connectProxy { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + p := &connectProxy{url: "http://" + ln.Addr().String()} + var conns sync.WaitGroup + quit := make(chan struct{}) + t.Cleanup(func() { + close(quit) + _ = ln.Close() + conns.Wait() + }) + go func() { + for { + client, errAccept := ln.Accept() + if errAccept != nil { + return + } + p.accepted.Add(1) + conns.Add(1) + go func() { + <-quit + _ = client.Close() + }() + go func() { + defer conns.Done() + defer client.Close() + if !silent { + p.tunnel(client) + } + <-quit + }() + } + }() + return p +} + +func (p *connectProxy) tunnel(client net.Conn) { + req, err := http.ReadRequest(bufio.NewReader(client)) + if err != nil || req.Method != http.MethodConnect { + return + } + _, port, _ := net.SplitHostPort(req.Host) + upstream, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", port)) + if err != nil { + return + } + defer upstream.Close() + p.opened.Add(1) + p.open.Add(1) + defer p.open.Add(-1) + if _, err = io.WriteString(client, "HTTP/1.1 200 Connection established\r\n\r\n"); err != nil { + return + } + tun := &proxyTunnel{client: client} + p.mu.Lock() + p.tunnels = append(p.tunnels, tun) + p.mu.Unlock() + done := make(chan struct{}, 2) + go func() { tun.pipe(upstream, client); done <- struct{}{} }() + go func() { tun.pipe(client, upstream); done <- struct{}{} }() + <-done +} + +func newProxiedUtlsClient(proxyURL string) *http.Client { + return NewUtlsHTTPClient(context.Background(), &config.Config{SDKConfig: sdkconfig.SDKConfig{ProxyURL: proxyURL}}, nil, 0) +} + +func getBody(ctx context.Context, proxyURL, url string) (string, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return "", err + } + resp, err := newProxiedUtlsClient(proxyURL).Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + return string(body), err +} + +// burst runs n concurrent GETs, each through a freshly built client, and returns the failures. +func burst(n int, proxyURL, url string) []error { + var wg sync.WaitGroup + errs := make(chan error, n) + for i := 0; i < n; i++ { + wg.Add(1) + go func() { + defer wg.Done() + body, err := getBody(context.Background(), proxyURL, url) + if err == nil && body != "HTTP/2.0" { + err = fmt.Errorf("proto = %q, want HTTP/2.0", body) + } + if err != nil { + errs <- err + } + }() + } + wg.Wait() + close(errs) + var out []error + for err := range errs { + out = append(out, err) + } + return out +} + +func writeProto(w http.ResponseWriter, r *http.Request) { _, _ = io.WriteString(w, r.Proto) } + +func TestUtlsClientsShareOneTunnel(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + + if errs := burst(50, p.url, chatgptURL(server, "/")); len(errs) > 0 { + t.Fatalf("%d of 50 requests failed, first: %v", len(errs), errs[0]) + } + if got := p.opened.Load(); got != 1 { + t.Fatalf("tunnels opened = %d, want 1", got) + } +} + +func TestUtlsClientRedialsAfterServerClosesIdleConnection(t *testing.T) { + t.Parallel() + server := startChatgptServerWithIdleTimeout(t, true, http.HandlerFunc(writeProto), 200*time.Millisecond) + p := startConnectProxy(t, false) + url := chatgptURL(server, "/") + + if _, err := getBody(context.Background(), p.url, url); err != nil { + t.Fatal(err) + } + deadline := time.Now().Add(5 * time.Second) + for p.open.Load() != 0 { + if time.Now().After(deadline) { + t.Fatal("server never closed the idle connection") + } + time.Sleep(20 * time.Millisecond) + } + if body, err := getBody(context.Background(), p.url, url); err != nil || body != "HTTP/2.0" { + t.Fatalf("request after idle close: body=%q err=%v", body, err) + } + if got := p.opened.Load(); got != 2 { + t.Fatalf("tunnels opened = %d, want 2", got) + } +} + +func TestUtlsClientRequestsReturnOnDeadlineWhenProxyIsSilent(t *testing.T) { + t.Parallel() + p := startConnectProxy(t, true) + + const n = 5 + var wg sync.WaitGroup + results := make(chan error, n) + start := time.Now() + for i := 0; i < n; i++ { + wg.Add(1) + go func() { + defer wg.Done() + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond) + defer cancel() + _, err := getBody(ctx, p.url, "https://chatgpt.com:1/") + results <- err + }() + } + wg.Wait() + close(results) + + if elapsed := time.Since(start); elapsed > 2*time.Second { + t.Fatalf("requests took %v, want them back at their 300ms deadline", elapsed) + } + for err := range results { + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("err = %v, want context deadline exceeded", err) + } + } + if got := p.accepted.Load(); got != 1 { + t.Fatalf("proxy connections = %d, want 1 shared dial", got) + } +} + +func TestUtlsClientRetriesRequestsWhenServerSendsGoAway(t *testing.T) { + t.Parallel() + var served atomic.Int32 + // Each connection serves 50 streams before its GOAWAY, more than the 40 requests in flight, so a + // request refused above last-stream-id fits on the next connection. With 25 and retry backoff + // off in tests, one request could be refused on 7 connections in a row and run out of retries. + server := startChatgptServer(t, true, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if served.Add(1)%50 == 0 { + w.Header().Set("Connection", "close") // the HTTP/2 server answers with GOAWAY + } + writeProto(w, r) + })) + p := startConnectProxy(t, false) + + var wg sync.WaitGroup + var failed atomic.Int32 + var firstErr atomic.Value + for i := 0; i < 40; i++ { + wg.Add(1) + go func() { + defer wg.Done() + for j := 0; j < 5; j++ { + if _, err := getBody(context.Background(), p.url, chatgptURL(server, "/")); err != nil { + failed.Add(1) + firstErr.CompareAndSwap(nil, err) + } + } + }() + } + wg.Wait() + + if n := failed.Load(); n > 0 { + t.Fatalf("%d of 200 requests failed across GOAWAYs, first: %v", n, firstErr.Load()) + } + if got := p.opened.Load(); got < 2 { + t.Fatalf("tunnels opened = %d, want a reconnect after GOAWAY", got) + } +} + +func TestUtlsClientKeepsReceivingConnectionAfterCancelledRequest(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/slow": + <-r.Context().Done() + case "/stream": + for i := 0; i < 20; i++ { + _, _ = io.WriteString(w, "x") + w.(http.Flusher).Flush() + time.Sleep(20 * time.Millisecond) + } + default: + writeProto(w, r) + } + })) + p := startConnectProxy(t, false) + + streamDone := make(chan error, 1) + go func() { + _, err := getBody(context.Background(), p.url, chatgptURL(server, "/stream")) + streamDone <- err + }() + time.Sleep(60 * time.Millisecond) + ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond) + defer cancel() + if _, err := getBody(ctx, p.url, chatgptURL(server, "/slow")); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("slow request err = %v, want context deadline exceeded", err) + } + if err := <-streamDone; err != nil { + t.Fatalf("stream on the shared connection failed: %v", err) + } + if _, err := getBody(context.Background(), p.url, chatgptURL(server, "/")); err != nil { + t.Fatal(err) + } + if got := p.opened.Load(); got != 1 { + t.Fatalf("tunnels opened = %d, want 1", got) + } +} + +func TestUtlsClientDropsSilentTunnelAndRedials(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + url := chatgptURL(server, "/") + + if _, err := getBody(context.Background(), p.url, url); err != nil { + t.Fatal(err) + } + p.blackHoleOpenTunnels() + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond) + defer cancel() + if _, err := getBody(ctx, p.url, url); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("request on the silent tunnel: err = %v, want context deadline exceeded", err) + } + ctx2, cancel2 := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel2() + if body, err := getBody(ctx2, p.url, url); err != nil || body != "HTTP/2.0" { + t.Fatalf("request after the silent tunnel: body=%q err=%v", body, err) + } + if got := p.opened.Load(); got != 2 { + t.Fatalf("tunnels opened = %d, want a fresh tunnel after the silent one", got) + } +} + +func TestUtlsClientReplaysGetAfterAbruptClose(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + url := chatgptURL(server, "/") + + for i := 0; i < 100; i++ { + if _, err := getBody(context.Background(), p.url, url); err != nil { + t.Fatalf("iteration %d: %v", i, err) + } + p.closeOpenTunnels() + } +} + +// h2Action is what rawH2Server does with the nth complete request it receives (n from 1). +type h2Action int + +const ( + h2Respond h2Action = iota + h2ProtocolError + h2RefuseStream + h2CloseConn + h2GoAwayAbove // GOAWAY whose last stream id is below this stream, then close + h2GoAwayCovering // GOAWAY whose last stream id covers this stream, then close + h2MissingStatus // response HEADERS without :status +) + +// rawH2Server is an HTTP/2 server for chatgpt.com that can answer a request it has fully read +// with a stream reset or a dropped connection, which net/http's server cannot be made to do. +type rawH2Server struct { + port string + hits atomic.Int32 +} + +func startRawH2Server(t *testing.T, action func(hit int32) h2Action) *rawH2Server { + t.Helper() + startChatgptServer(t, true, http.NotFoundHandler()) // installs the chatgpt.com cert and test roots + ln, err := stdtls.Listen("tcp", "127.0.0.1:0", &stdtls.Config{Certificates: []stdtls.Certificate{chatgptCert}, NextProtos: []string{"h2"}}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = ln.Close() }) + s := &rawH2Server{} + _, s.port, _ = net.SplitHostPort(ln.Addr().String()) + go func() { + for { + conn, errAccept := ln.Accept() + if errAccept != nil { + return + } + go s.serve(conn, action) + } + }() + return s +} + +func (s *rawH2Server) url(proxyPath string) string { + return "https://chatgpt.com:" + s.port + proxyPath +} + +func (s *rawH2Server) serve(conn net.Conn, action func(hit int32) h2Action) { + defer conn.Close() + if _, err := io.ReadFull(conn, make([]byte, len(http2.ClientPreface))); err != nil { + return + } + framer := http2.NewFramer(conn, conn) + if err := framer.WriteSettings(); err != nil { + return + } + var headerBuf bytes.Buffer + encoder := hpack.NewEncoder(&headerBuf) + for { + frame, err := framer.ReadFrame() + if err != nil { + return + } + var streamID uint32 + switch f := frame.(type) { + case *http2.SettingsFrame: + if !f.IsAck() { + _ = framer.WriteSettingsAck() + } + case *http2.PingFrame: + if !f.IsAck() { + _ = framer.WritePing(true, f.Data) + } + case *http2.HeadersFrame: + if f.StreamEnded() { + streamID = f.StreamID + } + case *http2.DataFrame: + if f.StreamEnded() { + streamID = f.StreamID + } + } + if streamID == 0 { + continue + } + switch action(s.hits.Add(1)) { + case h2ProtocolError: + _ = framer.WriteRSTStream(streamID, http2.ErrCodeProtocol) + case h2RefuseStream: + _ = framer.WriteRSTStream(streamID, http2.ErrCodeRefusedStream) + case h2CloseConn: + return + case h2GoAwayAbove: + _ = framer.WriteGoAway(streamID-1, http2.ErrCodeNo, nil) + return + case h2GoAwayCovering: + _ = framer.WriteGoAway(streamID, http2.ErrCodeNo, nil) + return + case h2MissingStatus: + headerBuf.Reset() + _ = encoder.WriteField(hpack.HeaderField{Name: "content-type", Value: "text/plain"}) + _ = framer.WriteHeaders(http2.HeadersFrameParam{StreamID: streamID, BlockFragment: headerBuf.Bytes(), EndHeaders: true, EndStream: true}) + default: + headerBuf.Reset() + _ = encoder.WriteField(hpack.HeaderField{Name: ":status", Value: "200"}) + _ = framer.WriteHeaders(http2.HeadersFrameParam{StreamID: streamID, BlockFragment: headerBuf.Bytes(), EndHeaders: true, EndStream: true}) + } + } +} + +func doRequest(proxyURL string, req *http.Request) error { + resp, err := newProxiedUtlsClient(proxyURL).Do(req) + if err != nil { + return err + } + _, _ = io.Copy(io.Discard, resp.Body) + return resp.Body.Close() +} + +func TestUtlsClientSendsEachRequestAsOftenAsIsSafe(t *testing.T) { + t.Parallel() + firstThen := func(first h2Action) func(int32) h2Action { + return func(hit int32) h2Action { + if hit == 1 { + return first + } + return h2Respond + } + } + cases := []struct { + name string + method string + idempotencyKey bool + first h2Action + wantOK bool + wantHits int32 + }{ + {"POST is not replayed after PROTOCOL_ERROR", http.MethodPost, false, h2ProtocolError, false, 1}, + {"POST is not replayed after an abrupt close", http.MethodPost, false, h2CloseConn, false, 1}, + {"POST is retried after REFUSED_STREAM", http.MethodPost, false, h2RefuseStream, true, 2}, + {"POST is retried after a GOAWAY below its stream", http.MethodPost, false, h2GoAwayAbove, true, 2}, + {"POST is not resent after a GOAWAY covering its stream", http.MethodPost, false, h2GoAwayCovering, false, 1}, + {"POST with an idempotency key is replayed once after an abrupt close", http.MethodPost, true, h2CloseConn, true, 2}, + {"GET is replayed once after an abrupt close", http.MethodGet, false, h2CloseConn, true, 2}, + {"GET is replayed once after PROTOCOL_ERROR", http.MethodGet, false, h2ProtocolError, true, 2}, + {"GET is not replayed after a malformed response", http.MethodGet, false, h2MissingStatus, false, 1}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + server := startRawH2Server(t, firstThen(tc.first)) + p := startConnectProxy(t, false) + var body io.Reader + if tc.method == http.MethodPost { + body = strings.NewReader(`{"charge":"once"}`) + } + req, err := http.NewRequest(tc.method, server.url("/"), body) + if err != nil { + t.Fatal(err) + } + if tc.idempotencyKey { + req.Header.Set("Idempotency-Key", "charge-once") + } + + err = doRequest(p.url, req) + + if tc.wantOK && err != nil { + t.Fatalf("request failed: %v", err) + } + if !tc.wantOK && err == nil { + t.Fatal("request succeeded, want the first failure returned") + } + if got := server.hits.Load(); got != tc.wantHits { + t.Fatalf("server received the request %d times, want %d", got, tc.wantHits) + } + }) + } +} + +func TestUtlsClientReplaysOnlyOnceWhenTheFailureRepeats(t *testing.T) { + t.Parallel() + server := startRawH2Server(t, func(int32) h2Action { return h2CloseConn }) + p := startConnectProxy(t, false) + req, err := http.NewRequest(http.MethodGet, server.url("/"), nil) + if err != nil { + t.Fatal(err) + } + if err = doRequest(p.url, req); err == nil { + t.Fatal("request succeeded against a server that always drops the connection") + } + if got := server.hits.Load(); got != 2 { + t.Fatalf("server received the GET %d times, want 2", got) + } +} + +// countingBodies hands out request bodies and counts how often each is closed. +type countingBodies struct { + mu sync.Mutex + closes []*atomic.Int32 +} + +type countedBody struct { + io.Reader + closes *atomic.Int32 +} + +func (b countedBody) Close() error { b.closes.Add(1); return nil } + +func (c *countingBodies) next() io.ReadCloser { + c.mu.Lock() + defer c.mu.Unlock() + closes := &atomic.Int32{} + c.closes = append(c.closes, closes) + return countedBody{Reader: strings.NewReader("payload"), closes: closes} +} + +func TestUtlsClientClosesEveryAttemptsBodyExactlyOnce(t *testing.T) { + t.Parallel() + server := startRawH2Server(t, func(int32) h2Action { return h2RefuseStream }) + p := startConnectProxy(t, false) + bodies := &countingBodies{} + req, err := http.NewRequest(http.MethodPost, server.url("/"), bodies.next()) + if err != nil { + t.Fatal(err) + } + req.ContentLength = int64(len("payload")) + req.GetBody = func() (io.ReadCloser, error) { return bodies.next(), nil } + + if err = doRequest(p.url, req); err == nil { + t.Fatal("request succeeded against a server that refuses every stream") + } + bodies.mu.Lock() + defer bodies.mu.Unlock() + if len(bodies.closes) != maxUnprocessedRetries+1 { + t.Fatalf("bodies used = %d, want %d attempts", len(bodies.closes), maxUnprocessedRetries+1) + } + for i, closes := range bodies.closes { + if got := closes.Load(); got != 1 { + t.Fatalf("body %d closed %d times, want once", i, got) + } + } +} + +func TestUtlsClientCancelledRequestKeepsTheSharedConnection(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + url := chatgptURL(server, "/") + + if _, err := getBody(context.Background(), p.url, url); err != nil { + t.Fatal(err) + } + for i := 0; i < 4; i++ { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + if _, err := getBody(ctx, p.url, url); !errors.Is(err, context.Canceled) { + t.Fatalf("cancelled request: err = %v, want context canceled", err) + } + if _, err := getBody(context.Background(), p.url, url); err != nil { + t.Fatal(err) + } + } + if got := p.opened.Load(); got != 1 { + t.Fatalf("tunnels opened = %d, want 1", got) + } +} + +func TestUtlsClientClosesBodyWhenNoConnectionIsObtained(t *testing.T) { + t.Parallel() + p := startConnectProxy(t, true) + + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) + defer cancel() + body := &closeTrackingBody{Reader: strings.NewReader("payload")} + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://chatgpt.com:1/", body) + if err != nil { + t.Fatal(err) + } + if _, err = newProxiedUtlsClient(p.url).Do(req); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("err = %v, want context deadline exceeded", err) + } + if !body.closed.Load() { + t.Fatal("request body left open after waiting for the shared dial") + } +} + +func TestUtlsClientNamesSharedDialFailures(t *testing.T) { + t.Parallel() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + refused := "http://" + ln.Addr().String() + _ = ln.Close() + + if _, err = getBody(context.Background(), refused, "https://chatgpt.com/"); err == nil || !strings.Contains(err.Error(), "utls: shared dial to chatgpt.com:443 failed") { + t.Fatalf("err = %v, want a shared dial error", err) + } +} + +func TestEquivalentProxySettingsShareOneTransport(t *testing.T) { + t.Parallel() + same := func(a, b string) bool { + return UtlsTransport(a) == UtlsTransport(b) && proxyTransport(a, "") == proxyTransport(b, "") + } + if !same("http://127.0.0.1:10", " HTTP://127.0.0.1:10/ ") { + t.Fatal("equivalent proxy URLs got separate transports") + } + if UtlsTransport("") != UtlsTransport("DIRECT") || UtlsTransport("direct") != UtlsTransport("none") { + t.Fatal("direct settings got separate utls transports") + } + if same("http://127.0.0.1:10", "http://127.0.0.1:11") { + t.Fatal("different proxies share a transport") + } +} + +func TestUtlsClientKeepsOneConnectionPerAccount(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + cfg := &config.Config{SDKConfig: sdkconfig.SDKConfig{ProxyURL: p.url}} + + for _, id := range []string{"account-a", "account-b", "account-a", "account-b", "account-a"} { + resp, err := NewUtlsHTTPClient(context.Background(), cfg, &cliproxyauth.Auth{ID: id}, 0).Get(chatgptURL(server, "/")) + if err != nil { + t.Fatalf("%s: %v", id, err) + } + _, _ = io.Copy(io.Discard, resp.Body) + _ = resp.Body.Close() + } + if got := p.opened.Load(); got != 2 { + t.Fatalf("tunnels opened = %d, want one per account", got) + } + if NewProxyAwareHTTPClient(context.Background(), cfg, &cliproxyauth.Auth{ID: "account-a"}, 0).Transport == + NewProxyAwareHTTPClient(context.Background(), cfg, &cliproxyauth.Auth{ID: "account-b"}, 0).Transport { + t.Fatal("two accounts share the fallback transport") + } +} + +func TestUtlsClientRejectsServerThatDoesNotNegotiateHTTP2(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, false, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + + _, err := getBody(context.Background(), p.url, chatgptURL(server, "/")) + if err == nil || !strings.Contains(err.Error(), `want "h2"`) { + t.Fatalf("err = %v, want an ALPN h2 error", err) + } +} + +func TestInvalidProxyURLFailsEveryRequestAndIsNotCached(t *testing.T) { + t.Parallel() + const badProxy = "ftp://proxy.invalid:21" + cfg := &config.Config{SDKConfig: sdkconfig.SDKConfig{ProxyURL: badProxy}} + + for name, client := range map[string]*http.Client{ + "utls": NewUtlsHTTPClient(context.Background(), cfg, nil, 0), + "proxy-aware": NewProxyAwareHTTPClient(context.Background(), cfg, nil, 0), + } { + for _, url := range []string{"https://chatgpt.com/", "https://example.com/"} { + if _, err := client.Get(url); err == nil || !strings.Contains(err.Error(), "invalid proxy") { + t.Fatalf("%s %s: err = %v, want invalid proxy error", name, url, err) + } + } + } + sharedUtlsMu.Lock() + for key := range sharedUtlsTransports { + if strings.Contains(key.proxy, "proxy.invalid") { + t.Errorf("invalid proxy cached in utls transports: %s", key.proxy) + } + } + sharedUtlsMu.Unlock() + sharedProxyMu.Lock() + for key := range sharedProxyTransports { + if strings.Contains(key.proxy, "proxy.invalid") { + t.Errorf("invalid proxy cached in proxy transports: %s", key.proxy) + } + } + sharedProxyMu.Unlock() +} + +func TestClientsForTheSameProxyShareTheFallbackTransport(t *testing.T) { + t.Parallel() + cfg := &config.Config{SDKConfig: sdkconfig.SDKConfig{ProxyURL: "http://127.0.0.1:9"}} + + first := NewProxyAwareHTTPClient(context.Background(), cfg, nil, 0).Transport + if second := NewProxyAwareHTTPClient(context.Background(), cfg, nil, 0).Transport; first != second { + t.Fatal("proxy-aware clients built separate transports for one proxy") + } + if fallback := NewUtlsHTTPClient(context.Background(), cfg, nil, 0).Transport.(*fallbackRoundTripper).fallback; fallback != first { + t.Fatal("utls client fallback does not share the proxy transport") + } +} + +type closeTrackingBody struct { + io.Reader + closed atomic.Bool +} + +func (b *closeTrackingBody) Close() error { b.closed.Store(true); return nil } + +func TestInvalidProxyURLClosesTheRequestBody(t *testing.T) { + t.Parallel() + cfg := &config.Config{SDKConfig: sdkconfig.SDKConfig{ProxyURL: "ftp://proxy.invalid:21"}} + + for name, client := range map[string]*http.Client{ + "utls": NewUtlsHTTPClient(context.Background(), cfg, nil, 0), + "proxy-aware": NewProxyAwareHTTPClient(context.Background(), cfg, nil, 0), + } { + for _, url := range []string{"https://chatgpt.com/", "https://example.com/"} { + body := &closeTrackingBody{Reader: strings.NewReader("payload")} + req, err := http.NewRequest(http.MethodPost, url, body) + if err != nil { + t.Fatal(err) + } + if _, err = client.Do(req); err == nil { + t.Fatalf("%s %s: expected invalid proxy error", name, url) + } + if !body.closed.Load() { + t.Fatalf("%s %s: request body left open", name, url) + } + } + } +} + +// startFlakyFirstConnServer serves healthy HTTP/2 except on its first connection, which it +// closes right after the TLS handshake, before any stream. +func startFlakyFirstConnServer(t *testing.T) (string, *atomic.Int32) { + t.Helper() + startChatgptServer(t, true, http.NotFoundHandler()) + ln, err := stdtls.Listen("tcp", "127.0.0.1:0", &stdtls.Config{Certificates: []stdtls.Certificate{chatgptCert}, NextProtos: []string{"h2"}}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = ln.Close() }) + var accepted atomic.Int32 + h2s := &http2.Server{} + go func() { + for { + conn, errAccept := ln.Accept() + if errAccept != nil { + return + } + first := accepted.Add(1) == 1 + go func() { + if errHandshake := conn.(*stdtls.Conn).Handshake(); errHandshake != nil || first { + _ = conn.Close() + return + } + h2s.ServeConn(conn, &http2.ServeConnOpts{Handler: http.HandlerFunc(writeProto)}) + }() + } + }() + _, port, _ := net.SplitHostPort(ln.Addr().String()) + return "https://chatgpt.com:" + port + "/", &accepted +} + +func TestReviewNotEstablishedConnIsReused(t *testing.T) { + t.Parallel() + url, accepted := startFlakyFirstConnServer(t) + proxy := startConnectProxy(t, false) + + // A caller that gives up at once leaves the shared dial to finish on its own, so the pool + // holds a conn the server closes before its first stream. + ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond) + _, _ = getBody(ctx, proxy.url, url) + cancel() + time.Sleep(300 * time.Millisecond) + + fails := 0 + for i := 0; i < 20; i++ { + if _, err := getBody(context.Background(), proxy.url, url); err == nil { + if fails > 1 { + t.Fatalf("%d requests failed on the dead conn before one redialled", fails) + } + if accepted.Load() < 2 { + t.Fatal("request succeeded without a new connection") + } + return + } + fails++ + time.Sleep(50 * time.Millisecond) + } + t.Fatalf("all %d requests failed on a conn that died before its first stream; none redialled", fails) +} + +func TestUtlsClientWaitsBeforeRetryingAndStopsWhenCancelled(t *testing.T) { + old := retryBackoff + retryBackoff = func(int) time.Duration { return time.Hour } + defer func() { retryBackoff = old }() + server := startRawH2Server(t, func(int32) h2Action { return h2RefuseStream }) + proxy := startConnectProxy(t, false) + + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond) + defer cancel() + body := &closeTrackingBody{Reader: strings.NewReader("payload")} + rewound := &closeTrackingBody{Reader: strings.NewReader("payload")} + req, err := http.NewRequestWithContext(ctx, http.MethodPost, server.url("/"), body) + if err != nil { + t.Fatal(err) + } + req.GetBody = func() (io.ReadCloser, error) { return rewound, nil } + + start := time.Now() + err = doRequest(proxy.url, req) + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("err = %v, want the request's deadline", err) + } + if elapsed := time.Since(start); elapsed > 5*time.Second { + t.Fatalf("returned after %v; the cancelled wait did not stop", elapsed) + } + if hits := server.hits.Load(); hits != 1 { + t.Fatalf("server hits = %d, want 1: the retry did not wait", hits) + } + if !body.closed.Load() || !rewound.closed.Load() { + t.Fatalf("bodies left open: original closed=%v, rewound closed=%v", body.closed.Load(), rewound.closed.Load()) + } +} + +func TestUtlsClientReturnsTheRewindErrorWithoutWaiting(t *testing.T) { + old := retryBackoff + retryBackoff = func(int) time.Duration { return time.Hour } + defer func() { retryBackoff = old }() + server := startRawH2Server(t, func(int32) h2Action { return h2RefuseStream }) + proxy := startConnectProxy(t, false) + + errRewind := errors.New("payload gone") + req, err := http.NewRequest(http.MethodPost, server.url("/"), io.NopCloser(strings.NewReader("payload"))) + if err != nil { + t.Fatal(err) + } + req.GetBody = func() (io.ReadCloser, error) { return nil, errRewind } + + done := make(chan error, 1) + go func() { done <- doRequest(proxy.url, req) }() + select { + case err = <-done: + case <-time.After(5 * time.Second): + t.Fatal("waited for the retry before finding the body could not be rewound") + } + if !errors.Is(err, errRewind) { + t.Fatalf("err = %v, want the rewind error", err) + } +} + +// pooledConns returns the account-less shared connections to server through proxyURL. Keying by +// the test's own server keeps a reused proxy port from mixing in another test's connections. +func pooledConns(t *testing.T, proxyURL string, server *httptest.Server) []*utlsPooledConn { + t.Helper() + rt, err := sharedUtlsTransport(proxyURL, "") + if err != nil { + t.Fatal(err) + } + _, port, _ := net.SplitHostPort(server.Listener.Addr().String()) + rt.pool.mu.Lock() + defer rt.pool.mu.Unlock() + return slices.Clone(rt.pool.conns[net.JoinHostPort("chatgpt.com", port)]) +} + +func waitFor(t *testing.T, what string, cond func() bool) { + t.Helper() + deadline := time.Now().Add(3 * time.Second) + for !cond() { + if time.Now().After(deadline) { + t.Fatalf("timed out waiting for %s", what) + } + time.Sleep(10 * time.Millisecond) + } +} + +func TestUtlsClientRetiresSilentTunnelWhenAStreamedResponseIsCancelled(t *testing.T) { + t.Parallel() + quit := make(chan struct{}) + server := startChatgptServer(t, true, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/sse" { + writeProto(w, r) + return + } + _, _ = io.WriteString(w, "data: first\n\n") + w.(http.Flusher).Flush() + select { + case <-r.Context().Done(): + case <-quit: + } + })) + t.Cleanup(func() { close(quit) }) + p := startConnectProxy(t, false) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, chatgptURL(server, "/sse"), nil) + if err != nil { + t.Fatal(err) + } + resp, err := newProxiedUtlsClient(p.url).Do(req) + if err != nil { + t.Fatal(err) + } + if _, err = resp.Body.Read(make([]byte, 64)); err != nil { + t.Fatalf("first event: %v", err) + } + p.blackHoleOpenTunnels() + cancel() + if _, err = io.ReadAll(resp.Body); !errors.Is(err, context.Canceled) { + t.Fatalf("read after cancel: err = %v, want context canceled", err) + } + _ = resp.Body.Close() + + next, cancelNext := context.WithTimeout(context.Background(), 5*time.Second) + defer cancelNext() + if body, err := getBody(next, p.url, chatgptURL(server, "/")); err != nil || body != "HTTP/2.0" { + t.Fatalf("request after the silent stream: body=%q err=%v", body, err) + } + if got := p.opened.Load(); got != 2 { + t.Fatalf("tunnels opened = %d, want a fresh tunnel after the silent one", got) + } + waitFor(t, "the silent tunnel to close", func() bool { return p.open.Load() == 1 }) +} + +func TestUtlsClientClosesARetiredConnectionStuckBehindAFullSendBuffer(t *testing.T) { + t.Parallel() + startChatgptServer(t, true, http.NotFoundHandler()) // installs the chatgpt.com cert and test roots + // Large flow-control windows let the client write until its TCP send buffer is full. + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = io.Copy(io.Discard, r.Body) + writeProto(w, r) + })) + server.EnableHTTP2 = true + server.Config.HTTP2 = &http.HTTP2Config{MaxReceiveBufferPerConnection: 64 << 20, MaxReceiveBufferPerStream: 64 << 20} + server.TLS = &stdtls.Config{Certificates: []stdtls.Certificate{chatgptCert}} + server.StartTLS() + t.Cleanup(server.Close) + p := startConnectProxy(t, false) + url := chatgptURL(server, "/") + + if _, err := getBody(context.Background(), p.url, url); err != nil { + t.Fatal(err) + } + conns := pooledConns(t, p.url, server) + if len(conns) != 1 { + t.Fatalf("pooled conns = %d, want 1", len(conns)) + } + resume := p.stallOpenTunnels() + defer resume() + + ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(make([]byte, 48<<20))) + if err != nil { + t.Fatal(err) + } + if err = doRequest(p.url, req); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("POST on the stalled tunnel: err = %v, want context deadline exceeded", err) + } + waitFor(t, "the retired connection's socket to close", conns[0].dead.Load) + if body, err := getBody(context.Background(), p.url, url); err != nil || body != "HTTP/2.0" { + t.Fatalf("request after the stalled tunnel: body=%q err=%v", body, err) + } +} + +func TestUtlsClientDropsClosedConnectionsFromThePool(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + url := chatgptURL(server, "/") + + if _, err := getBody(context.Background(), p.url, url); err != nil { + t.Fatal(err) + } + first := pooledConns(t, p.url, server) + if len(first) != 1 { + t.Fatalf("pooled conns = %d, want 1", len(first)) + } + p.closeOpenTunnels() + waitFor(t, "the cut connection to be seen as closed", first[0].dead.Load) + if _, err := getBody(context.Background(), p.url, url); err != nil { + t.Fatal(err) + } + if got := pooledConns(t, p.url, server); len(got) != 1 || got[0] == first[0] { + t.Fatalf("pool after the cut = %d conns (closed one kept: %v), want only the new one", len(got), slices.Contains(got, first[0])) + } +} + +func TestUtlsClientReplaysOnlySafeRequestsAfterAForcedClose(t *testing.T) { + t.Parallel() + for _, method := range []string{http.MethodGet, http.MethodPost} { + t.Run(method, func(t *testing.T) { + t.Parallel() + var hits atomic.Int32 + arrived := make(chan struct{}, 1) + quit := make(chan struct{}) + server := startChatgptServer(t, true, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if hits.Add(1) == 1 { + arrived <- struct{}{} + select { + case <-r.Context().Done(): + case <-quit: + } + return + } + writeProto(w, r) + })) + t.Cleanup(func() { close(quit) }) + p := startConnectProxy(t, false) + + req, err := http.NewRequest(method, chatgptURL(server, "/"), strings.NewReader("payload")) + if err != nil { + t.Fatal(err) + } + done := make(chan error, 1) + go func() { done <- doRequest(p.url, req) }() + <-arrived + conns := pooledConns(t, p.url, server) + if len(conns) != 1 { + t.Fatalf("pooled conns = %d, want 1", len(conns)) + } + _ = conns[0].cc.Close() + err = <-done + if method == http.MethodGet { + if err != nil || hits.Load() != 2 { + t.Fatalf("GET: err=%v hits=%d, want replayed once and succeeded", err, hits.Load()) + } + return + } + if err == nil || hits.Load() != 1 { + t.Fatalf("POST: err=%v hits=%d, want the forced close returned and no resend", err, hits.Load()) + } + }) + } +} + +// A request that reserved a stream must keep its connection even when another request's cancel +// retires that connection before the first one is sent: closing it would abort a plain POST the +// server already received, and such a POST is never resent. +func TestUtlsClientRetireNeverClosesUnderAReservedRequest(t *testing.T) { + aStarted := make(chan struct{}) + postReceived := make(chan struct{}) + var postHits atomic.Int32 + quit := make(chan struct{}) + server := startChatgptServer(t, true, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/quiet": + close(aStarted) + select { + case <-r.Context().Done(): + case <-quit: + } + case "/post": + if postHits.Add(1) == 1 { + close(postReceived) + } + time.Sleep(300 * time.Millisecond) + _, _ = io.WriteString(w, "ok") + default: + writeProto(w, r) + } + })) + t.Cleanup(func() { close(quit) }) + p := startConnectProxy(t, false) + if _, err := getBody(context.Background(), p.url, chatgptURL(server, "/")); err != nil { + t.Fatal(err) + } + + bReserved := make(chan struct{}) + goB := make(chan struct{}) + var releaseB sync.Once + letBGo := func() { releaseB.Do(func() { close(goB) }) } + testHooks.Store(&utlsTestHooks{ + afterReserve: func(req *http.Request) { + if req.URL.Path == "/post" { + close(bReserved) + <-goB + } + }, + beforeRetiredClose: func() { + letBGo() + select { + case <-postReceived: + case <-time.After(2 * time.Second): + } + }, + }) + defer testHooks.Store(nil) + + bDone := make(chan error, 1) + go func() { + req, err := http.NewRequest(http.MethodPost, chatgptURL(server, "/post"), strings.NewReader("charge-once")) + if err != nil { + bDone <- err + return + } + resp, err := newProxiedUtlsClient(p.url).Do(req) + if err != nil { + bDone <- err + return + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err == nil && string(body) != "ok" { + err = fmt.Errorf("body = %q, want ok", body) + } + bDone <- err + }() + <-bReserved + + ctx, cancel := context.WithCancel(context.Background()) + aDone := make(chan error, 1) + go func() { + _, err := getBody(ctx, p.url, chatgptURL(server, "/quiet")) + aDone <- err + }() + <-aStarted + cancel() + if err := <-aDone; !errors.Is(err, context.Canceled) { + t.Fatalf("quiet request err = %v, want context canceled", err) + } + letBGo() + + if err := <-bDone; err != nil { + t.Fatalf("reserved POST failed after another request retired its connection: %v", err) + } + if got := postHits.Load(); got != 1 { + t.Fatalf("POST reached the server %d times, want 1", got) + } +} + +func TestUtlsClientNeverReservesARetiredConnection(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + if _, err := getBody(context.Background(), p.url, chatgptURL(server, "/")); err != nil { + t.Fatal(err) + } + conns := pooledConns(t, p.url, server) + if len(conns) != 1 { + t.Fatalf("pooled conns = %d, want 1", len(conns)) + } + conn := conns[0] + if !conn.tryReserve() { + t.Fatal("healthy conn refused a reservation") + } + conn.retire() + if conn.tryReserve() { + t.Fatal("retired conn accepted a new reservation") + } + if conn.cc.State().Closed { + t.Fatal("retire closed the conn under a reserved request") + } + conn.done() + waitFor(t, "the retired conn to close", func() bool { return p.open.Load() == 0 }) + rt, err := sharedUtlsTransport(p.url, "") + if err != nil { + t.Fatal(err) + } + rt.pool.MarkDead(conn.cc) +} diff --git a/internal/runtime/executor/xai_websockets_executor.go b/internal/runtime/executor/xai_websockets_executor.go index 057845446e5..00fcb8f3e5a 100644 --- a/internal/runtime/executor/xai_websockets_executor.go +++ b/internal/runtime/executor/xai_websockets_executor.go @@ -877,7 +877,10 @@ func (e *XAIWebsocketsExecutor) prepareResponsesWebsocketRequest(ctx context.Con } func (e *XAIWebsocketsExecutor) dialXAIWebsocket(ctx context.Context, auth *cliproxyauth.Auth, wsURL string, headers http.Header) (*websocket.Conn, *http.Response, error) { - dialer := newProxyAwareWebsocketDialer(e.cfg, auth) + dialer, errDialer := newProxyAwareWebsocketDialer(e.cfg, auth) + if errDialer != nil { + return nil, nil, errDialer + } dialer.HandshakeTimeout = codexResponsesWebsocketHandshakeTO dialer.EnableCompression = true if ctx == nil { diff --git a/internal/util/proxy.go b/internal/util/proxy.go index 781dd54dc0e..10c9c55ae79 100644 --- a/internal/util/proxy.go +++ b/internal/util/proxy.go @@ -4,6 +4,7 @@ package util import ( + "fmt" "net/http" "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" @@ -12,8 +13,8 @@ import ( ) // SetProxy configures the provided HTTP client with proxy settings from the configuration. -// It supports SOCKS5, HTTP, and HTTPS proxies. The function modifies the client's transport -// to route requests through the configured proxy server. +// It supports SOCKS5, HTTP, and HTTPS proxies. An unusable proxy setting gives the client a +// transport that fails every request, so traffic never silently goes direct. func SetProxy(cfg *config.SDKConfig, httpClient *http.Client) *http.Client { if cfg == nil || httpClient == nil { return httpClient @@ -21,7 +22,10 @@ func SetProxy(cfg *config.SDKConfig, httpClient *http.Client) *http.Client { transport, _, errBuild := proxyutil.BuildHTTPTransport(cfg.ProxyURL) if errBuild != nil { - log.Errorf("%v", errBuild) + err := fmt.Errorf("invalid proxy %s: %w", proxyutil.Redact(cfg.ProxyURL), errBuild) + log.Error(err) + httpClient.Transport = proxyutil.FailingTransport(err) + return httpClient } if transport != nil { httpClient.Transport = transport diff --git a/internal/util/proxy_test.go b/internal/util/proxy_test.go new file mode 100644 index 00000000000..4c12d546f88 --- /dev/null +++ b/internal/util/proxy_test.go @@ -0,0 +1,26 @@ +package util + +import ( + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + + "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" +) + +func TestSetProxyWithInvalidProxyFailsInsteadOfGoingDirect(t *testing.T) { + t.Parallel() + var reached atomic.Bool + server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached.Store(true) })) + defer server.Close() + + client := SetProxy(&config.SDKConfig{ProxyURL: "ftp://proxy.invalid:21"}, &http.Client{}) + if _, err := client.Get(server.URL); err == nil || !strings.Contains(err.Error(), "invalid proxy") { + t.Fatalf("err = %v, want invalid proxy error", err) + } + if reached.Load() { + t.Fatal("request went direct despite the invalid proxy") + } +} diff --git a/sdk/cliproxy/antigravity_models.go b/sdk/cliproxy/antigravity_models.go index 11f7c408d9a..767620de753 100644 --- a/sdk/cliproxy/antigravity_models.go +++ b/sdk/cliproxy/antigravity_models.go @@ -38,7 +38,13 @@ func (s *Service) fetchAntigravityModelCapabilityHintsForAuth(ctx context.Contex } client := &http.Client{} - if transport, _, errProxy := proxyutil.BuildHTTPTransport(s.antigravityModelFetchProxyURL(auth)); errProxy == nil && transport != nil { + proxyURL := s.antigravityModelFetchProxyURL(auth) + transport, _, errProxy := proxyutil.BuildHTTPTransport(proxyURL) + if errProxy != nil { + log.Errorf("antigravity model hints: invalid proxy %s: %v", proxyutil.Redact(proxyURL), errProxy) + return antigravityModelCapabilityHints{} + } + if transport != nil { client.Transport = transport } diff --git a/sdk/cliproxy/rtprovider.go b/sdk/cliproxy/rtprovider.go index d07b4cb4f97..0dddc38e3e2 100644 --- a/sdk/cliproxy/rtprovider.go +++ b/sdk/cliproxy/rtprovider.go @@ -1,13 +1,13 @@ package cliproxy import ( + "fmt" "net/http" "strings" "sync" coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" "github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil" - log "github.com/sirupsen/logrus" ) // defaultRoundTripperProvider returns a per-auth HTTP RoundTripper based on @@ -38,8 +38,8 @@ func (p *defaultRoundTripperProvider) RoundTripperFor(auth *coreauth.Auth) http. } transport, _, errBuild := proxyutil.BuildHTTPTransport(proxyStr) if errBuild != nil { - log.Errorf("%v", errBuild) - return nil + // Fail every request rather than silently bypassing the configured proxy; not cached. + return proxyutil.FailingTransport(fmt.Errorf("invalid proxy %s: %w", proxyutil.Redact(proxyStr), errBuild)) } if transport == nil { return nil diff --git a/sdk/cliproxy/rtprovider_test.go b/sdk/cliproxy/rtprovider_test.go index 6ea08432c13..64de8a2a8b9 100644 --- a/sdk/cliproxy/rtprovider_test.go +++ b/sdk/cliproxy/rtprovider_test.go @@ -20,3 +20,16 @@ func TestRoundTripperForDirectBypassesProxy(t *testing.T) { t.Fatal("expected direct transport to disable proxy function") } } + +func TestRoundTripperForInvalidProxyFailsRequests(t *testing.T) { + t.Parallel() + + rt := newDefaultRoundTripperProvider().RoundTripperFor(&coreauth.Auth{ProxyURL: "ftp://proxy.example.com:21"}) + if rt == nil { + t.Fatal("invalid proxy returned nil, which silently falls back to a direct connection") + } + req, _ := http.NewRequest(http.MethodGet, "https://example.com/", nil) + if _, err := rt.RoundTrip(req); err == nil { + t.Fatal("expected an invalid proxy error") + } +} diff --git a/sdk/proxyutil/proxy.go b/sdk/proxyutil/proxy.go index 507d5e09e88..1a61eb969ca 100644 --- a/sdk/proxyutil/proxy.go +++ b/sdk/proxyutil/proxy.go @@ -9,7 +9,9 @@ import ( "net" "net/http" "net/url" + "strconv" "strings" + "time" "golang.org/x/net/proxy" ) @@ -55,20 +57,49 @@ func Parse(raw string) (Setting, error) { setting.Mode = ModeInvalid return setting, fmt.Errorf("parse proxy URL failed") } - if parsedURL.Scheme == "" || parsedURL.Host == "" { + if parsedURL.Scheme == "" || parsedURL.Hostname() == "" { setting.Mode = ModeInvalid return setting, fmt.Errorf("proxy URL missing scheme/host") } switch parsedURL.Scheme { case "socks5", "socks5h", "http", "https": - setting.Mode = ModeProxy - setting.URL = parsedURL - return setting, nil default: setting.Mode = ModeInvalid return setting, fmt.Errorf("unsupported proxy scheme: %s", parsedURL.Scheme) } + if errPort := checkProxyPort(parsedURL); errPort != nil { + setting.Mode = ModeInvalid + return setting, errPort + } + setting.Mode = ModeProxy + setting.URL = parsedURL + return setting, nil +} + +// checkProxyPort requires a well-formed host:port with a port in 1-65535 when a port is given, +// and always for SOCKS5. +func checkProxyPort(u *url.URL) error { + if u.Port() == "" && !strings.HasSuffix(u.Host, ":") { + if u.Scheme == "socks5" || u.Scheme == "socks5h" { + return fmt.Errorf("SOCKS5 proxy URL needs a port") + } + return nil + } + host, port, errSplit := net.SplitHostPort(u.Host) + if errSplit != nil { + return fmt.Errorf("proxy URL host and port are malformed") + } + if host == "" { + return fmt.Errorf("proxy URL missing scheme/host") + } + if port == "" { + return fmt.Errorf("proxy URL has an empty port") + } + if n, errAtoi := strconv.Atoi(port); errAtoi != nil || n < 1 || n > 65535 { + return fmt.Errorf("proxy URL port %q is out of range", port) + } + return nil } func cloneDefaultTransport() *http.Transport { @@ -152,22 +183,43 @@ func BuildDialer(raw string) (proxy.Dialer, Mode, error) { type httpConnectDialer struct { proxyURL *url.URL - dialer proxy.Dialer + dialer proxy.ContextDialer } func (d *httpConnectDialer) Dial(network, addr string) (net.Conn, error) { - proxyConn, errDial := d.dialer.Dial(network, proxyDialAddr(d.proxyURL)) + return d.DialContext(context.Background(), network, addr) +} + +// DialContext dials through the proxy; ctx bounds the proxy dial, its TLS handshake and the CONNECT exchange. +func (d *httpConnectDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { + proxyConn, errDial := d.dialer.DialContext(ctx, network, proxyDialAddr(d.proxyURL)) if errDial != nil { return nil, fmt.Errorf("dial HTTP proxy failed: %w", errDial) } + conn, errConnect := d.connect(ctx, proxyConn, addr) + if errConnect != nil { + if errClose := proxyConn.Close(); errClose != nil { + return nil, fmt.Errorf("%w; close failed: %v", errConnect, errClose) + } + return nil, errConnect + } + return conn, nil +} + +func (d *httpConnectDialer) connect(ctx context.Context, proxyConn net.Conn, addr string) (net.Conn, error) { + if deadline, ok := ctx.Deadline(); ok { + if errDeadline := proxyConn.SetDeadline(deadline); errDeadline != nil { + return nil, fmt.Errorf("set proxy deadline failed: %w", errDeadline) + } + } + stop := context.AfterFunc(ctx, func() { _ = proxyConn.SetDeadline(time.Unix(1, 0)) }) + defer stop() + conn := proxyConn if d.proxyURL.Scheme == "https" { tlsConn := tls.Client(conn, &tls.Config{ServerName: d.proxyURL.Hostname()}) - if errHandshake := tlsConn.Handshake(); errHandshake != nil { - if errClose := conn.Close(); errClose != nil { - return nil, fmt.Errorf("HTTPS proxy TLS handshake failed: %w; close failed: %v", errHandshake, errClose) - } + if errHandshake := tlsConn.HandshakeContext(ctx); errHandshake != nil { return nil, fmt.Errorf("HTTPS proxy TLS handshake failed: %w", errHandshake) } conn = tlsConn @@ -183,36 +235,44 @@ func (d *httpConnectDialer) Dial(network, addr string) (net.Conn, error) { req.Header.Set("Proxy-Authorization", proxyAuthorization(d.proxyURL.User)) } if errWrite := req.Write(conn); errWrite != nil { - if errClose := conn.Close(); errClose != nil { - return nil, fmt.Errorf("write CONNECT request failed: %w; close failed: %v", errWrite, errClose) - } - return nil, fmt.Errorf("write CONNECT request failed: %w", errWrite) + return nil, fmt.Errorf("write CONNECT request failed: %w", contextError(ctx, errWrite)) } reader := bufio.NewReader(conn) resp, errRead := http.ReadResponse(reader, req) if errRead != nil { - if errClose := conn.Close(); errClose != nil { - return nil, fmt.Errorf("read CONNECT response failed: %w; close failed: %v", errRead, errClose) - } - return nil, fmt.Errorf("read CONNECT response failed: %w", errRead) + return nil, fmt.Errorf("read CONNECT response failed: %w", contextError(ctx, errRead)) } - if resp.StatusCode != http.StatusOK { - if resp.Body != nil { - _ = resp.Body.Close() - } - if errClose := conn.Close(); errClose != nil { - return nil, fmt.Errorf("proxy CONNECT returned status %s; close failed: %v", resp.Status, errClose) - } + // RFC 9110 9.3.6: a 2xx CONNECT reply has no body, so never read resp.Body; bytes after the headers belong to the tunnel. + // A rejected CONNECT is not drained: the caller closes the socket. + if resp.StatusCode < 200 || resp.StatusCode > 299 { return nil, fmt.Errorf("proxy CONNECT returned status %s", resp.Status) } + if !stop() { + return nil, ctx.Err() + } + if errDeadline := proxyConn.SetDeadline(time.Time{}); errDeadline != nil { + return nil, fmt.Errorf("clear proxy deadline failed: %w", errDeadline) + } if reader.Buffered() > 0 { return &bufferedConn{Conn: conn, reader: reader}, nil } return conn, nil } +// contextError prefers the context's error when the I/O failed because ctx ended. +// The conn deadline can fire just before ctx records its own expiry, so a passed deadline counts too. +func contextError(ctx context.Context, err error) error { + if ctxErr := ctx.Err(); ctxErr != nil { + return ctxErr + } + if deadline, ok := ctx.Deadline(); ok && !time.Now().Before(deadline) { + return context.DeadlineExceeded + } + return err +} + func proxyDialAddr(proxyURL *url.URL) string { port := proxyURL.Port() if port == "" { @@ -264,3 +324,16 @@ func (c *bufferedConn) Read(p []byte) (int, error) { } return c.Conn.Read(p) } + +// FailingTransport returns a RoundTripper that fails every request with err, for a proxy +// setting that cannot be used; it closes the request body as the RoundTripper contract requires. +func FailingTransport(err error) http.RoundTripper { return failingTransport{err: err} } + +type failingTransport struct{ err error } + +func (f failingTransport) RoundTrip(req *http.Request) (*http.Response, error) { + if req.Body != nil { + _ = req.Body.Close() + } + return nil, f.err +} diff --git a/sdk/proxyutil/proxy_connect_test.go b/sdk/proxyutil/proxy_connect_test.go new file mode 100644 index 00000000000..7d1127548c8 --- /dev/null +++ b/sdk/proxyutil/proxy_connect_test.go @@ -0,0 +1,160 @@ +package proxyutil + +import ( + "bufio" + "context" + "errors" + "io" + "net" + "net/http" + "testing" + "time" + + "golang.org/x/net/proxy" +) + +// startFakeConnectProxy runs respond on every connection after reading its CONNECT request. +func startFakeConnectProxy(t *testing.T, respond func(net.Conn)) string { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = ln.Close() }) + go func() { + for { + conn, errAccept := ln.Accept() + if errAccept != nil { + return + } + go func() { + defer func() { _ = conn.Close() }() + if _, errRead := http.ReadRequest(bufio.NewReader(conn)); errRead != nil { + return + } + respond(conn) + }() + } + }() + return "http://" + ln.Addr().String() +} + +func connectDialer(t *testing.T, proxyURL string) proxy.ContextDialer { + t.Helper() + dialer, _, err := BuildDialer(proxyURL) + if err != nil { + t.Fatal(err) + } + return dialer.(proxy.ContextDialer) +} + +func TestConnectDialReturnsWhenContextEndsAndClosesProxyConn(t *testing.T) { + t.Parallel() + for name, tc := range map[string]struct { + ctx func() (context.Context, context.CancelFunc) + want error + }{ + "deadline": {func() (context.Context, context.CancelFunc) { + return context.WithTimeout(context.Background(), 200*time.Millisecond) + }, context.DeadlineExceeded}, + "cancel": {func() (context.Context, context.CancelFunc) { + ctx, cancel := context.WithCancel(context.Background()) + time.AfterFunc(100*time.Millisecond, cancel) + return ctx, cancel + }, context.Canceled}, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + closed := make(chan struct{}) + proxyURL := startFakeConnectProxy(t, func(conn net.Conn) { + _, _ = conn.Read(make([]byte, 1)) // stays silent until the client hangs up + close(closed) + }) + ctx, cancel := tc.ctx() + defer cancel() + start := time.Now() + _, err := connectDialer(t, proxyURL).DialContext(ctx, "tcp", "example.com:443") + if !errors.Is(err, tc.want) || time.Since(start) > 2*time.Second { + t.Fatalf("err = %v after %v, want %v promptly", err, time.Since(start), tc.want) + } + select { + case <-closed: + case <-time.After(2 * time.Second): + t.Fatal("proxy connection left open after failed dial") + } + }) + } +} + +func TestConnectTunnelOutlivesDialContext(t *testing.T) { + t.Parallel() + proxyURL := startFakeConnectProxy(t, func(conn net.Conn) { + _, _ = io.WriteString(conn, "HTTP/1.1 200 Connection Established\r\n\r\n") + time.Sleep(400 * time.Millisecond) + _, _ = io.WriteString(conn, "late") + _, _ = conn.Read(make([]byte, 1)) + }) + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) + conn, err := connectDialer(t, proxyURL).DialContext(ctx, "tcp", "example.com:443") + cancel() + if err != nil { + t.Fatal(err) + } + defer func() { _ = conn.Close() }() + _ = conn.SetReadDeadline(time.Now().Add(2 * time.Second)) + buf := make([]byte, 4) + if _, errRead := io.ReadFull(conn, buf); errRead != nil || string(buf) != "late" { + t.Fatalf("tunnel read after dial context ended: err = %v, got %q", errRead, buf) + } +} + +// RFC 9110 9.3.6: framing headers on a 2xx CONNECT reply must be ignored; the bytes after it are tunnel data. +func TestConnectSuccessIgnoresFramingHeadersAndKeepsTunnelBytes(t *testing.T) { + t.Parallel() + for name, header := range map[string]string{ + "content-length": "Content-Length: 3\r\n", + "chunked": "Transfer-Encoding: chunked\r\n", + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + proxyURL := startFakeConnectProxy(t, func(conn net.Conn) { + _, _ = io.WriteString(conn, "HTTP/1.1 200 Connection Established\r\n"+header+"\r\nTLS!") + _, _ = conn.Read(make([]byte, 1)) + }) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + start := time.Now() + conn, err := connectDialer(t, proxyURL).DialContext(ctx, "tcp", "example.com:443") + if err != nil || time.Since(start) > time.Second { + t.Fatalf("dial: err = %v after %v", err, time.Since(start)) + } + defer func() { _ = conn.Close() }() + _ = conn.SetReadDeadline(time.Now().Add(time.Second)) + buf := make([]byte, 4) + if _, errRead := io.ReadFull(conn, buf); errRead != nil || string(buf) != "TLS!" { + t.Fatalf("tunnel bytes: err = %v, got %q, want %q", errRead, buf, "TLS!") + } + }) + } +} + +func TestConnectRejectedReturnsStatusError(t *testing.T) { + t.Parallel() + closed := make(chan struct{}) + proxyURL := startFakeConnectProxy(t, func(conn net.Conn) { + _, _ = io.WriteString(conn, "HTTP/1.1 407 Proxy Authentication Required\r\nTransfer-Encoding: chunked\r\n\r\n") + _, _ = conn.Read(make([]byte, 1)) + close(closed) + }) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + _, err := connectDialer(t, proxyURL).DialContext(ctx, "tcp", "example.com:443") + if err == nil || errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("err = %v, want a 407 status error", err) + } + select { + case <-closed: + case <-time.After(time.Second): + t.Fatal("proxy connection left open after rejected CONNECT") + } +} diff --git a/sdk/proxyutil/proxy_test.go b/sdk/proxyutil/proxy_test.go index 1c957ef7a0b..73564110e49 100644 --- a/sdk/proxyutil/proxy_test.go +++ b/sdk/proxyutil/proxy_test.go @@ -39,6 +39,21 @@ func TestParse(t *testing.T) { {name: "socks5", input: "socks5://proxy.example.com:1080", want: ModeProxy}, {name: "socks5h", input: "socks5h://proxy.example.com:1080", want: ModeProxy}, {name: "invalid", input: "bad-value", want: ModeInvalid, wantErr: true}, + {name: "direct any case padded", input: " DIRECT ", want: ModeDirect}, + {name: "none any case", input: "None", want: ModeDirect}, + {name: "http without port", input: "http://proxy.example.com", want: ModeProxy}, + {name: "empty hostname", input: "http://:8080", want: ModeInvalid, wantErr: true}, + {name: "empty socks hostname", input: "socks5://:1080", want: ModeInvalid, wantErr: true}, + {name: "empty port", input: "http://proxy.example.com:", want: ModeInvalid, wantErr: true}, + {name: "port zero", input: "http://proxy.example.com:0", want: ModeInvalid, wantErr: true}, + {name: "port too large", input: "http://127.0.0.1:65536", want: ModeInvalid, wantErr: true}, + {name: "ipv6 port too large", input: "https://[::1]:99999", want: ModeInvalid, wantErr: true}, + {name: "socks5 without port", input: "socks5://proxy.example.com", want: ModeInvalid, wantErr: true}, + {name: "socks5h port too large", input: "socks5h://proxy.example.com:99999", want: ModeInvalid, wantErr: true}, + {name: "socks5 extra port", input: "socks5://localhost:1080:80", want: ModeInvalid, wantErr: true}, + {name: "http extra port", input: "http://proxy.example.com:8080:80", want: ModeInvalid, wantErr: true}, + {name: "ipv6 with port", input: "socks5://[::1]:1080", want: ModeProxy}, + {name: "ipv6 without port", input: "http://[::1]", want: ModeProxy}, } for _, tt := range tests {