2015-02-24 08:55:24 +00:00
|
|
|
package hostess
|
|
|
|
|
2015-02-24 12:17:31 +00:00
|
|
|
import (
|
2015-02-25 10:33:18 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-02-24 12:17:31 +00:00
|
|
|
"net"
|
2015-03-02 07:15:02 +00:00
|
|
|
"sort"
|
2015-02-24 12:17:31 +00:00
|
|
|
)
|
|
|
|
|
2015-03-02 08:22:05 +00:00
|
|
|
// ErrInvalidVersionArg is raised when a function expects IPv 4 or 6 but is
|
|
|
|
// passed a value not 4 or 6.
|
|
|
|
var ErrInvalidVersionArg = errors.New("Version argument must be 4 or 6")
|
2015-03-02 05:54:51 +00:00
|
|
|
|
2015-03-02 08:22:05 +00:00
|
|
|
// Hostlist is a collection of Hostnames. When in a Hostlist, hostnames must
|
|
|
|
// follow some rules:
|
|
|
|
// - Hostlist may contain IPv4 AND IPv6 (collectively, "IP version") hostnames.
|
|
|
|
// - Names are only allowed to overlap if IP version is different.
|
|
|
|
// - Adding a hostname for an existing name will replace the old one.
|
|
|
|
// See docs for the Sort and Add for more details.
|
2015-02-25 10:33:18 +00:00
|
|
|
type Hostlist []*Hostname
|
|
|
|
|
2015-03-02 08:22:05 +00:00
|
|
|
// NewHostlist initializes a new Hostlist
|
2015-02-25 10:33:18 +00:00
|
|
|
func NewHostlist() *Hostlist {
|
|
|
|
return &Hostlist{}
|
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// Len returns the number of hostnames in the list, part of sort.Interface
|
|
|
|
func (h Hostlist) Len() int {
|
|
|
|
return len(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less determines the sort order of two hostnames, part of sort.Interface
|
|
|
|
func (h Hostlist) Less(i, j int) bool {
|
|
|
|
// Sort 127.0.0.1, 127.0.1.1 and "localhost" at the top
|
|
|
|
if h[i].Domain == "localhost" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if h[j].Domain == "localhost" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:22:05 +00:00
|
|
|
// Sort IPv4 before IPv6
|
2015-03-02 07:15:02 +00:00
|
|
|
if h[i].Ipv6 && !h[j].Ipv6 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !h[i].Ipv6 && h[j].Ipv6 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:22:05 +00:00
|
|
|
// Compare the the IP addresses (byte array)
|
|
|
|
for c := range h[i].Ip {
|
2015-03-02 07:15:02 +00:00
|
|
|
if h[i].Ip[c] < h[j].Ip[c] {
|
|
|
|
return true
|
|
|
|
} else if h[i].Ip[c] > h[j].Ip[c] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prep for domain sorting
|
|
|
|
ilen := len(h[i].Domain)
|
|
|
|
jlen := len(h[j].Domain)
|
|
|
|
max := ilen
|
|
|
|
if jlen > max {
|
|
|
|
max = jlen
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort domains alphabetically
|
|
|
|
// Note: This works best if domains are lowercased. However, we do not
|
|
|
|
// enforce lowercase because of UTF-8 domain names, which may be broken by
|
|
|
|
// case folding. There is a way to do this correctly but it's completed so
|
|
|
|
// I'm not going to do it right now.
|
|
|
|
for c := 0; c < max; c++ {
|
|
|
|
if c > ilen {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if c > jlen {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if h[i].Domain[c] < h[j].Domain[c] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if h[i].Domain[c] > h[j].Domain[c] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seems like everything was the same, so it can't be Less
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap changes the position of two hostnames, part of sort.Interface
|
|
|
|
func (h Hostlist) Swap(i, j int) {
|
|
|
|
h[i], h[j] = h[j], h[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort this list of Hostnames, according to Hostlist sorting rules:
|
|
|
|
// 1. localhost comes first
|
2015-03-02 08:22:05 +00:00
|
|
|
// 2. IPv4 comes first
|
|
|
|
// 3. IPs are sorted in numerical order
|
2015-03-02 07:15:02 +00:00
|
|
|
// 4. domains are sorted in alphabetical
|
|
|
|
func (h *Hostlist) Sort() {
|
|
|
|
sort.Sort(*h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains returns true if this Hostlist has the specified Hostname
|
|
|
|
func (h *Hostlist) Contains(b *Hostname) bool {
|
2015-02-25 10:33:18 +00:00
|
|
|
for _, a := range *h {
|
2015-02-25 07:08:53 +00:00
|
|
|
if a.Equal(b) {
|
2015-02-24 08:55:24 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// ContainsDomain returns true if a Hostname in this Hostlist matches domain
|
2015-02-25 10:33:18 +00:00
|
|
|
func (h *Hostlist) ContainsDomain(domain string) bool {
|
|
|
|
for _, hostname := range *h {
|
2015-02-25 07:08:53 +00:00
|
|
|
if hostname.Domain == domain {
|
2015-02-24 08:55:24 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:22:05 +00:00
|
|
|
// ContainsIP returns true if a Hostname in this Hostlist matches IP
|
|
|
|
func (h *Hostlist) ContainsIP(IP net.IP) bool {
|
2015-02-25 10:33:18 +00:00
|
|
|
for _, hostname := range *h {
|
2015-03-02 08:22:05 +00:00
|
|
|
if hostname.EqualIp(IP) {
|
2015-02-24 08:55:24 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2015-02-25 10:33:18 +00:00
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// Add a new Hostname to this hostlist. If a Hostname with the same domain name
|
2015-03-02 08:22:05 +00:00
|
|
|
// and IP version is found, it will be replaced and an error will be returned.
|
2015-03-02 07:15:02 +00:00
|
|
|
// If you try to add an identical Hostname, an error will be returned.
|
|
|
|
// Note that in normal operation, you will sometimes expect an error, and the
|
|
|
|
// error data is mainly to alert you that you mis-entered information, not that
|
|
|
|
// the application has a problem.
|
2015-02-25 10:33:18 +00:00
|
|
|
func (h *Hostlist) Add(host *Hostname) error {
|
|
|
|
for _, found := range *h {
|
|
|
|
if found.Equal(host) {
|
2015-03-02 08:22:05 +00:00
|
|
|
return fmt.Errorf("Duplicate hostname entry for %s -> %s",
|
|
|
|
host.Domain, host.Ip)
|
2015-02-25 10:33:18 +00:00
|
|
|
} else if found.Domain == host.Domain && found.Ipv6 == host.Ipv6 {
|
2015-03-02 08:22:05 +00:00
|
|
|
return fmt.Errorf("Conflicting hostname entries for %s -> %s and -> %s",
|
|
|
|
host.Domain, host.Ip, found.Ip)
|
2015-02-25 10:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-25 10:37:57 +00:00
|
|
|
*h = append(*h, host)
|
2015-02-25 10:33:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// IndexOf will indicate the index of a Hostname in Hostlist, or -1 if it is
|
|
|
|
// not found.
|
2015-02-25 10:33:18 +00:00
|
|
|
func (h *Hostlist) IndexOf(host *Hostname) int {
|
|
|
|
for index, found := range *h {
|
|
|
|
if found.Equal(host) {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// IndexOfDomainV will indicate the index of a Hostname in Hostlist that has
|
|
|
|
// the same domain and IP version, or -1 if it is not found.
|
|
|
|
// This function will panic if IP version is not 4 or 6.
|
2015-03-02 05:54:51 +00:00
|
|
|
func (h *Hostlist) IndexOfDomainV(domain string, version int) int {
|
2015-03-02 07:15:02 +00:00
|
|
|
if version != 4 && version != 6 {
|
2015-03-02 08:22:05 +00:00
|
|
|
panic(ErrInvalidVersionArg)
|
2015-03-02 07:15:02 +00:00
|
|
|
}
|
2015-03-02 05:54:51 +00:00
|
|
|
for index, hostname := range *h {
|
|
|
|
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {
|
2015-02-25 10:33:18 +00:00
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// Remove will delete the Hostname at the specified index. If index is out of
|
|
|
|
// bounds (i.e. -1), Remove silently no-ops.
|
2015-02-25 10:33:18 +00:00
|
|
|
func (h *Hostlist) Remove(index int) {
|
2015-03-02 05:54:51 +00:00
|
|
|
if index > -1 && index < len(*h) {
|
|
|
|
*h = append((*h)[:index], (*h)[index+1:]...)
|
|
|
|
}
|
2015-02-25 10:33:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// RemoveDomain removes both Ipv4 and Ipv6 Hostname entries matching domain.
|
2015-03-02 05:54:51 +00:00
|
|
|
func (h *Hostlist) RemoveDomain(domain string) {
|
|
|
|
h.Remove(h.IndexOfDomainV(domain, 4))
|
|
|
|
h.Remove(h.IndexOfDomainV(domain, 6))
|
2015-02-25 10:33:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// RemoveDomainV removes a Hostname entry matching the domain and IP version.
|
|
|
|
// This function will panic if IP version is not 4 or 6.
|
2015-03-02 05:54:51 +00:00
|
|
|
func (h *Hostlist) RemoveDomainV(domain string, version int) {
|
|
|
|
if version != 4 && version != 6 {
|
2015-03-02 08:22:05 +00:00
|
|
|
panic(ErrInvalidVersionArg)
|
2015-03-02 05:54:51 +00:00
|
|
|
}
|
|
|
|
h.Remove(h.IndexOfDomainV(domain, version))
|
2015-02-25 10:33:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// Enable will change any Hostnames matching domain to be enabled.
|
2015-02-25 10:33:18 +00:00
|
|
|
func (h *Hostlist) Enable(domain string) {
|
|
|
|
for _, hostname := range *h {
|
|
|
|
if hostname.Domain == domain {
|
|
|
|
hostname.Enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
// EnableV will change a Hostname matching domain and IP version to be enabled.
|
|
|
|
// This function will panic if IP version is not 4 or 6.
|
2015-03-02 05:54:51 +00:00
|
|
|
func (h *Hostlist) EnableV(domain string, version int) {
|
2015-03-02 07:15:02 +00:00
|
|
|
if version != 4 && version != 6 {
|
2015-03-02 08:22:05 +00:00
|
|
|
panic(ErrInvalidVersionArg)
|
2015-03-02 07:15:02 +00:00
|
|
|
}
|
2015-03-02 05:54:51 +00:00
|
|
|
for _, hostname := range *h {
|
|
|
|
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {
|
|
|
|
hostname.Enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:22:05 +00:00
|
|
|
// Disable will change any Hostnames matching domain to be disabled.
|
2015-02-25 10:33:18 +00:00
|
|
|
func (h *Hostlist) Disable(domain string) {
|
|
|
|
for _, hostname := range *h {
|
|
|
|
if hostname.Domain == domain {
|
|
|
|
hostname.Enabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:22:05 +00:00
|
|
|
// DisableV will change any Hostnames matching domain and IP version to be disabled.
|
2015-03-02 07:15:02 +00:00
|
|
|
// This function will panic if IP version is not 4 or 6.
|
2015-03-02 05:54:51 +00:00
|
|
|
func (h *Hostlist) DisableV(domain string, version int) {
|
2015-03-02 07:15:02 +00:00
|
|
|
if version != 4 && version != 6 {
|
2015-03-02 08:22:05 +00:00
|
|
|
panic(ErrInvalidVersionArg)
|
2015-03-02 07:15:02 +00:00
|
|
|
}
|
2015-03-02 05:54:51 +00:00
|
|
|
for _, hostname := range *h {
|
|
|
|
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {
|
|
|
|
hostname.Enabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 02:10:01 +00:00
|
|
|
// Format takes the current list of Hostnames in this Hostfile and turns it
|
|
|
|
// into a string suitable for use as an /etc/hosts file.
|
|
|
|
// Sorting uses the following logic:
|
|
|
|
// 1. List is sorted by IP address
|
|
|
|
// 2. Commented items are left in place
|
|
|
|
// 3. 127.* appears at the top of the list (so boot resolvers don't break)
|
|
|
|
// 4. When present, localhost will always appear first in the domain list
|
|
|
|
func (h *Hostlist) Format() string {
|
2015-03-02 07:15:02 +00:00
|
|
|
h.Sort()
|
2015-03-02 02:10:01 +00:00
|
|
|
out := ""
|
|
|
|
for _, hostname := range *h {
|
|
|
|
out += hostname.Format() + "\n"
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|