-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
262 lines (219 loc) · 5.31 KB
/
client.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
package gowasd
import (
"errors"
"fmt"
"github.com/miekg/dns"
"sort"
"strconv"
"strings"
"time"
)
const DefaultPropertyVersion = 1
type VersionedProperties map[int]map[string]string
type Client struct {
c *dns.Client
Addr string
RequestTimeout time.Duration
}
func New(c *dns.Client, addr string) (out Client, err error) {
if addr == "" {
addr, err = addrFromResolvConf("/etc/resolv.conf")
if err != nil {
return out, err
}
}
return Client{c: c, Addr: addr, RequestTimeout: time.Second}, nil
}
type Service struct {
Subtype string
Name string
Protocol string
Domain string
}
func (srv Service) DnsName() string {
return dumpDnsName(srv.DnsLabels())
}
func (srv Service) HasSubtype() bool {
return srv.Subtype != ""
}
func (srv Service) DnsLabels() (out []string) {
out = []string{"_" + srv.Name, "_" + srv.Protocol, srv.Domain}
if srv.HasSubtype() {
out = append([]string{"_" + srv.Subtype, "_sub"}, out...)
}
return
}
type Instance struct {
Service
Description string
returnedName string
}
func (inst Instance) DnsName() string {
if inst.returnedName == "" {
return dumpDnsName(inst.DnsLabels())
} else {
return inst.returnedName
}
}
func (inst Instance) DnsLabels() []string {
return append([]string{inst.Description}, inst.Service.DnsLabels()...)
}
type Endpoint struct {
Host string
Port int
priority int
}
func (self Endpoint) Addr() string {
return fmt.Sprintf("%s:%d", self.Host, self.Port)
}
type EndpointList []Endpoint
func (self EndpointList) Len() int {
return len(self)
}
func (self EndpointList) Less(i, j int) bool {
// Note we wanted this sorted descending, so we flip the inequality
return self[i].priority > self[j].priority
}
func (self EndpointList) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
type InstanceResolution struct {
Instance
Targets EndpointList
Properties VersionedProperties
}
func (self *Client) ServiceInstances(srv Service) (out []Instance, err error) {
msg := new(dns.Msg)
msg.SetQuestion(srv.DnsName(), dns.TypePTR)
resp, _, err := self.c.Exchange(msg, self.Addr)
if err != nil {
return nil, err
}
var parts []string
out = make([]Instance, 0, len(resp.Answer))
for _, ans := range resp.Answer {
if ansPtr, ok := ans.(*dns.PTR); ok {
parts = parseDnsName(ansPtr.Ptr, 2)
out = append(out, Instance{
Description: parts[0],
returnedName: ansPtr.Ptr,
Service: srv,
})
}
}
return
}
func (self *Client) ResolveInstance(inst Instance) (out InstanceResolution, err error) {
record_types := [...]uint16{dns.TypeSRV, dns.TypeTXT}
responses := make(chan *dns.Msg, len(record_types))
errCh := make(chan error, len(record_types))
for _, record_type := range record_types {
go func(t uint16, n string) {
msg := new(dns.Msg)
msg.SetQuestion(n, t)
resp, _, err := self.c.Exchange(msg, self.Addr)
if err != nil {
errCh <- err
return
}
responses <- resp
}(record_type, inst.DnsName())
}
out.Instance = inst
out.Targets = make(EndpointList, 0, 3) // 3 is a fair guess!
out.Properties = make(VersionedProperties)
for i := 0; i < len(record_types); i++ {
select {
case r := <-responses:
for _, anyRR := range r.Answer {
switch rr := anyRR.(type) {
case *dns.SRV:
// TODO: weight
out.Targets = append(out.Targets, Endpoint{
Host: rr.Target,
Port: int(rr.Port),
priority: int(rr.Priority),
})
case *dns.TXT:
parseTxtRecordForProperties(rr, &out)
}
}
case err := <-errCh:
return InstanceResolution{}, err
case _ = <-time.After(self.RequestTimeout):
return InstanceResolution{}, errors.New("timeout")
}
}
sort.Sort(out.Targets)
return
}
func parseTxtRecordForProperties(rr *dns.TXT, out *InstanceResolution) {
var propParts []string
var k, v string
propVersion := DefaultPropertyVersion
for i, prop := range rr.Txt {
propParts = strings.SplitN(prop, "=", 2)
if len(propParts) != 2 || propParts[0] == "" {
continue
}
k, v = propParts[0], propParts[1]
if i == 0 && k == "txtvers" {
pv64, err := strconv.ParseInt(v, 0, 64)
if err == nil {
propVersion = int(pv64)
continue
}
}
if _, ok := out.Properties[propVersion]; !ok {
out.Properties[propVersion] = make(map[string]string)
}
out.Properties[propVersion][k] = v
}
}
func parseDnsName(s string, n int) (out []string) {
// must be .-terminated
var escaped bool
var label string
out = make([]string, 0, 10)
for i, c := range s {
if c == '\\' {
escaped = true
continue
} else if c == '.' && !escaped {
out = append(out, label)
label = ""
escaped = false
if n > 0 && len(out) == n-1 {
if i < len(s)-1 {
out = append(out, s[i+1:len(s)-1])
}
break
}
} else {
escaped = false
label += string(c)
}
}
return
}
func dumpDnsName(n []string) (out string) {
for i, l := range n {
if i < len(n)-1 {
l = strings.Replace(l, "\\", "\\\\", -1)
l = strings.Replace(l, ".", "\\.", -1)
l = strings.Replace(l, " ", "\\ ", -1)
}
out += l + "."
}
return
}
func addrFromResolvConf(fn string) (out string, err error) {
clientConfig, err := dns.ClientConfigFromFile(fn)
if err != nil {
return
}
if len(clientConfig.Servers) == 0 {
return out, errors.New("no DNS servers found in " + fn)
}
return clientConfig.Servers[0] + ":" + clientConfig.Port, nil
}