netx

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 18 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 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

func Dials

func Dials() uint64

Dials returns the number of dial attempts made by netx clients in this process. Tests use it to prove that a run made no network calls.

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

func NewAllowlist(patterns ...string) (*Allowlist, error)

NewAllowlist builds an Allowlist from patterns, lowercased. Any malformed pattern fails with code config.

func (*Allowlist) Allowed

func (a *Allowlist) Allowed(host string) bool

Allowed reports whether host (a bare host name, no port) is allowed. The host is lowercased; IP literals and malformed names are never allowed.

type Client

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

Client is an HTTP client bound to one purpose and one allowlist.

func New

func New(p Purpose, cfg Config, allow *Allowlist) (*Client, error)

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

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

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

func (e *Error) Error() string

Error returns "netx <purpose> <host>: <code>", leaving out an empty purpose or host.

func (*Error) GoString

func (e *Error) GoString() string

GoString returns the same safe fields for %#v.

func (*Error) Timeout

func (e *Error) Timeout() bool

Timeout reports whether the error is a timeout.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the sentinel cause (context.DeadlineExceeded or context.Canceled), or nil.

func (*Error) WarningCode added in v1.0.0

func (e *Error) WarningCode() string

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).

func Purposes

func Purposes() []Purpose

Purposes returns every valid purpose in a fixed order.

func (Purpose) Valid

func (p Purpose) Valid() bool

Valid reports whether p is one of Purposes.

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.

Jump to

Keyboard shortcuts

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