netenv

package
v1.6.28 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: GPL-3.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModuleName               = "netenv"
	NetworkChangedEvent      = "network changed"
	OnlineStatusChangedEvent = "online status changed"
)

Event Names.

Variables

View Source
var (
	PortalTestIP  = net.IPv4(192, 0, 2, 1)
	PortalTestURL = fmt.Sprintf("http://%s/", PortalTestIP)

	// IP address -> 100.127.247.245 is a special ip used by the android VPN service. Must be ignored during online check.
	IgnoreIPsInOnlineStatusCheck = []net.IP{net.IPv4(100, 127, 247, 245)}

	DNSTestDomain     = "online-check.safing.io."
	DNSTestExpectedIP = net.IPv4(0, 65, 67, 75) // Ascii: \0ACK
	DNSTestQueryFunc  func(ctx context.Context, fdqn string) (ips []net.IP, ok bool, err error)

	ConnectedToSPN = abool.New()
	ConnectedToDNS = abool.New()

	// SpecialCaptivePortalDomain is the domain name used to point to the detected captive portal IP
	// or the captive portal test IP. The default value should be overridden by the resolver package,
	// which defines the custom internal domain name to use.
	SpecialCaptivePortalDomain = "captiveportal.invalid."

	// ConnectivityDomains holds all connectivity domains. This slice must not be modified.
	ConnectivityDomains = []string{
		SpecialCaptivePortalDomain,

		"dns.msftncsi.com.",
		"msftncsi.com.",
		"www.msftncsi.com.",
		"microsoftconnecttest.com.",
		"www.microsoftconnecttest.com.",
		"ipv6.microsoftconnecttest.com.",

		"captive.apple.com.",

		"connectivity-check.ubuntu.com.",
		"nmcheck.gnome.org.",
		"network-test.debian.org.",
		"204.pop-os.org.",
		"conncheck.opensuse.org.",
		"ping.archlinux.org",

		"connectivitycheck.gstatic.com.",

		"neverssl.com.",
		"detectportal.firefox.com.",
	}
)

Online Status and Resolver.

Functions

func Gateways

func Gateways() []net.IP

Gateways returns the currently active gateways.

func GetApproximateInternetLocation

func GetApproximateInternetLocation() (net.IP, error)

GetApproximateInternetLocation returns the approximate Internet location. Deprecated: Please use GetInternetLocation instead.

func GetAssignedAddresses

func GetAssignedAddresses() (ipv4 []net.IP, ipv6 []net.IP, err error)

GetAssignedAddresses returns the assigned IPv4 and IPv6 addresses of the host.

func GetAssignedGlobalAddresses

func GetAssignedGlobalAddresses() (ipv4 []net.IP, ipv6 []net.IP, err error)

GetAssignedGlobalAddresses returns the assigned global IPv4 and IPv6 addresses of the host.

func GetLocalNetwork

func GetLocalNetwork(ip net.IP) (myNet *net.IPNet, err error)

GetLocalNetwork uses the given IP to search for a network configured on the device and returns it.

func GetNetworkChangedFlag

func GetNetworkChangedFlag() *utils.Flag

GetNetworkChangedFlag returns a flag to be notified about a network change.

func IPv6Enabled

func IPv6Enabled() bool

IPv6Enabled returns whether the device has an active IPv6 stack. This is only checked once on startup in order to maintain consistency.

func IsConnectivityDomain

func IsConnectivityDomain(domain string) bool

IsConnectivityDomain checks whether the given domain (fqdn) is used for any connectivity related network connections and should always be resolved using the network assigned DNS server.

func IsMyIP

func IsMyIP(ip net.IP) (yes bool, err error)

IsMyIP returns whether the given unicast IP is currently configured on the local host. Broadcast or multicast addresses will never match, even if valid and in use. Function is optimized with the assumption that is likely that the IP is mine.

func ListenToICMP

func ListenToICMP(targetIP net.IP) (packets chan packet.Packet, done func())

ListenToICMP returns a new channel for listenting to icmp packets. Please note that any icmp packet will be passed and filtering must be done on the side of the caller. The caller must call the returned done function when done with the listener.

func Online

func Online() bool

Online returns true if online status is either SemiOnline or Online.

func ReportFailedConnection

func ReportFailedConnection()

ReportFailedConnection hints the online status monitoring system that a connection attempt has failed. This function has extremely low overhead and may be called as much as wanted.

func ReportSuccessfulConnection

func ReportSuccessfulConnection()

ReportSuccessfulConnection hints the online status monitoring system that a connection attempt was successful.

func SetLocalAddrFactory

func SetLocalAddrFactory(laf func(network string) net.Addr)

SetLocalAddrFactory supplies the environment package with a function to get permitted local addresses for connections.

func SubmitPacketToICMPListener

func SubmitPacketToICMPListener(pkt packet.Packet) (submitted bool)

SubmitPacketToICMPListener checks if an ICMP packet should be submitted to the listener. If so, it is submitted right away. The function returns whether or not the packet should be submitted, not if it was successful.

func TriggerNetworkChangeCheck

func TriggerNetworkChangeCheck()

TriggerNetworkChangeCheck triggers a network change check.

func TriggerOnlineStatusInvestigation

