utility

package
v0.0.0-...-66aed2d Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: MPL-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const ADAPTER_PATTERN = `(?i)^answer\s+VNET_(?P<slot_number>\d+)_(?P<key>[a-z0-9_]+)\s+(?P<value>.+?)$`
View Source
const CERTIFICATE_EXPIRES_IN = 10

number of years until certificate expiry

View Source
const DHCP_RES_PATTERN = `(?i)^add_dhcp_mac_to_ip (?P<device>\d+)\s+(?P<mac>[^\s]+)\s+(?P<address>.+?)$`
View Source
const MACOS_END_PATTERN = `` // No time in lease
View Source
const MACOS_HOSTNAME_PATTERN = `(?im)name=(?P<hostname>[^\s]+)`
View Source
const MACOS_LEASE_PATTERN = `(?ims)^\{(?P<info>.+?ip_address=(?P<address>\d+.\d+.\d+.\d+).+?)\}`
View Source
const MACOS_MAC_PATTERN = `(?im)hw_address=.*,(?P<mac>[^\s]+)`
View Source
const MACOS_START_PATTERN = `` // No time in lease
View Source
const MACOS_TIME_FORMAT = `2006/01/02 15:04:05` // This isn't used, no time information in leases

Patterns used for macOS provided networking + dhcpd

View Source
const PORTFWD_PATTERN = `` /* 159-byte string literal not displayed */
View Source
const VMWARE_END_PATTERN = `(?im)^\s+ends\s+(?P<end_weekday>\d)\s+(?P<end_date>[^;]+);\s*`
View Source
const VMWARE_HOSTNAME_PATTERN = `(?im)^\s+client-hostname\s+"(?P<hostname>[^"]+)";`
View Source
const VMWARE_LEASE_FILE_PREFIX = "/var/db/vmware"

Path prefix for where VMware DHCP file is located

View Source
const VMWARE_LEASE_PATTERN = `(?ims)^\s*lease\s+(?P<address>\d+.\d+.\d+.\d+)\s*\{(?P<info>.+?)\}`
View Source
const VMWARE_MAC_PATTERN = `(?im)^\s+hardware\s+ethernet\s+(?P<mac>[^;]+);\s*`
View Source
const VMWARE_START_PATTERN = `(?im)^\s+starts\s+(?P<start_weekday>\d)\s+(?P<start_date>[^;]+);\s*`
View Source
const VMWARE_TIME_FORMAT = `2006/01/02 15:04:05`

Patterns used for VMware provided networking + dhcpd

Variables

This section is empty.

Functions

func DirectoryFor

func DirectoryFor(thing string) string

func Execute

func Execute(cmd *exec.Cmd) int

Runs the given command and returns the exit code.

func ExecuteWithOutput

func ExecuteWithOutput(cmd *exec.Cmd) (exitCode int, output string)

Runs the given command and returns combined stdout/stderr output

func FileExists

func FileExists(path string) bool

func GenerateCertificate

func GenerateCertificate() error

func GenerateConfigFile

func GenerateConfigFile(content interface{}) ([]byte, error)

func GetDarwinMajor

func GetDarwinMajor() (m int, err error)

func InstallDirectory

func InstallDirectory() string

func IsBigSurMin

func IsBigSurMin() bool

func IsRoot

func IsRoot() bool

Check if running as root user.

func MatchPattern

func MatchPattern(regexpPattern string, content string) (map[string]string, error)

func NamePatternResults

func NamePatternResults(matches, names []string) (result map[string]string)

func RootOwned

func RootOwned(checkPath string, andOperated bool) bool

Check if a given file is owned by the root user, with write access restricted to the root user, and optionally if the given path is executable by root.

func WriteConfigFile

func WriteConfigFile(file string, content interface{}) (err error)

Types

type CertificatePaths

type CertificatePaths struct {
	Certificate       string
	PrivateKey        string
	ClientCertificate string
	ClientKey         string
}

func GetCertificatePaths

func GetCertificatePaths() (*CertificatePaths, error)

Paths are based on platform. If the platform can't be detected then we just use the executable's directory as the base and create a certificate directory within.

type Device

type Device struct {
	Name            string
	Number          int
	Dhcp            bool
	Nat             bool
	HostonlyNetmask string
	HostonlySubnet  string
	VirtualAdapter  bool
}

type DhcpEntry

