From 0b5e416593b489a0437ba791e858643a106731c9 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 08:37:45 +0000 Subject: [PATCH 01/14] Share one uTLS HTTP/2 connection per proxy across requests Each Codex/Claude request built a fresh uTLS round tripper, so no connection was reused and every request left an idle proxy tunnel open until the server closed it (~7 min). Keep one round tripper per proxy URL for the process and close idle HTTP/2 connections after 90s, with 30s/15s health pings. Co-Authored-By: Claude Opus 5.5 --- .../runtime/executor/helps/utls_client.go | 33 +++++- .../executor/helps/utls_client_test.go | 100 ++++++++++++++++++ 2 files changed, 129 insertions(+), 4 deletions(-) diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index ad3315c6633..887d06af556 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -2,6 +2,7 @@ package helps import ( "context" + "crypto/x509" "net" "net/http" "strings" @@ -24,6 +25,26 @@ type utlsRoundTripper struct { connections map[string]*http2.ClientConn pending map[string]*sync.Cond dialer proxy.Dialer + h2 *http2.Transport + rootCAs *x509.CertPool +} + +// One round tripper per proxy URL for the whole process, so requests reuse +// HTTP/2 connections instead of leaving one idle tunnel per request. +var ( + sharedUtlsMu sync.Mutex + sharedUtlsRTs = map[string]*utlsRoundTripper{} +) + +func sharedUtlsRoundTripper(proxyURL string) *utlsRoundTripper { + sharedUtlsMu.Lock() + defer sharedUtlsMu.Unlock() + rt, ok := sharedUtlsRTs[proxyURL] + if !ok { + rt = newUtlsRoundTripper(proxyURL) + sharedUtlsRTs[proxyURL] = rt + } + return rt } func newUtlsRoundTripper(proxyURL string) *utlsRoundTripper { @@ -40,6 +61,11 @@ func newUtlsRoundTripper(proxyURL string) *utlsRoundTripper { connections: make(map[string]*http2.ClientConn), pending: make(map[string]*sync.Cond), dialer: dialer, + h2: &http2.Transport{ + IdleConnTimeout: 90 * time.Second, + ReadIdleTimeout: 30 * time.Second, + PingTimeout: 15 * time.Second, + }, } } @@ -85,7 +111,7 @@ func (t *utlsRoundTripper) createConnection(host, addr string) (*http2.ClientCon return nil, err } - tlsConfig := &tls.Config{ServerName: host} + tlsConfig := &tls.Config{ServerName: host, RootCAs: t.rootCAs} tlsConn := tls.UClient(conn, tlsConfig, tls.HelloChrome_Auto) if err := tlsConn.Handshake(); err != nil { @@ -93,8 +119,7 @@ func (t *utlsRoundTripper) createConnection(host, addr string) (*http2.ClientCon return nil, err } - tr := &http2.Transport{} - h2Conn, err := tr.NewClientConn(tlsConn) + h2Conn, err := t.h2.NewClientConn(tlsConn) if err != nil { tlsConn.Close() return nil, err @@ -169,7 +194,7 @@ func NewUtlsHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyau ctxRoundTripper, _ = ctx.Value("cliproxy.roundtripper").(http.RoundTripper) } - var utlsRT http.RoundTripper = newUtlsRoundTripper(proxyURL) + var utlsRT http.RoundTripper = sharedUtlsRoundTripper(proxyURL) var standardTransport http.RoundTripper = http.DefaultTransport if proxyURL != "" { if transport := buildProxyTransport(proxyURL); transport != nil { diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index 093ad4bef7c..abd68a7d286 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -1,11 +1,17 @@ package helps import ( + "bufio" "context" + "crypto/x509" "io" + "net" "net/http" + "net/http/httptest" "strings" + "sync/atomic" "testing" + "time" ) type utlsClientRoundTripFunc func(*http.Request) (*http.Response, error) @@ -43,3 +49,97 @@ func TestNewUtlsHTTPClientUsesContextRoundTripperForProtectedHost(t *testing.T) t.Fatal("expected context RoundTripper to handle protected host request") } } + +// countingConnectProxy is an HTTP CONNECT proxy that counts open tunnels. +type countingConnectProxy struct { + ln net.Listener + opened atomic.Int32 + open atomic.Int32 +} + +func startCountingConnectProxy(t *testing.T) *countingConnectProxy { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + p := &countingConnectProxy{ln: ln} + t.Cleanup(func() { _ = ln.Close() }) + go func() { + for { + client, errAccept := ln.Accept() + if errAccept != nil { + return + } + go p.serve(client) + } + }() + return p +} + +func (p *countingConnectProxy) serve(client net.Conn) { + defer client.Close() + req, err := http.ReadRequest(bufio.NewReader(client)) + if err != nil || req.Method != http.MethodConnect { + return + } + upstream, err := net.Dial("tcp", req.Host) + 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 + } + done := make(chan struct{}, 2) + go func() { _, _ = io.Copy(upstream, client); done <- struct{}{} }() + go func() { _, _ = io.Copy(client, upstream); done <- struct{}{} }() + <-done +} + +func TestUtlsRoundTripperSharesOneTunnelPerProxyAndClosesItWhenIdle(t *testing.T) { + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = io.WriteString(w, r.Proto) + })) + server.EnableHTTP2 = true + server.StartTLS() + defer server.Close() + + proxyServer := startCountingConnectProxy(t) + proxyURL := "http://" + proxyServer.ln.Addr().String() + + rt := sharedUtlsRoundTripper(proxyURL) + rt.rootCAs = x509.NewCertPool() + rt.rootCAs.AddCert(server.Certificate()) + rt.h2.IdleConnTimeout = 300 * time.Millisecond + + for i := 0; i < 5; i++ { + req, err := http.NewRequest(http.MethodGet, server.URL, nil) + if err != nil { + t.Fatal(err) + } + resp, err := sharedUtlsRoundTripper(proxyURL).RoundTrip(req) + if err != nil { + t.Fatalf("request %d: %v", i, err) + } + body, _ := io.ReadAll(resp.Body) + _ = resp.Body.Close() + if string(body) != "HTTP/2.0" { + t.Fatalf("request %d proto = %q, want HTTP/2.0", i, body) + } + } + + if got := proxyServer.opened.Load(); got != 1 { + t.Fatalf("tunnels opened = %d, want 1", got) + } + deadline := time.Now().Add(5 * time.Second) + for proxyServer.open.Load() != 0 { + if time.Now().After(deadline) { + t.Fatalf("idle tunnel still open after idle timeout") + } + time.Sleep(20 * time.Millisecond) + } +} From d168104cd10e2b63a9ac7c9610884444b268cc67 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 09:02:28 +0000 Subject: [PATCH 02/14] Use http2.Transport with a cancellable conn pool for shared uTLS connections Review fixes for the shared uTLS connection: - One http2.Transport per proxy URL with a ConnPool that dials once per host:port, detached from any one request and bounded at 30s, and lets each waiting request give up on its own context. The transport supplies stream reservation and retries for GOAWAY, REFUSED_STREAM and unusable connections; a cancelled stream no longer drops the connection. - Proxy CONNECT dialer honours the context (dial, TLS, CONNECT exchange). - Fail loudly when ALPN is not h2. - Invalid proxy URLs fail every request instead of silently going direct or to the default transport, and are never cached. - Share the standard proxy transport per proxy URL too. - Claude OAuth client uses the shared uTLS transport; its copy is removed. - Tests go through NewUtlsHTTPClient with a fresh client per request. - Release 7.2.62+edgee.8. Co-Authored-By: Claude Opus 5.5 --- cmd/server/main.go | 2 +- cmd/server/version_test.go | 4 +- .../auth/claude/anthropic_auth_proxy_test.go | 18 +- internal/auth/claude/utls_transport.go | 154 +------- internal/buildinfo/buildinfo.go | 2 +- .../runtime/executor/helps/proxy_helpers.go | 40 +- .../runtime/executor/helps/utls_client.go | 245 +++++++------ .../executor/helps/utls_client_test.go | 341 +++++++++++++++--- sdk/proxyutil/proxy.go | 67 +++- 9 files changed, 521 insertions(+), 352 deletions(-) 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/internal/auth/claude/anthropic_auth_proxy_test.go b/internal/auth/claude/anthropic_auth_proxy_test.go index 7cab9cd2f1f..8e048d41d88 100644 --- a/internal/auth/claude/anthropic_auth_proxy_test.go +++ b/internal/auth/claude/anthropic_auth_proxy_test.go @@ -4,30 +4,22 @@ 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 transport.dialer == proxy.Direct { - t.Fatalf("expected proxy dialer, got %T", transport.dialer) + if auth.httpClient.Transport != helps.UtlsTransport("socks5://proxy.example.com:1080") { + t.Fatalf("expected the socks5 utls transport, got %T", auth.httpClient.Transport) } } diff --git a/internal/auth/claude/utls_transport.go b/internal/auth/claude/utls_transport.go index bb82e7ddecd..9974ac11451 100644 --- a/internal/auth/claude/utls_transport.go +++ b/internal/auth/claude/utls_transport.go @@ -1,162 +1,20 @@ // 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. +// for Anthropic domains by using the shared utls Chrome-fingerprint transport. func NewAnthropicHttpClient(cfg *config.SDKConfig) *http.Client { - return &http.Client{ - Transport: newUtlsRoundTripper(cfg), + var proxyURL string + if cfg != nil { + proxyURL = strings.TrimSpace(cfg.ProxyURL) } + return &http.Client{Transport: helps.UtlsTransport(proxyURL)} } 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/runtime/executor/helps/proxy_helpers.go b/internal/runtime/executor/helps/proxy_helpers.go index 572f87c7a1c..33c333dac64 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: @@ -42,15 +43,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) + return httpClient } // Priority 3: Use RoundTripper from context (typically from RoundTripperFor) @@ -61,19 +56,24 @@ 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 URL for the whole process, so connections are pooled across clients. +var ( + sharedProxyMu sync.Mutex + sharedProxyTransports = map[string]*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 string) http.RoundTripper { + sharedProxyMu.Lock() + defer sharedProxyMu.Unlock() + if transport, ok := sharedProxyTransports[proxyURL]; ok { + return transport + } transport, _, errBuild := proxyutil.BuildHTTPTransport(proxyURL) if errBuild != nil { - log.Errorf("%v", errBuild) - return nil + return errorRoundTripper{err: fmt.Errorf("invalid proxy %s: %w", proxyutil.Redact(proxyURL), errBuild)} } + sharedProxyTransports[proxyURL] = transport return transport } diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index 887d06af556..0b567ae1846 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -3,8 +3,10 @@ package helps import ( "context" "crypto/x509" + "fmt" "net" "net/http" + "slices" "strings" "sync" "time" @@ -13,147 +15,182 @@ import ( "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 - h2 *http2.Transport - rootCAs *x509.CertPool -} +// utlsDialTimeout bounds proxy CONNECT plus the TLS handshake for one shared connection. +const utlsDialTimeout = 30 * time.Second + +// utlsTestRootCAs is a test hook; nil means the system roots. +var utlsTestRootCAs *x509.CertPool -// One round tripper per proxy URL for the whole process, so requests reuse -// HTTP/2 connections instead of leaving one idle tunnel per request. +// One HTTP/2 transport per proxy URL for the whole process, so requests share +// connections instead of leaving one idle tunnel per request. var ( - sharedUtlsMu sync.Mutex - sharedUtlsRTs = map[string]*utlsRoundTripper{} + sharedUtlsMu sync.Mutex + sharedUtlsTransports = map[string]*http2.Transport{} ) -func sharedUtlsRoundTripper(proxyURL string) *utlsRoundTripper { - sharedUtlsMu.Lock() - defer sharedUtlsMu.Unlock() - rt, ok := sharedUtlsRTs[proxyURL] - if !ok { - rt = newUtlsRoundTripper(proxyURL) - sharedUtlsRTs[proxyURL] = rt +// UtlsTransport returns the process-wide Chrome-fingerprinted HTTP/2 transport for +// proxyURL ("" dials directly). An invalid proxy URL yields a transport that fails every request. +func UtlsTransport(proxyURL string) http.RoundTripper { + rt, err := sharedUtlsTransport(proxyURL) + if err != nil { + return errorRoundTripper{err: err} } return rt } -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 - } +func sharedUtlsTransport(proxyURL string) (*http2.Transport, error) { + sharedUtlsMu.Lock() + defer sharedUtlsMu.Unlock() + if rt, ok := sharedUtlsTransports[proxyURL]; ok { + return rt, nil } - return &utlsRoundTripper{ - connections: make(map[string]*http2.ClientConn), - pending: make(map[string]*sync.Cond), - dialer: dialer, - h2: &http2.Transport{ - IdleConnTimeout: 90 * time.Second, - ReadIdleTimeout: 30 * time.Second, - PingTimeout: 15 * time.Second, - }, + dialer, err := utlsDialer(proxyURL) + if err != nil { + return nil, err + } + pool := &utlsConnPool{ + dialer: dialer, + conns: map[string][]*http2.ClientConn{}, + dialing: map[string]*utlsDial{}, } + rt := &http2.Transport{ + ConnPool: pool, + IdleConnTimeout: 90 * time.Second, + ReadIdleTimeout: 30 * time.Second, + PingTimeout: 15 * time.Second, + } + pool.h2 = rt + sharedUtlsTransports[proxyURL] = rt + return rt, nil } -func (t *utlsRoundTripper) getOrCreateConnection(host, addr string) (*http2.ClientConn, error) { - t.mu.Lock() - - if h2Conn, ok := t.connections[host]; ok && h2Conn.CanTakeNewRequest() { - t.mu.Unlock() - return h2Conn, nil +func utlsDialer(proxyURL string) (proxy.ContextDialer, error) { + if proxyURL == "" { + return proxy.Direct, nil } - - if cond, ok := t.pending[host]; ok { - cond.Wait() - if h2Conn, ok := t.connections[host]; ok && h2Conn.CanTakeNewRequest() { - t.mu.Unlock() - return h2Conn, 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 +} - cond := sync.NewCond(&t.mu) - t.pending[host] = cond - t.mu.Unlock() - - h2Conn, err := t.createConnection(host, addr) +// 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 + mu sync.Mutex + conns map[string][]*http2.ClientConn + dialing map[string]*utlsDial +} - t.mu.Lock() - defer t.mu.Unlock() +type utlsDial struct { + done chan struct{} + cc *http2.ClientConn + err error +} - delete(t.pending, host) - cond.Broadcast() +func (p *utlsConnPool) GetClientConn(req *http.Request, addr string) (*http2.ClientConn, error) { + for { + p.mu.Lock() + for _, cc := range p.conns[addr] { + if cc.ReserveNewRequest() { + p.mu.Unlock() + return cc, 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() - if err != nil { - return nil, err + select { + case <-d.done: + case <-req.Context().Done(): + return nil, req.Context().Err() + } + if d.err != nil { + return nil, d.err + } + if d.cc.ReserveNewRequest() { + return d.cc, nil + } } - - t.connections[host] = h2Conn - return h2Conn, nil } -func (t *utlsRoundTripper) createConnection(host, addr string) (*http2.ClientConn, error) { - conn, err := t.dialer.Dial("tcp", addr) - if err != nil { - return nil, err +func (p *utlsConnPool) MarkDead(cc *http2.ClientConn) { + p.mu.Lock() + defer p.mu.Unlock() + for addr, list := range p.conns { + if i := slices.Index(list, cc); i >= 0 { + if list = slices.Delete(list, i, i+1); len(list) == 0 { + delete(p.conns, addr) + } else { + p.conns[addr] = list + } + return + } } +} - tlsConfig := &tls.Config{ServerName: host, RootCAs: t.rootCAs} - tlsConn := tls.UClient(conn, tlsConfig, tls.HelloChrome_Auto) - - if err := tlsConn.Handshake(); err != nil { - conn.Close() - return nil, err +// 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 { + if d.cc, err = p.h2.NewClientConn(conn); err != nil { + _ = conn.Close() + } } + d.err = err - h2Conn, err := t.h2.NewClientConn(tlsConn) - if err != nil { - tlsConn.Close() - return nil, err + p.mu.Lock() + delete(p.dialing, addr) + if err == nil { + p.conns[addr] = append(p.conns[addr], d.cc) } - - return h2Conn, nil + p.mu.Unlock() + close(d.done) } -func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { - hostname := req.URL.Hostname() - port := req.URL.Port() - if port == "" { - port = "443" - } - addr := net.JoinHostPort(hostname, port) - - h2Conn, err := t.getOrCreateConnection(hostname, addr) +func (p *utlsConnPool) dialTLS(ctx context.Context, addr string) (net.Conn, error) { + host, _, err := net.SplitHostPort(addr) if err != nil { return nil, err } - - resp, err := h2Conn.RoundTrip(req) + conn, err := p.dialer.DialContext(ctx, "tcp", 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 + tlsConn := tls.UClient(conn, &tls.Config{ServerName: host, RootCAs: utlsTestRootCAs}, 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 } +type errorRoundTripper struct{ err error } + +func (e errorRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, e.err } + // utlsProtectedHosts contains the hosts that should use utls Chrome TLS fingerprint // to bypass Cloudflare's TLS fingerprinting. var utlsProtectedHosts = map[string]struct{}{ @@ -194,12 +231,10 @@ func NewUtlsHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyau ctxRoundTripper, _ = ctx.Value("cliproxy.roundtripper").(http.RoundTripper) } - var utlsRT http.RoundTripper = sharedUtlsRoundTripper(proxyURL) + utlsRT := UtlsTransport(proxyURL) var standardTransport http.RoundTripper = http.DefaultTransport if proxyURL != "" { - if transport := buildProxyTransport(proxyURL); transport != nil { - standardTransport = transport - } + standardTransport = proxyTransport(proxyURL) } 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 abd68a7d286..d5f7280787f 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -3,15 +3,27 @@ package helps import ( "bufio" "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" "strings" + "sync" "sync/atomic" "testing" "time" + + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" + sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" ) type utlsClientRoundTripFunc func(*http.Request) (*http.Response, error) @@ -50,40 +62,108 @@ func TestNewUtlsHTTPClientUsesContextRoundTripperForProtectedHost(t *testing.T) } } -// countingConnectProxy is an HTTP CONNECT proxy that counts open tunnels. -type countingConnectProxy struct { - ln net.Listener - opened atomic.Int32 - open atomic.Int32 +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() + 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} + utlsTestRootCAs = x509.NewCertPool() + utlsTestRootCAs.AddCert(leaf) + }) + server := httptest.NewUnstartedServer(handler) + server.EnableHTTP2 = h2 + 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 } -func startCountingConnectProxy(t *testing.T) *countingConnectProxy { +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 := &countingConnectProxy{ln: ln} - t.Cleanup(func() { _ = ln.Close() }) + 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 } - go p.serve(client) + 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 *countingConnectProxy) serve(client net.Conn) { - defer client.Close() +func (p *connectProxy) tunnel(client net.Conn) { req, err := http.ReadRequest(bufio.NewReader(client)) if err != nil || req.Method != http.MethodConnect { return } - upstream, err := net.Dial("tcp", req.Host) + _, port, _ := net.SplitHostPort(req.Host) + upstream, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", port)) if err != nil { return } @@ -100,46 +180,223 @@ func (p *countingConnectProxy) serve(client net.Conn) { <-done } -func TestUtlsRoundTripperSharesOneTunnelPerProxyAndClosesItWhenIdle(t *testing.T) { - server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _, _ = io.WriteString(w, r.Proto) - })) - server.EnableHTTP2 = true - server.StartTLS() - defer server.Close() +func newProxiedUtlsClient(proxyURL string) *http.Client { + return NewUtlsHTTPClient(context.Background(), &config.Config{SDKConfig: sdkconfig.SDKConfig{ProxyURL: proxyURL}}, nil, 0) +} - proxyServer := startCountingConnectProxy(t) - proxyURL := "http://" + proxyServer.ln.Addr().String() +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 +} - rt := sharedUtlsRoundTripper(proxyURL) - rt.rootCAs = x509.NewCertPool() - rt.rootCAs.AddCert(server.Certificate()) - rt.h2.IdleConnTimeout = 300 * time.Millisecond +// 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 +} - for i := 0; i < 5; i++ { - req, err := http.NewRequest(http.MethodGet, server.URL, nil) - if err != nil { - t.Fatal(err) - } - resp, err := sharedUtlsRoundTripper(proxyURL).RoundTrip(req) - if err != nil { - t.Fatalf("request %d: %v", i, err) - } - body, _ := io.ReadAll(resp.Body) - _ = resp.Body.Close() - if string(body) != "HTTP/2.0" { - t.Fatalf("request %d proto = %q, want HTTP/2.0", i, body) - } +func writeProto(w http.ResponseWriter, r *http.Request) { _, _ = io.WriteString(w, r.Proto) } + +func TestUtlsClientsShareOneTunnelAndCloseItWhenIdle(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + rt, err := sharedUtlsTransport(p.url) + if err != nil { + t.Fatal(err) } + rt.IdleConnTimeout = 300 * time.Millisecond - if got := proxyServer.opened.Load(); got != 1 { + 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) } deadline := time.Now().Add(5 * time.Second) - for proxyServer.open.Load() != 0 { + for p.open.Load() != 0 { if time.Now().After(deadline) { - t.Fatalf("idle tunnel still open after idle timeout") + t.Fatal("idle tunnel still open after the idle timeout") } time.Sleep(20 * time.Millisecond) } } + +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 + server := startChatgptServer(t, true, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if served.Add(1)%25 == 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 TestUtlsClientKeepsSharedConnectionAfterCancelledRequest(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/slow" { + select { + case <-r.Context().Done(): + case <-time.After(5 * time.Second): + } + return + } + writeProto(w, r) + })) + p := startConnectProxy(t, false) + + if _, err := getBody(context.Background(), p.url, chatgptURL(server, "/")); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 100*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 := 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 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() + _, cachedUtls := sharedUtlsTransports[badProxy] + sharedUtlsMu.Unlock() + sharedProxyMu.Lock() + _, cachedProxy := sharedProxyTransports[badProxy] + sharedProxyMu.Unlock() + if cachedUtls || cachedProxy { + t.Fatalf("invalid proxy cached: utls=%v proxy=%v", cachedUtls, cachedProxy) + } +} + +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") + } +} diff --git a/sdk/proxyutil/proxy.go b/sdk/proxyutil/proxy.go index 507d5e09e88..e9f31288968 100644 --- a/sdk/proxyutil/proxy.go +++ b/sdk/proxyutil/proxy.go @@ -10,6 +10,7 @@ import ( "net/http" "net/url" "strings" + "time" "golang.org/x/net/proxy" ) @@ -152,22 +153,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 +205,41 @@ 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.Body != nil { + _ = resp.Body.Close() } 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) - } 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. +func contextError(ctx context.Context, err error) error { + if ctxErr := ctx.Err(); ctxErr != nil { + return ctxErr + } + return err +} + func proxyDialAddr(proxyURL *url.URL) string { port := proxyURL.Port() if port == "" { From c65ae5c945940ececcd6b77c6ace9e540173bb1e Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 09:40:59 +0000 Subject: [PATCH 03/14] Fix CONNECT tunnel bytes, close bodies on invalid proxy, validate proxy-url early - Never read or close the body of a 2xx CONNECT reply; framing headers are ignored per RFC 9110 9.3.6 so tunnel bytes stay intact. - Treat a passed dial deadline as context.DeadlineExceeded even if ctx has not recorded it yet. - proxyutil.FailingTransport replaces the two error round-trippers and closes the request body. - Codex/xAI websocket dialer and the per-auth round-tripper provider fail on an invalid proxy instead of going direct. - proxy-url is validated at config load and on management PUT/PATCH and auth-file edits (400). - uTLS test root CAs are copied into the pool under the shared mutex. - Version strings and README rebase range bumped to edgee.8; release gate covers proxyutil, auth/claude and the shared-connection race tests. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/edgee-release.yml | 10 +- Dockerfile | 2 +- README.md | 4 +- docker-compose.yml | 2 +- .../api/handlers/management/auth_files.go | 3 + .../api/handlers/management/config_basic.go | 13 +- .../api/handlers/management/config_lists.go | 54 ++++++ internal/config/config.go | 55 ++++++ internal/config/parse.go | 3 + internal/config/proxy_url_validation_test.go | 25 +++ .../executor/codex_websockets_executor.go | 23 +-- .../codex_websockets_executor_test.go | 15 +- .../runtime/executor/helps/proxy_helpers.go | 3 +- .../runtime/executor/helps/utls_client.go | 18 +- .../executor/helps/utls_client_test.go | 38 ++++- .../executor/xai_websockets_executor.go | 5 +- sdk/cliproxy/rtprovider.go | 6 +- sdk/cliproxy/rtprovider_test.go | 13 ++ sdk/proxyutil/proxy.go | 24 ++- sdk/proxyutil/proxy_connect_test.go | 160 ++++++++++++++++++ 20 files changed, 437 insertions(+), 39 deletions(-) create mode 100644 internal/config/proxy_url_validation_test.go create mode 100644 sdk/proxyutil/proxy_connect_test.go diff --git a/.github/workflows/edgee-release.yml b/.github/workflows/edgee-release.yml index c5bf8b4d8e9..da42f4e5ab6 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,12 @@ 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)' + ./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..4e5731a96aa 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 (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/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..f296682a1a8 100644 --- a/internal/api/handlers/management/auth_files.go +++ b/internal/api/handlers/management/auth_files.go @@ -1498,6 +1498,9 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { targetAuth.Metadata = make(map[string]any) } + if raw, isString := value.(string); isString && fieldPath == "proxy_url" && rejectInvalidProxyURL(c, "proxy_url", raw) { + return + } if fieldPath == "headers" { applyAuthFileHeadersPatch(targetAuth, value) } else if errSet := setAuthFileMetadataValue(targetAuth.Metadata, fieldPath, value); errSet != nil { 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/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/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 33c333dac64..81da60ee77f 100644 --- a/internal/runtime/executor/helps/proxy_helpers.go +++ b/internal/runtime/executor/helps/proxy_helpers.go @@ -57,6 +57,7 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip } // One transport per proxy URL for the whole process, so connections are pooled across clients. +// Proxy URLs come from config, so the map is not evicted. var ( sharedProxyMu sync.Mutex sharedProxyTransports = map[string]*http.Transport{} @@ -72,7 +73,7 @@ func proxyTransport(proxyURL string) http.RoundTripper { } transport, _, errBuild := proxyutil.BuildHTTPTransport(proxyURL) if errBuild != nil { - return errorRoundTripper{err: fmt.Errorf("invalid proxy %s: %w", proxyutil.Redact(proxyURL), errBuild)} + return proxyutil.FailingTransport(fmt.Errorf("invalid proxy %s: %w", proxyutil.Redact(proxyURL), errBuild)) } sharedProxyTransports[proxyURL] = transport return transport diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index 0b567ae1846..4e6adf98adf 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -22,14 +22,14 @@ import ( // utlsDialTimeout bounds proxy CONNECT plus the TLS handshake for one shared connection. const utlsDialTimeout = 30 * time.Second -// utlsTestRootCAs is a test hook; nil means the system roots. -var utlsTestRootCAs *x509.CertPool - // One HTTP/2 transport per proxy URL for the whole process, so requests share -// connections instead of leaving one idle tunnel per request. +// connections instead of leaving one idle tunnel per request. Proxy URLs come +// from config, so the map is not evicted. var ( sharedUtlsMu sync.Mutex sharedUtlsTransports = map[string]*http2.Transport{} + // 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 @@ -37,7 +37,7 @@ var ( func UtlsTransport(proxyURL string) http.RoundTripper { rt, err := sharedUtlsTransport(proxyURL) if err != nil { - return errorRoundTripper{err: err} + return proxyutil.FailingTransport(err) } return rt } @@ -54,6 +54,7 @@ func sharedUtlsTransport(proxyURL string) (*http2.Transport, error) { } pool := &utlsConnPool{ dialer: dialer, + rootCAs: utlsTestRootCAs, conns: map[string][]*http2.ClientConn{}, dialing: map[string]*utlsDial{}, } @@ -88,6 +89,7 @@ func utlsDialer(proxyURL string) (proxy.ContextDialer, error) { type utlsConnPool struct { h2 *http2.Transport dialer proxy.ContextDialer + rootCAs *x509.CertPool mu sync.Mutex conns map[string][]*http2.ClientConn dialing map[string]*utlsDial @@ -175,7 +177,7 @@ func (p *utlsConnPool) dialTLS(ctx context.Context, addr string) (net.Conn, erro if err != nil { return nil, err } - tlsConn := tls.UClient(conn, &tls.Config{ServerName: host, RootCAs: utlsTestRootCAs}, tls.HelloChrome_Auto) + 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 @@ -187,10 +189,6 @@ func (p *utlsConnPool) dialTLS(ctx context.Context, addr string) (net.Conn, erro return tlsConn, nil } -type errorRoundTripper struct{ err error } - -func (e errorRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, e.err } - // utlsProtectedHosts contains the hosts that should use utls Chrome TLS fingerprint // to bypass Cloudflare's TLS fingerprinting. var utlsProtectedHosts = map[string]struct{}{ diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index d5f7280787f..0fb8ec5954c 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -94,8 +94,11 @@ func startChatgptServer(t *testing.T, h2 bool, handler http.Handler) *httptest.S panic(err) } chatgptCert = stdtls.Certificate{Certificate: [][]byte{der}, PrivateKey: key, Leaf: leaf} - utlsTestRootCAs = x509.NewCertPool() - utlsTestRootCAs.AddCert(leaf) + roots := x509.NewCertPool() + roots.AddCert(leaf) + sharedUtlsMu.Lock() + utlsTestRootCAs = roots + sharedUtlsMu.Unlock() }) server := httptest.NewUnstartedServer(handler) server.EnableHTTP2 = h2 @@ -400,3 +403,34 @@ func TestClientsForTheSameProxyShareTheFallbackTransport(t *testing.T) { 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) + } + } + } +} 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/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 e9f31288968..9ff3f86d19b 100644 --- a/sdk/proxyutil/proxy.go +++ b/sdk/proxyutil/proxy.go @@ -213,10 +213,9 @@ func (d *httpConnectDialer) connect(ctx context.Context, proxyConn net.Conn, add if errRead != nil { return nil, fmt.Errorf("read CONNECT response failed: %w", contextError(ctx, errRead)) } - if resp.Body != nil { - _ = resp.Body.Close() - } - if resp.StatusCode != http.StatusOK { + // 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) } @@ -233,10 +232,14 @@ func (d *httpConnectDialer) connect(ctx context.Context, proxyConn net.Conn, add } // 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 } @@ -291,3 +294,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") + } +} From 2d814344643cba3ec923bc0c7cb71c9fb72a1213 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 10:01:32 +0000 Subject: [PATCH 04/14] Drop idle and ping timeouts from the shared uTLS transport AGENTS.md forbids timeouts after a connection is established; idle agents must keep their connection. The server's own idle close (chatgpt.com ~6.5 min) is redialled transparently on the next request. Co-Authored-By: Claude Opus 5.5 --- .../runtime/executor/helps/utls_client.go | 8 ++--- .../executor/helps/utls_client_test.go | 32 +++++++++++++++---- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index 4e6adf98adf..e7177c62e1f 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -58,12 +58,8 @@ func sharedUtlsTransport(proxyURL string) (*http2.Transport, error) { conns: map[string][]*http2.ClientConn{}, dialing: map[string]*utlsDial{}, } - rt := &http2.Transport{ - ConnPool: pool, - IdleConnTimeout: 90 * time.Second, - ReadIdleTimeout: 30 * time.Second, - PingTimeout: 15 * time.Second, - } + // No idle or ping timeouts: AGENTS.md forbids timeouts after a connection is established. + rt := &http2.Transport{ConnPool: pool} pool.h2 = rt sharedUtlsTransports[proxyURL] = rt return rt, nil diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index 0fb8ec5954c..b8357555808 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -69,6 +69,11 @@ var ( // 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) @@ -102,6 +107,7 @@ func startChatgptServer(t *testing.T, h2 bool, handler http.Handler) *httptest.S }) 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) @@ -229,15 +235,10 @@ func burst(n int, proxyURL, url string) []error { func writeProto(w http.ResponseWriter, r *http.Request) { _, _ = io.WriteString(w, r.Proto) } -func TestUtlsClientsShareOneTunnelAndCloseItWhenIdle(t *testing.T) { +func TestUtlsClientsShareOneTunnel(t *testing.T) { t.Parallel() server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) p := startConnectProxy(t, false) - rt, err := sharedUtlsTransport(p.url) - if err != nil { - t.Fatal(err) - } - rt.IdleConnTimeout = 300 * time.Millisecond if errs := burst(50, p.url, chatgptURL(server, "/")); len(errs) > 0 { t.Fatalf("%d of 50 requests failed, first: %v", len(errs), errs[0]) @@ -245,13 +246,30 @@ func TestUtlsClientsShareOneTunnelAndCloseItWhenIdle(t *testing.T) { 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("idle tunnel still open after the idle timeout") + 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) { From 4bb0249ee037ec8fa3cc76b4814843c0bc5fddef Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 11:22:43 +0000 Subject: [PATCH 05/14] Drop silent shared tunnels, replay safe requests after abrupt close, tighten proxy-url checks - Retire a shared HTTP/2 connection when a request's context ends and the connection received no bytes meanwhile (dead tunnel), without any timer. - Replay an idempotent request once after an abrupt close (unexpected EOF). - Close the request body when no connection was obtained. - Key shared transports by the normalised proxy setting. - Wrap shared dial errors as "utls: shared dial to failed". - proxyutil.Parse requires a hostname, a 1-65535 port when given, and a port for SOCKS5. - util.SetProxy and the Antigravity model fetch fail loudly on a bad proxy. - Auth-file PATCH validates the whole patch (proxy_url must be a valid string, no nested writes) before mutating a deep copy; uploads reject a bad proxy_url with 400. Co-Authored-By: Claude Opus 5.5 --- .../api/handlers/management/auth_files.go | 113 ++++++++-- .../management/proxy_url_validation_test.go | 173 +++++++++++++++ .../runtime/executor/helps/proxy_helpers.go | 8 +- .../runtime/executor/helps/utls_client.go | 193 +++++++++++++++-- .../executor/helps/utls_client_test.go | 200 ++++++++++++++++-- internal/util/proxy.go | 10 +- internal/util/proxy_test.go | 26 +++ sdk/cliproxy/antigravity_models.go | 8 +- sdk/proxyutil/proxy.go | 31 ++- sdk/proxyutil/proxy_test.go | 11 + 10 files changed, 705 insertions(+), 68 deletions(-) create mode 100644 internal/api/handlers/management/proxy_url_validation_test.go create mode 100644 internal/util/proxy_test.go diff --git a/internal/api/handlers/management/auth_files.go b/internal/api/handlers/management/auth_files.go index f296682a1a8..31b27c6457c 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,20 +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 raw, isString := value.(string); isString && fieldPath == "proxy_url" && rejectInvalidProxyURL(c, "proxy_url", raw) { - return - } - 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 @@ -1531,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() 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..edda3fc75dd --- /dev/null +++ b/internal/api/handlers/management/proxy_url_validation_test.go @@ -0,0 +1,173 @@ +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") + } + }) + } +} diff --git a/internal/runtime/executor/helps/proxy_helpers.go b/internal/runtime/executor/helps/proxy_helpers.go index 81da60ee77f..dab431859f5 100644 --- a/internal/runtime/executor/helps/proxy_helpers.go +++ b/internal/runtime/executor/helps/proxy_helpers.go @@ -66,15 +66,19 @@ var ( // 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 string) http.RoundTripper { + key, errKey := proxyPoolKey(proxyURL) + 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[proxyURL]; ok { + if transport, ok := sharedProxyTransports[key]; ok { return transport } transport, _, errBuild := proxyutil.BuildHTTPTransport(proxyURL) if errBuild != nil { return proxyutil.FailingTransport(fmt.Errorf("invalid proxy %s: %w", proxyutil.Redact(proxyURL), errBuild)) } - sharedProxyTransports[proxyURL] = transport + sharedProxyTransports[key] = transport return transport } diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index e7177c62e1f..0d4f962eee3 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -3,12 +3,17 @@ package helps import ( "context" "crypto/x509" + "errors" "fmt" + "io" "net" "net/http" + "net/url" "slices" "strings" "sync" + "sync/atomic" + "syscall" "time" tls "github.com/refraction-networking/utls" @@ -22,12 +27,11 @@ import ( // utlsDialTimeout bounds proxy CONNECT plus the TLS handshake for one shared connection. const utlsDialTimeout = 30 * time.Second -// One HTTP/2 transport per proxy URL for the whole process, so requests share -// connections instead of leaving one idle tunnel per request. Proxy URLs come -// from config, so the map is not evicted. +// One HTTP/2 transport per proxy for the whole process, so requests share connections +// instead of leaving one idle tunnel per request. Proxy URLs come from config, so the map is not evicted. var ( sharedUtlsMu sync.Mutex - sharedUtlsTransports = map[string]*http2.Transport{} + sharedUtlsTransports = map[string]*utlsRoundTripper{} // utlsTestRootCAs is a test hook guarded by sharedUtlsMu; nil means the system roots. utlsTestRootCAs *x509.CertPool ) @@ -42,10 +46,14 @@ func UtlsTransport(proxyURL string) http.RoundTripper { return rt } -func sharedUtlsTransport(proxyURL string) (*http2.Transport, error) { +func sharedUtlsTransport(proxyURL string) (*utlsRoundTripper, error) { + key, err := proxyPoolKey(proxyURL) + 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[proxyURL]; ok { + if rt, ok := sharedUtlsTransports[key]; ok { return rt, nil } dialer, err := utlsDialer(proxyURL) @@ -55,18 +63,33 @@ func sharedUtlsTransport(proxyURL string) (*http2.Transport, error) { pool := &utlsConnPool{ dialer: dialer, rootCAs: utlsTestRootCAs, - conns: map[string][]*http2.ClientConn{}, + conns: map[string][]*utlsPooledConn{}, dialing: map[string]*utlsDial{}, } // No idle or ping timeouts: AGENTS.md forbids timeouts after a connection is established. - rt := &http2.Transport{ConnPool: pool} - pool.h2 = rt - sharedUtlsTransports[proxyURL] = rt + h2 := &http2.Transport{ConnPool: pool} + pool.h2 = h2 + rt := &utlsRoundTripper{h2: h2, pool: pool} + sharedUtlsTransports[key] = rt return rt, nil } +// proxyPoolKey names the pool for a proxy setting: "direct" for no proxy, otherwise the +// proxy URL without path or case differences, so equivalent settings share one pool. +func proxyPoolKey(raw string) (string, error) { + setting, err := proxyutil.Parse(raw) + if err != nil { + return "", err + } + if setting.Mode != proxyutil.ModeProxy { + return "direct", nil + } + key := url.URL{Scheme: setting.URL.Scheme, User: setting.URL.User, Host: strings.ToLower(setting.URL.Host)} + return key.String(), nil +} + func utlsDialer(proxyURL string) (proxy.ContextDialer, error) { - if proxyURL == "" { + if strings.TrimSpace(proxyURL) == "" { return proxy.Direct, nil } dialer, _, err := proxyutil.BuildDialer(proxyURL) @@ -80,6 +103,81 @@ func utlsDialer(proxyURL string) (proxy.ContextDialer, error) { return contextDialer, nil } +// utlsRoundTripper wraps the shared HTTP/2 transport with what it lacks for a long-lived +// shared connection: closing the body when no connection was obtained, dropping a +// connection that has gone silent, and one replay of a safe request after an abrupt close. +type utlsRoundTripper struct { + h2 *http2.Transport + pool *utlsConnPool +} + +type utlsAttemptKey struct{} + +// utlsAttempt records the connection the pool handed to one request attempt. +type utlsAttempt struct { + conn *utlsPooledConn + readAtStart uint64 +} + +func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + for replayed := false; ; replayed = true { + attempt := &utlsAttempt{} + resp, err := t.h2.RoundTrip(req.WithContext(context.WithValue(req.Context(), utlsAttemptKey{}, attempt))) + if err == nil { + return resp, nil + } + if attempt.conn == nil { + // No connection was obtained, so http2 never took ownership of the body. + if req.Body != nil { + _ = req.Body.Close() + } + return nil, err + } + if req.Context().Err() != nil { + t.pool.dropIfSilent(attempt) + return nil, err + } + if replayed || !isAbruptClose(err) { + return nil, err + } + next, ok := replayableRequest(req) + if !ok { + return nil, err + } + req = next + } +} + +// isAbruptClose reports a connection that ended without a GOAWAY before the response headers. +func isAbruptClose(err error) bool { + return errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, syscall.ECONNRESET) || errors.Is(err, syscall.EPIPE) +} + +// replayableRequest returns a fresh copy of req when it is safe to send again: an idempotent +// method (or an idempotency key, as net/http allows) and a body that can be rewound. +func replayableRequest(req *http.Request) (*http.Request, bool) { + switch req.Method { + case http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodTrace: + default: + if req.Header.Get("Idempotency-Key") == "" && req.Header.Get("X-Idempotency-Key") == "" { + return nil, false + } + } + next := req.Clone(req.Context()) + if req.Body == nil || req.Body == http.NoBody { + return next, true + } + if req.GetBody == nil { + return nil, false + } + body, err := req.GetBody() + if err != nil { + return nil, false + } + next.Body = body + return next, true +} + // 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 { @@ -87,23 +185,57 @@ type utlsConnPool struct { dialer proxy.ContextDialer rootCAs *x509.CertPool mu sync.Mutex - conns map[string][]*http2.ClientConn + 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. +type utlsPooledConn struct { + cc *http2.ClientConn + read atomic.Uint64 +} + +type countingConn struct { + net.Conn + read *atomic.Uint64 +} + +func (c countingConn) Read(b []byte) (int, error) { + n, err := c.Conn.Read(b) + c.read.Add(uint64(n)) + return n, err +} + type utlsDial struct { done chan struct{} - cc *http2.ClientConn + conn *utlsPooledConn err error } func (p *utlsConnPool) GetClientConn(req *http.Request, addr string) (*http2.ClientConn, error) { + attempt, _ := req.Context().Value(utlsAttemptKey{}).(*utlsAttempt) + if attempt != nil { + attempt.conn = nil + } + conn, err := p.reserve(req, addr) + if err != nil { + return nil, err + } + if attempt != nil { + attempt.conn = conn + attempt.readAtStart = conn.read.Load() + } + return conn.cc, nil +} + +func (p *utlsConnPool) reserve(req *http.Request, addr string) (*utlsPooledConn, error) { for { p.mu.Lock() - for _, cc := range p.conns[addr] { - if cc.ReserveNewRequest() { + for _, conn := range p.conns[addr] { + if conn.cc.ReserveNewRequest() { p.mu.Unlock() - return cc, nil + return conn, nil } } d, ok := p.dialing[addr] @@ -122,8 +254,8 @@ func (p *utlsConnPool) GetClientConn(req *http.Request, addr string) (*http2.Cli if d.err != nil { return nil, d.err } - if d.cc.ReserveNewRequest() { - return d.cc, nil + if d.conn.cc.ReserveNewRequest() { + return d.conn, nil } } } @@ -132,7 +264,7 @@ func (p *utlsConnPool) MarkDead(cc *http2.ClientConn) { p.mu.Lock() defer p.mu.Unlock() for addr, list := range p.conns { - if i := slices.Index(list, cc); i >= 0 { + 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 { @@ -143,22 +275,39 @@ func (p *utlsConnPool) MarkDead(cc *http2.ClientConn) { } } +// dropIfSilent retires the attempt's connection when the request's context ended and the +// connection received nothing at all meanwhile: the tunnel is dead, so new requests must +// dial a fresh one. Shutdown lets any other streams on it finish, then closes it. +func (p *utlsConnPool) dropIfSilent(attempt *utlsAttempt) { + if attempt.conn.read.Load() != attempt.readAtStart { + return + } + cc := attempt.conn.cc + p.MarkDead(cc) + go func() { _ = cc.Shutdown(context.Background()) }() +} + // 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 { - if d.cc, err = p.h2.NewClientConn(conn); err != nil { + pooled := &utlsPooledConn{} + if pooled.cc, err = p.h2.NewClientConn(countingConn{Conn: conn, read: &pooled.read}); err != nil { _ = conn.Close() + } else { + d.conn = pooled } } - d.err = err + if err != nil { + 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.cc) + p.conns[addr] = append(p.conns[addr], d.conn) } p.mu.Unlock() close(d.done) diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index b8357555808..1da4371c3eb 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -125,6 +125,48 @@ type connectProxy struct { accepted atomic.Int32 opened atomic.Int32 open atomic.Int32 + + mu sync.Mutex + tunnels []*proxyTunnel +} + +type proxyTunnel struct { + client net.Conn + dropping atomic.Bool +} + +// 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 { + 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 { @@ -183,9 +225,13 @@ func (p *connectProxy) tunnel(client net.Conn) { 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() { _, _ = io.Copy(upstream, client); done <- struct{}{} }() - go func() { _, _ = io.Copy(client, upstream); done <- struct{}{} }() + go func() { tun.pipe(upstream, client); done <- struct{}{} }() + go func() { tun.pipe(client, upstream); done <- struct{}{} }() <-done } @@ -342,28 +388,38 @@ func TestUtlsClientRetriesRequestsWhenServerSendsGoAway(t *testing.T) { } } -func TestUtlsClientKeepsSharedConnectionAfterCancelledRequest(t *testing.T) { +func TestUtlsClientKeepsReceivingConnectionAfterCancelledRequest(t *testing.T) { t.Parallel() server := startChatgptServer(t, true, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/slow" { - select { - case <-r.Context().Done(): - case <-time.After(5 * time.Second): + 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) } - return + default: + writeProto(w, r) } - writeProto(w, r) })) p := startConnectProxy(t, false) - if _, err := getBody(context.Background(), p.url, chatgptURL(server, "/")); err != nil { - t.Fatal(err) - } - ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + 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) } @@ -372,6 +428,109 @@ func TestUtlsClientKeepsSharedConnectionAfterCancelledRequest(t *testing.T) { } } +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() + } +} + +func TestUtlsClientDoesNotReplayPost(t *testing.T) { + t.Parallel() + req, err := http.NewRequest(http.MethodPost, "https://chatgpt.com/", strings.NewReader("{}")) + if err != nil { + t.Fatal(err) + } + if _, ok := replayableRequest(req); ok { + t.Fatal("a POST without an idempotency key was marked replayable") + } + req.Header.Set("Idempotency-Key", "k") + if next, ok := replayableRequest(req); !ok || next.Body == nil { + t.Fatal("a POST with an idempotency key and a rewindable body should be replayable") + } +} + +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 TestUtlsClientRejectsServerThatDoesNotNegotiateHTTP2(t *testing.T) { t.Parallel() server := startChatgptServer(t, false, http.HandlerFunc(writeProto)) @@ -399,14 +558,19 @@ func TestInvalidProxyURLFailsEveryRequestAndIsNotCached(t *testing.T) { } } sharedUtlsMu.Lock() - _, cachedUtls := sharedUtlsTransports[badProxy] + for key := range sharedUtlsTransports { + if strings.Contains(key, "proxy.invalid") { + t.Errorf("invalid proxy cached in utls transports: %s", key) + } + } sharedUtlsMu.Unlock() sharedProxyMu.Lock() - _, cachedProxy := sharedProxyTransports[badProxy] - sharedProxyMu.Unlock() - if cachedUtls || cachedProxy { - t.Fatalf("invalid proxy cached: utls=%v proxy=%v", cachedUtls, cachedProxy) + for key := range sharedProxyTransports { + if strings.Contains(key, "proxy.invalid") { + t.Errorf("invalid proxy cached in proxy transports: %s", key) + } } + sharedProxyMu.Unlock() } func TestClientsForTheSameProxyShareTheFallbackTransport(t *testing.T) { 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/proxyutil/proxy.go b/sdk/proxyutil/proxy.go index 9ff3f86d19b..33321749e48 100644 --- a/sdk/proxyutil/proxy.go +++ b/sdk/proxyutil/proxy.go @@ -9,6 +9,7 @@ import ( "net" "net/http" "net/url" + "strconv" "strings" "time" @@ -56,20 +57,42 @@ 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 port in 1-65535 when one is given, and always for SOCKS5. +func checkProxyPort(u *url.URL) error { + port := u.Port() + if port == "" { + if strings.HasSuffix(u.Host, ":") { + return fmt.Errorf("proxy URL has an empty port") + } + if u.Scheme == "socks5" || u.Scheme == "socks5h" { + return fmt.Errorf("SOCKS5 proxy URL needs a port") + } + return nil + } + 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 { diff --git a/sdk/proxyutil/proxy_test.go b/sdk/proxyutil/proxy_test.go index 1c957ef7a0b..6dba34278c3 100644 --- a/sdk/proxyutil/proxy_test.go +++ b/sdk/proxyutil/proxy_test.go @@ -39,6 +39,17 @@ 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}, } for _, tt := range tests { From 88b50d26eb3cf28859ab8e2b8677107bb1d3b0b6 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 11:31:41 +0000 Subject: [PATCH 06/14] Keep one shared connection per proxy and account Both shared pools are keyed by the normalised proxy setting plus the auth ID, so subscription accounts never share a connection. Callers with no auth (config-level clients, login flows) use their own key. Co-Authored-By: Claude Opus 5.5 --- .../runtime/executor/helps/proxy_helpers.go | 15 +++--- .../runtime/executor/helps/utls_client.go | 51 ++++++++++++------- .../executor/helps/utls_client_test.go | 34 +++++++++++-- 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/internal/runtime/executor/helps/proxy_helpers.go b/internal/runtime/executor/helps/proxy_helpers.go index dab431859f5..132f08cab2a 100644 --- a/internal/runtime/executor/helps/proxy_helpers.go +++ b/internal/runtime/executor/helps/proxy_helpers.go @@ -33,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 @@ -44,7 +45,7 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip } if proxyURL != "" { - httpClient.Transport = proxyTransport(proxyURL) + httpClient.Transport = proxyTransport(proxyURL, authID) return httpClient } @@ -56,17 +57,17 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip return httpClient } -// One transport per proxy URL for the whole process, so connections are pooled across clients. -// Proxy URLs come from config, so the map is not evicted. +// One transport per proxy and account for the whole process, so connections are pooled across +// clients but never shared between accounts. These come from config, so the map is not evicted. var ( sharedProxyMu sync.Mutex - sharedProxyTransports = map[string]*http.Transport{} + 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 string) http.RoundTripper { - key, errKey := proxyPoolKey(proxyURL) +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)) } diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index 0d4f962eee3..d48e09166c9 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -27,27 +27,33 @@ import ( // utlsDialTimeout bounds proxy CONNECT plus the TLS handshake for one shared connection. const utlsDialTimeout = 30 * time.Second -// One HTTP/2 transport per proxy for the whole process, so requests share connections -// instead of leaving one idle tunnel per request. Proxy URLs come from config, so the map is not evicted. +// 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. +// Proxy URLs and accounts come from config, so the map is not evicted. var ( sharedUtlsMu sync.Mutex - sharedUtlsTransports = map[string]*utlsRoundTripper{} + 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). An invalid proxy URL yields a transport that fails every request. +// 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 { - rt, err := sharedUtlsTransport(proxyURL) + return utlsTransportFor(proxyURL, "") +} + +func utlsTransportFor(proxyURL, authID string) http.RoundTripper { + rt, err := sharedUtlsTransport(proxyURL, authID) if err != nil { return proxyutil.FailingTransport(err) } return rt } -func sharedUtlsTransport(proxyURL string) (*utlsRoundTripper, error) { - key, err := proxyPoolKey(proxyURL) +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) } @@ -74,18 +80,24 @@ func sharedUtlsTransport(proxyURL string) (*utlsRoundTripper, error) { return rt, nil } -// proxyPoolKey names the pool for a proxy setting: "direct" for no proxy, otherwise the -// proxy URL without path or case differences, so equivalent settings share one pool. -func proxyPoolKey(raw string) (string, error) { - setting, err := proxyutil.Parse(raw) +// 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 +} + +func newTransportKey(proxyURL, authID string) (transportKey, error) { + setting, err := proxyutil.Parse(proxyURL) if err != nil { - return "", err + return transportKey{}, err } - if setting.Mode != proxyutil.ModeProxy { - return "direct", nil + 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() } - key := url.URL{Scheme: setting.URL.Scheme, User: setting.URL.User, Host: strings.ToLower(setting.URL.Host)} - return key.String(), nil + return key, nil } func utlsDialer(proxyURL string) (proxy.ContextDialer, error) { @@ -361,9 +373,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) @@ -374,10 +387,10 @@ func NewUtlsHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyau ctxRoundTripper, _ = ctx.Value("cliproxy.roundtripper").(http.RoundTripper) } - utlsRT := UtlsTransport(proxyURL) + utlsRT := utlsTransportFor(proxyURL, authID) var standardTransport http.RoundTripper = http.DefaultTransport if proxyURL != "" { - standardTransport = proxyTransport(proxyURL) + 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 1da4371c3eb..b9daf9a14e8 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -23,6 +23,7 @@ import ( "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" ) @@ -518,7 +519,7 @@ func TestUtlsClientNamesSharedDialFailures(t *testing.T) { func TestEquivalentProxySettingsShareOneTransport(t *testing.T) { t.Parallel() same := func(a, b string) bool { - return UtlsTransport(a) == UtlsTransport(b) && proxyTransport(a) == proxyTransport(b) + 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") @@ -531,6 +532,29 @@ func TestEquivalentProxySettingsShareOneTransport(t *testing.T) { } } +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)) @@ -559,15 +583,15 @@ func TestInvalidProxyURLFailsEveryRequestAndIsNotCached(t *testing.T) { } sharedUtlsMu.Lock() for key := range sharedUtlsTransports { - if strings.Contains(key, "proxy.invalid") { - t.Errorf("invalid proxy cached in utls transports: %s", key) + 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.invalid") { - t.Errorf("invalid proxy cached in proxy transports: %s", key) + if strings.Contains(key.proxy, "proxy.invalid") { + t.Errorf("invalid proxy cached in proxy transports: %s", key.proxy) } } sharedProxyMu.Unlock() From d6a12af4d402e291fe4288e8ac61bec447831fd0 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 12:11:28 +0000 Subject: [PATCH 07/14] Never replay a request the server may have processed, release account connections, tighten proxy checks - Send requests on the shared connections directly: retry only what the server confirmed it never processed (unusable conn, GOAWAY above last stream, REFUSED_STREAM); replay an abrupt close or PROTOCOL_ERROR once, only for idempotent or keyed requests. - Close each attempt's body exactly once, including rewound retry bodies. - A request whose context already ended reserves nothing and never evicts a connection. - Claude token refreshes use the account's own shared connection. - Drop an account's shared transports when it is removed or its proxy changes. - Reject malformed host:port in proxy URLs (socks5://h:1080:80). - A null proxy_url in an auth-file PATCH clears the live proxy. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/edgee-release.yml | 4 +- .../api/handlers/management/auth_files.go | 6 +- .../management/proxy_url_validation_test.go | 54 ++++ internal/auth/claude/anthropic_auth.go | 8 +- .../auth/claude/anthropic_auth_proxy_test.go | 16 + internal/auth/claude/utls_transport.go | 7 +- internal/runtime/executor/claude_executor.go | 2 +- .../runtime/executor/helps/proxy_helpers.go | 2 +- .../runtime/executor/helps/utls_client.go | 237 ++++++++++---- .../executor/helps/utls_client_test.go | 305 +++++++++++++++++- sdk/cliproxy/auth/conductor.go | 9 + sdk/cliproxy/auth/released.go | 28 ++ sdk/proxyutil/proxy.go | 19 +- sdk/proxyutil/proxy_test.go | 4 + 14 files changed, 622 insertions(+), 79 deletions(-) create mode 100644 sdk/cliproxy/auth/released.go diff --git a/.github/workflows/edgee-release.yml b/.github/workflows/edgee-release.yml index da42f4e5ab6..b3211ddc2ec 100644 --- a/.github/workflows/edgee-release.yml +++ b/.github/workflows/edgee-release.yml @@ -51,7 +51,9 @@ jobs: - name: Race-test shared proxy connections run: >- go test -race -count=1 - -run 'Test(UtlsClient|InvalidProxyURL|ClientsForTheSameProxy|Connect)' + -run 'Test(UtlsClient|InvalidProxyURL|ClientsForTheSameProxy|Connect|ReleasedAccounts|EquivalentProxySettings|NewClaudeAuth|PatchAuthFileFields|ConfigHandlersRejectInvalidProxyURL|UploadAuthFileRejectsBadProxyURL)' + ./internal/api/handlers/management + ./internal/auth/claude ./internal/runtime/executor/helps ./sdk/proxyutil diff --git a/internal/api/handlers/management/auth_files.go b/internal/api/handlers/management/auth_files.go index 31b27c6457c..90ad3d6217e 100644 --- a/internal/api/handlers/management/auth_files.go +++ b/internal/api/handlers/management/auth_files.go @@ -1725,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/proxy_url_validation_test.go b/internal/api/handlers/management/proxy_url_validation_test.go index edda3fc75dd..b1131f05a02 100644 --- a/internal/api/handlers/management/proxy_url_validation_test.go +++ b/internal/api/handlers/management/proxy_url_validation_test.go @@ -171,3 +171,57 @@ func TestUploadAuthFileRejectsBadProxyURL(t *testing.T) { }) } } + +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 8e048d41d88..269a27134b3 100644 --- a/internal/auth/claude/anthropic_auth_proxy_test.go +++ b/internal/auth/claude/anthropic_auth_proxy_test.go @@ -23,3 +23,19 @@ func TestNewClaudeAuthWithProxyURL_OverrideProxyAppliedWithoutConfig(t *testing. 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 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 9974ac11451..a3ee1e2c093 100644 --- a/internal/auth/claude/utls_transport.go +++ b/internal/auth/claude/utls_transport.go @@ -10,11 +10,12 @@ import ( ) // NewAnthropicHttpClient creates an HTTP client that bypasses TLS fingerprinting -// for Anthropic domains by using the shared utls Chrome-fingerprint transport. -func NewAnthropicHttpClient(cfg *config.SDKConfig) *http.Client { +// 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.UtlsTransport(proxyURL)} + return &http.Client{Transport: helps.UtlsTransportForAuth(proxyURL, authID)} } 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/helps/proxy_helpers.go b/internal/runtime/executor/helps/proxy_helpers.go index 132f08cab2a..5f73701f13d 100644 --- a/internal/runtime/executor/helps/proxy_helpers.go +++ b/internal/runtime/executor/helps/proxy_helpers.go @@ -58,7 +58,7 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip } // One transport per proxy and account for the whole process, so connections are pooled across -// clients but never shared between accounts. These come from config, so the map is not evicted. +// clients but never shared between accounts. releaseAccountTransports drops an account's entries. var ( sharedProxyMu sync.Mutex sharedProxyTransports = map[transportKey]*http.Transport{} diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index d48e09166c9..389af1a95b1 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -29,7 +29,7 @@ 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. -// Proxy URLs and accounts come from config, so the map is not evicted. +// releaseAccountTransports drops an account's entries when it is removed or its proxy changes. var ( sharedUtlsMu sync.Mutex sharedUtlsTransports = map[transportKey]*utlsRoundTripper{} @@ -37,6 +37,43 @@ var ( utlsTestRootCAs *x509.CertPool ) +func init() { + cliproxyauth.OnAuthReleased(releaseAccountTransports) +} + +// releaseAccountTransports forgets every shared transport of authID and shuts down their +// connections once their open streams finish. The account-less ("") transports stay. +func releaseAccountTransports(authID string) { + if authID == "" { + return + } + var pools []*utlsConnPool + sharedUtlsMu.Lock() + for key, rt := range sharedUtlsTransports { + if key.authID == authID { + pools = append(pools, rt.pool) + delete(sharedUtlsTransports, key) + } + } + sharedUtlsMu.Unlock() + for _, pool := range pools { + pool.closeAll() + } + + var transports []*http.Transport + sharedProxyMu.Lock() + for key, transport := range sharedProxyTransports { + if key.authID == authID { + transports = append(transports, transport) + delete(sharedProxyTransports, key) + } + } + sharedProxyMu.Unlock() + for _, transport := range transports { + transport.CloseIdleConnections() + } +} + // 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. @@ -44,6 +81,11 @@ func UtlsTransport(proxyURL string) http.RoundTripper { return utlsTransportFor(proxyURL, "") } +// 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 { @@ -75,7 +117,7 @@ func sharedUtlsTransport(proxyURL, authID string) (*utlsRoundTripper, error) { // No idle or ping timeouts: AGENTS.md forbids timeouts after a connection is established. h2 := &http2.Transport{ConnPool: pool} pool.h2 = h2 - rt := &utlsRoundTripper{h2: h2, pool: pool} + rt := &utlsRoundTripper{pool: pool} sharedUtlsTransports[key] = rt return rt, nil } @@ -115,44 +157,58 @@ func utlsDialer(proxyURL string) (proxy.ContextDialer, error) { return contextDialer, nil } -// utlsRoundTripper wraps the shared HTTP/2 transport with what it lacks for a long-lived -// shared connection: closing the body when no connection was obtained, dropping a -// connection that has gone silent, and one replay of a safe request after an abrupt close. +// 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 { - h2 *http2.Transport pool *utlsConnPool } -type utlsAttemptKey struct{} - -// utlsAttempt records the connection the pool handed to one request attempt. -type utlsAttempt struct { - conn *utlsPooledConn - readAtStart uint64 -} +// maxUnprocessedRetries bounds retries of a request the server confirmed it never processed. +const maxUnprocessedRetries = 6 func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { - for replayed := false; ; replayed = true { - attempt := &utlsAttempt{} - resp, err := t.h2.RoundTrip(req.WithContext(context.WithValue(req.Context(), utlsAttemptKey{}, attempt))) + 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 + } + readAtStart := conn.read.Load() + resp, err := conn.cc.RoundTrip(attempt) if err == nil { return resp, nil } - if attempt.conn == nil { - // No connection was obtained, so http2 never took ownership of the body. - if req.Body != nil { - _ = req.Body.Close() - } + body.Close() + if req.Context().Err() != nil { + t.pool.dropIfSilent(conn, readAtStart) return nil, err } - if req.Context().Err() != nil { - t.pool.dropIfSilent(attempt) + if err.Error() == errTextConnNotEstablished { + t.pool.MarkDead(conn.cc) return nil, err } - if replayed || !isAbruptClose(err) { + switch { + case serverDidNotProcess(err) && retry < maxUnprocessedRetries: + case !replayed && mayHaveBeenProcessed(err) && safeToSendTwice(req): + replayed = true + default: return nil, err } - next, ok := replayableRequest(req) + next, ok := rewoundRequest(req) if !ok { return nil, err } @@ -160,21 +216,45 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) } } -// isAbruptClose reports a connection that ended without a GOAWAY before the response headers. -func isAbruptClose(err error) bool { - return errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, syscall.ECONNRESET) || errors.Is(err, syscall.EPIPE) +// 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" +) + +// 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 } -// replayableRequest returns a fresh copy of req when it is safe to send again: an idempotent -// method (or an idempotency key, as net/http allows) and a body that can be rewound. -func replayableRequest(req *http.Request) (*http.Request, bool) { +// mayHaveBeenProcessed reports a failure that is often transient but leaves unknown whether the +// server acted on the request: an abrupt close before the response, or a peer PROTOCOL_ERROR. +func mayHaveBeenProcessed(err error) bool { + if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, syscall.ECONNRESET) || errors.Is(err, syscall.EPIPE) { + return true + } + var se http2.StreamError + return errors.As(err, &se) && se.Code == http2.ErrCodeProtocol +} + +// 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: - default: - if req.Header.Get("Idempotency-Key") == "" && req.Header.Get("X-Idempotency-Key") == "" { - return nil, false - } + 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, when that is possible. +func rewoundRequest(req *http.Request) (*http.Request, bool) { next := req.Clone(req.Context()) if req.Body == nil || req.Body == http.NoBody { return next, true @@ -190,6 +270,39 @@ func replayableRequest(req *http.Request) (*http.Request, bool) { return next, true } +// 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 (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 { @@ -225,24 +338,22 @@ type utlsDial struct { err error } +// 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) { - attempt, _ := req.Context().Value(utlsAttemptKey{}).(*utlsAttempt) - if attempt != nil { - attempt.conn = nil - } - conn, err := p.reserve(req, addr) + conn, err := p.reserve(req.Context(), addr) if err != nil { return nil, err } - if attempt != nil { - attempt.conn = conn - attempt.readAtStart = conn.read.Load() - } return conn.cc, nil } -func (p *utlsConnPool) reserve(req *http.Request, addr string) (*utlsPooledConn, error) { +// reserve returns a shared connection with a stream reserved 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() for _, conn := range p.conns[addr] { if conn.cc.ReserveNewRequest() { @@ -260,13 +371,13 @@ func (p *utlsConnPool) reserve(req *http.Request, addr string) (*utlsPooledConn, select { case <-d.done: - case <-req.Context().Done(): - return nil, req.Context().Err() + case <-ctx.Done(): + return nil, ctx.Err() } if d.err != nil { return nil, d.err } - if d.conn.cc.ReserveNewRequest() { + if ctx.Err() == nil && d.conn.cc.ReserveNewRequest() { return d.conn, nil } } @@ -287,16 +398,30 @@ func (p *utlsConnPool) MarkDead(cc *http2.ClientConn) { } } -// dropIfSilent retires the attempt's connection when the request's context ended and the -// connection received nothing at all meanwhile: the tunnel is dead, so new requests must -// dial a fresh one. Shutdown lets any other streams on it finish, then closes it. -func (p *utlsConnPool) dropIfSilent(attempt *utlsAttempt) { - if attempt.conn.read.Load() != attempt.readAtStart { +// dropIfSilent retires conn when a request sent 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. +// Shutdown lets any other streams on it finish, then closes it. +func (p *utlsConnPool) dropIfSilent(conn *utlsPooledConn, readAtStart uint64) { + if conn.read.Load() != readAtStart { return } - cc := attempt.conn.cc - p.MarkDead(cc) - go func() { _ = cc.Shutdown(context.Background()) }() + p.MarkDead(conn.cc) + go func() { _ = conn.cc.Shutdown(context.Background()) }() +} + +// closeAll forgets every connection and shuts each down once its streams finish. +func (p *utlsConnPool) closeAll() { + p.mu.Lock() + var conns []*utlsPooledConn + for _, list := range p.conns { + conns = append(conns, list...) + } + p.conns = map[string][]*utlsPooledConn{} + p.mu.Unlock() + for _, conn := range conns { + go func() { _ = conn.cc.Shutdown(context.Background()) }() + } } // dial runs detached from any one request, so a cancelled first caller does not fail the others. diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index b9daf9a14e8..9660d9167fe 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -2,6 +2,7 @@ package helps import ( "bufio" + "bytes" "context" "crypto/ecdsa" "crypto/elliptic" @@ -25,6 +26,8 @@ import ( "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" ) type utlsClientRoundTripFunc func(*http.Request) (*http.Response, error) @@ -468,18 +471,306 @@ func TestUtlsClientReplaysGetAfterAbruptClose(t *testing.T) { } } -func TestUtlsClientDoesNotReplayPost(t *testing.T) { +// 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 +) + +// 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 + 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() - req, err := http.NewRequest(http.MethodPost, "https://chatgpt.com/", strings.NewReader("{}")) + 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 + first h2Action + wantOK bool + wantHits int32 + }{ + {"POST is not replayed after PROTOCOL_ERROR", http.MethodPost, h2ProtocolError, false, 1}, + {"POST is not replayed after an abrupt close", http.MethodPost, h2CloseConn, false, 1}, + {"POST is retried after REFUSED_STREAM", http.MethodPost, h2RefuseStream, true, 2}, + {"GET is replayed once after an abrupt close", http.MethodGet, h2CloseConn, true, 2}, + {"GET is replayed once after PROTOCOL_ERROR", http.MethodGet, h2ProtocolError, true, 2}, + } + 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) + } + + 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 _, ok := replayableRequest(req); ok { - t.Fatal("a POST without an idempotency key was marked replayable") + if err = doRequest(p.url, req); err == nil { + t.Fatal("request succeeded against a server that always drops the connection") } - req.Header.Set("Idempotency-Key", "k") - if next, ok := replayableRequest(req); !ok || next.Body == nil { - t.Fatal("a POST with an idempotency key and a rewindable body should be replayable") + 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 TestReleasedAccountsDropTheirSharedConnections(t *testing.T) { + t.Parallel() + server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) + p := startConnectProxy(t, false) + manager := cliproxyauth.NewManager(nil, nil, nil) + ctx := context.Background() + send := func(auth *cliproxyauth.Auth) { + t.Helper() + resp, err := NewUtlsHTTPClient(ctx, nil, auth, 0).Get(chatgptURL(server, "/")) + if err != nil { + t.Fatal(err) + } + _, _ = io.Copy(io.Discard, resp.Body) + _ = resp.Body.Close() + } + waitForOpenTunnels := func(want int32) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for p.open.Load() != want { + if time.Now().After(deadline) { + t.Fatalf("open tunnels = %d, want %d", p.open.Load(), want) + } + time.Sleep(20 * time.Millisecond) + } + } + hasTransport := func(authID string) bool { + sharedUtlsMu.Lock() + defer sharedUtlsMu.Unlock() + for key := range sharedUtlsTransports { + if key.authID == authID { + return true + } + } + return false + } + + moved, _ := manager.Register(ctx, &cliproxyauth.Auth{ID: "released-moved", Provider: "codex", ProxyURL: p.url}) + removed, _ := manager.Register(ctx, &cliproxyauth.Auth{ID: "released-removed", Provider: "codex", ProxyURL: p.url}) + send(moved) + send(removed) + waitForOpenTunnels(2) + + moved.ProxyURL = p.url + "/" + if _, err := manager.Update(ctx, moved); err != nil { + t.Fatal(err) + } + waitForOpenTunnels(1) + if hasTransport("released-moved") { + t.Fatal("a proxy change kept the account's old transport") + } + + manager.Remove(ctx, "released-removed") + waitForOpenTunnels(0) + if hasTransport("released-removed") { + t.Fatal("a removed account kept its transport") } } diff --git a/sdk/cliproxy/auth/conductor.go b/sdk/cliproxy/auth/conductor.go index b492cdf6fd1..e7403821ab6 100644 --- a/sdk/cliproxy/auth/conductor.go +++ b/sdk/cliproxy/auth/conductor.go @@ -2197,8 +2197,12 @@ func (m *Manager) Register(ctx context.Context, auth *Auth) (*Auth, error) { m.mu.Unlock() return nil, fmt.Errorf("persist registered auth %s: %w", auth.ID, errPersist) } + proxyChanged := proxyURLChanged(m.auths[auth.ID], persistAuth) m.auths[auth.ID] = persistAuth m.mu.Unlock() + if proxyChanged { + notifyAuthReleased(auth.ID) + } if !shouldDeferAPIKeyModelAliasRebuild(ctx) { m.rebuildAPIKeyModelAliasFromRuntimeConfig() } @@ -2252,8 +2256,12 @@ func (m *Manager) Update(ctx context.Context, auth *Auth) (*Auth, error) { m.mu.Unlock() return nil, fmt.Errorf("persist updated auth %s: %w", auth.ID, errPersist) } + proxyChanged := proxyURLChanged(existing, persistAuth) m.auths[auth.ID] = persistAuth m.mu.Unlock() + if proxyChanged { + notifyAuthReleased(auth.ID) + } if !shouldDeferAPIKeyModelAliasRebuild(ctx) { m.rebuildAPIKeyModelAliasFromRuntimeConfig() } @@ -2304,6 +2312,7 @@ func (m *Manager) Remove(ctx context.Context, id string) { } } m.mu.Unlock() + notifyAuthReleased(id) if !shouldDeferAPIKeyModelAliasRebuild(ctx) { m.rebuildAPIKeyModelAliasFromRuntimeConfig() diff --git a/sdk/cliproxy/auth/released.go b/sdk/cliproxy/auth/released.go new file mode 100644 index 00000000000..d57b0c9a937 --- /dev/null +++ b/sdk/cliproxy/auth/released.go @@ -0,0 +1,28 @@ +package auth + +import "sync" + +var ( + authReleasedMu sync.RWMutex + authReleasedFuncs []func(authID string) +) + +// OnAuthReleased registers fn to run after an auth is removed or its proxy URL changes, so +// resources kept per account, such as shared connections, can be released. +func OnAuthReleased(fn func(authID string)) { + authReleasedMu.Lock() + defer authReleasedMu.Unlock() + authReleasedFuncs = append(authReleasedFuncs, fn) +} + +func notifyAuthReleased(authID string) { + authReleasedMu.RLock() + defer authReleasedMu.RUnlock() + for _, fn := range authReleasedFuncs { + fn(authID) + } +} + +func proxyURLChanged(existing, next *Auth) bool { + return existing != nil && next != nil && existing.ProxyURL != next.ProxyURL +} diff --git a/sdk/proxyutil/proxy.go b/sdk/proxyutil/proxy.go index 33321749e48..1a61eb969ca 100644 --- a/sdk/proxyutil/proxy.go +++ b/sdk/proxyutil/proxy.go @@ -77,18 +77,25 @@ func Parse(raw string) (Setting, error) { return setting, nil } -// checkProxyPort requires a port in 1-65535 when one is given, and always for SOCKS5. +// 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 { - port := u.Port() - if port == "" { - if strings.HasSuffix(u.Host, ":") { - return fmt.Errorf("proxy URL has an empty port") - } + 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) } diff --git a/sdk/proxyutil/proxy_test.go b/sdk/proxyutil/proxy_test.go index 6dba34278c3..73564110e49 100644 --- a/sdk/proxyutil/proxy_test.go +++ b/sdk/proxyutil/proxy_test.go @@ -50,6 +50,10 @@ func TestParse(t *testing.T) { {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 { From fbf365c19fca978cd26e756648b84b4b429fc454 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 12:41:32 +0000 Subject: [PATCH 08/14] Drop the account-release hook, back off between retries, replay only peer PROTOCOL_ERROR - Remove OnAuthReleased and the per-account pool release: it raced in-flight requests and global proxy changes. Upstreams close idle tunnels themselves, so a stale pool holds memory. - Wait x/net's 1s, 2s, 4s... (plus jitter) between retries, stopping when the request ends. - Replay after PROTOCOL_ERROR only when the server sent the reset; a malformed response detected locally means the server already answered. - Drop the not-established branch: x/net already removes that connection from the pool. - Tests for GOAWAY below/covering a POST, an idempotency-keyed POST, a malformed response and the backoff schedule. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/edgee-release.yml | 2 +- .../runtime/executor/helps/proxy_helpers.go | 2 +- .../runtime/executor/helps/utls_client.go | 101 ++++++-------- .../executor/helps/utls_client_test.go | 126 ++++++++---------- sdk/cliproxy/auth/conductor.go | 9 -- sdk/cliproxy/auth/released.go | 28 ---- 6 files changed, 100 insertions(+), 168 deletions(-) delete mode 100644 sdk/cliproxy/auth/released.go diff --git a/.github/workflows/edgee-release.yml b/.github/workflows/edgee-release.yml index b3211ddc2ec..9363b2ed067 100644 --- a/.github/workflows/edgee-release.yml +++ b/.github/workflows/edgee-release.yml @@ -51,7 +51,7 @@ jobs: - name: Race-test shared proxy connections run: >- go test -race -count=1 - -run 'Test(UtlsClient|InvalidProxyURL|ClientsForTheSameProxy|Connect|ReleasedAccounts|EquivalentProxySettings|NewClaudeAuth|PatchAuthFileFields|ConfigHandlersRejectInvalidProxyURL|UploadAuthFileRejectsBadProxyURL)' + -run 'Test(UtlsClient|InvalidProxyURL|ClientsForTheSameProxy|Connect|RetryBackoff|EquivalentProxySettings|NewClaudeAuth|PatchAuthFileFields|ConfigHandlersRejectInvalidProxyURL|UploadAuthFileRejectsBadProxyURL)' ./internal/api/handlers/management ./internal/auth/claude ./internal/runtime/executor/helps diff --git a/internal/runtime/executor/helps/proxy_helpers.go b/internal/runtime/executor/helps/proxy_helpers.go index 5f73701f13d..e8f11c1765f 100644 --- a/internal/runtime/executor/helps/proxy_helpers.go +++ b/internal/runtime/executor/helps/proxy_helpers.go @@ -58,7 +58,7 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip } // One transport per proxy and account for the whole process, so connections are pooled across -// clients but never shared between accounts. releaseAccountTransports drops an account's entries. +// clients but never shared between accounts. Entries are never evicted; idle connections close. var ( sharedProxyMu sync.Mutex sharedProxyTransports = map[transportKey]*http.Transport{} diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index 389af1a95b1..5090c4f946b 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "math/rand/v2" "net" "net/http" "net/url" @@ -29,7 +30,8 @@ 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. -// releaseAccountTransports drops an account's entries when it is removed or its proxy changes. +// 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{} @@ -37,43 +39,6 @@ var ( utlsTestRootCAs *x509.CertPool ) -func init() { - cliproxyauth.OnAuthReleased(releaseAccountTransports) -} - -// releaseAccountTransports forgets every shared transport of authID and shuts down their -// connections once their open streams finish. The account-less ("") transports stay. -func releaseAccountTransports(authID string) { - if authID == "" { - return - } - var pools []*utlsConnPool - sharedUtlsMu.Lock() - for key, rt := range sharedUtlsTransports { - if key.authID == authID { - pools = append(pools, rt.pool) - delete(sharedUtlsTransports, key) - } - } - sharedUtlsMu.Unlock() - for _, pool := range pools { - pool.closeAll() - } - - var transports []*http.Transport - sharedProxyMu.Lock() - for key, transport := range sharedProxyTransports { - if key.authID == authID { - transports = append(transports, transport) - delete(sharedProxyTransports, key) - } - } - sharedProxyMu.Unlock() - for _, transport := range transports { - transport.CloseIdleConnections() - } -} - // 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. @@ -197,10 +162,6 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) t.pool.dropIfSilent(conn, readAtStart) return nil, err } - if err.Error() == errTextConnNotEstablished { - t.pool.MarkDead(conn.cc) - return nil, err - } switch { case serverDidNotProcess(err) && retry < maxUnprocessedRetries: case !replayed && mayHaveBeenProcessed(err) && safeToSendTwice(req): @@ -208,6 +169,9 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) default: return nil, err } + if waitErr := waitBeforeRetry(req.Context(), retryBackoff(retry)); waitErr != nil { + return nil, waitErr + } next, ok := rewoundRequest(req) if !ok { return nil, err @@ -216,11 +180,38 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) } } +// retryBackoff is the wait before the retry that follows attempt retry; tests replace it. +var retryBackoff = xnetRetryBackoff + +// 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()) +} + +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() + } +} + // 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" + errTextConnUnusable = "http2: client conn not usable" + errTextConnGotGoAway = "http2: Transport received Server's graceful shutdown GOAWAY" + errTextFromPeer = "received from peer" ) // serverDidNotProcess reports an error after which the server cannot have acted on the request: @@ -235,13 +226,15 @@ func serverDidNotProcess(err error) bool { } // mayHaveBeenProcessed reports a failure that is often transient but leaves unknown whether the -// server acted on the request: an abrupt close before the response, or a peer PROTOCOL_ERROR. +// server acted on the request: an abrupt close before the response, 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) { return true } var se http2.StreamError - return errors.As(err, &se) && se.Code == http2.ErrCodeProtocol + 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. @@ -410,20 +403,6 @@ func (p *utlsConnPool) dropIfSilent(conn *utlsPooledConn, readAtStart uint64) { go func() { _ = conn.cc.Shutdown(context.Background()) }() } -// closeAll forgets every connection and shuts each down once its streams finish. -func (p *utlsConnPool) closeAll() { - p.mu.Lock() - var conns []*utlsPooledConn - for _, list := range p.conns { - conns = append(conns, list...) - } - p.conns = map[string][]*utlsPooledConn{} - p.mu.Unlock() - for _, conn := range conns { - go func() { _ = conn.cc.Shutdown(context.Background()) }() - } -} - // 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) diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index 9660d9167fe..049223f7003 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -17,6 +17,7 @@ import ( "net" "net/http" "net/http/httptest" + "os" "strings" "sync" "sync/atomic" @@ -30,6 +31,32 @@ import ( "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) { @@ -479,6 +506,9 @@ const ( 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 @@ -559,6 +589,16 @@ func (s *rawH2Server) serve(conn net.Conn, action func(hit int32) h2Action) { _ = 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"}) @@ -587,17 +627,22 @@ func TestUtlsClientSendsEachRequestAsOftenAsIsSafe(t *testing.T) { } } cases := []struct { - name string - method string - first h2Action - wantOK bool - wantHits int32 + name string + method string + idempotencyKey bool + first h2Action + wantOK bool + wantHits int32 }{ - {"POST is not replayed after PROTOCOL_ERROR", http.MethodPost, h2ProtocolError, false, 1}, - {"POST is not replayed after an abrupt close", http.MethodPost, h2CloseConn, false, 1}, - {"POST is retried after REFUSED_STREAM", http.MethodPost, h2RefuseStream, true, 2}, - {"GET is replayed once after an abrupt close", http.MethodGet, h2CloseConn, true, 2}, - {"GET is replayed once after PROTOCOL_ERROR", http.MethodGet, h2ProtocolError, true, 2}, + {"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) { @@ -612,6 +657,9 @@ func TestUtlsClientSendsEachRequestAsOftenAsIsSafe(t *testing.T) { if err != nil { t.Fatal(err) } + if tc.idempotencyKey { + req.Header.Set("Idempotency-Key", "charge-once") + } err = doRequest(p.url, req) @@ -716,64 +764,6 @@ func TestUtlsClientCancelledRequestKeepsTheSharedConnection(t *testing.T) { } } -func TestReleasedAccountsDropTheirSharedConnections(t *testing.T) { - t.Parallel() - server := startChatgptServer(t, true, http.HandlerFunc(writeProto)) - p := startConnectProxy(t, false) - manager := cliproxyauth.NewManager(nil, nil, nil) - ctx := context.Background() - send := func(auth *cliproxyauth.Auth) { - t.Helper() - resp, err := NewUtlsHTTPClient(ctx, nil, auth, 0).Get(chatgptURL(server, "/")) - if err != nil { - t.Fatal(err) - } - _, _ = io.Copy(io.Discard, resp.Body) - _ = resp.Body.Close() - } - waitForOpenTunnels := func(want int32) { - t.Helper() - deadline := time.Now().Add(5 * time.Second) - for p.open.Load() != want { - if time.Now().After(deadline) { - t.Fatalf("open tunnels = %d, want %d", p.open.Load(), want) - } - time.Sleep(20 * time.Millisecond) - } - } - hasTransport := func(authID string) bool { - sharedUtlsMu.Lock() - defer sharedUtlsMu.Unlock() - for key := range sharedUtlsTransports { - if key.authID == authID { - return true - } - } - return false - } - - moved, _ := manager.Register(ctx, &cliproxyauth.Auth{ID: "released-moved", Provider: "codex", ProxyURL: p.url}) - removed, _ := manager.Register(ctx, &cliproxyauth.Auth{ID: "released-removed", Provider: "codex", ProxyURL: p.url}) - send(moved) - send(removed) - waitForOpenTunnels(2) - - moved.ProxyURL = p.url + "/" - if _, err := manager.Update(ctx, moved); err != nil { - t.Fatal(err) - } - waitForOpenTunnels(1) - if hasTransport("released-moved") { - t.Fatal("a proxy change kept the account's old transport") - } - - manager.Remove(ctx, "released-removed") - waitForOpenTunnels(0) - if hasTransport("released-removed") { - t.Fatal("a removed account kept its transport") - } -} - func TestUtlsClientClosesBodyWhenNoConnectionIsObtained(t *testing.T) { t.Parallel() p := startConnectProxy(t, true) diff --git a/sdk/cliproxy/auth/conductor.go b/sdk/cliproxy/auth/conductor.go index e7403821ab6..b492cdf6fd1 100644 --- a/sdk/cliproxy/auth/conductor.go +++ b/sdk/cliproxy/auth/conductor.go @@ -2197,12 +2197,8 @@ func (m *Manager) Register(ctx context.Context, auth *Auth) (*Auth, error) { m.mu.Unlock() return nil, fmt.Errorf("persist registered auth %s: %w", auth.ID, errPersist) } - proxyChanged := proxyURLChanged(m.auths[auth.ID], persistAuth) m.auths[auth.ID] = persistAuth m.mu.Unlock() - if proxyChanged { - notifyAuthReleased(auth.ID) - } if !shouldDeferAPIKeyModelAliasRebuild(ctx) { m.rebuildAPIKeyModelAliasFromRuntimeConfig() } @@ -2256,12 +2252,8 @@ func (m *Manager) Update(ctx context.Context, auth *Auth) (*Auth, error) { m.mu.Unlock() return nil, fmt.Errorf("persist updated auth %s: %w", auth.ID, errPersist) } - proxyChanged := proxyURLChanged(existing, persistAuth) m.auths[auth.ID] = persistAuth m.mu.Unlock() - if proxyChanged { - notifyAuthReleased(auth.ID) - } if !shouldDeferAPIKeyModelAliasRebuild(ctx) { m.rebuildAPIKeyModelAliasFromRuntimeConfig() } @@ -2312,7 +2304,6 @@ func (m *Manager) Remove(ctx context.Context, id string) { } } m.mu.Unlock() - notifyAuthReleased(id) if !shouldDeferAPIKeyModelAliasRebuild(ctx) { m.rebuildAPIKeyModelAliasFromRuntimeConfig() diff --git a/sdk/cliproxy/auth/released.go b/sdk/cliproxy/auth/released.go deleted file mode 100644 index d57b0c9a937..00000000000 --- a/sdk/cliproxy/auth/released.go +++ /dev/null @@ -1,28 +0,0 @@ -package auth - -import "sync" - -var ( - authReleasedMu sync.RWMutex - authReleasedFuncs []func(authID string) -) - -// OnAuthReleased registers fn to run after an auth is removed or its proxy URL changes, so -// resources kept per account, such as shared connections, can be released. -func OnAuthReleased(fn func(authID string)) { - authReleasedMu.Lock() - defer authReleasedMu.Unlock() - authReleasedFuncs = append(authReleasedFuncs, fn) -} - -func notifyAuthReleased(authID string) { - authReleasedMu.RLock() - defer authReleasedMu.RUnlock() - for _, fn := range authReleasedFuncs { - fn(authID) - } -} - -func proxyURLChanged(existing, next *Auth) bool { - return existing != nil && next != nil && existing.ProxyURL != next.ProxyURL -} From 931c90f88a866d9d2e3b1edc561291f0e58dd2e5 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 12:53:40 +0000 Subject: [PATCH 09/14] Evict conns that die before their first stream; rewind before backoff - Restore MarkDead on 'client conn could not be established': x/net leaves such a conn reservable, so every later request failed on it without redialling. - Rewind the body before the backoff wait, return a GetBody failure as its own error, and close the rewound body if the wait is cancelled. - Tests: redial after a conn dies before its first stream; the RoundTrip backoff is used and stops on cancel with both bodies closed; a rewind error returns without waiting. Added to the release race gate. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/edgee-release.yml | 2 +- .../runtime/executor/helps/utls_client.go | 34 +++-- .../executor/helps/utls_client_test.go | 120 ++++++++++++++++++ 3 files changed, 142 insertions(+), 14 deletions(-) diff --git a/.github/workflows/edgee-release.yml b/.github/workflows/edgee-release.yml index 9363b2ed067..eb0d7aac3bd 100644 --- a/.github/workflows/edgee-release.yml +++ b/.github/workflows/edgee-release.yml @@ -51,7 +51,7 @@ jobs: - name: Race-test shared proxy connections run: >- go test -race -count=1 - -run 'Test(UtlsClient|InvalidProxyURL|ClientsForTheSameProxy|Connect|RetryBackoff|EquivalentProxySettings|NewClaudeAuth|PatchAuthFileFields|ConfigHandlersRejectInvalidProxyURL|UploadAuthFileRejectsBadProxyURL)' + -run 'Test(UtlsClient|InvalidProxyURL|ClientsForTheSameProxy|Connect|RetryBackoff|ReviewNotEstablishedConnIsReused|EquivalentProxySettings|NewClaudeAuth|PatchAuthFileFields|ConfigHandlersRejectInvalidProxyURL|UploadAuthFileRejectsBadProxyURL)' ./internal/api/handlers/management ./internal/auth/claude ./internal/runtime/executor/helps diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index 5090c4f946b..ca82a139ef8 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -158,6 +158,11 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) return resp, nil } body.Close() + 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 @@ -169,13 +174,14 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) 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 } - next, ok := rewoundRequest(req) - if !ok { - return nil, err - } req = next } } @@ -209,9 +215,10 @@ func waitBeforeRetry(ctx context.Context, d time.Duration) error { // 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" - errTextFromPeer = "received from peer" + errTextConnUnusable = "http2: client conn not usable" + errTextConnGotGoAway = "http2: Transport received Server's graceful shutdown GOAWAY" + errTextConnNotEstablished = "http2: client conn could not be established" + errTextFromPeer = "received from peer" ) // serverDidNotProcess reports an error after which the server cannot have acted on the request: @@ -246,21 +253,22 @@ func safeToSendTwice(req *http.Request) bool { 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, when that is possible. -func rewoundRequest(req *http.Request) (*http.Request, bool) { +// 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, true + return next, nil } if req.GetBody == nil { - return nil, false + return nil, cause } body, err := req.GetBody() if err != nil { - return nil, false + return nil, fmt.Errorf("utls: rewinding the request body after %v: %w", cause, err) } next.Body = body - return next, true + return next, nil } // onceClosedBody lets both http2 and the round tripper close an attempt's body, closing it once. diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index 049223f7003..251fadd28e9 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -921,3 +921,123 @@ func TestInvalidProxyURLClosesTheRequestBody(t *testing.T) { } } } + +// 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) + } +} From aa5be62e45a27bba752fadd4d3f738346781262b Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 13:33:18 +0000 Subject: [PATCH 10/14] Retire silent tunnels after headers, close retired conns directly, drop closed conns - Track response bodies: a body ending by its context with nothing received since the last read retires the conn, and releases it when done. - Close a retired conn with ClientConn.Close once no live-context request uses it, instead of Shutdown, which blocks behind a writer stuck on a full send buffer. - Mark a conn dead when its read loop stops; drop such conns from the pool on reserve and after a failed round trip. - Treat x/net's forced-close error as an abrupt close (safe requests only). - README: one pool per proxy URL and account. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- .../runtime/executor/helps/utls_client.go | 127 +++++++++-- .../executor/helps/utls_client_test.go | 209 ++++++++++++++++++ 3 files changed, 320 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4e5731a96aa..ea03aab619a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ 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.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 (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. +> **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.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). > diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index ca82a139ef8..72249fbdc63 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -152,12 +152,24 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) body.Close() return nil, err } + release := conn.use(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) @@ -218,6 +230,7 @@ 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" ) @@ -233,11 +246,12 @@ func serverDidNotProcess(err error) bool { } // mayHaveBeenProcessed reports a failure that is often transient but leaves unknown whether the -// server acted on the request: an abrupt close before the response, 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. +// 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) { + 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 @@ -315,24 +329,94 @@ type utlsConnPool struct { 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. +// 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 + cc *http2.ClientConn + read atomic.Uint64 + dead atomic.Bool // its read loop stopped, so it can never carry another request + live atomic.Int32 // requests using it whose contexts have not ended + retired atomic.Bool + closeOnce sync.Once +} + +// use counts one request on conn until 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) use(ctx context.Context) (release func()) { + c.live.Add(1) + var once sync.Once + done := func() { + once.Do(func() { + if c.live.Add(-1) == 0 && c.retired.Load() { + c.close() + } + }) + } + stop := context.AfterFunc(ctx, done) + return func() { stop(); done() } +} + +func (c *utlsPooledConn) retire() { + c.retired.Store(true) + if c.live.Load() == 0 { + 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() { _ = c.cc.Close() }) } + type countingConn struct { net.Conn - read *atomic.Uint64 + conn *utlsPooledConn } func (c countingConn) Read(b []byte) (int, error) { n, err := c.Conn.Read(b) - c.read.Add(uint64(n)) + c.conn.read.Add(uint64(n)) + if err != nil { + c.conn.dead.Store(true) + } return n, err } +// 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 +} + +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 @@ -356,6 +440,7 @@ func (p *utlsConnPool) reserve(ctx context.Context, addr string) (*utlsPooledCon return nil, err } p.mu.Lock() + p.dropDeadLocked(addr) for _, conn := range p.conns[addr] { if conn.cc.ReserveNewRequest() { p.mu.Unlock() @@ -384,6 +469,14 @@ func (p *utlsConnPool) reserve(ctx context.Context, addr string) (*utlsPooledCon } } +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 (p *utlsConnPool) MarkDead(cc *http2.ClientConn) { p.mu.Lock() defer p.mu.Unlock() @@ -399,16 +492,16 @@ func (p *utlsConnPool) MarkDead(cc *http2.ClientConn) { } } -// dropIfSilent retires conn when a request sent on it ended with its context and the connection +// 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. -// Shutdown lets any other streams on it finish, then closes it. -func (p *utlsConnPool) dropIfSilent(conn *utlsPooledConn, readAtStart uint64) { - if conn.read.Load() != readAtStart { +// 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 } p.MarkDead(conn.cc) - go func() { _ = conn.cc.Shutdown(context.Background()) }() + conn.retire() } // dial runs detached from any one request, so a cancelled first caller does not fail the others. @@ -418,7 +511,7 @@ func (p *utlsConnPool) dial(d *utlsDial, addr string) { conn, err := p.dialTLS(ctx, addr) if err == nil { pooled := &utlsPooledConn{} - if pooled.cc, err = p.h2.NewClientConn(countingConn{Conn: conn, read: &pooled.read}); err != nil { + if pooled.cc, err = p.h2.NewClientConn(countingConn{Conn: conn, conn: pooled}); err != nil { _ = conn.Close() } else { d.conn = pooled diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index 251fadd28e9..5981d60a5e3 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -18,6 +18,7 @@ import ( "net/http" "net/http/httptest" "os" + "slices" "strings" "sync" "sync/atomic" @@ -164,6 +165,23 @@ type connectProxy struct { 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. @@ -188,6 +206,9 @@ func (p *connectProxy) closeOpenTunnels() { 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 { @@ -1041,3 +1062,191 @@ func TestUtlsClientReturnsTheRewindErrorWithoutWaiting(t *testing.T) { t.Fatalf("err = %v, want the rewind error", err) } } + +// pooledConns returns the account-less shared connections for proxyURL. +func pooledConns(t *testing.T, proxyURL string) []*utlsPooledConn { + t.Helper() + rt, err := sharedUtlsTransport(proxyURL, "") + if err != nil { + t.Fatal(err) + } + rt.pool.mu.Lock() + defer rt.pool.mu.Unlock() + var out []*utlsPooledConn + for _, list := range rt.pool.conns { + out = append(out, list...) + } + return out +} + +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) + 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) + 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); 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) + 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()) + } + }) + } +} From 15cc44b0391c930e6deddc34ae8f45bf29e87e1c Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 13:50:16 +0000 Subject: [PATCH 11/14] Reserve and count a request on its conn in one step against retirement A request reserved a stream, then counted itself live in a separate step. A retire in between saw zero live requests and closed the conn under the new request, aborting a plain POST the server had already received. tryReserve now does both under the conn's mutex, and retire and release decide to close under the same mutex, so a conn retire has chosen to close is never handed out and is never closed under a reserved request. Co-Authored-By: Claude Opus 5.5 --- .../runtime/executor/helps/utls_client.go | 82 ++++++++---- .../executor/helps/utls_client_test.go | 121 ++++++++++++++++++ 2 files changed, 181 insertions(+), 22 deletions(-) diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index 72249fbdc63..4acc85451a7 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -152,7 +152,10 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) body.Close() return nil, err } - release := conn.use(req.Context()) + if h := testHooks.Load(); h != nil { + h.afterReserve(req) + } + release := conn.track(req.Context()) readAtStart := conn.read.Load() resp, err := conn.cc.RoundTrip(attempt) if err == nil { @@ -334,37 +337,71 @@ type utlsConnPool struct { type utlsPooledConn struct { cc *http2.ClientConn read atomic.Uint64 - dead atomic.Bool // its read loop stopped, so it can never carry another request - live atomic.Int32 // requests using it whose contexts have not ended - retired atomic.Bool + 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 } -// use counts one request on conn until 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) use(ctx context.Context) (release func()) { - c.live.Add(1) - var once sync.Once - done := func() { - once.Do(func() { - if c.live.Add(-1) == 0 && c.retired.Load() { - c.close() - } - }) +// 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.retired.Store(true) - if c.live.Load() == 0 { + 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() { _ = c.cc.Close() }) } +func (c *utlsPooledConn) close() { + c.closeOnce.Do(func() { + if h := testHooks.Load(); h != 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 @@ -429,11 +466,12 @@ func (p *utlsConnPool) GetClientConn(req *http.Request, addr string) (*http2.Cli if err != nil { 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 for one request, dialling one -// when none can take it. A request whose context has already ended reserves nothing. +// 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 { @@ -442,7 +480,7 @@ func (p *utlsConnPool) reserve(ctx context.Context, addr string) (*utlsPooledCon p.mu.Lock() p.dropDeadLocked(addr) for _, conn := range p.conns[addr] { - if conn.cc.ReserveNewRequest() { + if conn.tryReserve() { p.mu.Unlock() return conn, nil } @@ -463,7 +501,7 @@ func (p *utlsConnPool) reserve(ctx context.Context, addr string) (*utlsPooledCon if d.err != nil { return nil, d.err } - if ctx.Err() == nil && d.conn.cc.ReserveNewRequest() { + if ctx.Err() == nil && d.conn.tryReserve() { return d.conn, nil } } diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index 5981d60a5e3..e2f69b899d0 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -1250,3 +1250,124 @@ func TestUtlsClientReplaysOnlySafeRequestsAfterAForcedClose(t *testing.T) { }) } } + +// 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) + 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 }) +} From b4fd0951332bdef1a3ea8c1c12043aca38642651 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 14:14:14 +0000 Subject: [PATCH 12/14] Key pooledConns by the test's own server and drop the retired test conn Two parallel tests could share one transport through a reused proxy port, so TestUtlsClientDropsClosedConnectionsFromThePool sometimes counted another test's conn. Test-only change. Co-Authored-By: Claude Opus 5.5 --- .../executor/helps/utls_client_test.go | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index e2f69b899d0..c3c855b7b4b 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -1063,20 +1063,18 @@ func TestUtlsClientReturnsTheRewindErrorWithoutWaiting(t *testing.T) { } } -// pooledConns returns the account-less shared connections for proxyURL. -func pooledConns(t *testing.T, proxyURL string) []*utlsPooledConn { +// 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() - var out []*utlsPooledConn - for _, list := range rt.pool.conns { - out = append(out, list...) - } - return out + return slices.Clone(rt.pool.conns[net.JoinHostPort("chatgpt.com", port)]) } func waitFor(t *testing.T, what string, cond func() bool) { @@ -1158,7 +1156,7 @@ func TestUtlsClientClosesARetiredConnectionStuckBehindAFullSendBuffer(t *testing if _, err := getBody(context.Background(), p.url, url); err != nil { t.Fatal(err) } - conns := pooledConns(t, p.url) + conns := pooledConns(t, p.url, server) if len(conns) != 1 { t.Fatalf("pooled conns = %d, want 1", len(conns)) } @@ -1189,7 +1187,7 @@ func TestUtlsClientDropsClosedConnectionsFromThePool(t *testing.T) { if _, err := getBody(context.Background(), p.url, url); err != nil { t.Fatal(err) } - first := pooledConns(t, p.url) + first := pooledConns(t, p.url, server) if len(first) != 1 { t.Fatalf("pooled conns = %d, want 1", len(first)) } @@ -1198,7 +1196,7 @@ func TestUtlsClientDropsClosedConnectionsFromThePool(t *testing.T) { if _, err := getBody(context.Background(), p.url, url); err != nil { t.Fatal(err) } - if got := pooledConns(t, p.url); len(got) != 1 || got[0] == first[0] { + 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])) } } @@ -1232,7 +1230,7 @@ func TestUtlsClientReplaysOnlySafeRequestsAfterAForcedClose(t *testing.T) { done := make(chan error, 1) go func() { done <- doRequest(p.url, req) }() <-arrived - conns := pooledConns(t, p.url) + conns := pooledConns(t, p.url, server) if len(conns) != 1 { t.Fatalf("pooled conns = %d, want 1", len(conns)) } @@ -1353,7 +1351,7 @@ func TestUtlsClientNeverReservesARetiredConnection(t *testing.T) { if _, err := getBody(context.Background(), p.url, chatgptURL(server, "/")); err != nil { t.Fatal(err) } - conns := pooledConns(t, p.url) + conns := pooledConns(t, p.url, server) if len(conns) != 1 { t.Fatalf("pooled conns = %d, want 1", len(conns)) } @@ -1370,4 +1368,9 @@ func TestUtlsClientNeverReservesARetiredConnection(t *testing.T) { } 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) } From 39f3cca3a22c04fed1f043f486f3e5993552e74e Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 14:21:03 +0000 Subject: [PATCH 13/14] Guard each test hook field before calling it Co-Authored-By: Claude Opus 5.5 --- internal/runtime/executor/helps/utls_client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/runtime/executor/helps/utls_client.go b/internal/runtime/executor/helps/utls_client.go index 4acc85451a7..e74a6e3a0d6 100644 --- a/internal/runtime/executor/helps/utls_client.go +++ b/internal/runtime/executor/helps/utls_client.go @@ -152,7 +152,7 @@ func (t *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) body.Close() return nil, err } - if h := testHooks.Load(); h != nil { + if h := testHooks.Load(); h != nil && h.afterReserve != nil { h.afterReserve(req) } release := conn.track(req.Context()) @@ -388,7 +388,7 @@ func (c *utlsPooledConn) retire() { // 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 { + if h := testHooks.Load(); h != nil && h.beforeRetiredClose != nil { h.beforeRetiredClose() } _ = c.cc.Close() From f4cd4dc7451e35f77721683de8cbaee706ef3dc2 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 14:44:03 +0000 Subject: [PATCH 14/14] Give the GOAWAY retry test a per-connection quota above its in-flight requests With 25 streams per connection and 40 requests in flight, and retry backoff off in tests, one request could be refused above last-stream-id on 7 connections in a row and run out of retries. Test only. Co-Authored-By: Claude Opus 5.5 --- internal/runtime/executor/helps/utls_client_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/runtime/executor/helps/utls_client_test.go b/internal/runtime/executor/helps/utls_client_test.go index c3c855b7b4b..ac89f3c07dd 100644 --- a/internal/runtime/executor/helps/utls_client_test.go +++ b/internal/runtime/executor/helps/utls_client_test.go @@ -407,8 +407,11 @@ func TestUtlsClientRequestsReturnOnDeadlineWhenProxyIsSilent(t *testing.T) { 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)%25 == 0 { + if served.Add(1)%50 == 0 { w.Header().Set("Connection", "close") // the HTTP/2 server answers with GOAWAY } writeProto(w, r)