-
Notifications
You must be signed in to change notification settings - Fork 712
/
resolver.go
152 lines (133 loc) · 3.1 KB
/
resolver.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
package appclient
import (
"net"
"strconv"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/miekg/dns"
"github.com/weaveworks/scope/common/xfer"
)
const (
dnsPollInterval = 10 * time.Second
)
var (
tick = time.Tick
)
type setter func(string, []string)
// Resolver is a thing that can be stopped...
type Resolver interface {
Stop()
}
type staticResolver struct {
setters []setter
targets []target
quit chan struct{}
lookup LookupIP
}
// LookupIP type is used for looking up IPs.
type LookupIP func(host string) (ips []net.IP, err error)
type target struct{ host, port string }
func (t target) String() string { return net.JoinHostPort(t.host, t.port) }
// NewResolver periodically resolves the targets, and calls the set
// function with all the resolved IPs. It explictiy supports targets which
// resolve to multiple IPs. It uses the supplied DNS server name.
func NewResolver(targets []string, lookup LookupIP, setters ...setter) Resolver {
r := staticResolver{
targets: prepare(targets),
setters: setters,
quit: make(chan struct{}),
lookup: lookup,
}
go r.loop()
return r
}
// LookupUsing produces a LookupIP function for the given DNS server.
func LookupUsing(dnsServer string) func(host string) (ips []net.IP, err error) {
client := dns.Client{
Net: "tcp",
}
return func(host string) (ips []net.IP, err error) {
m := &dns.Msg{}
m.SetQuestion(dns.Fqdn(host), dns.TypeA)
in, _, err := client.Exchange(m, dnsServer)
if err != nil {
return nil, err
}
result := []net.IP{}
for _, answer := range in.Answer {
if a, ok := answer.(*dns.A); ok {
result = append(result, a.A)
}
}
return result, nil
}
}
func (r staticResolver) loop() {
r.resolve()
t := tick(dnsPollInterval)
for {
select {
case <-t:
r.resolve()
case <-r.quit:
return
}
}
}
func (r staticResolver) Stop() {
close(r.quit)
}
func prepare(strs []string) []target {
var targets []target
for _, s := range strs {
var host, port string
if strings.Contains(s, ":") {
var err error
host, port, err = net.SplitHostPort(s)
if err != nil {
log.Errorf("invalid address %s: %v", s, err)
continue
}
} else {
host, port = s, strconv.Itoa(xfer.AppPort)
}
targets = append(targets, target{host, port})
}
return targets
}
func (r staticResolver) resolve() {
for t, endpoints := range r.resolveMany(r.targets) {
for _, setter := range r.setters {
setter(t.String(), endpoints)
}
}
}
func (r staticResolver) resolveMany(targets []target) map[target][]string {
result := map[target][]string{}
for _, t := range targets {
result[t] = r.resolveOne(t)
}
return result
}
func (r staticResolver) resolveOne(t target) []string {
var addrs []net.IP
if addr := net.ParseIP(t.host); addr != nil {
addrs = []net.IP{addr}
} else {
var err error
addrs, err = r.lookup(t.host)
if err != nil {
return []string{}
}
}
endpoints := make([]string, 0, len(addrs))
for _, addr := range addrs {
// For now, ignore IPv6
if addr.To4() == nil {
continue
}
endpoints = append(endpoints, net.JoinHostPort(addr.String(), t.port))
}
return endpoints
}