client

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package client provides an HTTP/2 client with TLS and HTTP/2 fingerprinting. It reuses the same frame, hpack, flowcontrol, and stream packages as the server.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnClosed  = errors.New("client: connection closed")
	ErrGoAway      = errors.New("client: server sent GOAWAY")
	ErrStreamReset = errors.New("client: stream reset by server")
	ErrNotH2       = errors.New("client: server did not negotiate h2")
)

Errors returned by ClientConn.

View Source
var ErrPoolClosed = errors.New("client: connection pool closed")

ErrPoolClosed is returned when an operation is attempted on a closed pool.

View Source
var ErrProxyConnect = errors.New("client: proxy connection failed")

ErrProxyConnect is returned when a proxy CONNECT or SOCKS5 connection fails.

View Source
var ErrTooManyRedirects = errors.New("client: too many redirects")

ErrTooManyRedirects is returned when the redirect limit is exceeded.

Functions

This section is empty.

Types

type Client

type Client struct {
	UserAgent       string
	FollowRedirects bool
	MaxRedirects    int
	CookieJar       *CookieJar
	RetryConfig     *RetryConfig
	RequestTimeout  time.Duration
	// contains filtered or unexported fields
}

Client is the high-level HTTP/2 client with browser fingerprinting, cookie jar, redirect following, retry with backoff, and proxy support.

func NewChromeClient

func NewChromeClient() *Client

NewChromeClient creates a client mimicking Chrome's TLS and HTTP/2 fingerprint.

func NewFirefoxClient

func NewFirefoxClient() *Client

NewFirefoxClient creates a client mimicking Firefox's TLS and HTTP/2 fingerprint.

func NewRandomClient

func NewRandomClient() *Client

NewRandomClient creates a client with a randomized TLS fingerprint.

func NewSafariClient

func NewSafariClient() *Client

NewSafariClient creates a client mimicking Safari's TLS fingerprint (uses Chrome H2 profile since Safari doesn't have a distinct one).

func (*Client) Close

func (c *Client) Close() error

Close shuts down the client and its connection pool.

func (*Client) Do

func (c *Client) Do(req *Request) (*Response, error)

Do sends a Request and returns a Response. It handles cookie injection, redirect following, and retry.

func (*Client) DoBatch

func (c *Client) DoBatch(reqs []*Request) ([]*Response, error)

DoBatch sends multiple requests concurrently and returns responses in order. Returns on the first error encountered.

func (*Client) Get

func (c *Client) Get(rawURL string) (*Response, error)

Get sends a GET request to the given URL.

func (*Client) Head

func (c *Client) Head(rawURL string) (*Response, error)

Head sends a HEAD request to the given URL.

func (*Client) Post

func (c *Client) Post(rawURL, contentType string, body []byte) (*Response, error)

Post sends a POST request with the given content type and body.

func (*Client) SetInsecureSkipVerify

func (c *Client) SetInsecureSkipVerify(v bool) *Client

SetInsecureSkipVerify disables TLS certificate verification. Testing only.

func (*Client) SetProxy

func (c *Client) SetProxy(proxyURL string) error

SetProxy configures an HTTP CONNECT proxy.

func (*Client) SetProxyWithAuth

func (c *Client) SetProxyWithAuth(proxyURL, user, pass string) error

SetProxyWithAuth configures an HTTP CONNECT proxy with credentials.

func (*Client) SetSOCKS5Proxy

func (c *Client) SetSOCKS5Proxy(addr, user, pass string) error

SetSOCKS5Proxy configures a SOCKS5 proxy.

type ClientConn

type ClientConn struct {
	// contains filtered or unexported fields
}

ClientConn is a single HTTP/2 client connection.

func Dial

func Dial(addr string, tlsDialer *blazetls.TLSDialer, profile *h2fingerprint.H2Profile) (*ClientConn, error)

Dial establishes an HTTP/2 connection to addr (host:port) using the given TLS dialer and HTTP/2 fingerprint profile.

func (*ClientConn) ActiveStreams

func (cc *ClientConn) ActiveStreams() int

ActiveStreams returns the number of active streams.

func (*ClientConn) Close

func (cc *ClientConn) Close() error

Close gracefully closes the connection.

func (*ClientConn) GoingAway

func (cc *ClientConn) GoingAway() bool

GoingAway reports whether the server has sent GOAWAY.

func (*ClientConn) IsClosed

func (cc *ClientConn) IsClosed() bool

IsClosed reports whether the connection is closed.

func (*ClientConn) PeerSettings

func (cc *ClientConn) PeerSettings() peerSettings

PeerSettings returns the server's current settings.

func (*ClientConn) Ping

func (cc *ClientConn) Ping()

