Documentation
¶
Overview ¶
Package txeh provides /etc/hosts file management capabilities. It offers a simple interface for adding, removing, and querying hostname-to-IP mappings with thread-safe operations and cross-platform support (Linux, macOS, Windows).
Index ¶
- Constants
- Variables
- func ExecCommandFunc() func(string, ...string) *exec.Cmd
- func FlushDNSCache() error
- func SetExecCommandFunc(fn func(string, ...string) *exec.Cmd)
- type AddressLocations
- type FlushError
- type HostFileLine
- type HostFileLines
- type HostLocations
- type Hosts
- func (h *Hosts) AddHost(addressRaw, hostRaw string)
- func (h *Hosts) AddHostWithComment(addressRaw, hostRaw, comment string)
- func (h *Hosts) AddHosts(address string, hosts []string)
- func (h *Hosts) AddHostsWithComment(address string, hosts []string, comment string)
- func (h *Hosts) GetHostFileLines() HostFileLines
- func (h *Hosts) HostAddressLookup(host string, ipFamily IPFamily) (found bool, address string, idx int)
- func (h *Hosts) ListAddressesByHost(hostname string, exact bool) [][]string
- func (h *Hosts) ListHostsByCIDR(cidr string) [][]string
- func (h *Hosts) ListHostsByComment(comment string) []string
- func (h *Hosts) ListHostsByIP(address string) []string
- func (h *Hosts) Reload() error
- func (h *Hosts) RemoveAddress(address string)
- func (h *Hosts) RemoveAddresses(addresses []string)
- func (h *Hosts) RemoveByComment(comment string)
- func (h *Hosts) RemoveByComments(comments []string)
- func (h *Hosts) RemoveCIDRs(cidrs []string) error
- func (h *Hosts) RemoveFirstAddress(address string) bool
- func (h *Hosts) RemoveFirstHost(host string) bool
- func (h *Hosts) RemoveHost(host string)
- func (h *Hosts) RemoveHosts(hosts []string)
- func (h *Hosts) RenderHostsFile() string
- func (h *Hosts) Save() error
- func (h *Hosts) SaveAs(fileName string) error
- type HostsConfig
- type IPFamily
Constants ¶
const ( UNKNOWN = 0 // Unknown line type. EMPTY = 10 // Empty line. COMMENT = 20 // Comment line starting with #. ADDRESS = 30 // Address line with IP and hostnames. )
Line type constants for HostFileLine.
const DefaultMaxHostsPerLineWindows = 9
DefaultMaxHostsPerLineWindows is the default maximum number of hostnames per line on Windows. Windows has a limitation where lines with more than ~9 hostnames may not resolve correctly.
Variables ¶
var ErrNoResolver = errors.New(
"systemd-resolved not detected (tried resolvectl, systemd-resolve). " +
"If your system does not cache DNS locally, hosts file changes take effect immediately without flushing",
)
ErrNoResolver is returned when no supported DNS resolver is found on the system. If your system does not cache DNS locally (e.g., no systemd-resolved, dnsmasq, or unbound), hosts file changes take effect immediately without flushing.
Functions ¶
func ExecCommandFunc ¶ added in v1.8.0
ExecCommandFunc returns the current exec command function. This is intended for test code that needs to save and restore the original.
func FlushDNSCache ¶ added in v1.8.0
func FlushDNSCache() error
FlushDNSCache flushes the operating system's DNS cache.
Platform commands (all are OS-provided utilities, not installable dependencies):
- macOS: dscacheutil -flushcache + killall -HUP mDNSResponder (ships with macOS)
- Linux: resolvectl flush-caches (systemd 239+) or systemd-resolve --flush-caches (older systemd). Returns ErrNoResolver if neither binary is found, which typically means the system does not cache DNS locally and hosts file changes take effect immediately.
- Windows: ipconfig /flushdns (ships with Windows)
Returns a *FlushError on failure, or nil on unsupported platforms where no resolver is detected.
Types ¶
type AddressLocations ¶
AddressLocations maps an address to its location in the HFL.
type FlushError ¶ added in v1.8.0
FlushError represents a DNS cache flush failure with platform context.
func (*FlushError) Error ¶ added in v1.8.0
func (e *FlushError) Error() string
Error returns a human-readable error message.
func (*FlushError) Unwrap ¶ added in v1.8.0
func (e *FlushError) Unwrap() error
Unwrap returns the underlying error.
type HostFileLine ¶
type HostFileLine struct {
OriginalLineNum int
LineType int
Address string
Parts []string
Hostnames []string
Raw string
Trimmed string
Comment string
}
HostFileLine represents a single line in a hosts file.
func ParseHosts ¶
func ParseHosts(path string) ([]HostFileLine, error)
ParseHosts reads and parses a hosts file from the given path.
func ParseHostsFromString ¶ added in v1.5.3
func ParseHostsFromString(input string) ([]HostFileLine, error)
ParseHostsFromString parses hosts file content from a string.
type HostFileLines ¶
type HostFileLines []HostFileLine
HostFileLines is a slice of HostFileLine entries.
type HostLocations ¶
HostLocations maps a hostname to an original line number.
type Hosts ¶
type Hosts struct {
*HostsConfig
// contains filtered or unexported fields
}
Hosts represents a parsed hosts file with thread-safe operations.
func NewHostsDefault ¶
NewHostsDefault returns a hosts object with default configuration.
func (*Hosts) AddHost ¶
AddHost adds a host to an address and removes the host from any existing address it may be associated with.
func (*Hosts) AddHostWithComment ¶ added in v1.7.0
AddHostWithComment adds a host to an address with an inline comment. The comment will appear after the hostnames on the line (e.g., "127.0.0.1 host # comment"). If the address already exists with the same comment, the host is appended to that line (respecting MaxHostsPerLine). If the address exists with a different comment, a new line is created with the specified comment.
func (*Hosts) AddHosts ¶
AddHosts adds an array of hosts to the first matching address it finds or creates the address and adds the hosts.
func (*Hosts) AddHostsWithComment ¶ added in v1.7.0
AddHostsWithComment adds an array of hosts to an address with a comment. All hosts will share the same comment. If the address already exists with the same comment, hosts are appended to that line (respecting MaxHostsPerLine). If the address exists with a different comment, a new line is created.
func (*Hosts) GetHostFileLines ¶
func (h *Hosts) GetHostFileLines() HostFileLines
GetHostFileLines returns a copy of all parsed host file lines.
func (*Hosts) HostAddressLookup ¶
func (h *Hosts) HostAddressLookup(host string, ipFamily IPFamily) (found bool, address string, idx int)
HostAddressLookup returns true if the host is found, the address string, and the index of the host file line. This is part of the public API for consumers that need direct address lookups by IP family.
func (*Hosts) ListAddressesByHost ¶ added in v1.5.2
ListAddressesByHost returns a list of IPs associated with a given hostname.
func (*Hosts) ListHostsByCIDR ¶ added in v1.5.2
ListHostsByCIDR returns a list of IPs and hostnames associated with a given CIDR.
func (*Hosts) ListHostsByComment ¶ added in v1.7.0
ListHostsByComment returns all hostnames on lines with the given comment.
func (*Hosts) ListHostsByIP ¶ added in v1.5.2
ListHostsByIP returns a list of hostnames associated with a given IP address.
func (*Hosts) Reload ¶
Reload re-reads the hosts file from disk and replaces the in-memory state. This is part of the public API for consumers who manage long-lived Hosts instances.
func (*Hosts) RemoveAddress ¶
RemoveAddress removes all entries (lines) with the provided address.
func (*Hosts) RemoveAddresses ¶
RemoveAddresses removes all entries (lines) with the provided address.
func (*Hosts) RemoveByComment ¶ added in v1.7.0
RemoveByComment removes all host entries that have the specified comment. This removes entire lines where the comment matches.
func (*Hosts) RemoveByComments ¶ added in v1.7.0
RemoveByComments removes all host entries that have any of the specified comments. This removes entire lines where the comment matches. This is part of the public API for bulk comment-based cleanup (e.g., kubefwd teardown).
func (*Hosts) RemoveCIDRs ¶ added in v1.5.4
RemoveCIDRs Remove CIDR Range (Classless inter-domain routing) examples:
127.1.0.0/16 = 127.1.0.0 -> 127.1.255.255 127.1.27.0/24 = 127.1.27.0 -> 127.1.27.255
func (*Hosts) RemoveFirstAddress ¶
RemoveFirstAddress removes the first entry (line) found with the provided address.
func (*Hosts) RemoveFirstHost ¶
RemoveFirstHost removes the first hostname entry found and returns true if successful.
func (*Hosts) RemoveHost ¶
RemoveHost removes all hostname entries of provided host.
func (*Hosts) RemoveHosts ¶
RemoveHosts removes all hostname entries of the provided host slice.
func (*Hosts) RenderHostsFile ¶
RenderHostsFile returns the hosts file content as a formatted string.
type HostsConfig ¶
type HostsConfig struct {
ReadFilePath string
WriteFilePath string
// RawText for input. If RawText is set ReadFilePath, WriteFilePath are ignored. Use RenderHostsFile rather
// than save to get the results.
RawText *string
// MaxHostsPerLine limits the number of hostnames per line when adding hosts.
// This is useful for Windows which has a limitation of ~9 hostnames per line.
// Values:
// 0 = auto-detect (Windows: 9, others: unlimited)
// -1 = force unlimited (no limit)
// >0 = explicit limit
MaxHostsPerLine int
// AutoFlush triggers a DNS cache flush after every successful Save/SaveAs.
// Flush failures are returned as *FlushError, distinguishable via errors.As.
AutoFlush bool
}
HostsConfig contains configuration for reading and writing hosts files.