type DhcpEntry struct {
	Address  string
	Mac      string
	Hostname string
	Created  time.Time
	Expires  time.Time
}

func (*DhcpEntry) NormalizeMac

func (d *DhcpEntry) NormalizeMac()

type DhcpLeaseFile

type DhcpLeaseFile struct {
	Path    string
	Entries []*DhcpEntry
	// contains filtered or unexported fields
}

func LoadDhcpLeaseFile

func LoadDhcpLeaseFile(path string, logger hclog.Logger) (leaseFile *DhcpLeaseFile, err error)

func (*DhcpLeaseFile) AddEntry

func (d *DhcpLeaseFile) AddEntry(entry *DhcpEntry) error

func (*DhcpLeaseFile) IpForMac

func (d *DhcpLeaseFile) IpForMac(mac string) (string, error)

func (*DhcpLeaseFile) Load

func (d *DhcpLeaseFile) Load() error

type DhcpReservation

type DhcpReservation struct {
	Mac     string
	Address string
	Device  int
}

type InterfacesGetter

type InterfacesGetter func() ([]NetworkInterface, error)

type NatEntry

type NatEntry struct {
	Key   string
	Value string
}

func (*NatEntry) Match

func (e *NatEntry) Match(key string) bool

Check if entry matches given configuration key

type NatFile

type NatFile interface {
	Load() error
	Save() error
	GetSection(name string) NatSection
}

type NatFileMock

type NatFileMock struct {
	Path     string
	Sections []*NatSection
}

func LoadNatFileMock

func LoadNatFileMock(path string, sections []*NatSection) *NatFileMock

func (*NatFileMock) GetSection

func (n *NatFileMock) GetSection(name string) *NatSection

func (*NatFileMock) Load

func (n *NatFileMock) Load() error

func (*NatFileMock) Save

func (n *NatFileMock) Save() error

type NatSection

type NatSection struct {
	Name    string
	Entries []*NatEntry
}

func (*NatSection) DeleteEntry

func (s *NatSection) DeleteEntry(entry *NatEntry) error

Remove NAT entry from NAT section

func (*NatSection) DeleteEntryAt

func (s *NatSection) DeleteEntryAt(idx int) error

Remove NAT entry from NAT section by index

type NetworkInterface

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

func (*NetworkInterface) Addrs

func (ni *NetworkInterface) Addrs() []net.Addr

func (*NetworkInterface) Name

func (ni *NetworkInterface) Name() string

type NetworkingFile

type NetworkingFile interface {
	AddDhcpReservation(device int, mac, address string) error
	AddPortFwd(fwd *PortFwd) error
	CreateDevice(params ...string) *Device
	GetDeviceByName(devName string) (foundDevice *Device)
	GetDeviceBySlot(slotNumber int) (foundDevice *Device)
	GetPath() string
	GetPortFwds() []*PortFwd
	GetDevices() []*Device
	HostPortFwd(port int, protocol string) *PortFwd
	Load() error
	LookupDhcpReservation(device int, mac string) (addr string, err error)
	Merge(netF NetworkingFile) error
	MergeFwds(fwds []*PortFwd) error
	PortfwdExists(fwd *PortFwd) bool
	RemovePortFwd(fwd *PortFwd) error
	RemoveDeviceByName(devName string) error
	RemoveDeviceBySlot(slotNumber int) error
	Save() (string, error)
}

type NetworkingFileMock

type NetworkingFileMock struct {
	Path             string
	Devices          []*Device
	DhcpReservations []*DhcpReservation
	PortFwds         []*PortFwd
}

func LoadNetworkingFileMock

func LoadNetworkingFileMock(path string, devices []*Device, dhcp []*DhcpReservation, portfwds []*PortFwd) (*NetworkingFileMock, error)

Loads the networking file at the given path and parses the network adapters defined within the file

func (*NetworkingFileMock) AddDhcpReservation

func (n *NetworkingFileMock) AddDhcpReservation(device int, mac, address string) error

func (*NetworkingFileMock) AddPortFwd

func (n *NetworkingFileMock) AddPortFwd(fwd *PortFwd) error

Add a new port forward

func (*NetworkingFileMock) CreateDevice

func (n *NetworkingFileMock) CreateDevice(params ...string) *Device

func (*NetworkingFileMock) GetDeviceByName

func (n *NetworkingFileMock) GetDeviceByName(devName string) (foundDevice *Device)

