-
Notifications
You must be signed in to change notification settings - Fork 17
/
hosts_file.go
89 lines (80 loc) · 1.97 KB
/
hosts_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package dnsp
import (
"bufio"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
func readConfig(src io.Reader, fn func(string)) {
scanner := bufio.NewScanner(src)
for scanner.Scan() {
line := scanner.Text()
line = strings.SplitN(line, "#", 2)[0]
parts := strings.Fields(line)
switch len(parts) {
case 0: // empty line
continue
case 1: // single hostname
fn(parts[0])
default: // hosts file like syntax
if parts[0] == "127.0.0.1" || parts[0] == "0.0.0.0" || parts[0] == "::1" {
for _, host := range parts[1:] {
fn(host)
}
}
}
}
}
// readHosts reads hosts from a URL or a file.
func readHosts(path string, fn func(string)) error {
if u, err := url.Parse(path); err == nil && u.Host != "" {
return readHostsURL(path, fn)
}
return readHostsFile(path, fn)
}
// readHostsFile reads hosts from a file.
func readHostsFile(path string, fn func(string)) error {
file, err := os.Open(path)
if err != nil {
return err
}
readConfig(file, fn)
file.Close()
return nil
}
// readHostsURL reads hosts from a URL.
func readHostsURL(url string, fn func(string)) error {
res, err := http.Get(url)
if err != nil {
return err
}
readConfig(res.Body, fn)
res.Body.Close()
return nil
}
// hostsFileMetadata returns metadata about the hosts file.
//
// If path is a URL, it will make a HEAD request to get the headers (leaving
// the TCP connection open for subsequent GETs, if needed).
//
// If path is a file path, the mtime and file size is returned.
func hostsFileMetadata(path string) (time.Time, int64, error) {
if u, err := url.Parse(path); err == nil && u.Host != "" {
res, err := http.Head(u.String())
if err != nil {
return time.Time{}, 0, err
}
size, _ := strconv.ParseInt(res.Header.Get("Content-Length"), 10, 64)
mtime, _ := time.Parse(time.RFC1123, res.Header.Get("Last-Modified"))
return mtime, size, nil
}
fi, err := os.Stat(path)
if err != nil {
return time.Time{}, 0, err
}
return fi.ModTime(), fi.Size(), nil
}