lib127

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lib127 provides methods for mapping hostnames to random loopback addresses.

Index

Constants

View Source
const DefaultHostsFile = "/etc/hosts"

DefaultHostsFile is the default hosts file location.

Variables

View Source
var (
	// ErrHostnameInvalid indicates an invalid hostname.
	ErrHostnameInvalid = hosts.ErrHostnameInvalid

	// ErrHostnameIsIP indicates that an IP address was given as hostname.
	ErrHostnameIsIP = hosts.ErrHostnameIsIP

	// ErrCannotUnmapLocalhost indicates a request to unmap localhost.
	ErrCannotUnmapLocalhost = errors.New("127: cannot unmap localhost")
)

These errors can be tested against using errors.Is. They are never returned directly.

Functions

This section is empty.

Types

type Hosts

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

Hosts provide methods for mapping hostnames to random IP addresses. Its zero value is usable and operates on the default hosts file.

func Open added in v0.5.0

func Open(filename string) (*Hosts, error)

NewHosts opens a new Hosts using the given file. If filename is "" the default hosts file is opened.

Returned file system errors wrap *fs.PathError.

func (*Hosts) IP added in v0.5.0

func (h *Hosts) IP(hostname string) (string, error)

IP returns the IP address associated with the specified hostname. Returns an empty string if hostname were not found.

Returned hostname errors can be matched against ErrInvalidHostname and ErrHostnameIsIP.

func (*Hosts) Map added in v0.5.0

func (h *Hosts) Map(hostname string) (string, error)

Map maps the specified hostname to a random unnasigned loopback address, and returns that IP. If the hostname is already mapped, we return the already assigned IP address instead.

Returned hostname errors can be matched against ErrInvalidHostname and ErrHostnameIsIP.

func (*Hosts) RandomIP

func (h *Hosts) RandomIP() (string, error)

RandomIP returns a random unassigned loopback address.

func (*Hosts) Save added in v0.5.0

func (h *Hosts) Save() error

Save saves the modified hosts file to disk. Does nothing if no changes were made.

Returned file system errors wrap *fs.PathError.

func (*Hosts) Unmap added in v0.5.0

func (h *Hosts) Unmap(hostname string) (string, error)

Unmap unmaps the specified hostname and returns the associated IP. Returns an empty string if hostname were not found.

Returned hostname errors can be matched against ErrInvalidHostname and ErrHostnameIsIP.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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