/
hosts.go
177 lines (145 loc) · 2.99 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ddns
import (
"bufio"
"net"
"os"
"regexp"
"strings"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/miekg/dns"
"github.com/0x6666/backup/log"
"github.com/0x6666/ddns/config"
)
type Hosts struct {
fileHosts *FileHosts
hostWatcher *fsnotify.Watcher
}
func NewHosts(hs config.HostsSettings) Hosts {
fileHosts := &FileHosts{
file: hs.HostsFile,
hosts: make(map[string]string),
}
hosts := Hosts{fileHosts, nil}
hosts.refresh()
return hosts
}
func (h *Hosts) Get(domain string, qtype uint16) ([]net.IP, bool) {
var sips []string
var ips []net.IP
sips, _ = h.fileHosts.Get(domain)
if sips == nil {
return nil, false
}
for _, sip := range sips {
var ip net.IP = net.ParseIP(sip)
switch {
case ip == nil:
log.Error("invalid ip address [%v]", sip)
return []net.IP{ip}, true
case qtype == dns.TypeA:
ip = ip.To4()
case qtype == dns.TypeAAAA:
if ip.To4() == nil {
ip = ip.To16()
} else {
ip = nil
}
}
if ip != nil {
ips = append(ips, ip)
}
}
return ips, (ips != nil)
}
func (h *Hosts) refresh() {
if h.hostWatcher != nil {
return
}
var err error
h.hostWatcher, err = fsnotify.NewWatcher()
if err != nil {
log.Error(err.Error())
}
go func() {
for {
select {
case event := <-h.hostWatcher.Events:
log.Info("event: %v", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Info("modified file: %v", event.Name)
h.fileHosts.Refresh()
}
case err := <-h.hostWatcher.Errors:
if err != nil {
log.Info("error: %v", err)
}
}
}
}()
err = h.hostWatcher.Add(h.fileHosts.file)
if err != nil {
log.Error(err.Error())
}
}
type FileHosts struct {
file string
hosts map[string]string
mu sync.RWMutex
}
func (f *FileHosts) Get(domain string) ([]string, bool) {
domain = strings.ToLower(domain)
f.mu.RLock()
ip, ok := f.hosts[domain]
f.mu.RUnlock()
if !ok {
return nil, false
}
return []string{ip}, true
}
func (f *FileHosts) Refresh() {
buf, err := os.Open(f.file)
if err != nil {
log.Warn("Update hosts records from file failed %s", err)
return
}
defer buf.Close()
f.mu.Lock()
defer f.mu.Unlock()
f.clear()
scanner := bufio.NewScanner(buf)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "#") || line == "" {
continue
}
sli := strings.Split(line, " ")
if len(sli) == 1 {
sli = strings.Split(line, "\t")
}
if len(sli) < 2 {
continue
}
domain := sli[len(sli)-1]
ip := sli[0]
if !f.isDomain(domain) || !f.isIP(ip) {
continue
}
f.hosts[strings.ToLower(domain)] = ip
}
log.Debug("update hosts records from %s", f.file)
}
func (f *FileHosts) clear() {
f.hosts = make(map[string]string)
}
func (f *FileHosts) isDomain(domain string) bool {
if f.isIP(domain) {
return false
}
match, _ := regexp.MatchString(`^([a-zA-Z0-9\*]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$`, domain)
return match
}
func (f *FileHosts) isIP(ip string) bool {
return (net.ParseIP(ip) != nil)
}