-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
112 lines (92 loc) · 1.94 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package hosts
import (
"regexp"
"strings"
"github.com/go-zoox/errors"
"github.com/go-zoox/fs"
)
type Hosts struct {
FilePath string
Mapping map[string]string
}
func New(filepath string) *Hosts {
return &Hosts{
FilePath: filepath,
Mapping: make(map[string]string),
}
}
func (h *Hosts) Load() error {
lines, err := fs.ReadFileLines(h.FilePath)
if err != nil {
return err
}
for _, line := range lines {
// emty line
if len(strings.TrimSpace(line)) == 0 {
continue
// comment line
} else if strings.HasPrefix(line, "#") {
continue
}
host := &Host{}
if err := host.Parse(line); err != nil {
return err
}
if host.IsIPv4 {
for _, name := range host.Names {
h.Mapping[name+":4"] = host.IP
}
} else {
for _, name := range host.Names {
h.Mapping[name+":6"] = host.IP
}
}
}
return nil
}
func (h *Hosts) LookUp(host string, typ ...int) (string, error) {
typX := 4
if len(typ) > 0 {
typX = typ[0]
}
// IPv4
if typX == 4 {
return h.lookUpIPv4(host)
} else if typX == 6 {
return h.lookUpIPv6(host)
} else {
return "", errors.Errorf("invalid lookup type: %d, allow: 4, 6", typX)
}
}
func (h *Hosts) lookUpIPv4(host string) (string, error) {
if ip, ok := h.Mapping[host+":4"]; ok {
return ip, nil
}
return "", errors.Errorf("not found: %s", host)
}
func (h *Hosts) lookUpIPv6(host string) (string, error) {
if ip, ok := h.Mapping[host+":6"]; ok {
return ip, nil
}
return "", errors.Errorf("not found: %s", host)
}
func (h *Hosts) Length() int {
return len(h.Mapping)
}
type Host struct {
IP string
Names []string
IsIPv4 bool
}
func (h *Host) Parse(text string) error {
re := regexp.MustCompile(`\s+`)
textX := re.ReplaceAllString(text, " ")
parts := strings.Split(strings.TrimSpace(textX), " ")
if len(parts) < 2 {
return errors.Errorf("failed to parse invalid host: %s", text)
}
h.IP = parts[0]
h.Names = parts[1:]
h.IsIPv4 = strings.Contains(h.IP, ".")
return nil
}