-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
331 lines (288 loc) · 6.7 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package hosts
import (
"bufio"
"context"
"io"
"net"
"strings"
"sync"
"time"
"github.com/dolfly/core/hosts"
"github.com/dolfly/core/logger"
"github.com/dolfly/x/internal/loader"
)
type Mapping struct {
Hostname string
IP net.IP
}
type options struct {
mappings []Mapping
fileLoader loader.Loader
redisLoader loader.Loader
httpLoader loader.Loader
period time.Duration
logger logger.Logger
}
type Option func(opts *options)
func MappingsOption(mappings []Mapping) Option {
return func(opts *options) {
opts.mappings = mappings
}
}
func ReloadPeriodOption(period time.Duration) Option {
return func(opts *options) {
opts.period = period
}
}
func FileLoaderOption(fileLoader loader.Loader) Option {
return func(opts *options) {
opts.fileLoader = fileLoader
}
}
func RedisLoaderOption(redisLoader loader.Loader) Option {
return func(opts *options) {
opts.redisLoader = redisLoader
}
}
func HTTPLoaderOption(httpLoader loader.Loader) Option {
return func(opts *options) {
opts.httpLoader = httpLoader
}
}
func LoggerOption(logger logger.Logger) Option {
return func(opts *options) {
opts.logger = logger
}
}
// hostMapper is a static table lookup for hostnames.
// For each host a single line should be present with the following information:
// IP_address canonical_hostname [aliases...]
// Fields of the entry are separated by any number of blanks and/or tab characters.
// Text from a "#" character until the end of the line is a comment, and is ignored.
type hostMapper struct {
mappings map[string][]net.IP
mu sync.RWMutex
cancelFunc context.CancelFunc
options options
}
func NewHostMapper(opts ...Option) hosts.HostMapper {
var options options
for _, opt := range opts {
opt(&options)
}
ctx, cancel := context.WithCancel(context.TODO())
p := &hostMapper{
mappings: make(map[string][]net.IP),
cancelFunc: cancel,
options: options,
}
if err := p.reload(ctx); err != nil {
options.logger.Warnf("reload: %v", err)
}
if p.options.period > 0 {
go p.periodReload(ctx)
}
return p
}
// Lookup searches the IP address corresponds to the given network and host from the host table.
// The network should be 'ip', 'ip4' or 'ip6', default network is 'ip'.
// the host should be a hostname (example.org) or a hostname with dot prefix (.example.org).
func (h *hostMapper) Lookup(ctx context.Context, network, host string, opts ...hosts.Option) (ips []net.IP, ok bool) {
h.options.logger.Debugf("lookup %s/%s", host, network)
ips = h.lookup(host)
if ips == nil {
ips = h.lookup("." + host)
}
if ips == nil {
s := host
for {
if index := strings.IndexByte(s, '.'); index > 0 {
ips = h.lookup(s[index:])
s = s[index+1:]
if ips == nil {
continue
}
}
break
}
}
if ips == nil {
return
}
switch network {
case "ip4":
var v []net.IP
for _, ip := range ips {
if ip = ip.To4(); ip != nil {
v = append(v, ip)
}
}
ips = v
case "ip6":
var v []net.IP
for _, ip := range ips {
if ip.To4() == nil {
v = append(v, ip)
}
}
ips = v
default:
}
if len(ips) > 0 {
h.options.logger.Debugf("host mapper: %s/%s -> %s", host, network, ips)
}
return
}
func (h *hostMapper) lookup(host string) []net.IP {
if h == nil || len(h.mappings) == 0 {
return nil
}
h.mu.RLock()
defer h.mu.RUnlock()
return h.mappings[host]
}
func (h *hostMapper) periodReload(ctx context.Context) error {
period := h.options.period
if period < time.Second {
period = time.Second
}
ticker := time.NewTicker(period)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := h.reload(ctx); err != nil {
h.options.logger.Warnf("reload: %v", err)
// return err
}
h.options.logger.Debug("hosts reload done")
case <-ctx.Done():
return ctx.Err()
}
}
}
func (h *hostMapper) reload(ctx context.Context) (err error) {
mappings := make(map[string][]net.IP)
mapf := func(hostname string, ip net.IP) {
ips := mappings[hostname]
found := false
for i := range ips {
if ip.Equal(ips[i]) {
found = true
break
}
}
if !found {
ips = append(ips, ip)
}
mappings[hostname] = ips
}
for _, mapping := range h.options.mappings {
mapf(mapping.Hostname, mapping.IP)
}
m, err := h.load(ctx)
for i := range m {
mapf(m[i].Hostname, m[i].IP)
}
h.options.logger.Debugf("load items %d", len(mappings))
h.mu.Lock()
defer h.mu.Unlock()
h.mappings = mappings
return
}
func (h *hostMapper) load(ctx context.Context) (mappings []Mapping, err error) {
if h.options.fileLoader != nil {
if lister, ok := h.options.fileLoader.(loader.Lister); ok {
list, er := lister.List(ctx)
if er != nil {
h.options.logger.Warnf("file loader: %v", er)
}
for _, s := range list {
mappings = append(mappings, h.parseLine(s)...)
}
} else {
r, er := h.options.fileLoader.Load(ctx)
if er != nil {
h.options.logger.Warnf("file loader: %v", er)
}
mappings, _ = h.parseMapping(r)
}
}
if h.options.redisLoader != nil {
if lister, ok := h.options.redisLoader.(loader.Lister); ok {
list, er := lister.List(ctx)
if er != nil {
h.options.logger.Warnf("redis loader: %v", er)
}
for _, s := range list {
mappings = append(mappings, h.parseLine(s)...)
}
} else {
r, er := h.options.redisLoader.Load(ctx)
if er != nil {
h.options.logger.Warnf("redis loader: %v", er)
}
if m, _ := h.parseMapping(r); m != nil {
mappings = append(mappings, m...)
}
}
}
if h.options.httpLoader != nil {
r, er := h.options.httpLoader.Load(ctx)
if er != nil {
h.options.logger.Warnf("http loader: %v", er)
}
if m, _ := h.parseMapping(r); m != nil {
mappings = append(mappings, m...)
}
}
return
}
func (h *hostMapper) parseMapping(r io.Reader) (mappings []Mapping, err error) {
if r == nil {
return
}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
mappings = append(mappings, h.parseLine(scanner.Text())...)
}
err = scanner.Err()
return
}
func (h *hostMapper) parseLine(s string) (mappings []Mapping) {
line := strings.Replace(s, "\t", " ", -1)
line = strings.TrimSpace(line)
if n := strings.IndexByte(line, '#'); n >= 0 {
line = line[:n]
}
var sp []string
for _, s := range strings.Split(line, " ") {
if s = strings.TrimSpace(s); s != "" {
sp = append(sp, s)
}
}
if len(sp) < 2 {
return // invalid lines are ignored
}
ip := net.ParseIP(sp[0])
if ip == nil {
return // invalid IP addresses are ignored
}
for _, v := range sp[1:] {
mappings = append(mappings, Mapping{
Hostname: v,
IP: ip,
})
}
return
}
func (h *hostMapper) Close() error {
h.cancelFunc()
if h.options.fileLoader != nil {
h.options.fileLoader.Close()
}
if h.options.redisLoader != nil {
h.options.redisLoader.Close()
}
return nil
}