Ping sends a PING frame and does not wait for the response.

type ConnPool

type ConnPool struct {
	// contains filtered or unexported fields
}

ConnPool manages a pool of HTTP/2 connections with transparent multiplexing. It auto-scales connections when existing ones are saturated, detects dead connections, and handles GOAWAY gracefully.

func NewConnPool

func NewConnPool(dialer *blazetls.TLSDialer, profile *h2fingerprint.H2Profile, opts ...PoolOption) *ConnPool

NewConnPool creates a new connection pool.

func (*ConnPool) Close

func (p *ConnPool) Close() error

Close shuts down the pool and all connections.

func (*ConnPool) ConnCount

func (p *ConnPool) ConnCount(addr string) int

ConnCount returns the number of connections to addr. For testing.

func (*ConnPool) GetConn

func (p *ConnPool) GetConn(addr string) (*ClientConn, error)

GetConn returns an available connection to addr, creating one if needed.

type CookieJar

type CookieJar struct {
	// contains filtered or unexported fields
}

CookieJar is a thread-safe cookie jar with domain and path matching.

func NewCookieJar

func NewCookieJar() *CookieJar

NewCookieJar creates a new empty CookieJar.

func (*CookieJar) Cookies

func (j *CookieJar) Cookies(u *url.URL) []*http.Cookie

Cookies returns the cookies that should be sent with a request to the given URL.

func (*CookieJar) SetCookies

func (j *CookieJar) SetCookies(u *url.URL, cookies []*http.Cookie)

SetCookies stores cookies for the given URL.

type Header struct {
	Name  string
	Value string
}

Header is a name-value pair for HTTP headers.

type PoolOption

type PoolOption func(*ConnPool)

PoolOption configures a ConnPool.

func WithDialTimeout

func WithDialTimeout(d time.Duration) PoolOption

WithDialTimeout sets the timeout for establishing new connections.

func WithHealthCheckInterval

func WithHealthCheckInterval(d time.Duration) PoolOption

WithHealthCheckInterval sets the interval between health checks. Set to 0 to disable health checks.

func WithMaxConnsPerHost

func WithMaxConnsPerHost(n int) PoolOption

WithMaxConnsPerHost sets the maximum number of connections per host.

func WithMaxIdlePerHost

func WithMaxIdlePerHost(n int) PoolOption

WithMaxIdlePerHost sets the maximum number of idle connections to keep per host.

func WithWaitTimeout

func WithWaitTimeout(d time.Duration) PoolOption

WithWaitTimeout sets the maximum time to wait for an available connection when all connections are saturated and the max connections limit is reached.

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request is a high-level HTTP request builder.

func NewRequest

func NewRequest(method, rawURL string) *Request

NewRequest creates a new Request with the given method and URL.

func (*Request) SetBody

func (r *Request) SetBody(body []byte) *Request

SetBody sets the request body.

func (*Request) SetCookie

func (r *Request) SetCookie(name, value string) *Request

SetCookie adds a cookie to the request.

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

SetHeader sets a header (upsert by lowercased key).

func (*Request) SetHeaderOrder

func (r *Request) SetHeaderOrder(order []string) *Request

SetHeaderOrder reorders the headers slice to match the given order. Headers not in the order list are appended after.

func (*Request) SetHeaders

func (r *Request) SetHeaders(headers map[string]string) *Request

SetHeaders sets multiple headers at once (upsert each).

type Response

type Response struct {
	StatusCode int
	Headers    []Header
	Body       []byte
	URL        string
}

Response is a high-level HTTP response.

func (*Response) Cookies

func (r *Response) Cookies() []*http.Cookie

Cookies parses Set-Cookie headers from the response using net/http.

func (*Response) Header

func (r *Response) Header(key string) string

Header returns the first value for the given header name (case-insensitive). Returns empty string if not found.

func (*Response) Release

func (r *Response) Release()

Release nils out the body to allow GC.

type RetryConfig

type RetryConfig struct {
	MaxRetries   int
	InitialDelay time.Duration
	MaxDelay     time.Duration
	Multiplier   float64
	Jitter       float64
	RetryOn      []int // status codes to retry on
	RetryOnError bool  // retry on connection errors
}

RetryConfig configures retry behavior with exponential backoff.

func DefaultRetryConfig

func DefaultRetryConfig() *RetryConfig

DefaultRetryConfig returns sensible retry defaults.

Directories

Path Synopsis
Package h2fingerprint defines HTTP/2 connection fingerprint profiles.
Package h2fingerprint defines HTTP/2 connection fingerprint profiles.
Package tls provides TLS fingerprinting for the BlazeHTTP client.
Package tls provides TLS fingerprinting for the BlazeHTTP client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL