Files
CLIProxyAPI/internal/runtime/executor/helps/utls_client.go
Luis Pater 2c5b01f439 fix(claude): disable default upstream connection reuse and revert post-stream draining
- Revert post-message_stop response body draining and detachable request context from Claude streaming execution so completed streams close immediately instead of returning connections to the idle pool.
- Set MaxIdleConnsPerHost to -1 on the default Claude Code uTLS transport to disable idle connection reuse for OAuth and unpooled credentials while preserving the Connection: keep-alive wire header and explicit connection-pool opt-in.
- Add regression tests verifying short-lived default Claude Code connections, explicit per-key pool reuse, and non-reused streaming connections.

Closes: #7357
2026-11-09 02:46:14 +08:00

560 lines
19 KiB
Go

package helps
import (
"context"
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
"sync"
"time"
tls "github.com/refraction-networking/utls"
internalcache "github.com/router-for-me/CLIProxyAPI/v8/internal/cache"
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
"github.com/router-for-me/CLIProxyAPI/v8/internal/httpwire"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
"github.com/router-for-me/CLIProxyAPI/v8/sdk/httptransport"
"github.com/router-for-me/CLIProxyAPI/v8/sdk/proxyutil"
log "github.com/sirupsen/logrus"
"golang.org/x/net/http2"
"golang.org/x/net/proxy"
)
// utlsRoundTripper implements http.RoundTripper using a Chrome fingerprint for
// providers that require browser-like TLS. The HTTP version follows ALPN. Each
// request gets a dedicated connection that is closed with the response body.
type utlsRoundTripper struct {
dialer proxy.Dialer
}
type closeConnectionBody struct {
io.ReadCloser
closeConnection func() error
once sync.Once
err error
}
func (b *closeConnectionBody) Close() error {
if b == nil {
return nil
}
b.once.Do(func() {
var errConnection error
if b.closeConnection != nil {
errConnection = b.closeConnection()
}
var errBody error
if b.ReadCloser != nil {
errBody = b.ReadCloser.Close()
}
b.err = errors.Join(errBody, errConnection)
})
return b.err
}
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
}
}
return &utlsRoundTripper{dialer: dialer}
}
func (t *utlsRoundTripper) createConnection(ctx context.Context, host, addr string) (*tls.UConn, error) {
contextDialer, ok := t.dialer.(proxy.ContextDialer)
if !ok {
return nil, fmt.Errorf("utls: dialer does not support context cancellation")
}
conn, errDial := contextDialer.DialContext(ctx, "tcp", addr)
if errDial != nil {
return nil, fmt.Errorf("utls: dial upstream: %w", errDial)
}
tlsConfig := &tls.Config{ServerName: host}
tlsConn := tls.UClient(conn, tlsConfig, tls.HelloChrome_Auto)
handshakeCtx, cancelHandshake := handshakeContext(ctx)
defer cancelHandshake()
if errHandshake := tlsConn.HandshakeContext(handshakeCtx); errHandshake != nil {
errHandshake = classifyHandshakeError(ctx, handshakeCtx, errHandshake)
if errors.Is(errHandshake, context.Canceled) || errors.Is(errHandshake, context.DeadlineExceeded) {
return nil, fmt.Errorf("utls: TLS handshake: %w", errHandshake)
}
if errClose := conn.Close(); errClose != nil {
return nil, fmt.Errorf("utls: TLS handshake: %w; close connection: %v", errHandshake, errClose)
}
return nil, fmt.Errorf("utls: TLS handshake: %w", errHandshake)
}
return tlsConn, nil
}
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)
tlsConn, err := t.createConnection(req.Context(), hostname, addr)
if err != nil {
return nil, err
}
return roundTripUtlsConnection(req, tlsConn)
}
// roundTripUtlsConnection selects the HTTP protocol before sending the request.
// Empty ALPN is HTTP/1.1, including when a TLS-inspecting proxy omits ALPN.
func roundTripUtlsConnection(req *http.Request, tlsConn *tls.UConn) (*http.Response, error) {
closeConnection := func() error {
// The HTTP/1.1 transport may already have closed its non-pooled
// connection after reading the response or canceling the request.
if errClose := tlsConn.Close(); errClose != nil && !errors.Is(errClose, net.ErrClosed) {
return errClose
}
return nil
}
var resp *http.Response
var err error
switch protocol := tlsConn.ConnectionState().NegotiatedProtocol; protocol {
case "h2":
h2Conn, errClientConn := (&http2.Transport{}).NewClientConn(tlsConn)
if errClientConn != nil {
err = fmt.Errorf("utls: initialize HTTP/2 connection: %w", errClientConn)
break
}
closeConnection = h2Conn.Close
resp, err = h2Conn.RoundTrip(req)
case "", "http/1.1":
// Reuse the already-handshaken uTLS connection. A fresh, non-pooling
// transport retains net/http's cancellation and request-body handling
// without changing the TLS fingerprint or redialing through another path.
transport := &http.Transport{
DisableKeepAlives: true,
DialTLSContext: func(context.Context, string, string) (net.Conn, error) {
return tlsConn, nil
},
}
resp, err = transport.RoundTrip(req)
transport.CloseIdleConnections()
default:
err = fmt.Errorf("utls: unsupported negotiated protocol %q", protocol)
}
if err != nil {
if errClose := closeConnection(); errClose != nil {
log.Debugf("utls: close connection after round trip failure: %v", errClose)
}
return nil, err
}
if resp == nil {
if errClose := closeConnection(); errClose != nil {
log.Debugf("utls: close connection after empty response: %v", errClose)
}
return nil, fmt.Errorf("utls: upstream returned an empty response")
}
if resp.Body == nil {
resp.Body = http.NoBody
}
resp.Body = &closeConnectionBody{
ReadCloser: resp.Body,
closeConnection: closeConnection,
}
return resp, nil
}
// claudeCodeSessionCacheCapacity bounds the per-transport TLS session cache for
// the Anthropic inference plane.
const claudeCodeSessionCacheCapacity = 32
// newClaudeCodeTLSConfig builds the uTLS config for one inference-plane dial.
//
// OmitEmptyPsk keeps the pre_shared_key extension silent until a session is
// cached, so an unresumed ClientHello stays byte-identical to the captured
// native handshake. PreferSkipResumptionOnNilExtension turns uTLS's HelloCustom
// "resume without the matching extension" panic into a skipped resumption.
func newClaudeCodeTLSConfig(host string, sessionCache tls.ClientSessionCache) *tls.Config {
return newClaudeCodeTLSConfigWithRoots(host, sessionCache, nil)
}
func newClaudeCodeTLSConfigWithRoots(host string, sessionCache tls.ClientSessionCache, roots *x509.CertPool) *tls.Config {
return &tls.Config{
ServerName: host,
RootCAs: roots,
ClientSessionCache: sessionCache,
OmitEmptyPsk: true,
PreferSkipResumptionOnNilExtension: true,
}
}
// claudeCodeTLSClientHelloSpec reproduces the deterministic Node/OpenSSL
// ClientHello emitted by Claude Code 2.1.220 on macOS arm64. Keep this spec in
// sync with a fresh native capture whenever the advertised Claude Code version
// changes.
func claudeCodeTLSClientHelloSpec() *tls.ClientHelloSpec {
return &tls.ClientHelloSpec{
CipherSuites: []uint16{
tls.TLS_AES_128_GCM_SHA256,
tls.TLS_AES_256_GCM_SHA384,
tls.TLS_CHACHA20_POLY1305_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
CompressionMethods: []uint8{0},
Extensions: []tls.TLSExtension{
&tls.SNIExtension{},
&tls.ExtendedMasterSecretExtension{},
&tls.RenegotiationInfoExtension{Renegotiation: tls.RenegotiateOnceAsClient},
&tls.SupportedCurvesExtension{Curves: []tls.CurveID{tls.X25519, tls.CurveP256, tls.CurveP384}},
&tls.SupportedPointsExtension{SupportedPoints: []byte{0}},
&tls.SessionTicketExtension{},
&tls.ALPNExtension{AlpnProtocols: []string{"http/1.1"}},
&tls.StatusRequestExtension{},
&tls.SignatureAlgorithmsExtension{SupportedSignatureAlgorithms: []tls.SignatureScheme{
tls.ECDSAWithP256AndSHA256,
tls.PSSWithSHA256,
tls.PKCS1WithSHA256,
tls.ECDSAWithP384AndSHA384,
tls.PSSWithSHA384,
tls.PKCS1WithSHA384,
tls.PSSWithSHA512,
tls.PKCS1WithSHA512,
tls.PKCS1WithSHA1,
}},
&tls.SCTExtension{},
&tls.KeyShareExtension{KeyShares: []tls.KeyShare{{Group: tls.X25519}}},
&tls.PSKKeyExchangeModesExtension{Modes: []uint8{tls.PskModeDHE}},
&tls.SupportedVersionsExtension{Versions: []uint16{tls.VersionTLS13, tls.VersionTLS12}},
&tls.UtlsPaddingExtension{GetPaddingLen: tls.BoringPaddingStyle},
// pre_shared_key MUST be the final extension (RFC 8446 4.2.11), after
// padding. It contributes zero bytes until a cached session exists.
&tls.UtlsPreSharedKeyExtension{},
},
}
}
const claudeCodeRoundTripperCacheCapacity = 64
var claudeCodeRoundTripperCache = internalcache.NewBoundedLRU[string, http.RoundTripper](
claudeCodeRoundTripperCacheCapacity,
func(_ string, roundTripper http.RoundTripper) {
if transport, ok := roundTripper.(interface{ CloseIdleConnections() }); ok {
transport.CloseIdleConnections()
}
},
)
var claudeCodeMessagesHeaderOrder = []string{
"Accept",
"Authorization",
"Content-Type",
"User-Agent",
"X-Claude-Code-Session-Id",
"X-Stainless-Arch",
"X-Stainless-Lang",
"X-Stainless-OS",
"X-Stainless-Package-Version",
"X-Stainless-Retry-Count",
"X-Stainless-Runtime",
"X-Stainless-Runtime-Version",
"X-Stainless-Timeout",
"anthropic-beta",
"anthropic-dangerous-direct-browser-access",
"anthropic-version",
"x-app",
"x-client-request-id",
"Connection",
"Host",
"Accept-Encoding",
"Content-Length",
}
var claudeCodeCountTokensHeaderOrder = []string{
"Accept",
"Authorization",
"Content-Type",
"User-Agent",
"X-Claude-Code-Session-Id",
"X-Stainless-Arch",
"X-Stainless-Lang",
"X-Stainless-OS",
"X-Stainless-Package-Version",
"X-Stainless-Retry-Count",
"X-Stainless-Runtime",
"X-Stainless-Runtime-Version",
"anthropic-beta",
"anthropic-dangerous-direct-browser-access",
"anthropic-version",
"x-app",
"x-client-request-id",
"Connection",
"Host",
"Accept-Encoding",
"Content-Length",
}
func claudeCodeRequestHeaderOrder(_, requestTarget string) []string {
if strings.HasPrefix(requestTarget, "/v1/messages/count_tokens") {
return claudeCodeCountTokensHeaderOrder
}
return claudeCodeMessagesHeaderOrder
}
func cachedClaudeCodeRoundTripper(proxyURL string) http.RoundTripper {
return claudeCodeRoundTripperCache.GetOrAdd(proxyURL, func() http.RoundTripper {
return newClaudeCodeRoundTripper(proxyURL)
})
}
func newClaudeCodeRoundTripper(proxyURL string) http.RoundTripper {
return newClaudeCodeRoundTripperForDialer(claudeCodeDialer(proxyURL))
}
// claudeCodeDialer resolves the dialer the Claude Code transport uses for one proxy
// setting. It is shared by the cached transport and the per-credential pooled one so
// both keep the same proxy semantics.
func claudeCodeDialer(proxyURL string) proxy.Dialer {
var dialer proxy.Dialer = proxy.Direct
if proxyURL != "" {
proxyDialer, mode, errBuild := proxyutil.BuildDialer(proxyURL)
if errBuild != nil {
log.Errorf("claude tls: failed to configure proxy dialer for %q: %v", proxyutil.Redact(proxyURL), errBuild)
} else if mode != proxyutil.ModeInherit && proxyDialer != nil {
dialer = proxyDialer
}
}
return dialer
}
// pooledClaudeCodeTransport returns the credential-scoped Claude Code uTLS transport
// when the credential opted into pooling, or nil so the caller keeps the historical
// shared per-proxy transport. Per credential pooling matters here because this
// transport is otherwise shared by every key that uses the same proxy, which mixes
// distinct identities on one connection and ignores the key's pool settings.
func pooledClaudeCodeTransport(proxyURL string, cfg *config.Config, auth *cliproxyauth.Auth) *http.Transport {
tr, pooled := pooledTransport(proxyPoolKindClaudeCode, proxyURL, cfg, auth, func() (*http.Transport, error) {
return newClaudeCodeRoundTripperForDialer(claudeCodeDialer(proxyURL)), nil
})
if !pooled {
return nil
}
return tr
}
// newClaudeCodeRoundTripperForDialer builds the Claude Code transport on top
// of an already-resolved dialer. It is split out from
// newClaudeCodeRoundTripper so tests can inject in-memory connections.
func newClaudeCodeRoundTripperForDialer(dialer proxy.Dialer) *http.Transport {
return newClaudeCodeRoundTripperForDialerAndRoots(dialer, nil)
}
func newClaudeCodeRoundTripperForDialerAndRoots(dialer proxy.Dialer, roots *x509.CertPool) *http.Transport {
// The cache is scoped to this round tripper, which is already keyed by proxy,
// so resumption never crosses proxy boundaries.
sessionCache := tls.NewLRUClientSessionCache(claudeCodeSessionCacheCapacity)
// Default to short-lived connections (MaxIdleConnsPerHost < 0) so OAuth and
// unpooled API keys never park idle connections that intermediate NATs or
// transparent proxies may silently drop, while keeping DisableKeepAlives false
// so net/http does not overwrite the native Connection: keep-alive wire header.
// Credentials that explicitly opt into connection-pool override MaxIdleConnsPerHost
// and IdleConnTimeout in pooledTransport.
transport := &http.Transport{
ForceAttemptHTTP2: false,
MaxIdleConnsPerHost: -1,
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
var (
conn net.Conn
err error
)
if contextDialer, ok := dialer.(proxy.ContextDialer); ok {
conn, err = contextDialer.DialContext(ctx, network, addr)
} else {
conn, err = dialer.Dial(network, addr)
}
if err != nil {
return nil, fmt.Errorf("claude tls: dial upstream: %w", err)
}
conn = stampProxyKeepAlive(conn)
host, _, errSplit := net.SplitHostPort(addr)
if errSplit != nil {
if errClose := conn.Close(); errClose != nil {
log.Debugf("claude tls: close failed connection: %v", errClose)
}
return nil, fmt.Errorf("claude tls: split upstream address: %w", errSplit)
}
tlsConn := tls.UClient(conn, newClaudeCodeTLSConfigWithRoots(host, sessionCache, roots), tls.HelloCustom)
if errPreset := tlsConn.ApplyPreset(claudeCodeTLSClientHelloSpec()); errPreset != nil {
if errClose := tlsConn.Close(); errClose != nil {
log.Debugf("claude tls: close connection after preset failure: %v", errClose)
}
return nil, fmt.Errorf("claude tls: apply Claude Code ClientHello: %w", errPreset)
}
handshakeCtx, cancelHandshake := handshakeContext(ctx)
defer cancelHandshake()
if errHandshake := tlsConn.HandshakeContext(handshakeCtx); errHandshake != nil {
errHandshake = classifyHandshakeError(ctx, handshakeCtx, errHandshake)
if errClose := tlsConn.Close(); errClose != nil {
log.Debugf("claude tls: close connection after handshake failure: %v", errClose)
}
return nil, fmt.Errorf("claude tls: handshake upstream: %w", errHandshake)
}
return httpwire.NewOrderedRequestConn(tlsConn, claudeCodeRequestHeaderOrder), nil
},
}
return transport
}
// fallbackRoundTripper uses provider-specific TLS fingerprints for protected
// HTTPS hosts and falls back to the standard transport for all other requests.
//
// Each pooled branch may be resolved lazily on first use: a single request only
// ever takes one branch, and building both pools up front would spend two cache
// slots per key (evicting live standard pools with unused Anthropic pools).
type fallbackRoundTripper struct {
mu sync.Mutex
anthropic http.RoundTripper
anthropicFn func() http.RoundTripper
chrome http.RoundTripper
fallback http.RoundTripper
fallbackFn func() http.RoundTripper
}
func (f *fallbackRoundTripper) anthropicRoundTripper() http.RoundTripper {
f.mu.Lock()
defer f.mu.Unlock()
if f.anthropicFn != nil {
if rt := f.anthropicFn(); rt != nil {
f.anthropic = rt
}
f.anthropicFn = nil
}
return f.anthropic
}
func (f *fallbackRoundTripper) fallbackRoundTripper() http.RoundTripper {
f.mu.Lock()
defer f.mu.Unlock()
if f.fallbackFn != nil {
if rt := f.fallbackFn(); rt != nil {
f.fallback = rt
}
f.fallbackFn = nil
}
return f.fallback
}
func (f *fallbackRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if IsAnthropicUpstreamURL(req.URL) {
return f.anthropicRoundTripper().RoundTrip(req)
}
if req.URL.Scheme == "https" && strings.EqualFold(req.URL.Hostname(), "chatgpt.com") {
return f.chrome.RoundTrip(req)
}
return f.fallbackRoundTripper().RoundTrip(req)
}
// NewUtlsHTTPClient creates an HTTP client using provider-specific TLS
// fingerprints for protected hosts. It uses Claude Code's Node/OpenSSL profile
// for Anthropic and a Chrome profile for ChatGPT, with a standard-transport
// fallback for other hosts.
func NewUtlsHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) (clientOut *http.Client) {
// Carry the opt-in TLS establishment bound (requests
// tls-handshake-timeout-seconds) on a request-scoped wrapper. This
// client is shared, so the bound intentionally reaches both the Claude
// Code Anthropic dial path and the Chrome ChatGPT/Codex dial path; the
// standard fallback transport ignores the context value. The uTLS dial
// paths read it per dial, so cached per-proxy transports need no rebuild
// when the value changes. A disabled bound (0) leaves requests untouched.
var handshakeTimeout time.Duration
if cfg != nil {
handshakeTimeout = cfg.TLSHandshakeTimeout()
}
defer func() {
clientOut = httptransport.Decorate(ctx, clientOut)
if clientOut != nil && handshakeTimeout > 0 {
clientOut.Transport = &tlsHandshakeTimeoutRoundTripper{base: clientOut.Transport, timeout: handshakeTimeout}
}
}()
proxyURL := effectiveProxyURL(ctx, cfg, auth)
var ctxRoundTripper http.RoundTripper
if ctx != nil {
ctxRoundTripper, _ = ctx.Value("cliproxy.roundtripper").(http.RoundTripper)
}
var chromeRT http.RoundTripper = newUtlsRoundTripper(proxyURL)
var anthropicRT http.RoundTripper = cachedClaudeCodeRoundTripper(proxyURL)
var standardTransport http.RoundTripper = http.DefaultTransport
var anthropicFn, fallbackFn func() http.RoundTripper
if proxyURL != "" {
fallbackFn = func() http.RoundTripper {
if transport := pooledProxyTransport(proxyURL, cfg, auth); transport != nil {
return transport
}
return standardTransport
}
// The Anthropic path has its own TLS fingerprint, so it needs its own pool kind.
anthropicFn = func() http.RoundTripper {
if transport := pooledClaudeCodeTransport(proxyURL, cfg, auth); transport != nil {
return transport
}
return anthropicRT
}
} else if ctxRoundTripper != nil {
chromeRT = ctxRoundTripper
anthropicRT = ctxRoundTripper
standardTransport = ctxRoundTripper
} else {
// A credential opt-in applies here too; an injected RoundTripper keeps priority.
fallbackFn = func() http.RoundTripper {
if transport := pooledProxyTransport("", cfg, auth); transport != nil {
return transport
}
return standardTransport
}
anthropicFn = func() http.RoundTripper {
if transport := pooledClaudeCodeTransport("", cfg, auth); transport != nil {
return transport
}
return anthropicRT
}
}
client := &http.Client{
Transport: &fallbackRoundTripper{
anthropic: anthropicRT,
anthropicFn: anthropicFn,
chrome: chromeRT,
fallback: standardTransport,
fallbackFn: fallbackFn,
},
}
if timeout > 0 {
client.Timeout = timeout
}
return client
}