2015-05-06 18:23:01 -04:00
|
|
|
package etchosts
|
|
|
|
|
|
|
|
import (
|
2015-10-19 12:22:29 -04:00
|
|
|
"bufio"
|
2015-05-06 18:23:01 -04:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-06-19 02:37:36 -04:00
|
|
|
"os"
|
2015-05-06 18:23:01 -04:00
|
|
|
"regexp"
|
2016-02-13 18:37:42 -05:00
|
|
|
"strings"
|
2015-10-21 01:50:23 -04:00
|
|
|
"sync"
|
2015-05-06 18:23:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Record Structure for a single host record
|
|
|
|
type Record struct {
|
|
|
|
Hosts string
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo writes record to file and returns bytes written or error
|
|
|
|
func (r Record) WriteTo(w io.Writer) (int64, error) {
|
|
|
|
n, err := fmt.Fprintf(w, "%s\t%s\n", r.IP, r.Hosts)
|
|
|
|
return int64(n), err
|
|
|
|
}
|
|
|
|
|
2015-10-21 01:50:23 -04:00
|
|
|
var (
|
|
|
|
// Default hosts config records slice
|
|
|
|
defaultContent = []Record{
|
|
|
|
{Hosts: "localhost", IP: "127.0.0.1"},
|
|
|
|
{Hosts: "localhost ip6-localhost ip6-loopback", IP: "::1"},
|
|
|
|
{Hosts: "ip6-localnet", IP: "fe00::0"},
|
|
|
|
{Hosts: "ip6-mcastprefix", IP: "ff00::0"},
|
|
|
|
{Hosts: "ip6-allnodes", IP: "ff02::1"},
|
|
|
|
{Hosts: "ip6-allrouters", IP: "ff02::2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// A cache of path level locks for synchronizing /etc/hosts
|
|
|
|
// updates on a file level
|
|
|
|
pathMap = make(map[string]*sync.Mutex)
|
|
|
|
|
|
|
|
// A package level mutex to synchronize the cache itself
|
|
|
|
pathMutex sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathLock(path string) func() {
|
|
|
|
pathMutex.Lock()
|
|
|
|
defer pathMutex.Unlock()
|
|
|
|
|
|
|
|
pl, ok := pathMap[path]
|
|
|
|
if !ok {
|
|
|
|
pl = &sync.Mutex{}
|
|
|
|
pathMap[path] = pl
|
|
|
|
}
|
|
|
|
|
|
|
|
pl.Lock()
|
|
|
|
return func() {
|
|
|
|
pl.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop drops the path string from the path cache
|
|
|
|
func Drop(path string) {
|
|
|
|
pathMutex.Lock()
|
|
|
|
defer pathMutex.Unlock()
|
|
|
|
|
|
|
|
delete(pathMap, path)
|
2015-05-06 18:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build function
|
|
|
|
// path is path to host file string required
|
|
|
|
// IP, hostname, and domainname set main record leave empty for no master record
|
|
|
|
// extraContent is an array of extra host records.
|
|
|
|
func Build(path, IP, hostname, domainname string, extraContent []Record) error {
|
2015-10-21 01:50:23 -04:00
|
|
|
defer pathLock(path)()
|
|
|
|
|
2015-05-06 18:23:01 -04:00
|
|
|
content := bytes.NewBuffer(nil)
|
|
|
|
if IP != "" {
|
|
|
|
//set main record
|
|
|
|
var mainRec Record
|
|
|
|
mainRec.IP = IP
|
2016-02-13 18:37:42 -05:00
|
|
|
// User might have provided a FQDN in hostname or split it across hostname
|
|
|
|
// and domainname. We want the FQDN and the bare hostname.
|
|
|
|
fqdn := hostname
|
2015-05-06 18:23:01 -04:00
|
|
|
if domainname != "" {
|
2016-02-13 18:37:42 -05:00
|
|
|
fqdn = fmt.Sprintf("%s.%s", fqdn, domainname)
|
|
|
|
}
|
|
|
|
parts := strings.SplitN(fqdn, ".", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
mainRec.Hosts = fmt.Sprintf("%s %s", fqdn, parts[0])
|
2015-05-06 18:23:01 -04:00
|
|
|
} else {
|
2016-02-13 18:37:42 -05:00
|
|
|
mainRec.Hosts = fqdn
|
2015-05-06 18:23:01 -04:00
|
|
|
}
|
|
|
|
if _, err := mainRec.WriteTo(content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write defaultContent slice to buffer
|
|
|
|
for _, r := range defaultContent {
|
|
|
|
if _, err := r.WriteTo(content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write extra content from function arguments
|
|
|
|
for _, r := range extraContent {
|
|
|
|
if _, err := r.WriteTo(content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(path, content.Bytes(), 0644)
|
|
|
|
}
|
|
|
|
|
2015-06-19 02:37:36 -04:00
|
|
|
// Add adds an arbitrary number of Records to an already existing /etc/hosts file
|
|
|
|
func Add(path string, recs []Record) error {
|
2015-10-21 01:50:23 -04:00
|
|
|
defer pathLock(path)()
|
|
|
|
|
2015-06-24 14:42:53 -04:00
|
|
|
if len(recs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-01 19:29:51 -04:00
|
|
|
b, err := mergeRecords(path, recs)
|
2015-06-19 02:37:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-01 19:29:51 -04:00
|
|
|
return ioutil.WriteFile(path, b, 0644)
|
|
|
|
}
|
2015-06-19 02:37:36 -04:00
|
|
|
|
2016-07-01 19:29:51 -04:00
|
|
|
func mergeRecords(path string, recs []Record) ([]byte, error) {
|
|
|
|
f, err := os.Open(path)
|
2015-06-19 02:37:36 -04:00
|
|
|
if err != nil {
|
2016-07-01 19:29:51 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
content := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
if _, err := content.ReadFrom(f); err != nil {
|
|
|
|
return nil, err
|
2015-06-19 02:37:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range recs {
|
|
|
|
if _, err := r.WriteTo(content); err != nil {
|
2016-07-01 19:29:51 -04:00
|
|
|
return nil, err
|
2015-06-19 02:37:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 19:29:51 -04:00
|
|
|
return content.Bytes(), nil
|
2015-06-19 02:37:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes an arbitrary number of Records already existing in /etc/hosts file
|
|
|
|
func Delete(path string, recs []Record) error {
|
2015-10-21 01:50:23 -04:00
|
|
|
defer pathLock(path)()
|
|
|
|
|
2015-06-24 14:42:53 -04:00
|
|
|
if len(recs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-19 12:22:29 -04:00
|
|
|
old, err := os.Open(path)
|
2015-06-19 02:37:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-19 12:22:29 -04:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
s := bufio.NewScanner(old)
|
|
|
|
eol := []byte{'\n'}
|
|
|
|
loop:
|
|
|
|
for s.Scan() {
|
|
|
|
b := s.Bytes()
|
2016-03-06 13:03:03 -05:00
|
|
|
if len(b) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-19 12:22:29 -04:00
|
|
|
if b[0] == '#' {
|
|
|
|
buf.Write(b)
|
|
|
|
buf.Write(eol)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, r := range recs {
|
|
|
|
if bytes.HasSuffix(b, []byte("\t"+r.Hosts)) {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.Write(b)
|
|
|
|
buf.Write(eol)
|
2015-06-19 02:37:36 -04:00
|
|
|
}
|
2015-10-19 12:22:29 -04:00
|
|
|
old.Close()
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(path, buf.Bytes(), 0644)
|
2015-06-19 02:37:36 -04:00
|
|
|
}
|
|
|
|
|
2015-05-06 18:23:01 -04:00
|
|
|
// Update all IP addresses where hostname matches.
|
|
|
|
// path is path to host file
|
|
|
|
// IP is new IP address
|
|
|
|
// hostname is hostname to search for to replace IP
|
|
|
|
func Update(path, IP, hostname string) error {
|
2015-10-21 01:50:23 -04:00
|
|
|
defer pathLock(path)()
|
|
|
|
|
2015-05-06 18:23:01 -04:00
|
|
|
old, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-10 10:58:49 -05:00
|
|
|
var re = regexp.MustCompile(fmt.Sprintf("(\\S*)(\\t%s)(\\s|\\.)", regexp.QuoteMeta(hostname)))
|
|
|
|
return ioutil.WriteFile(path, re.ReplaceAll(old, []byte(IP+"$2"+"$3")), 0644)
|
2015-05-06 18:23:01 -04:00
|
|
|
}
|