netguard

package
v1.54.2 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 8 Imported by: ? 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 netguard refuses connections to internal and reserved addresses, so a destination a caller does not trust cannot reach the host's own network.

A dial guard sees the address being connected to, which is the proxy's when the client uses one, so a guarded http.Transport should set Proxy to nil.

Index

Constants

This section is empty.

Variables

View Source
var ErrBlockedAddress = errors.New("blocked destination address")

ErrBlockedAddress marks a destination the guard refuses. It threads through a transport's url.Error, so callers can classify it as permanent.

Functions

func BlockInternal

func BlockInternal(_, address string, _ syscall.RawConn) error

BlockInternal is a net.Dialer Control function that refuses every address IsBlockedIP reports, and any zoned address.

func CheckHost

func CheckHost(host string) error

CheckHost rejects a URL host that is localhost, a blocked IP literal, or an IP literal with a zone. A port, and brackets around an IPv6 literal, are accepted. It only catches configuration mistakes early: host names, and IPv4 spellings netip.ParseAddr rejects such as 127.1, pass here and are checked by a dial guard once resolved.

func IsBlockedIP

func IsBlockedIP(ip net.IP) bool

IsBlockedIP reports whether ip falls in a range a guarded client must never dial: loopback, RFC1918 private and IPv6 ULA (fc00::/7), link-local unicast (including the 169.254.169.254 cloud-metadata IP and fe80::/10), link-local multicast, the unspecified address, any multicast, and the reserved ranges listed in blockedPrefixes. An address in the NAT64 well-known prefix is judged by the IPv4 address it carries. A nil or malformed ip is reported as blocked.

func NewDialControl

func NewDialControl(blockPrivate bool, blocked, allowed []netip.Prefix) func(network, address string, c syscall.RawConn) error

NewDialControl returns a net.Dialer Control function that refuses addresses in blocked, and private ones when blockPrivate is set, unless they are also in allowed. An allowed address is never refused, so a broad allowed range such as 0.0.0.0/0 also lets loopback and the cloud metadata address through. Prefixes should come from ParseCIDR.

func ParseCIDR

func ParseCIDR(cidr string) (netip.Prefix, error)

ParseCIDR parses a range for NewDialControl. A dialed address is unmapped before matching, so an IPv4-mapped range is converted to its IPv4 range.

func ReportOnly

func ReportOnly(
	control func(network, address string, c syscall.RawConn) error,
	report func(address string, err error),
) func(network, address string, c syscall.RawConn) error

ReportOnly wraps a net.Dialer Control function so that an address it refuses with ErrBlockedAddress is passed to report and the connection goes ahead. Other errors from control are returned unchanged.

func ReportOnlyContext

func ReportOnlyContext(
	control func(network, address string, c syscall.RawConn) error,
	report func(ctx context.Context, address string, err error),
) func(ctx context.Context, network, address string, c syscall.RawConn) error

ReportOnlyContext is ReportOnly for net.Dialer.ControlContext, passing the dial context to report. Under http.Transport that context carries the values of the request that started the dial; a request that reuses a pooled connection does not dial, so it is not reported.

Types

This section is empty.

Jump to

Keyboard shortcuts

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