Documentation
¶
Overview ¶
Package netproxy routes outbound connections through an HTTP CONNECT proxy.
It exists for hosts where the only way out is an egress proxy that allows "CONNECT host:443" (hosted agent sandboxes, locked-down corporate VMs): outbound UDP is blocked, direct TCP is killed, and local DNS for the target hostnames may be poisoned. Everything here therefore keeps the target as a HOSTNAME all the way to the proxy — nothing in this package resolves a target address locally — and TLS stays end-to-end between the caller and the real server (the proxy only ever sees an opaque tunnel).
Two pieces:
- Resolver decides, per target, which proxy (if any) to use: an explicit URL, the conventional environment variables (HTTPS_PROXY, ALL_PROXY, NO_PROXY, ...), or none. The same decision is exposed as a per-address lookup for raw TCP dials (ProxyForAddr) and as an http.Transport.Proxy function (ProxyForRequest), so HTTP clients and raw TCP dials follow identical rules.
- Dialer is a DialContext-compatible dialer that tunnels through the proxy the Resolver picks, or dials directly when there is none.
Proxy credentials (URL userinfo) are only ever written to the proxy in a Proxy-Authorization header. They never appear in errors or in the output of Redact / Resolver.String, which is what callers must use for logging.
Index ¶
Constants ¶
const ( // ModeAuto takes the proxy from the environment (see FromEnvironment). ModeAuto = "auto" // ModeOff never uses a proxy. ModeOff = "off" // ModeExplicit uses one explicitly configured proxy URL for every target. ModeExplicit = "explicit" )
Mode names. Parse accepts ModeAuto and ModeOff (case-insensitive) in addition to an explicit proxy URL; Resolver.Mode reports one of the three.
const DefaultTimeout = 30 * time.Second
DefaultTimeout bounds a Dialer's connection establishment when neither Dialer.Timeout nor the context sets a tighter limit.
Variables ¶
This section is empty.
Functions ¶
func Redact ¶
Redact renders a proxy URL for logging as scheme://host:port, replacing any userinfo (user name and password alike) with "***" and dropping the path, query and fragment. Redact(nil) is "".
A URL with "@" in its path, query, fragment or opaque part is taken to be a proxy URL whose credentials held an unescaped '/', '?' or '#', which url.Parse splits so that part of the credentials lands in Host. Redact then withholds everything before the last "@" and shows only what follows it as the host.
Types ¶
type ConnectError ¶
type ConnectError struct {
// Target is the "host:port" the CONNECT asked for.
Target string
// StatusCode is the proxy's HTTP status, e.g. 407 or 403.
StatusCode int
}
ConnectError is returned when the proxy answers CONNECT with a non-2xx status. Its message never includes credentials or proxy-supplied text.
func (*ConnectError) Error ¶
func (e *ConnectError) Error() string
type Dialer ¶
type Dialer struct {
// Resolver picks the proxy per target. nil never proxies.
Resolver *Resolver
// Timeout bounds the whole establishment: the TCP connect to the proxy
// (or to the target when dialing directly), the TLS handshake with an
// https:// proxy, and the CONNECT exchange. Zero means DefaultTimeout.
// An earlier deadline on the DialContext context wins.
Timeout time.Duration
// Forward dials the proxy itself, and the target when no proxy applies.
// nil uses a net.Dialer.
Forward func(ctx context.Context, network, addr string) (net.Conn, error)
// TLSConfig configures the TLS session with an https:// proxy (not with
// the target). nil uses defaults; ServerName defaults to the proxy host.
TLSConfig *tls.Config
}
Dialer opens stream connections, tunnelling them through the HTTP CONNECT proxy its Resolver picks for each target and dialing directly when there is none. The zero value (or a nil *Dialer) always dials directly.
DialContext has the signature of (*net.Dialer).DialContext, so a Dialer drops into anything that accepts a dial function:
d := netproxy.NewDialer(resolver) conn, err := d.DialContext(ctx, "tcp", "registry.example.net:443")
The target address is sent to the proxy verbatim ("CONNECT host:port") and never resolved locally, so it works when local DNS for the target is broken or poisoned. Callers that want TLS wrap the returned conn with tls.Client themselves; TLS then runs end-to-end with the real server.
func (*Dialer) DialContext ¶
DialContext connects to addr ("host:port") on network, through the proxy the Resolver selects for addr or directly when it selects none. Only "tcp", "tcp4" and "tcp6" can be tunnelled.
type EnvError ¶
type EnvError struct {
// Var is the environment variable, e.g. "HTTP_PROXY".
Var string
// Err says why the value is unusable.
Err error
}
EnvError reports a proxy environment variable whose value cannot be used. Its message names the variable and never includes the value's credentials.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver decides which proxy, if any, an outbound connection to a given target should go through. A nil *Resolver is valid and never proxies. Resolvers are immutable and safe for concurrent use.
func Explicit ¶
Explicit returns a Resolver that sends every target — including loopback and anything NO_PROXY would exempt — through proxyURL. The URL must use the http or https scheme ("http://[user:pass@]host[:port]"); a URL with no scheme is taken as http. The port defaults to 80 for http and 443 for https. Everything up to the last "@" is the userinfo, so credentials may hold unescaped '/', '?', '#' or '@'; percent-escapes in them are decoded.
func FromEnvironment ¶
FromEnvironment returns a Resolver built from a snapshot of the conventional proxy environment variables, taken now:
- TLS and raw TCP targets (ProxyForAddr, and https:// / wss:// requests) use the first usable one of HTTPS_PROXY, https_proxy, ALL_PROXY, all_proxy.
- Plain http:// / ws:// requests use the first usable one of HTTP_PROXY, http_proxy, and otherwise the same proxy as TLS targets. HTTP_PROXY (upper case) is ignored when REQUEST_METHOD is set, as net/http does, so a CGI request header cannot inject a proxy.
- NO_PROXY / no_proxy exempts targets: a comma- or space-separated list of host names (matching the name and its subdomains; a leading "." or "*." matches subdomains only), IP addresses, CIDR ranges, each optionally with ":port", or "*" for everything. localhost and loopback addresses are always exempt.
Each variable is parsed on its own, so one bad value never disables another. A value is unusable when it is malformed or names a scheme other than http or https (socks5://, for example). An unusable HTTP_PROXY, http_proxy, ALL_PROXY or all_proxy is skipped (the next variable in its list applies) and reported by Warnings. Only an unusable HTTPS_PROXY or https_proxy, which explicitly names the TLS proxy, makes FromEnvironment fail, with an *EnvError naming it.
An environment with no usable proxy variables yields a Resolver that never proxies (Enabled reports false). Neither errors nor warnings ever echo a value's credentials.
func Parse ¶
Parse builds a Resolver from a -proxy style setting: "auto" (or "") reads the environment via FromEnvironment, "off" disables proxying, and anything else is an explicit proxy URL (see Explicit). Deciding whether "auto" applies at all (for example only in a TCP-only transport mode) is the caller's policy; compare the setting against ModeAuto for that.
func (*Resolver) Enabled ¶
Enabled reports whether the Resolver can route any target through a proxy. It is false for Off, for a nil Resolver and for an environment without proxy variables.
func (*Resolver) ProxyForAddr ¶
ProxyForAddr returns the proxy to tunnel a raw TCP (or TLS) connection to addr ("host:port") through, or nil to dial directly. addr is inspected as text only; host names are never resolved.
func (*Resolver) ProxyForRequest ¶
ProxyForRequest returns the proxy for req, or nil for a direct connection. Its signature matches http.Transport.Proxy:
tr.Proxy = resolver.ProxyForRequest
https:// and wss:// requests follow exactly the same rules as ProxyForAddr (net/http then tunnels them with CONNECT, sending the host name); http:// and ws:// requests prefer HTTP_PROXY in ModeAuto.