etchosts.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package etchosts
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "os"
  8. "regexp"
  9. "strings"
  10. "sync"
  11. )
  12. // Record Structure for a single host record
  13. type Record struct {
  14. Hosts string
  15. IP string
  16. }
  17. // WriteTo writes record to file and returns bytes written or error
  18. func (r Record) WriteTo(w io.Writer) (int64, error) {
  19. n, err := fmt.Fprintf(w, "%s\t%s\n", r.IP, r.Hosts)
  20. return int64(n), err
  21. }
  22. var (
  23. // Default hosts config records slice
  24. defaultContent = []Record{
  25. {Hosts: "localhost", IP: "127.0.0.1"},
  26. {Hosts: "localhost ip6-localhost ip6-loopback", IP: "::1"},
  27. {Hosts: "ip6-localnet", IP: "fe00::0"},
  28. {Hosts: "ip6-mcastprefix", IP: "ff00::0"},
  29. {Hosts: "ip6-allnodes", IP: "ff02::1"},
  30. {Hosts: "ip6-allrouters", IP: "ff02::2"},
  31. }
  32. // A cache of path level locks for synchronizing /etc/hosts
  33. // updates on a file level
  34. pathMap = make(map[string]*sync.Mutex)
  35. // A package level mutex to synchronize the cache itself
  36. pathMutex sync.Mutex
  37. )
  38. func pathLock(path string) func() {
  39. pathMutex.Lock()
  40. defer pathMutex.Unlock()
  41. pl, ok := pathMap[path]
  42. if !ok {
  43. pl = &sync.Mutex{}
  44. pathMap[path] = pl
  45. }
  46. pl.Lock()
  47. return func() {
  48. pl.Unlock()
  49. }
  50. }
  51. // Drop drops the path string from the path cache
  52. func Drop(path string) {
  53. pathMutex.Lock()
  54. defer pathMutex.Unlock()
  55. delete(pathMap, path)
  56. }
  57. // Build function
  58. // path is path to host file string required
  59. // IP, hostname, and domainname set main record leave empty for no master record
  60. // extraContent is an array of extra host records.
  61. func Build(path, IP, hostname, domainname string, extraContent []Record) error {
  62. defer pathLock(path)()
  63. content := bytes.NewBuffer(nil)
  64. if IP != "" {
  65. // set main record
  66. var mainRec Record
  67. mainRec.IP = IP
  68. // User might have provided a FQDN in hostname or split it across hostname
  69. // and domainname. We want the FQDN and the bare hostname.
  70. fqdn := hostname
  71. if domainname != "" {
  72. fqdn += "." + domainname
  73. }
  74. mainRec.Hosts = fqdn
  75. if hostName, _, ok := strings.Cut(fqdn, "."); ok {
  76. mainRec.Hosts += " " + hostName
  77. }
  78. if _, err := mainRec.WriteTo(content); err != nil {
  79. return err
  80. }
  81. }
  82. // Write defaultContent slice to buffer
  83. for _, r := range defaultContent {
  84. if _, err := r.WriteTo(content); err != nil {
  85. return err
  86. }
  87. }
  88. // Write extra content from function arguments
  89. for _, r := range extraContent {
  90. if _, err := r.WriteTo(content); err != nil {
  91. return err
  92. }
  93. }
  94. return os.WriteFile(path, content.Bytes(), 0o644)
  95. }
  96. // Add adds an arbitrary number of Records to an already existing /etc/hosts file
  97. func Add(path string, recs []Record) error {
  98. defer pathLock(path)()
  99. if len(recs) == 0 {
  100. return nil
  101. }
  102. b, err := mergeRecords(path, recs)
  103. if err != nil {
  104. return err
  105. }
  106. return os.WriteFile(path, b, 0o644)
  107. }
  108. func mergeRecords(path string, recs []Record) ([]byte, error) {
  109. f, err := os.Open(path)
  110. if err != nil {
  111. return nil, err
  112. }
  113. defer f.Close()
  114. content := bytes.NewBuffer(nil)
  115. if _, err := content.ReadFrom(f); err != nil {
  116. return nil, err
  117. }
  118. for _, r := range recs {
  119. if _, err := r.WriteTo(content); err != nil {
  120. return nil, err
  121. }
  122. }
  123. return content.Bytes(), nil
  124. }
  125. // Delete deletes an arbitrary number of Records already existing in /etc/hosts file
  126. func Delete(path string, recs []Record) error {
  127. defer pathLock(path)()
  128. if len(recs) == 0 {
  129. return nil
  130. }
  131. old, err := os.Open(path)
  132. if err != nil {
  133. return err
  134. }
  135. var buf bytes.Buffer
  136. s := bufio.NewScanner(old)
  137. eol := []byte{'\n'}
  138. loop:
  139. for s.Scan() {
  140. b := s.Bytes()
  141. if len(b) == 0 {
  142. continue
  143. }
  144. if b[0] == '#' {
  145. buf.Write(b)
  146. buf.Write(eol)
  147. continue
  148. }
  149. for _, r := range recs {
  150. if bytes.HasSuffix(b, []byte("\t"+r.Hosts)) {
  151. continue loop
  152. }
  153. }
  154. buf.Write(b)
  155. buf.Write(eol)
  156. }
  157. old.Close()
  158. if err := s.Err(); err != nil {
  159. return err
  160. }
  161. return os.WriteFile(path, buf.Bytes(), 0o644)
  162. }
  163. // Update all IP addresses where hostname matches.
  164. // path is path to host file
  165. // IP is new IP address
  166. // hostname is hostname to search for to replace IP
  167. func Update(path, IP, hostname string) error {
  168. defer pathLock(path)()
  169. old, err := os.ReadFile(path)
  170. if err != nil {
  171. return err
  172. }
  173. re := regexp.MustCompile(fmt.Sprintf("(\\S*)(\\t%s)(\\s|\\.)", regexp.QuoteMeta(hostname)))
  174. return os.WriteFile(path, re.ReplaceAll(old, []byte(IP+"$2"+"$3")), 0o644)
  175. }