Documentation
¶
Overview ¶
Package netx is the only package that builds HTTP clients and transports: HTTPS only, a closed purpose enum, registry-declared hosts for verification, no redirects for verify, proxy and custom CA support.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Allowlist ¶
type Allowlist struct {
// contains filtered or unexported fields
}
Allowlist is the set of hosts a client may reach. A pattern is a literal host ("api.openai.com") or a template with exactly one "*" label followed by at least two literal labels ("*.openai.azure.com", "bedrock.*.amazonaws.com"). The "*" matches exactly one label of 1 to 63 characters from [a-z0-9-], never a dot. An empty Allowlist allows no host.
func NewAllowlist ¶
NewAllowlist builds an Allowlist from patterns, lowercased. Any malformed pattern fails with code config.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client bound to one purpose and one allowlist.
func New ¶
New returns a client for purpose p that reaches only hosts in allow. It fails with code purpose for an unknown purpose and code config for a nil allowlist, a bad proxy URL, a CA bundle without certificates, a negative timeout or a DialAddrs value that is not a loopback ip:port.
func (*Client) CloseIdleConnections ¶
func (c *Client) CloseIdleConnections()
CloseIdleConnections closes idle keep-alive connections.
func (*Client) Do ¶
Do sends req. Every error is a *Error, including errors from reading or closing the response body; the original error, which may hold the full URL, is discarded. For purpose verify a 3xx response with a Location is closed and returned as code redirect.
func (*Client) Transport ¶
func (c *Client) Transport() http.RoundTripper
Transport returns the checking RoundTripper for libraries that take one. It enforces the scheme, host and allowlist checks before any dial on every hop, like Do. For purpose verify it closes any 3xx response with a Location and returns code redirect, so no client can follow it. Other purposes leave redirect limits to the caller's client. Only response headers are bounded (by Config.Timeout): callers own the overall request and body deadline (a context deadline or a client timeout).
type Code ¶
type Code string
Code is the closed set of netx failure codes. Callers map a Code to a warning detail code; the error never carries foreign text.
const ( // CodeDNS: the host name did not resolve. CodeDNS Code = "dns" // CodeTLS: the TLS handshake failed (untrusted certificate, version below 1.2). CodeTLS Code = "tls" // CodeTimeout: a deadline or the client timeout expired. CodeTimeout Code = "timeout" // CodeRefused: the connection was refused. CodeRefused Code = "refused" // CodeRedirect: a verify call was answered with a redirect, which is never followed. CodeRedirect Code = "redirect" // CodeHostNotAllowed: the URL host is not on the allowlist or is malformed. CodeHostNotAllowed Code = "host_not_allowed" // CodeHTTPScheme: the URL is not https. CodeHTTPScheme Code = "http_scheme" // CodeCanceled: the request context was canceled. CodeCanceled Code = "canceled" // CodeNetwork: any other network failure. CodeNetwork Code = "network" // CodePurpose: the purpose is not in the closed enum. CodePurpose Code = "purpose" // CodeConfig: the client configuration is invalid (proxy, CA bundle, allowlist). CodeConfig Code = "config" // CodeBenchmarkNetworkBlocked: a benchmark build (-tags benchmark) refused the // request before any dial: every purpose but verify, and a verify client without // a responder. Default builds never return it. CodeBenchmarkNetworkBlocked Code = "benchmark_network_blocked" // CodeNotInGroundTruth: a benchmark build had no ground truth answer for a verify // request (the credential is not listed), so nothing was sent. Default builds // never return it. CodeNotInGroundTruth Code = "not_in_ground_truth" )
Error codes.
type Config ¶
type Config struct {
// HTTPSProxy is the proxy URL (http or https scheme; a bare host:port means
// http). Userinfo becomes Basic proxy credentials held as a secret.Secret.
HTTPSProxy string
// NoProxy is a comma list of hosts that bypass the proxy: "*", a host, a domain
// with or without a leading dot (matching its subdomains too), each optionally
// with ":port". IP and CIDR entries never match, since hosts are never IPs.
NoProxy string
// CABundle is PEM data appended to the system certificate pool.
CABundle []byte
// FromEnv reads HTTPS_PROXY/https_proxy and NO_PROXY/no_proxy for the fields
// above that are empty. HTTP_PROXY and ALL_PROXY are never read.
FromEnv bool
// Timeout bounds a whole request, response headers included; 0 means 30s.
Timeout time.Duration
// DialAddrs maps a dial address "host:port" to the address actually dialled.
// It is a test hook: every value must be "ip:port" with a loopback IP literal,
// else New fails with code config. TLS still verifies the requested host name.
DialAddrs map[string]string
// contains filtered or unexported fields
}
Config holds the transport settings. It mirrors the transport fields of tokenhound.NetworkOptions plus a timeout and a test hook.
type Error ¶
type Error struct {
Code Code
Purpose Purpose
Host string
// contains filtered or unexported fields
}
Error is every error netx returns. It holds the code, the purpose, the lowercased host name (no port, path, query, userinfo or headers) and at most a sentinel cause (context.DeadlineExceeded or context.Canceled) for errors.Is. It never holds or prints foreign error text.
func (*Error) Error ¶
Error returns "netx <purpose> <host>: <code>", leaving out an empty purpose or host.
func (*Error) Unwrap ¶
Unwrap returns the sentinel cause (context.DeadlineExceeded or context.Canceled), or nil.
func (*Error) WarningCode ¶ added in v1.0.0
WarningCode returns the Report warning code this error maps to on its own, or "" when the caller chooses the code. warnx.From uses it: a request a benchmark build refused becomes a benchmark_network_blocked warning.
type Purpose ¶
type Purpose string
Purpose tags every client with why it talks to the network. The set is closed: New refuses any other value with code purpose.
const ( // PurposeVerify: a read-only call to the provider that issued a key. PurposeVerify Purpose = "verify" // PurposeRegistry: fetching a registry snapshot. PurposeRegistry Purpose = "registry" // PurposeForge: calls to a code forge (GitHub, GitLab) for org scans and fixes. PurposeForge Purpose = "forge" // PurposeAlert: alert reporters (webhooks, chat). PurposeAlert Purpose = "alert" // PurposeImage: pulling container image layers. PurposeImage Purpose = "image" // PurposeGit: network access handed to git. PurposeGit Purpose = "git" // PurposePing: the opt-in usage ping. PurposePing Purpose = "ping" )
The closed purpose enum (AD-7).
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package netxtest holds loopback test wiring for code that talks through internal/netx: a throwaway certificate authority, TLS test servers whose leaf certificates name any host, and a CONNECT-only proxy.
|
Package netxtest holds loopback test wiring for code that talks through internal/netx: a throwaway certificate authority, TLS test servers whose leaf certificates name any host, and a CONNECT-only proxy. |