-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhosts.go
60 lines (55 loc) · 1.31 KB
/
hosts.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
// Copyright (C) 2019 Christopher E. Miller
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package smallprox
import (
"bufio"
"io"
"os"
"strings"
)
// LoadHosts loads one host per line or in /etc/hosts format.
func LoadHosts(f io.Reader) ([]string, error) {
var hosts []string
scan := bufio.NewScanner(f)
for scan.Scan() {
ent := strings.TrimSpace(scan.Text())
ihash := strings.IndexByte(ent, '#')
if ihash != -1 {
ent = strings.TrimSpace(ent[:ihash])
}
if len(ent) == 0 {
continue
}
parts := strings.Fields(ent)
switch len(parts) {
case 0:
case 1:
hosts = append(hosts, parts[0])
default: // Treat as /etc/hosts format:
for _, host := range parts[1:] {
hosts = append(hosts, host)
}
}
}
err := scan.Err()
if err != nil {
return nil, err
}
return hosts, nil
}
// LoadHostsFile loads hosts from the file, see LoadHosts
func LoadHostsFile(path string) ([]string, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return LoadHosts(f)
}
// ContainsHost returns true if host is found in hosts.
func ContainsHost(hosts []string, host string) bool {
return HasAnyFold(hosts, host)
}