netguard

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package netguard owns the outbound-address policy: which destinations the router may dial on behalf of untrusted input (client-supplied URLs, plugin requests, third-party proxy feeds). Every outbound path that dials a caller-chosen host goes through this package, so the blocked ranges exist in one place only.

Index

Constants

This section is empty.

Variables

View Source
var ErrBlockedAddress = errors.New("destination address is not public")

ErrBlockedAddress reports a destination outside the public unicast space.

Functions

func CheckURL

func CheckURL(target *url.URL) error

CheckURL validates scheme and host of one URL without DNS. Dial-time resolution still applies through the guarded transport.

func DialContext

func DialContext(dialer *net.Dialer, resolver Resolver) func(ctx context.Context, network, address string) (net.Conn, error)

DialContext returns a dial function that resolves the target through ResolvePublic and dials the pinned address.

func IsLocalHostname

func IsLocalHostname(host string) bool

IsLocalHostname reports hostnames that name the local machine or a local zone. They are refused before resolution: their answers are host-defined.

func IsPublicAddress

func IsPublicAddress(address netip.Addr) bool

IsPublicAddress reports whether address is a globally routable unicast address. IPv4-mapped IPv6 addresses are unmapped first.

func IsPublicIP

func IsPublicIP(ip net.IP) bool

IsPublicIP is IsPublicAddress for net.IP values.

func NewHTTPClient

func NewHTTPClient(timeout time.Duration) *http.Client

NewHTTPClient returns a client that only reaches public http(s) destinations: every dial pins a validated public address, every redirect hop re-checks scheme and host, and environment proxies are ignored (a proxy would dial the target on the client's behalf, bypassing the pin).

func PublicHostLiteral

func PublicHostLiteral(host string) (literal bool, public bool)

PublicHostLiteral classifies host without DNS: literal reports whether host is an IP literal, public whether that literal (or, for hostnames, the name itself) may be dialed. Hostnames still need ResolvePublic at dial time.

func ResolvePublic

func ResolvePublic(ctx context.Context, resolver Resolver, host string, ipv4Only bool) (net.IP, error)

ResolvePublic resolves host and returns the first public address. IP literals are checked directly. ipv4Only restricts both literals and answers to IPv4. Callers dial the returned IP, never the name again, which closes DNS rebinding between check and dial.

Types

type Resolver

type Resolver interface {
	LookupIP(ctx context.Context, network, host string) ([]net.IP, error)
}

Resolver is the DNS surface ResolvePublic needs; net.DefaultResolver satisfies it.

Jump to

Keyboard shortcuts

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