network

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: MPL-2.0 Imports: 15 Imported by: 5

README

network

A Go library that provides a host independent abstraction for network operations.

Simplified successor to: noisysockets/network and noisysockets/resolver.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DialContext

type DialContext func(ctx context.Context, network, address string) (net.Conn, error)

DialContext is a function that connects to the address on the named network using the provided context.

type FilteredNetwork added in v0.3.0

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

FilteredNetwork is a network that filters connections based on allowed and denied destination prefixes. It forwards connections to the upstream network if the destination is allowed. If the destination is denied, it returns an error.

func Filtered added in v0.3.0

func Filtered(conf *FilteredNetworkConfig) *FilteredNetwork

Filtered creates a new filtered network with the given configuration.

func (*FilteredNetwork) AddAllowedDestination added in v0.3.1

func (n *FilteredNetwork) AddAllowedDestination(prefix netip.Prefix)

AddAllowedDestination adds a prefix to the list of allowed destinations.

func (*FilteredNetwork) AddAllowedPort added in v0.3.3

func (n *FilteredNetwork) AddAllowedPort(port uint16)

AddAllowedPort adds a port to the list of allowed ports.

func (*FilteredNetwork) AddDeniedDestination added in v0.3.1

func (n *FilteredNetwork) AddDeniedDestination(prefix netip.Prefix)

AddDeniedDestination adds a prefix to the list of denied destinations.

func (*FilteredNetwork) AddDeniedPort added in v0.3.3

func (n *FilteredNetwork) AddDeniedPort(port uint16)

AddDeniedPort adds a port to the list of denied ports.

func (*FilteredNetwork) DialContext added in v0.3.0

func (n *FilteredNetwork) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

func (*FilteredNetwork) Listen added in v0.3.0

func (n *FilteredNetwork) Listen(network, address string) (net.Listener, error)

func (*FilteredNetwork) ListenPacket added in v0.3.0

func (n *FilteredNetwork) ListenPacket(network, address string) (net.PacketConn, error)

func (*FilteredNetwork) LookupHost added in v0.3.0

func (n *FilteredNetwork) LookupHost(ctx context.Context, host string) ([]string, error)

func (*FilteredNetwork) RemoveAllowedDestination added in v0.3.1

func (n *FilteredNetwork) RemoveAllowedDestination(prefix netip.Prefix)

RemoveAllowedDestination removes a prefix from the list of allowed destinations.

func (*FilteredNetwork) RemoveAllowedPort added in v0.3.3

func (n *FilteredNetwork) RemoveAllowedPort(port uint16)

RemoveAllowedPort removes a port from the list of allowed ports.

func (*FilteredNetwork) RemoveDeniedDestination added in v0.3.1

func (n *FilteredNetwork) RemoveDeniedDestination(prefix netip.Prefix)

RemoveDeniedDestination removes a prefix from the list of denied destinations.

func (*FilteredNetwork) RemoveDeniedPort added in v0.3.3

func (n *FilteredNetwork) RemoveDeniedPort(port uint16)

RemoveDeniedPort removes a port from the list of denied ports.

type FilteredNetworkConfig added in v0.3.0

type FilteredNetworkConfig struct {
	// Allowed destination prefixes.
	AllowedDestinations []netip.Prefix
	// Denied destination prefixes.
	DeniedDestinations []netip.Prefix
	// Allowed destination ports.
	AllowedPorts []uint16
	// Denied destination ports.
	DeniedPorts []uint16
	// The network to forward connections to.
	Upstream Network
}

FilteredNetworkConfig is the configuration for a FilteredNetwork.

type HostNetwork

type HostNetwork struct{}

func Host

func Host() *HostNetwork

Host returns a network implementation that uses the host's network stack.

func (*HostNetwork) DialContext

func (n *HostNetwork) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

func (*HostNetwork) Listen

func (n *HostNetwork) Listen(network, address string) (net.Listener, error)

func (*HostNetwork) ListenPacket

func (n *HostNetwork) ListenPacket(network, address string) (net.PacketConn, error)

func (*HostNetwork) LookupHost added in v0.2.0

func (n *HostNetwork) LookupHost(ctx context.Context, host string) ([]string, error)

type LoopbackNetwork

type LoopbackNetwork struct{}

func Loopback

func Loopback() *LoopbackNetwork

Loopback returns a network that only connects to localhost.

func (*LoopbackNetwork) DialContext

func (n *LoopbackNetwork) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

func (*LoopbackNetwork) Listen

func (n *LoopbackNetwork) Listen(network, address string) (net.Listener, error)

func (*LoopbackNetwork) ListenPacket

func (n *LoopbackNetwork) ListenPacket(network, address string) (net.PacketConn, error)

func (*LoopbackNetwork) LookupHost added in v0.2.0

func (n *LoopbackNetwork) LookupHost(ctx context.Context, host string) ([]string, error)

type NetstackNetwork

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

func Netstack

func Netstack(ipstack *stack.Stack, nicID tcpip.NICID, resolveConf *ResolveConfig) *NetstackNetwork

Netstack returns a network that uses the provided netstack stack and NIC ID.

func (*NetstackNetwork) DialContext

func (n *NetstackNetwork) DialContext(ctx context.Context, network, address string) (net.Conn, error)

func (*NetstackNetwork) Listen

func (n *NetstackNetwork) Listen(network, address string) (net.Listener, error)

func (*NetstackNetwork) ListenPacket

func (n *NetstackNetwork) ListenPacket(network, address string) (net.PacketConn, error)

func (*NetstackNetwork) LookupHost added in v0.2.0

func (n *NetstackNetwork) LookupHost(ctx context.Context, host string) ([]string, error)

type Network

type Network interface {
	// DialContext connects to the address on the named network using the provided context.
	DialContext(ctx context.Context, network, addr string) (net.Conn, error)
	// LookupHost looks up the given host using the local resolver.
	// It returns a slice of that host's addresses.
	LookupHost(ctx context.Context, host string) ([]string, error)
	// Listen listens for incoming connections on the network address.
	// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only).
	// If the address is an empty string, Listen listens on all available addresses.
	Listen(network, address string) (net.Listener, error)
	// ListenPacket listens for incoming packets addressed to the local address.
	// Known networks are "udp", "udp4" (IPv4-only), "udp6" (IPv6-only).
	ListenPacket(network, address string) (net.PacketConn, error)
}

Network is a simple network abstraction.

type ResolveConfig

type ResolveConfig struct {
	// Nameservers is a list of nameservers to use.
	// If empty, the system default resolver is used.
	Nameservers []string
	// SearchDomains is a list of search domains to use.
	SearchDomains []string
	// NDots is the number of dots in name to trigger absolute lookup.
	// Defaults to 1 if nil.
	NDots *int
}

ResolveConfig holds the resolver configuration.

func (*ResolveConfig) LookupHost added in v0.2.0

func (r *ResolveConfig) LookupHost(ctx context.Context, host string, dialContext DialContext) ([]string, error)

LookupHost looks up the given host using the resolver configuration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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