Find defined adapter with given name

func (*NetworkingFileMock) GetDeviceBySlot

func (n *NetworkingFileMock) GetDeviceBySlot(slotNumber int) (foundDevice *Device)

Find defined adapter with given slot number

func (*NetworkingFileMock) GetDevices

func (n *NetworkingFileMock) GetDevices() []*Device

func (*NetworkingFileMock) GetPath

func (n *NetworkingFileMock) GetPath() string

func (*NetworkingFileMock) GetPortFwds

func (n *NetworkingFileMock) GetPortFwds() []*PortFwd

func (*NetworkingFileMock) HostPortFwd

func (n *NetworkingFileMock) HostPortFwd(port int, protocol string) *PortFwd

func (*NetworkingFileMock) Load

func (n *NetworkingFileMock) Load() error

func (*NetworkingFileMock) LookupDhcpReservation

func (n *NetworkingFileMock) LookupDhcpReservation(device int, mac string) (addr string, err error)

func (*NetworkingFileMock) Merge

func (n *NetworkingFileMock) Merge(netF NetworkingFile) error

func (*NetworkingFileMock) MergeFwds

func (n *NetworkingFileMock) MergeFwds(fwds []*PortFwd) error

func (*NetworkingFileMock) PortfwdExists

func (n *NetworkingFileMock) PortfwdExists(fwd *PortFwd) bool

func (*NetworkingFileMock) RemoveDeviceByName

func (n *NetworkingFileMock) RemoveDeviceByName(devName string) error

Remove a defined adapter with given name

func (*NetworkingFileMock) RemoveDeviceBySlot

func (n *NetworkingFileMock) RemoveDeviceBySlot(slotNumber int) error

Remove a defined adapter with given slot number

func (*NetworkingFileMock) RemovePortFwd

func (n *NetworkingFileMock) RemovePortFwd(fwd *PortFwd) error

Remove a port forward

func (*NetworkingFileMock) Save

func (n *NetworkingFileMock) Save() (string, error)

type PortFwd

type PortFwd struct {
	Enable      bool   `json:"enable"`
	Device      string `json:"device"`
	Protocol    string `json:"protocol"`
	HostPort    int    `json:"hostport"`
	GuestIp     string `json:"guestip"`
	GuestPort   int    `json:"guestport"`
	Description string `json:"description"`
}

type RoutingDevice

type RoutingDevice struct {
	Name    string
	Address net.IP
	Netmask net.IPMask
}

func (*RoutingDevice) Match

func (r *RoutingDevice) Match(network string) bool

type RoutingTable

type RoutingTable struct {
	Devices []*RoutingDevice
	// contains filtered or unexported fields
}

func LoadRoutingTable

func LoadRoutingTable(igetter InterfacesGetter, logger hclog.Logger) (table *RoutingTable, err error)

func (*RoutingTable) DeviceByName

func (r *RoutingTable) DeviceByName(name string) *RoutingDevice

func (*RoutingTable) Load

func (r *RoutingTable) Load() error

type VMWareNatFile

type VMWareNatFile struct {
	Path     string
	Sections []*NatSection
	// contains filtered or unexported fields
}

func LoadNatFile

func LoadNatFile(path string, logger hclog.Logger) (*VMWareNatFile, error)

Load and parse the nat.conf file

func (*VMWareNatFile) GetSection

func (n *VMWareNatFile) GetSection(name string) *NatSection

Get NAT configuration section by name

func (*VMWareNatFile) Load

func (n *VMWareNatFile) Load() error

Read and parse nat.conf file

func (*VMWareNatFile) Save

func (n *VMWareNatFile) Save() error

Save nat.conf file

type VMWareNetworkingFile

type VMWareNetworkingFile struct {
	Path             string
	Devices          []*Device
	DhcpReservations []*DhcpReservation
	PortFwds         []*PortFwd
	// contains filtered or unexported fields
}

func LoadNetworkingFile

func LoadNetworkingFile(path string, logger hclog.Logger) (*VMWareNetworkingFile, error)

Loads the networking file at the given path and parses the network adapters defined within the file

func (*VMWareNetworkingFile) AddDhcpReservation

func (n *VMWareNetworkingFile) AddDhcpReservation(device int, mac, address string) error

Add a new DHCP reservation

func (*VMWareNetworkingFile) AddPortFwd

