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