network

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: GPL-3.0 Imports: 19 Imported by: 4

Documentation

Index

Constants

View Source
const (
	WORLD4 = "0.0.0.0/0"
	WORLD6 = "::/0"
	LAN4   = "4"
	LAN6   = "6"
)

Variables

View Source
var Network = struct {

	// default interface and gateway for
	// all WAN traffic (i.e. 0.0.0.0/0 & ::/0)
	DefaultIPv4Route *Route
	DefaultIPv6Route *Route

	// additional default routes scoped
	// to specific interfaces
	ScopedDefaults []*Route

	// all static routes
	StaticRoutes []*Route
}{}

global network properties

View Source
var (
	// negative offset will give chains created for
	// this router a high priority compared chains
	// in the kernal with default priorities
	NftChainPriorityOffset int32 = 0
)

Functions

func CanConnect

func CanConnect(host string, port int) bool

test tcp connection

func GetNextAvailabeInterface

func GetNextAvailabeInterface(prefix string) (string, error)

given a device name prefix return the next available one

func IncIP

func IncIP(ip net.IP)

func IsPrivateAddr

func IsPrivateAddr(addr netip.Addr) bool

returns whether the given ipv4 or ipv6 address is a private address

func ResolveNames

func ResolveNames(dnsNames []string, flatten bool) ([]string, [][]string, error)

resolves the given list of domain names and returns their corresponding as two lists. The first list will either be a flattened list of all resolved ips as stringsor just the first resolved ip giving a 1:1 mapping to the given names. The second list is a list of ips resolved for each name.

Types

type DNSManager

type DNSManager interface {
	AddDNSServers(servers []string) error
	AddSearchDomains(domains []string) error

	Clear()
}

type FilterRouter

type FilterRouter interface {
	AddIPsToDenyList(ips []netip.Addr) error
	DeleteIPsFromDenyList(ips []netip.Addr) error

	AddIPsToAllowList(ips []netip.Addr) error
	DeleteIPsFromAllowList(ips []netip.Addr) error

	SetSecurityGroups(sgs []SecurityGroup, iifName string) error
	DeleteSecurityGroups(sgs []SecurityGroup, iifName string) error

	ForwardPort(dstPort, forwardPort int, forwardIP netip.Addr, proto Protocol) (string, error)
	DeleteForwardPort(dstPort, forwardPort int, forwardIP netip.Addr, proto Protocol) error

	ForwardPortOnIP(dstPort, forwardPort int, dstIP, forwardIP netip.Addr, proto Protocol) (string, error)
	DeleteForwardPortOnIP(dstPort, forwardPort int, dstIP, forwardIP netip.Addr, proto Protocol) error

	ForwardTraffic(srcItfName, dstItfName string, srcNetwork, dstNetwork netip.Prefix, withNat bool) (string, error)
	DeleteForwardTraffic(srcItfName, dstItfName string, srcNetwork, dstNetwork netip.Prefix) error

	DeleteFilter(key string) error

	Clear()
}

type NetworkContext

type NetworkContext interface {
	DefaultDeviceName() string
	DefaultInterface() string
	DefaultGateway() string
	DefaultIP() string

	DisableIPv6() error

	NewDNSManager() (DNSManager, error)
	NewRouteManager() (RouteManager, error)

	Clear()
}

func NewNetworkContext

func NewNetworkContext() (NetworkContext, error)

type PortGroup

type PortGroup struct {
	Proto Protocol

	FromPort,
	ToPort int
}

type Protocol

type Protocol string
const (
	ICMP Protocol = "icmp"
	TCP  Protocol = "tcp"
	UDP  Protocol = "udp"
)

type RoutableInterface

type RoutableInterface interface {
	Name() string
	Address4() (string, string, error)
	Address6() (string, string, error)
	MakeDefaultRoute() error

	SetSecurityGroups(sgs []SecurityGroup) error
	DeleteSecurityGroups(sgs []SecurityGroup) error

	ForwardPortTo(proto Protocol, dstPort int, forwardPort int, forwardIP netip.Addr) (string, error)
	DeletePortForwardedTo(proto Protocol, dstPort int, forwardPort int, forwardIP netip.Addr) error

	FowardTrafficTo(dstItf RoutableInterface, srcNetwork, dstNetwork string, withNat bool) (string, error)
	DeleteTrafficForwardedTo(dstItf RoutableInterface, srcNetwork, dstNetwork string) error
	FowardTrafficFrom(srcItf RoutableInterface, srcNetwork, dstNetwork string, withNat bool) (string, error)
	DeleteTrafficForwardedFrom(srcItf RoutableInterface, srcNetwork, destNetwork string) error
}

type Route

type Route struct {
	InterfaceIndex int
	InterfaceName  string

	GatewayIP,
	SrcIP,
	DestIP netip.Addr
	DestCIDR netip.Prefix

	IsIPv6            bool
	IsInterfaceScoped bool
}

func (*Route) String

func (r *Route) String() string

type RouteManager

type RouteManager interface {
	GetDefaultInterface() (RoutableInterface, error)
	GetRoutableInterface(ifaceName string) (RoutableInterface, error)
	NewRoutableInterface(ifaceName, tunAddress string) (RoutableInterface, error)

	NewFilterRouter(denyAll bool) (FilterRouter, error)

	AddExternalRouteToIPs(ips []string) error
	AddDefaultRoute(gateway string) error

	Clear()
}

type SecurityGroup

type SecurityGroup struct {
	Deny bool // default to allow

	SrcNetwork,
	DstNetwork netip.Prefix

	Oifname string

	Ports []PortGroup
}

func (SecurityGroup) CreateSecurityGroupKeys

func (sg SecurityGroup) CreateSecurityGroupKeys(iifName string) (string, []string, error)

Jump to

Keyboard shortcuts

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