func (n *VMWareNetworkingFile) AddPortFwd(fwd *PortFwd) error

Add a new port forward

func (*VMWareNetworkingFile) CreateDevice

func (n *VMWareNetworkingFile) CreateDevice(params ...string) *Device

Creates a new virtual network adapter. If the adapter should be host only two parameters can be provided to configure: HostonlyNetmask - Ex: "255.255.255.0" HostonlySubnet - Ex: "172.32.3.0"

func (*VMWareNetworkingFile) GetDeviceByName

func (n *VMWareNetworkingFile) GetDeviceByName(devName string) (foundDevice *Device)

Find defined adapter with given name

func (*VMWareNetworkingFile) GetDeviceBySlot

func (n *VMWareNetworkingFile) GetDeviceBySlot(slotNumber int) (foundDevice *Device)

Find defined adapter with given slot number

func (*VMWareNetworkingFile) GetDevices

func (n *VMWareNetworkingFile) GetDevices() []*Device

func (*VMWareNetworkingFile) GetPath

func (n *VMWareNetworkingFile) GetPath() string

func (*VMWareNetworkingFile) GetPortFwds

func (n *VMWareNetworkingFile) GetPortFwds() []*PortFwd

func (*VMWareNetworkingFile) HostPortFwd

func (n *VMWareNetworkingFile) HostPortFwd(port int, protocol string) *PortFwd

func (*VMWareNetworkingFile) Load

func (n *VMWareNetworkingFile) Load() error

Parses the defined networking configuration file. This can also be used to "reload" if the file may have changed.

func (*VMWareNetworkingFile) LookupDhcpReservation

func (n *VMWareNetworkingFile) LookupDhcpReservation(device int, mac string) (addr string, err error)

Lookup a DHCP reservation

func (*VMWareNetworkingFile) Merge

func (n *VMWareNetworkingFile) Merge(netF NetworkingFile) error

This merges a networking file into the current networking file. Useful in places like Linux with Workstation where we lose all metadata within the actual networking file when settings are updated. NOTE: This only merges in port forward directives

func (*VMWareNetworkingFile) MergeFwds

func (n *VMWareNetworkingFile) MergeFwds(fwds []*PortFwd) error

func (*VMWareNetworkingFile) PortfwdExists

func (n *VMWareNetworkingFile) PortfwdExists(fwd *PortFwd) bool

Check if a port forward has already been added

func (*VMWareNetworkingFile) RemoveDeviceByName

func (n *VMWareNetworkingFile) RemoveDeviceByName(devName string) error

Remove a defined adapter with given name

func (*VMWareNetworkingFile) RemoveDeviceBySlot

func (n *VMWareNetworkingFile) RemoveDeviceBySlot(slotNumber int) error

Remove a defined adapter with given slot number

func (*VMWareNetworkingFile) RemovePortFwd

func (n *VMWareNetworkingFile) RemovePortFwd(fwd *PortFwd) error

Remove a port forward

func (*VMWareNetworkingFile) Save

func (n *VMWareNetworkingFile) Save() (string, error)

Saves the current configuration to the networking file at defined path

type VmwarePaths

type VmwarePaths struct {
	BridgePid    string `json:"bridge_pid"`
	DhcpLease    string `json:"dhcp_lease"`
	InstallDir   string `json:"install_dir"`
	NatConf      string `json:"nat_conf"`
	Networking   string `json:"networking"`
	Services     string `json:"services"`
	VmnetCli     string `json:"vmnet_cli"`
	Vnetlib      string `json:"vnetlib"`
	Vmx          string `json:"vmx"`
	Vmrun        string `json:"vmrun"`
	Vmrest       string `json:"vmrest"`
	Vdiskmanager string `json:"vdiskmanager"`
	// contains filtered or unexported fields
}

func LoadVmwarePaths

func LoadVmwarePaths(logger hclog.Logger) (*VmwarePaths, error)

func (*VmwarePaths) DhcpLeaseFile

func (v *VmwarePaths) DhcpLeaseFile(device string) string

func (*VmwarePaths) Load

func (v *VmwarePaths) Load() (err error)

func (*VmwarePaths) NatConfFile

func (v *VmwarePaths) NatConfFile(device string) string

func (*VmwarePaths) UpdateVmwareDhcpLeasePath

func (v *VmwarePaths) UpdateVmwareDhcpLeasePath(version string) error

Jump to

Keyboard shortcuts

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