netutil

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 6 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 netutil classifies network addresses, ranks the host's LAN interfaces and guards HTTP handlers so only LAN clients get through.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Addrs

func Addrs(cs []Candidate) []netip.Addr

Addrs returns the addresses of cs in order.

func Guard

func Guard(allowed []netip.Prefix) func(http.Handler) http.Handler

Guard returns middleware that answers 403 to any request whose source address is neither private (see IsPrivate) nor inside one of allowed.

func GuardFunc

func GuardFunc(allowed func() []netip.Prefix) func(http.Handler) http.Handler

GuardFunc is Guard with an allow-list that is looked up per request, for lists that an administrator can edit while the server runs.

func IsPrivate

func IsPrivate(addr netip.Addr) bool

IsPrivate reports whether addr is a private, link-local, CGNAT or loopback address. IPv4-mapped IPv6 addresses are judged by their IPv4 form and any IPv6 zone is ignored. The zero Addr is not private.

func IsVirtualName

func IsVirtualName(name string) bool

IsVirtualName reports whether an interface name looks like a virtual, container or Apple-internal adapter that must not supply LAN addresses.

Types

type Candidate

type Candidate struct {
	Interface string
	Index     int
	Addr      netip.Addr
}

Candidate is a private LAN address on a physical-looking interface.

func Candidates

func Candidates() ([]Candidate, error)

Candidates enumerates the host's network interfaces and returns the ranked private LAN addresses (see Rank).

func Rank

func Rank(ifaces []Interface) []Candidate

Rank filters ifaces down to usable LAN addresses and orders them best first. Interfaces that are down, loopback or virtual are dropped, as are loopback, unspecified and multicast addresses and anything IsPrivate rejects. Ties break on interface index, then address; duplicate addresses keep their first (best) occurrence.

type Interface

type Interface struct {
	Name     string
	Index    int
	Up       bool
	Loopback bool
	Addrs    []netip.Addr
}

Interface is a network interface reduced to the fields ranking needs, so Rank can be tested on fake data.

func Interfaces

func Interfaces() ([]Interface, error)

Interfaces lists the host's network interfaces with their unicast addresses. An interface whose addresses cannot be read is reported with no addresses rather than failing the whole scan.

Jump to

Keyboard shortcuts

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