-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnssd.go
194 lines (180 loc) · 4.1 KB
/
dnssd.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
package dnssd
import (
"container/ring"
"context"
"fmt"
"net"
"net/http"
"sync"
"time"
)
type Dialer interface {
Dial(network, address string) (net.Conn, error)
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}
type Opts struct {
TTL time.Duration
Dialer Dialer
RoundTripper http.RoundTripper
LookupSRV func(service, proto, name string) (cname string, addrs []*net.SRV, err error)
LookupIP func(host string) (ips []net.IP, err error)
}
// New returns a new DNSSD with the that performs SRV resolution for the
// given host with the provided opts.
// New returns an error, if SRV lookup for host fails
// Defaults are as follows:
// If opts.Dialer is nil, a default net.Dialer is used.
// If opts.RoundTripper is nil, http.DefaultTransport is used.
// If opts.TTL is zero, a TTL of 15 seconds is used.
// If opts.LookupSRV is nil, net.LookupSRV is used.
// If opts.LookupIP is nil, net.LookupIP is used.
func New(host string, opts Opts) (*DNSSD, error) {
c, err := initialize(host, opts)
if err == nil {
go func() {
timer := time.Tick(c.ttl)
for {
<-timer
addrs, err := c.refresh()
if err != nil {
// TODO: counter
continue
}
c.replace(addrs)
}
}()
}
return c, err
}
func initialize(host string, opts Opts) (*DNSSD, error) {
c := DNSSD{
host: host,
dialer: opts.Dialer,
rt: opts.RoundTripper,
ttl: opts.TTL,
lookupSRV: opts.LookupSRV,
lookupIP: opts.LookupIP,
}
if c.ttl == 0 {
c.ttl = 15 * time.Second
}
if c.dialer == nil {
c.dialer = &net.Dialer{}
}
if c.rt == nil {
c.rt = http.DefaultTransport
}
if c.lookupSRV == nil {
c.lookupSRV = net.LookupSRV
}
if c.lookupIP == nil {
c.lookupIP = net.LookupIP
}
addrs, err := c.refresh()
if err != nil {
return nil, err
}
c.replace(addrs)
return &c, nil
}
type ipport struct {
ip net.IP
port uint16
}
func (i ipport) String() string {
return net.JoinHostPort(i.ip.String(), fmt.Sprint(i.port))
}
type DNSSD struct {
mtx sync.Mutex
addrs *ring.Ring
host string
dialer Dialer
rt http.RoundTripper
ttl time.Duration
lookupSRV func(service, proto, name string) (cname string, addrs []*net.SRV, err error)
lookupIP func(host string) (ips []net.IP, err error)
}
func (c *DNSSD) next() ipport {
c.mtx.Lock()
defer c.mtx.Unlock()
c.addrs = c.addrs.Next()
return c.addrs.Value.(ipport)
}
func (c *DNSSD) replace(addrs []ipport) {
r := ring.New(len(addrs))
for _, addr := range addrs {
r.Value = addr
r = r.Next()
}
c.mtx.Lock()
defer c.mtx.Unlock()
c.addrs = r
}
func (c *DNSSD) refresh() ([]ipport, error) {
_, srvs, err := c.lookupSRV("", "", c.host)
if err != nil {
return []ipport{}, err
}
wg := sync.WaitGroup{}
ipports := make(chan ipport)
for _, srv := range srvs {
wg.Add(1)
go func(srv *net.SRV) {
defer wg.Done()
ips, err := c.lookupIP(srv.Target)
if err != nil {
// TODO: counter
return
}
for _, ip := range ips {
ipports <- ipport{ip, srv.Port}
}
}(srv)
}
go func() {
wg.Wait()
close(ipports)
}()
addrs := make([]ipport, 0, len(srvs))
for addr := range ipports {
addrs = append(addrs, addr)
}
if len(addrs) == 0 {
err = fmt.Errorf("no results for %q", c.host)
}
return addrs, err
}
func (c *DNSSD) Dial(network, addr string) (net.Conn, error) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
host = addr
}
if host != c.host {
return c.dialer.Dial(network, host)
}
return c.dialer.Dial(network, c.next().String())
}
func (c *DNSSD) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
host = addr
}
if host != c.host {
return c.dialer.DialContext(ctx, network, host)
}
return c.dialer.DialContext(ctx, network, c.next().String())
}
func (c *DNSSD) RoundTrip(r *http.Request) (*http.Response, error) {
host, _, err := net.SplitHostPort(r.URL.Host)
if err != nil {
host = r.URL.Host
}
if host != c.host {
return c.rt.RoundTrip(r)
}
url := *r.URL
url.Host = c.next().String()
req := *r
req.URL = &url
return c.rt.RoundTrip(&req)
}