Documentation
¶
Overview ¶
Package native is the pure-Go engine: the one place that assembles the request path (pool, dial, handshake, then h1 or h2 by ALPN, or h3 over QUIC), routing each request to its pool key and retrying on another connection a request nothing of which was sent.
HTTP/2 coalescing (when Config.Coalesce), HTTP/3 discovery and fallback, and WebSocket transports follow the profile's connections, http3 and websocket sections; see docs/concepts/http3.md, docs/concepts/websockets.md and decisions 0023, 0028 and 0037. Plan reuses RoundTrip's own steps and changes nothing (decision 0034).
Index ¶
- Constants
- func ReadTickets(r io.Reader, s *handshake.Store, now time.Time) error
- func WriteTickets(w io.Writer, s *handshake.Store, now time.Time) error
- type Config
- type Engine
- func (e *Engine) CloseIdleConnections()
- func (e *Engine) Handshake(ctx context.Context, rawURL string) (*tls.ConnectionState, error)
- func (e *Engine) OpenWebSocket(req *http.Request, key string) (*WebSocket, error)
- func (e *Engine) Plan(req *http.Request) (*Plan, error)
- func (e *Engine) RoundTrip(req *http.Request) (resp *http.Response, err error)
- type Field
- type Head
- type Plan
- type WebSocket
Constants ¶
const ( HTTP1 = "HTTP/1.1" HTTP2 = "HTTP/2.0" HTTP3 = "HTTP/3.0" )
The protocols, as a Plan names them: as responses' Proto does.
Variables ¶
This section is empty.
Functions ¶
func ReadTickets ¶ added in v0.5.0
ReadTickets adds to s the tickets of a JSON array WriteTickets wrote that are still usable at now, replacing those under the same key. A malformed entry, entries of two browsers, or entries of another browser than s's fail it before anything is added.
Types ¶
type Config ¶
type Config struct {
// Profile is the browser to imitate. New takes ownership: it must be
// valid (profile.Parse or Validate) and never changed afterwards.
Profile *profile.Spec
RootCAs *x509.CertPool
// DialTimeout bounds the connect (through a proxy: to the proxy, and the
// proxy's handshake) and, separately, the TLS handshake, on top of the
// request context's deadline. Zero means 30 seconds.
DialTimeout time.Duration
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
// LocalAddrs are the local addresses the engine's sockets bind to
// (dial.Dialer.LocalAddrs); checked by the caller.
LocalAddrs []netip.Addr
// Resolver looks host names up: for dials without DialContext, and to
// find another host's HTTP/2 connection a request may take
// (Coalesce, profile.CoalesceOnLookup). Nil uses
// net.DefaultResolver. A profile with tls.realEch also looks up HTTPS
// records through it (its Dial, else the system's nameservers);
// with DialContext set and no Dial, none (startECHLookup).
Resolver *net.Resolver
// Proxy picks each request's proxy, as http.Transport's does; nil, or
// a nil URL, connects directly.
Proxy func(*http.Request) (*url.URL, error)
// DisableHTTP3 keeps every request on TCP, whatever the profile's
// http3 section and servers' Alt-Svc say.
DisableHTTP3 bool
// AltSvc keeps the origins' HTTP/3 alternatives, shared with every
// engine given the same cache; nil gives the engine one of its own.
AltSvc *altsvc.Cache
// Tickets keeps the TLS and QUIC session tickets, shared with every
// engine given the same store; nil gives the engine one of its own. A
// store belongs to one browser (handshake.Store.Bind): another's is an
// error. A swept proxy's tickets leave it too (forgetProxy).
Tickets *handshake.Store
// BrowserFallback keeps the browser's timing where QUIC fails: requests
// wait on an unanswered 0-RTT connection until it gives up
// (ZeroRTTHolds, Fallback.Hold), and on a failed attempt until the race
// delay. Otherwise they go on TCP once the race delay (or, without
// one, the first probe timeout) has passed with no answer, and at once
// when the attempt failed.
BrowserFallback bool
// BrowserDialDelay holds a QUIC dial a request waits for back as the
// browser does (http3.discovery.dialDelayMs, resumedDialDelayMs).
// Otherwise it starts at once (decision 0050).
BrowserDialDelay bool
// Coalesce shares HTTP/2 connections between hosts as the profile says
// (connections.coalesce). Otherwise each host has its own connection:
// no lookup for another host's, and no connection opened and declined.
// The root package decides the default (decision 0037).
Coalesce bool
// MaxConnsPerHost is the most HTTP/1.1 connections open at once to one
// host; zero means the profile's connections.http1PerHost.
MaxConnsPerHost int
// IdleConnTimeout is how long an idle connection is kept (HTTP/1.1,
// HTTP/2, HTTP/3); zero means 90 seconds, negative never closes one
// for its idleness. Unused proxies' connections close early either
// way (proxyUse).
IdleConnTimeout time.Duration
// MaxIdleProxies, when set, is how many of the most recently used
// proxies keep their idle connections and their TLS sessions, 407s
// and HTTP/3 state (forgetProxy), the others losing both as soon as
// they fall out (proxyUse). Zero means keepProxies for connections
// and rememberProxies for state, or any proxy used in the last
// proxyUnused. Negative is an error.
MaxIdleProxies int
// HTTP3OverSOCKS5 lets requests through a socks5 or socks5h proxy use
// HTTP/3, over a SOCKS5 UDP association with the proxy (ListenUDPVia),
// as they would without the proxy. Otherwise, and through any other
// proxy, they stay on TCP (browsers never use HTTP/3 through a proxy).
HTTP3OverSOCKS5 bool
// ConnectUDPProxy, when set, gives the CONNECT-UDP proxy (an https URL
// reached over HTTP/3, RFC 9298) through which requests through an
// http or https proxy use HTTP/3 (listenConnectUDP); a nil URL keeps
// that proxy's requests on TCP, as without it.
ConnectUDPProxy func(proxy *url.URL) (*url.URL, error)
// HTTP3ReadBufferSize is the receive buffer QUIC sockets ask for: zero
// asks for dial.DefaultHTTP3ReadBufferSize (Chrome's), negative keeps the
// OS's default.
HTTP3ReadBufferSize int
// HTTP3ACKEvery, when set, is how many ack-eliciting packets a QUIC
// connection acknowledges at once after the profile's warm-up, in place
// of the profile's quic.ack.every; at least that, at most maxACKEvery
// (decision 0043).
HTTP3ACKEvery int
// BrowserProxyAuth gives an HTTP proxy the URL's credentials as the
// profile's browser does: only once the proxy has answered 407
// (proxy.connectCredentials, forwardCredentials). Otherwise they go
// from the first CONNECT or forwarded request, placed as the
// browser places them unasked (decision 0039).
BrowserProxyAuth bool
// VerifyConnection, when set, checks a verified connection for host on
// every TLS and QUIC handshake (resumed ones included) and before a request
// coalesces onto another host's HTTP/2 connection. An error fails the
// handshake or keeps the request off that connection.
VerifyConnection func(host string, cs tls.ConnectionState) error
// KeyLogWriter, when set, receives every TLS and QUIC handshake's
// secrets (handshake.Config.KeyLogWriter).
KeyLogWriter io.Writer
// ClientCertificate, when set, answers a server's request for a client
// certificate on a connection to host, TLS and QUIC alike
// (handshake.Config.ClientCertificate).
ClientCertificate func(host string, req *tls.CertificateRequestInfo) (*tls.Certificate, error)
// OnConnect, when set, is told each TCP connect's duration
// (dial.Dialer.OnConnect), which the rtt client hint estimates from.
OnConnect func(time.Duration)
// Traffic, when set, counts the bytes of every socket the engine
// opens, by protocol and peer (decision 0070).
Traffic *traffic.Meter
}
Config configures the engine.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the pure-Go engine: pooled connections, one dial path, uTLS handshakes and HTTP/1.1, HTTP/2 or HTTP/3 as the profile's browser chooses. It is safe for concurrent use.
func New ¶
New builds an engine for cfg.Profile, which it takes ownership of: the profile must not change afterwards, and engines given the same profile share what they compile from it.
func (*Engine) CloseIdleConnections ¶
func (e *Engine) CloseIdleConnections()
CloseIdleConnections closes idle HTTP/1.1 connections, and HTTP/2 and HTTP/3 connections with no requests in flight; then the connections to https proxies left with no tunnel.
func (*Engine) Handshake ¶
Handshake connects to rawURL's host as a request would, with the profile's TLS hello, and returns the verified TLS state. It sends no request and closes the connection.
func (*Engine) OpenWebSocket ¶
OpenWebSocket sends req, a WebSocket handshake with its headers resolved, as the profile's browser opens a socket: an extended CONNECT stream on the pool's HTTP/2 connection when the server allows it, else a new unpooled HTTP/1.1 connection. key is the Sec-WebSocket-Key (HTTP/1.1 only). See docs/concepts/websockets.md.
type Head ¶
Head is a request's head over one protocol: the HTTP/1.1 request line (Line; empty otherwise) and header lines in order. Err, when set, is why the request cannot go over the protocol; Line and Fields are then empty.
type Plan ¶
type Plan struct {
// Protocols are the protocols the request may take now, likeliest
// first; several when the choice is made while connecting.
Protocols []string
HTTP2 Head
HTTP1 Head
// HTTP3 is nil when the engine speaks no HTTP/3 (the profile has
// none, or Config.DisableHTTP3).
HTTP3 *Head
}
Plan is what the engine would write for a request.
type WebSocket ¶
type WebSocket struct {
// Response is the server's answer to the handshake. Its body is
// readable only when the handshake failed (not 101, or not 2xx over
// HTTP/2); closing it ends the transport.
Response *http.Response
// Conn carries the socket once the handshake succeeded, and Reader
// reads what the server sent on it (which may have come with the
// handshake's response).
Conn ws.Transport
Reader io.Reader
// H2 is set for an extended CONNECT stream (RFC 8441), whose DATA
// frames carry at most MaxData bytes.
H2 bool
MaxData int
}
WebSocket is the transport a WebSocket handshake opened, and the server's answer.