net

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package net implements the functions, types, and interfaces for the module.

Index

Constants

View Source
const (
	IPv4len = net.IPv4len
	IPv6len = net.IPv6len
)

Variables

View Source
var ErrNoIPFound = errors.New("no ip found")

Functions

func ExtractIP added in v0.3.24

func ExtractIP(scheme, host, hostPort string) (string, error)

ExtractIP will extract the IP address from the host, and if the host is empty, it will extract the local IP address.

func GenerateDynamic added in v0.3.24

func GenerateDynamic(option *EndpointOptions, scheme, addr string) (string, error)

GenerateDynamic will generate a dynamic endpoint.

func HostAddr

func HostAddr(provider NetworkInterfaceProvider, opts ...Option) string

HostAddr now accepts a NetworkInterfaceProvider.

func IsPublicRoutableIP added in v0.3.24

func IsPublicRoutableIP(addr string) bool

IsPublicRoutableIP checks if the given IP address is a global unicast address and not a private or multicast address.

func IsUsableHostIP added in v0.3.24

func IsUsableHostIP(addr string) bool

IsUsableHostIP checks if the given IP address is a usable host IP (not loopback, link-local, or unspecified).

func ParseIP

func ParseIP(ip IP) int64

ParseIP converts an IP address to its int64 representation.

func PreferIPv4Strategy added in v0.3.24

func PreferIPv4Strategy(ips []net.IP) (net.IP, error)

PreferIPv4Strategy prefers IPv4 addresses over IPv6

func PreferPublicIPStrategy added in v0.3.24

func PreferPublicIPStrategy(ips []net.IP) (net.IP, error)

PreferPublicIPStrategy prefers public IPs over private ones

func RealHostAddr added in v0.3.22

func RealHostAddr(opts ...Option) string

RealHostAddr is a convenience function for external callers to use the real network provider.

Types

type EndpointOptions added in v0.3.24

type EndpointOptions struct {
	// EnvVar specifies the environment variable name that may contain the host IP
	EnvVar string

	// HostIP directly specifies the host IP to use (overrides EnvVar if both are set)
	HostIP string

	// EndpointFunc allows custom endpoint generation logic
	EndpointFunc func(scheme, host, port string) (string, error)

	// IPStrategy defines how to select an IP when multiple are available
	IPStrategy IPSelectionStrategy

	// PreferPublicIP indicates whether to prefer public IPs over private ones
	PreferPublicIP bool

	// AllowedInterfaces specifies which network interfaces to consider
	AllowedInterfaces []string

	// CloudMetadataEnabled enables/disables cloud provider metadata lookup
	CloudMetadataEnabled bool
}

EndpointOptions contains configuration for endpoint generation

func DefaultOptions added in v0.3.24

func DefaultOptions() *EndpointOptions

DefaultOptions returns the default endpoint options

func (*EndpointOptions) WithCloudMetadata added in v0.3.24

func (o *EndpointOptions) WithCloudMetadata(enabled bool) *EndpointOptions

WithCloudMetadata enables/disables cloud metadata lookup

func (*EndpointOptions) WithEnvVar added in v0.3.24

func (o *EndpointOptions) WithEnvVar(name string) *EndpointOptions

WithEnvVar sets the environment variable name for host IP

func (*EndpointOptions) WithHostIP added in v0.3.24

func (o *EndpointOptions) WithHostIP(ip string) *EndpointOptions

WithHostIP directly sets the host IP

func (*EndpointOptions) WithIPStrategy added in v0.3.24

func (o *EndpointOptions) WithIPStrategy(strategy IPSelectionStrategy) *EndpointOptions

WithIPStrategy sets the IP selection strategy

type HostConfig added in v0.0.20

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

HostConfig defines the configuration for the host.

func DefaultConfig added in v0.3.21

func DefaultConfig() HostConfig

DefaultConfig returns the default host configuration.

type IP

type IP = net.IP

func IPv4

func IPv4(a, b, c, d byte) IP

func ToIP

func ToIP(ip int64) IP

ToIP converts an int64 representation back to an IP address.

type IPSelectionStrategy added in v0.3.24

type IPSelectionStrategy func([]net.IP) (net.IP, error)

IPSelectionStrategy defines how to select an IP from multiple available IPs

type InterfaceWithAddrs added in v0.3.21

type InterfaceWithAddrs interface {
	GetInterface() net.Interface
	GetAddrs() ([]net.Addr, error)
}

InterfaceWithAddrs abstracts a network interface and its addresses for mocking.

type NetworkInterfaceProvider added in v0.3.22

type NetworkInterfaceProvider interface {
	Interfaces() ([]InterfaceWithAddrs, error)
}

NetworkInterfaceProvider now returns InterfaceWithAddrs.

type Option added in v0.0.20

type Option func(*HostConfig)

Option is a function that configures a HostConfig.

func WithCIDRFilters added in v0.0.20

func WithCIDRFilters(cidrs []string) Option

WithCIDRFilters sets CIDR ranges to filter interfaces by.

func WithEnvVar added in v0.0.20

func WithEnvVar(name string) Option

WithEnvVar sets the environment variable name to look for an IP address.

func WithFallback added in v0.0.20

func WithFallback(enable bool) Option

WithFallback enables or disables falling back to the first available IP if no other method succeeds.

func WithInterfacePatterns added in v0.0.20

func WithInterfacePatterns(patterns []string) Option

WithInterfacePatterns sets patterns to match interface names against.

Directories

Path Synopsis
Package filter implements the functions, types, and interfaces for the module.
Package filter implements the functions, types, and interfaces for the module.

Jump to

Keyboard shortcuts

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