util

package
v5.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParseCIDREmptyString = fmt.Errorf("unable to parse CIDR as subnet, empty string")
	ErrParseCIDRInvalidIP   = fmt.Errorf("unable to parse CIDR as subnet, invalid IP address")
	ErrParseCIDRInvalidMask = fmt.Errorf("unable to parse CIDR as subnet, invalid mask")

	ErrIPIsNil = fmt.Errorf("ip is nil")
)
View Source
var (
	PublicNetworkUUID         = uuid.MustParse("ffffffff-ffff-ffff-ffff-ffffffffffff")
	PublicNetworkName         = "Public"
	UnknownPrivateNetworkUUID = uuid.MustParse("ffffffff-ffff-ffff-ffff-fffffffffffe")
	UnknownPrivateNetworkName = "Unknown Private"

	ErrFileSystemIsNil = errors.New("filesystem is nil")
	ErrInvalidPath     = errors.New("path cannot be empty string")

	ErrFileDoesNotExist = errors.New("file does not exist")
	ErrFileIsEmtpy      = errors.New("file is empty")
	ErrPathIsDir        = errors.New("given path is a directory, not a file")

	ErrDirDoesNotExist = errors.New("directory does not exist")
	ErrDirIsEmpty      = errors.New("directory is empty")
	ErrPathIsNotDir    = errors.New("given path is not a directory")

	ErrFetchingLatestRelease = errors.New("error fetching latest release")
	ErrParsingCurrentVersion = errors.New("error parsing current version")
	ErrParsingLatestVersion  = errors.New("error parsing latest version")
)

Functions

func CheckForNewerVersion

func CheckForNewerVersion(client *github.Client, currentVersion string) (bool, string, error)

CheckForNewerVersion checks if a newer version of the project is available on the GitHub repository

func ContainsDomain

func ContainsDomain(domains []string, host string) bool

ContainsDomain checks if a given host is in a list of domains

func ContainsIP

func ContainsIP(subnets []Subnet, ip net.IP) bool

ContainsIP checks if a collection of subnets contains an IP

func GetFileContents added in v5.1.0

func GetFileContents(afs afero.Fs, path string) ([]byte, error)

GetFileContents reads the contents of a file at the specified path

func GetLatestReleaseVersion added in v5.0.5

func GetLatestReleaseVersion(client *github.Client, owner, repo string) (string, error)

GetLatestReleaseVersion gets the latest release version from the GitHub repository

func GetRelativeFirstSeenTimestamp

func GetRelativeFirstSeenTimestamp(useCurrentTime bool, maxTimestamp time.Time) time.Time

GetRelativeFirstSeenTimestamp returns the timestamp to use for first seen calculation/display. This is a shortcut for a commonly used if statement

func IPIsPubliclyRoutable

func IPIsPubliclyRoutable(ip net.IP) bool

IPIsPubliclyRoutable checks if an IP address is publicly routable

func ParseNetworkID

func ParseNetworkID(ip net.IP, agentID string) uuid.UUID

ParseNetworkID returns the network ID for a given IP address and agent ID

func ParseRelativePath

func ParseRelativePath(dir string) (string, error)

ParseRelativePath parses a given directory path and returns the absolute path

func SortUInt32s

func SortUInt32s(data []uint32)

SortUInt32s sorts a slice of uint32 in ascending order

func UInt32sAreSorted

func UInt32sAreSorted(data []uint32) bool

UInt32sAreSorted returns true if a slice of uint32 is sorted in ascending order

func ValidFQDN

func ValidFQDN(value string) bool

func ValidateDirectory

func ValidateDirectory(afs afero.Fs, dir string) error

ValidateDirectory returns whether a directory exists and is empty

func ValidateFile

func ValidateFile(afs afero.Fs, file string) error

Validate File

func ValidateTimestamp

func ValidateTimestamp(timestamp time.Time) (time.Time, bool)

Types

type FixedString

type FixedString struct {
	Data [16]byte
	// contains filtered or unexported fields
}

func NewFixedStringFromHex

func NewFixedStringFromHex(h string) (FixedString, error)

NewFixedStringFromString creates a FixedString from a passed in hex string

func NewFixedStringHash

func NewFixedStringHash(args ...string) (FixedString, error)

NewFixedStringHash creates a FixedString from a hash of all the passed in strings

func (*FixedString) Hex

func (bin *FixedString) Hex() string

func (FixedString) MarshalBinary

func (bin FixedString) MarshalBinary() ([]byte, error)

Returns expected type for writing to the database

func (*FixedString) UnmarshalBinary

func (bin *FixedString) UnmarshalBinary(b []byte) error

Returns expected type for reading from the database

func (FixedString) Value

func (bin FixedString) Value() (driver.Value, error)

Returns value of FixedString as a pointer, used when sometimes writing to database

type Subnet added in v5.1.0

type Subnet struct {
	*net.IPNet
}

func CompactSubnets added in v5.1.0

func CompactSubnets(subnets []Subnet) []Subnet

CompactSubnets removes duplicate Subnets from a given slice

func IncludeMandatorySubnets added in v5.1.0

func IncludeMandatorySubnets(data []Subnet, mandatory []Subnet) []Subnet

IncludeMandatorySubnets ensures that a given slice of Subnets contains all elements of a mandatory list

func NewSubnet added in v5.1.0

func NewSubnet(ipNet *net.IPNet) Subnet

NewSubnet creates a new Subnet struct from an IPNet pointer

func NewSubnetList added in v5.1.0

func NewSubnetList(subnets []string) ([]Subnet, error)

NewSubnetList creates a list of Subnet structs from a list of strings

func NewTestSubnetList added in v5.1.0

func NewTestSubnetList(t *testing.T, subnets []string) []Subnet

NewTestSubnetList creates a list of Subnet structs from a list of strings, but asserts no error using testing library (used for tests in other packages too, so it must be in the non-test file)

func ParseSubnet added in v5.1.0

func ParseSubnet(str string) (Subnet, error)

ParseSubnet parses a CIDR string into a Subnet struct, which is formatted as IPv6 It supports both IPv4 and IPv6 CIDRs, as well as IPv4 in IPv6 CIDRs

func (*Subnet) MarshalJSON added in v5.1.0

func (s *Subnet) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Subnet struct into JSON bytes The IP is formatted into its most human readable format. If the CIDR mask is full/max (128 or 32), then it is not displayed. For IPv4, the address is specifically formatted to not display IPv4 in IPv6.

func (*Subnet) Scan added in v5.1.0

func (s *Subnet) Scan(src any) error

Scan implements the sql.Scanner interface for the Subnet struct; allows for scanning a Subnet struct from a database query

func (*Subnet) ToIPString added in v5.1.0

func (s *Subnet) ToIPString() (string, error)

ToIPString gets string representation of the IP address in the Subnet struct The CIDR is always included. IPv4 is always formatted as IPv4 in IPv6.

func (*Subnet) ToIPv6Notation added in v5.1.0

func (s *Subnet) ToIPv6Notation()

func (*Subnet) ToString added in v5.1.0

func (s *Subnet) ToString() string

ToString converts the Subnet struct to a proper string representation of the CIDR

func (*Subnet) UnmarshalJSON added in v5.1.0

func (s *Subnet) UnmarshalJSON(bytes []byte) error

UnmarshalJSON unmarshals the JSON bytes into the IPNet struct overrides the default unmarshalling method to allow for custom parsing

Jump to

Keyboard shortcuts

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