func TriggerOnlineStatusInvestigation()

TriggerOnlineStatusInvestigation manually triggers the online status check. It will not trigger it again, if it is already in progress.

Types

type CaptivePortal

type CaptivePortal struct {
	URL    string
	Domain string
	IP     net.IP
}

CaptivePortal holds information about a detected captive portal.

func GetCaptivePortal

func GetCaptivePortal() *CaptivePortal

GetCaptivePortal returns the current captive portal. The returned struct must not be edited.

type DeviceLocation

type DeviceLocation struct {
	IP             net.IP
	IPVersion      packet.IPVersion
	Location       *geoip.Location
	Source         DeviceLocationSource
	SourceAccuracy int
}

DeviceLocation represents a single IP and metadata. It must not be changed once created.

func SetInternetLocation

func SetInternetLocation(ip net.IP, source DeviceLocationSource) (dl *DeviceLocation, ok bool)

SetInternetLocation provides the location management system with a possible Internet location.

func (*DeviceLocation) IsMoreAccurateThan

func (dl *DeviceLocation) IsMoreAccurateThan(other *DeviceLocation) bool

IsMoreAccurateThan checks if the device location is more accurate than the given one.

func (*DeviceLocation) LocationOrNil

func (dl *DeviceLocation) LocationOrNil() *geoip.Location

LocationOrNil or returns the geoip location, or nil if not present.

func (*DeviceLocation) String

func (dl *DeviceLocation) String() string

type DeviceLocationSource

type DeviceLocationSource string

DeviceLocationSource is a location source.

const (
	SourceInterface  DeviceLocationSource = "interface"
	SourcePeer       DeviceLocationSource = "peer"
	SourceUPNP       DeviceLocationSource = "upnp"
	SourceTraceroute DeviceLocationSource = "traceroute"
	SourceTimezone   DeviceLocationSource = "timezone"
	SourceOther      DeviceLocationSource = "other"
)

Location Sources.

func (DeviceLocationSource) Accuracy

func (dls DeviceLocationSource) Accuracy() int

Accuracy returns the location accuracy of the source.

type DeviceLocations

type DeviceLocations struct {
	All []*DeviceLocation
}

DeviceLocations holds multiple device locations.

func GetInternetLocation

func GetInternetLocation() (deviceLocations *DeviceLocations, ok bool)

GetInternetLocation returns the possible device locations.

func (*DeviceLocations) AddIP

func (dls *DeviceLocations) AddIP(ip net.IP, source DeviceLocationSource) (dl *DeviceLocation, ok bool)

AddIP adds a new location based on the given IP.

func (*DeviceLocations) AddLocation

func (dls *DeviceLocations) AddLocation(dl *DeviceLocation)

AddLocation adds a location.

func (*DeviceLocations) Best

func (dls *DeviceLocations) Best() *DeviceLocation

Best returns the best (most accurate) device location.

func (*DeviceLocations) BestV4

func (dls *DeviceLocations) BestV4() *DeviceLocation

BestV4 returns the best (most accurate) IPv4 device location.

func (*DeviceLocations) BestV6

func (dls *DeviceLocations) BestV6() *DeviceLocation

BestV6 returns the best (most accurate) IPv6 device location.

func (*DeviceLocations) Copy

func (dls *DeviceLocations) Copy() *DeviceLocations

Copy creates a copy of the locations, but not the individual entries.

type Nameserver

type Nameserver struct {
	IP     net.IP
	Search []string
}

Nameserver describes a system assigned namserver.

func Nameservers

func Nameservers() []Nameserver

Nameservers returns the currently active nameservers.

type NetEnv added in v1.6.19

type NetEnv struct {
	EventNetworkChange      *mgr.EventMgr[struct{}]
	EventOnlineStatusChange *mgr.EventMgr[OnlineStatus]
	// contains filtered or unexported fields
}

func New added in v1.6.19

func New(instance instance) (*NetEnv, error)

New returns a new NetEnv module.

func (*NetEnv) Manager added in v1.6.19

func (ne *NetEnv) Manager() *mgr.Manager

func (*NetEnv) Start added in v1.6.19

func (ne *NetEnv) Start() error

func (*NetEnv) Stop added in v1.6.19

func (ne *NetEnv) Stop() error

type OnlineStatus

type OnlineStatus uint8

OnlineStatus represent a state of connectivity to the Internet.

const (
	StatusUnknown    OnlineStatus = 0
	StatusOffline    OnlineStatus = 1
	StatusLimited    OnlineStatus = 2 // local network only
	StatusPortal     OnlineStatus = 3 // there seems to be an internet connection, but we are being intercepted, possibly by a captive portal
	StatusSemiOnline OnlineStatus = 4 // we seem to online, but without full connectivity
	StatusOnline     OnlineStatus = 5
)

Online Status Values.

func CheckAndGetOnlineStatus

func CheckAndGetOnlineStatus() OnlineStatus

CheckAndGetOnlineStatus triggers a new online status check and returns the result.

func GetOnlineStatus

func GetOnlineStatus() OnlineStatus

GetOnlineStatus returns the current online stats.

func (OnlineStatus) String

func (os OnlineStatus) String() string

Jump to

Keyboard shortcuts

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