dial

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dial is the single path from an address to a connected socket, directly or through an HTTP, HTTPS, SOCKS5 or SOCKS4 proxy (the TLS to an https proxy and its HTTP/2 streams are the engine's, through ProxyTLS and TunnelH2), and to a UDP socket for QUIC, directly, through a SOCKS5 UDP association, or as a CONNECT-UDP stream of the engine's HTTP/3 connection to a proxy (ConnectUDP). Every step honours the context and its deadline (decision 0010); no other package opens connections.

Index

Constants

View Source
const (
	SchemeHTTP    = "http"
	SchemeHTTPS   = "https"
	SchemeSOCKS5  = "socks5"
	SchemeSOCKS5H = "socks5h"
	// SchemeSOCKS4 resolves the host locally (Resolver) and sends its
	// IPv4 address; SchemeSOCKS4A sends the host name (SOCKS4A).
	SchemeSOCKS4  = "socks4"
	SchemeSOCKS4A = "socks4a"
)

Supported proxy URL schemes. An https proxy is an HTTP proxy reached over TLS (ProxyTLS).

View Source
const DefaultHTTP3ReadBufferSize = 1 << 20

DefaultHTTP3ReadBufferSize is the receive buffer a QUIC socket asks for by default: Chrome's (kQuicSocketReceiveBufferSize, net/quic/ quic_context.h, set on every QUIC socket it opens).

Variables

View Source
var ErrNoConnectUDP = errors.New("the proxy offers no CONNECT-UDP")

ErrNoConnectUDP is a proxy whose HTTP/3 connection cannot carry CONNECT-UDP (ConnectUDPStream).

Functions

func Credentials

func Credentials(p *url.URL) string

Credentials returns p's Basic Proxy-Authorization value, or "".

Types

type ConnectStream added in v0.5.0

type ConnectStream func(ctx context.Context, header []profile.Header) (proxy.Answer, net.Conn, error)

ConnectStream sends one CONNECT as a new stream of an HTTP/2 connection to a proxy, header its fields after the pseudo-header fields, and returns the proxy's answer and, after a 2xx, the tunnel.

type ConnectUDPStream added in v0.5.0

type ConnectUDPStream func(ctx context.Context, header []profile.Header) (proxy.Answer, net.PacketConn, error)

ConnectUDPStream sends one CONNECT-UDP request (RFC 9298) as a new stream of an HTTP/3 connection to a proxy, header its fields after the pseudo-header fields, and returns the proxy's answer and, after a 2xx, the stream as a socket. A connection that cannot carry CONNECT-UDP fails with an error wrapping ErrNoConnectUDP before sending anything.

type Dialer

type Dialer struct {
	// Timeout bounds the connect (through a proxy: to the proxy, and the
	// proxy's handshake), on top of the context's deadline.
	Timeout time.Duration
	// DialContext, when set, opens the connection (to the proxy, when
	// there is one) instead of net.Dialer; Timeout still bounds it.
	DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
	// Resolver looks host names up for net.Dialer, and for a socks4
	// proxy's target; nil uses net.DefaultResolver. DialContext does its
	// own lookups; with it, a socks4 target's host is looked up only
	// through a Resolver with its own Dial (else refused, as it would
	// bypass DialContext).
	Resolver *net.Resolver
	// Proxy is how the profile's browser talks to proxies; nil (not
	// measured) makes proxies an error.
	Proxy *profile.Proxy
	// UserAgent is the profile's, which CONNECT requests carry.
	UserAgent string
	// BrowserAuth gives an HTTP proxy the URL's credentials as Proxy's
	// ConnectCredentials and ForwardCredentials say, after its 407.
	// Otherwise they go from the first request, placed as Proxy places
	// them unasked (ConnectAuth, ForwardAuth): only the proxy sees the
	// difference (decision 0039).
	BrowserAuth bool
	// HTTP3ReadBufferSize is the receive buffer a QUIC socket asks for
	// (SetReadBuffer; the OS may grant less): zero asks for
	// DefaultHTTP3ReadBufferSize, negative leaves the OS's default (see
	// setReadBuffer).
	HTTP3ReadBufferSize int
	// OnConnect, when set, is told how long each TCP connect took: from
	// its connect() to the connection being up, so without the lookup
	// and, through a proxy, without the tunnel's setup; with DialContext,
	// DialContext's whole call. The rtt client hint estimates from it.
	OnConnect func(time.Duration)
	// LocalAddrs, when set, are the local addresses the Dialer's own
	// sockets bind to, at most one per family: TCP connects (unless
	// DialContext opens them), QUIC's UDP sockets and SOCKS5 UDP relays.
	// A remote address of a family with none is not used.
	LocalAddrs []netip.Addr
	// ProxyTLS runs the TLS handshake with an https proxy p on conn, its
	// new connection, and returns the TLS connection: the engine's, with
	// the profile's hello. Required for https proxies; on error conn is
	// closed.
	ProxyTLS func(ctx context.Context, conn net.Conn, p *url.URL) (net.Conn, error)
	// contains filtered or unexported fields
}

Dialer opens TCP connections, directly or through a proxy, and UDP sockets for QUIC. It is the one place connections start.

func (*Dialer) Challenged

func (d *Dialer) Challenged(p *url.URL, h http.Header) bool

Challenged records that p asked for credentials (a 407 with header h) on a plain-HTTP request, and reports whether they can be given: p has them and asks for Basic.

func (*Dialer) CheckProxy

func (d *Dialer) CheckProxy(p *url.URL) error

CheckProxy reports whether p is a proxy URL the dialer can use. The engine checks that the profile describes the proxy protocol.

func (*Dialer) ConnectOn added in v0.5.0

func (d *Dialer) ConnectOn(ctx context.Context, conn net.Conn, p *url.URL, addr string) (net.Conn, error)

ConnectOn opens a CONNECT tunnel to addr over HTTP/1.1 on conn, a new connection to the http or https proxy p, closing it on failure. With credentials it sends them from the first CONNECT, or with BrowserAuth as the profile says (after_challenge or on_challenge): a 407 is then answered once, on a new connection if the proxy closed this one. A failure is a *ProxyError.

func (*Dialer) ConnectUDP added in v0.5.0

func (d *Dialer) ConnectUDP(ctx context.Context, p *url.URL, open ConnectUDPStream) (net.PacketConn, error)

ConnectUDP opens a CONNECT-UDP stream through the proxy p, an HTTP/3 one, with open (a request on its connection) and the credential flow of TunnelH2, the 407 answered on a new stream of the same connection. A proxy that cannot carry CONNECT-UDP, or refuses it whatever the target (refusesConnectUDP), is remembered (UDPRefused): its requests stay on TCP. A failure is a *ProxyError wrapping open's error.

func (*Dialer) Dial

func (d *Dialer) Dial(ctx context.Context, addr string) (net.Conn, error)

Dial connects to addr ("host:port"). It honours ctx's cancellation and deadline.

func (*Dialer) DialProxy

func (d *Dialer) DialProxy(ctx context.Context, p *url.URL) (net.Conn, error)

DialProxy connects to the proxy p itself: for plain-HTTP requests sent to an http proxy whole, or an https proxy's connection, TLS done (ProxyTLS), for the engine to send CONNECTs on over the protocol the proxy picked. Timeout bounds both. A failure is a *ProxyError.

func (*Dialer) DialVia

func (d *Dialer) DialVia(ctx context.Context, p *url.URL, addr string) (net.Conn, error)

DialVia connects to addr through the proxy p, an http, SOCKS5 or SOCKS4 one: an HTTP proxy's CONNECT tunnel or a SOCKS5 or SOCKS4 connection, as the profile's browser asks for them. An https proxy's tunnels are the engine's (DialProxy, then ConnectOn or TunnelH2 by the protocol it picked). Through socks4, addr's host is resolved first, as the browser does, to an IPv4 address. A failure on the way to or at the proxy is a *ProxyError; a SOCKS4 target its request cannot carry, or that lookup's failure, is not: no proxy was asked, and none would do better.

func (*Dialer) Forget added in v0.2.0

func (d *Dialer) Forget(via string)

Forget drops what d has learned of the proxy whose URL is via (url.URL.String): that it asked for credentials, or refused a UDP association or CONNECT-UDP. It is then a proxy d has never used.

func (*Dialer) ForwardAuthorization

func (d *Dialer) ForwardAuthorization(p *url.URL) string

ForwardAuthorization returns the Proxy-Authorization a plain-HTTP request to p carries unasked, or "": p's credentials from the first request, or with BrowserAuth once p has asked or where the profile always sends them.

func (*Dialer) ListenUDP

func (d *Dialer) ListenUDP(ctx context.Context, host string, port uint16) (net.PacketConn, netip.AddrPort, error)

ListenUDP resolves host with Resolver and opens a UDP socket for a QUIC connection to its first address at port (with LocalAddrs, its first of a family they give, the socket bound to that family's), returned with the socket. It honours ctx, bounded by Timeout. The socket asks for the receive buffer HTTP3ReadBufferSize says and keeps what the OS grants.

func (*Dialer) ListenUDPVia

func (d *Dialer) ListenUDPVia(ctx context.Context, p *url.URL, host string, port uint16) (net.PacketConn, netip.AddrPort, error)

ListenUDPVia opens a SOCKS5 UDP association through p (a socks5 or socks5h proxy) for a QUIC connection to host at port, and returns it with the peer address its datagrams are reported from: host's address when host is an IP address, else the unspecified IPv4 address (the proxy resolves the name, as for its TCP connections, and host never goes to Resolver). The association authenticates as DialVia does and lives as long as its control connection (proxy.Association). A failure is a *ProxyError; a refusal of UDP ASSOCIATE is remembered (UDPRefused). It honours ctx, bounded by Timeout; the socket asks for the receive buffer HTTP3ReadBufferSize says, as ListenUDP's does.

func (*Dialer) TunnelH2 added in v0.5.0

func (d *Dialer) TunnelH2(ctx context.Context, p *url.URL, addr string, open ConnectStream) (net.Conn, error)

TunnelH2 opens a tunnel to addr through the https proxy p as a stream of its HTTP/2 connection (open), with proxy.https's header lists and the credential flow of ConnectOn, the 407 answered on a new stream of the same connection. A failure is a *ProxyError wrapping open's error.

func (*Dialer) UDPRefused

func (d *Dialer) UDPRefused(p *url.URL) bool

UDPRefused reports whether p has refused a UDP association or CONNECT-UDP: its requests stay on TCP.

type ProxyError

type ProxyError struct {
	// Proxy is the proxy's URL, credentials included.
	Proxy *url.URL
	// StatusCode and Header are the proxy's answer, if any.
	StatusCode int
	Header     http.Header
	Err        error
}

ProxyError is a failure to reach a proxy or open a tunnel through it (connect, non-2xx CONNECT, SOCKS5 refusal, deadline). The root package turns it into mimicry.ProxyError.

func (*ProxyError) Error

func (e *ProxyError) Error() string

func (*ProxyError) Unwrap

func (e *ProxyError) Unwrap() error

Jump to

Keyboard shortcuts

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