hostsfile

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2023 License: Apache-2.0 Imports: 11 Imported by: 11

README

Go library for working with a system's hostsfile

codecov

Usage

Using system default hosts file

hfile, err := hostsfile.NewHosts()

Using a custom hostsfile at a specific location

hfile, err := hostsfile.NewCustomHosts("./my-custom-hostsfile")

Add an ip entry with it's hosts

err := hfile.Add("192.168.1.1", "my-hostname", "another-hostname")

Remove an ip/host combination

err := hfile.Remove("192.168.1.1", "another-hostname")

Flush the hostfile changes back to disk

err := hfile.Flush()

Full API

type Hosts
func NewCustomHosts(osHostsFilePath string) (*Hosts, error)
    func NewHosts() (*Hosts, error)
    func (h *Hosts) Add(ip string, hosts ...string) error
    func (h *Hosts) AddRaw(raw ...string) error
    func (h *Hosts) Clean()
    func (h *Hosts) Clear()
    func (h *Hosts) Flush() error
    func (h *Hosts) Has(ip string, host string) bool
    func (h *Hosts) HasHostname(host string) bool
    func (h *Hosts) HasIp(ip string) bool
    func (h *Hosts) HostsPerLine(count int)
    func (h *Hosts) IsWritable() bool
    func (h *Hosts) Load() error
    func (h *Hosts) Remove(ip string, hosts ...string) error
    func (h *Hosts) RemoveByHostname(host string) error
    func (h *Hosts) RemoveByIp(ip string) error
    func (h *Hosts) RemoveDuplicateHosts()
    func (h *Hosts) RemoveDuplicateIps()
    func (h *Hosts) SortByIp()
    func (h *Hosts) SortHosts()
type HostsLine
func NewHostsLine(raw string) HostsLine
    func (l *HostsLine) Combine(hostline HostsLine)
    func (l *HostsLine) HasComment() bool
    func (l *HostsLine) IsComment() bool
    func (l *HostsLine) IsMalformed() bool
    func (l *HostsLine) IsValid() bool
    func (l *HostsLine) RegenRaw()
    func (l *HostsLine) RemoveDuplicateHosts()
    func (l *HostsLine) SortHosts()
    func (l *HostsLine) ToRaw() string

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HostsPerLine  = -1 // unlimited
	HostsFilePath = "/etc/hosts"
)

Functions

This section is empty.

Types

type Hosts

type Hosts struct {
	Path  string
	Lines []HostsLine
	// contains filtered or unexported fields
}

func NewCustomHosts

func NewCustomHosts(osHostsFilePath string) (*Hosts, error)

NewCustomHosts return a new instance of Hosts using a custom hosts file path.

func NewHosts

func NewHosts() (*Hosts, error)

NewHosts return a new instance of Hosts using the default hosts file path.

func (*Hosts) Add

func (h *Hosts) Add(ip string, hosts ...string) error

Add an entry to the hosts file.

func (*Hosts) AddRaw added in v0.1.0

func (h *Hosts) AddRaw(raw ...string) error

AddRaw takes a line from a hosts file and parses/adds the HostsLine

func (*Hosts) Clean added in v0.0.5

func (h *Hosts) Clean()

Clean merge duplicate ips and hosts per ip

func (*Hosts) Clear added in v0.1.0

func (h *Hosts) Clear()

func (*Hosts) Flush

func (h *Hosts) Flush() error

Flush any changes made to hosts file.

func (*Hosts) Has

func (h *Hosts) Has(ip string, host string) bool

Has return a bool if ip/host combo in hosts file.

func (*Hosts) HasHostname

func (h *Hosts) HasHostname(host string) bool

HasHostname return a bool if hostname in hosts file.

func (*Hosts) HasIp

func (h *Hosts) HasIp(ip string) bool

func (*Hosts) HostsPerLine added in v0.0.6

func (h *Hosts) HostsPerLine(count int)

func (*Hosts) IsWritable

func (h *Hosts) IsWritable() bool

IsWritable return true if hosts file is writable.

func (*Hosts) Load

func (h *Hosts) Load() error

Load the hosts file into l.Lines. Load() is called by NewHosts() and Hosts.Flush() so you generally you won't need to call this yourself.

func (*Hosts) Remove

func (h *Hosts) Remove(ip string, hosts ...string) error

Remove an entry from the hosts file.

func (*Hosts) RemoveByHostname

func (h *Hosts) RemoveByHostname(host string) error

RemoveByHostname remove entries by hostname from the hosts file.

func (*Hosts) RemoveByIp

func (h *Hosts) RemoveByIp(ip string) error

RemoveByIp this got refactored and wont return an error any more leaving it for stable api purposes, will be removed in a major release

func (*Hosts) RemoveDuplicateHosts added in v0.0.8

func (h *Hosts) RemoveDuplicateHosts()

func (*Hosts) RemoveDuplicateIps added in v0.0.5

func (h *Hosts) RemoveDuplicateIps()

func (*Hosts) SortByIp added in v0.0.6

func (h *Hosts) SortByIp()

SortByIp convert to net.IP and byte.Compare

func (*Hosts) SortHosts added in v0.0.8

func (h *Hosts) SortHosts()

func (*Hosts) String added in v0.1.2

func (h *Hosts) String() string

String get a string of the contents of the contents to put in the hosts file

type HostsLine

type HostsLine struct {
	IP      string
	Hosts   []string
	Raw     string
	Err     error
	Comment string
}

func NewHostsLine

func NewHostsLine(raw string) HostsLine

NewHostsLine return a new instance of HostsLine.

func (*HostsLine) Combine added in v0.0.5

func (l *HostsLine) Combine(hostline HostsLine)

func (*HostsLine) HasComment added in v0.0.3

func (l *HostsLine) HasComment() bool

func (*HostsLine) IsComment

func (l *HostsLine) IsComment() bool

func (*HostsLine) IsMalformed

func (l *HostsLine) IsMalformed() bool

func (*HostsLine) IsValid

func (l *HostsLine) IsValid() bool

func (*HostsLine) RegenRaw

func (l *HostsLine) RegenRaw()

func (*HostsLine) RemoveDuplicateHosts added in v0.0.5

func (l *HostsLine) RemoveDuplicateHosts()

func (*HostsLine) SortHosts added in v0.0.5

func (l *HostsLine) SortHosts()

func (*HostsLine) ToRaw added in v0.0.3

func (l *HostsLine) ToRaw() string

Jump to

Keyboard shortcuts

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