/
server.go
380 lines (326 loc) · 9.27 KB
/
server.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2016 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dns
import (
"fmt"
"net"
"strings"
"math/rand"
"sort"
"github.com/Sirupsen/logrus"
"github.com/amalgam8/amalgam8/pkg/api"
"github.com/amalgam8/amalgam8/sidecar/util"
"github.com/miekg/dns"
)
// Server represent a DNS server. has config field for port,domain,and client discovery, and the DNS server itself
type Server struct {
dnsServer *dns.Server
discovery api.ServiceDiscovery
domain string
domainLabels int
}
// Config represents the DNS server configurations.
type Config struct {
Discovery api.ServiceDiscovery
Port uint16
Domain string
}
// NewServer creates a new instance of a DNS server with the given configurations
func NewServer(config Config) (*Server, error) {
err := validate(&config)
if err != nil {
return nil, err
}
s := &Server{
discovery: config.Discovery,
domain: config.Domain,
domainLabels: len(dns.Split(config.Domain)),
}
// Setup DNS muxing
mux := dns.NewServeMux()
mux.HandleFunc(config.Domain, s.handleRequest)
// Setup a DNS server
s.dnsServer = &dns.Server{
Addr: fmt.Sprintf(":%d", config.Port),
Net: "udp",
Handler: mux,
}
return s, nil
}
// ListenAndServe starts the DNS server
func (s *Server) ListenAndServe() error {
logrus.Info("Starting DNS server")
err := s.dnsServer.ListenAndServe()
if err != nil {
logrus.WithError(err).Errorf("Error starting DNS server")
}
return nil
}
// Shutdown stops the DNS server
func (s *Server) Shutdown() error {
logrus.Info("Shutting down DNS server")
err := s.dnsServer.Shutdown()
if err != nil {
logrus.WithError(err).Errorf("Error shutting down DNS server")
} else {
logrus.Info("DNS server has shutdown")
}
return err
}
func (s *Server) handleRequest(w dns.ResponseWriter, request *dns.Msg) {
response := new(dns.Msg)
response.SetReply(request)
response.Extra = request.Extra
response.Authoritative = true
response.RecursionAvailable = false
for i, question := range request.Question {
err := s.handleQuestion(question, request, response)
if err != nil {
logrus.WithError(err).Errorf("Error handling DNS question %d: %s", i, question.String())
break
}
}
err := w.WriteMsg(response)
if err != nil {
logrus.WithError(err).Errorf("Error writing DNS response")
}
}
func (s *Server) handleQuestion(question dns.Question, request, response *dns.Msg) error {
switch question.Qclass {
case dns.ClassINET:
default:
response.SetRcode(request, dns.RcodeServerFailure)
return fmt.Errorf("unsupported DNS question class: %v", dns.Class(question.Qclass).String())
}
switch question.Qtype {
case dns.TypeA:
case dns.TypeAAAA:
case dns.TypeSRV:
default:
response.SetRcode(request, dns.RcodeServerFailure)
return fmt.Errorf("unsupported DNS question type: %v", dns.Type(question.Qtype).String())
}
instances, err := s.retrieveInstances(question, request, response)
if err != nil {
return err
}
return s.createRecords(question, request, response, instances)
}
func (s *Server) retrieveInstances(question dns.Question, request, response *dns.Msg) ([]*api.ServiceInstance, error) {
// Validate the domain name in the question.
_, isValidDomain := dns.IsDomainName(question.Name)
if !isValidDomain {
response.SetRcode(request, dns.RcodeNameError)
return nil, fmt.Errorf("invalid domain name")
}
labels := dns.SplitDomainName(question.Name)
// Query format can be either of the following:
// 1. [tag|protocol|instanceID]*.<service>.<domain> (A/AAAA query)
// 2. _<service>._<tag|protocol|instanceID>.domain> (SRV query per RFC 2782)
if len(labels) < 1+s.domainLabels {
response.SetRcode(request, dns.RcodeNameError)
return nil, fmt.Errorf("no service specified")
}
// Extract service name and filtering labels (tags / protocol / instance ID)
var service string
var filters []string
switch question.Qtype {
case dns.TypeA, dns.TypeAAAA:
servicePos := len(labels) - s.domainLabels - 1
service = labels[servicePos]
filters = labels[0:servicePos]
case dns.TypeSRV:
// Make sure the query syntax complies to RFC 2782
if len(labels) != 2+s.domainLabels ||
!strings.HasPrefix(labels[0], "_") ||
!strings.HasPrefix(labels[1], "_") {
response.SetRcode(request, dns.RcodeNameError)
return nil, fmt.Errorf("invalid SRV query syntax")
}
service = strings.TrimPrefix(labels[0], "_")
filters = []string{strings.TrimPrefix(labels[1], "_")}
}
// Dispatch query to registry
instances, err := s.discovery.ListServiceInstances(service)
if err != nil {
response.SetRcode(request, dns.RcodeServerFailure)
return nil, err
}
filteredInstances, err := s.filterInstances(instances, filters)
if err != nil {
response.SetRcode(request, dns.RcodeNameError)
}
return filteredInstances, err
}
func (s *Server) filterInstances(instances []*api.ServiceInstance, filters []string) ([]*api.ServiceInstance, error) {
// If no filters are specified, all instances match vacuously
if len(filters) == 0 {
return instances, nil
}
// If only a single filter is specified, first attempt to match it as an instance ID
if len(filters) == 1 {
id := filters[0]
for _, instance := range instances {
if instance.ID == id {
return []*api.ServiceInstance{instance}, nil
}
}
}
// Split tags and protocol filters
protocol := ""
tags := make([]string, 0, len(filters))
for _, filter := range filters {
switch filter {
case "tcp", "udp", "http", "https":
if protocol != "" {
return nil, fmt.Errorf("invalid DNS query: more than one protocol specified")
}
protocol = filter
default:
tags = append(tags, filter)
}
}
// Sort the tags
sort.Strings(tags)
// Apply filters
// Note: filtering is done in-place, without allocating another array
k := 0
for _, instance := range instances {
// Apply protocol filter
if protocol != "" && instance.Endpoint.Type != protocol {
continue
}
// Apply tags filter
// Note: We presort the instance tags so that we can filter with a single pass
sort.Strings(instance.Tags)
j := 0
match := true
for _, tag := range tags {
found := false
for i := j; i < len(instance.Tags); i++ {
if tag == instance.Tags[i] {
found = true
j = i + 1
break
}
}
if !found {
match = false
break
}
}
if !match {
continue
}
instances[k] = instance
k++
}
return instances[0:k], nil
}
func (s *Server) createRecords(question dns.Question, request, response *dns.Msg, instances []*api.ServiceInstance) error {
answer := make([]dns.RR, 0, 3)
extra := make([]dns.RR, 0, 3)
for _, instance := range instances {
ip, port, err := util.SplitHostPort(instance.Endpoint)
if err != nil {
logrus.WithError(err).Warnf("unable to resolve ip address for instance '%s' in DNS query '%s'",
instance.ID, question.Name)
continue
}
switch question.Qtype {
case dns.TypeA:
ipV4 := ip.To4()
if ipV4 != nil {
answer = append(answer, createARecord(question.Name, ipV4))
}
case dns.TypeAAAA:
ipV4 := ip.To4()
if ipV4 == nil {
answer = append(answer, createARecord(question.Name, ip.To16()))
}
case dns.TypeSRV:
target := fmt.Sprintf("%s.%s.%s", instance.ID, instance.ServiceName, s.domain)
answer = append(answer, createSRVRecord(question.Name, port, target))
ipV4 := ip.To4()
if ipV4 != nil {
extra = append(extra, createARecord(target, ipV4))
} else {
extra = append(extra, createAAAARecord(target, ip.To16()))
}
}
}
if len(answer) == 0 {
response.SetRcode(request, dns.RcodeNameError)
return nil
}
// Poor-man's load balancing: randomize returned records order
shuffleRecords(answer)
shuffleRecords(extra)
response.Answer = append(response.Answer, answer...)
response.Extra = append(response.Extra, extra...)
response.SetRcode(request, dns.RcodeSuccess)
return nil
}
func createARecord(name string, ip net.IP) *dns.A {
record := &dns.A{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0,
},
A: ip,
}
return record
}
func createAAAARecord(name string, ip net.IP) *dns.AAAA {
record := &dns.AAAA{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 0,
},
AAAA: ip,
}
return record
}
func createSRVRecord(name string, port uint16, target string) *dns.SRV {
record := &dns.SRV{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeSRV,
Class: dns.ClassINET,
Ttl: 0,
},
Port: port,
Priority: 0,
Weight: 0,
Target: target,
}
return record
}
func validate(config *Config) error {
if config.Discovery == nil {
return fmt.Errorf("Discovery client is nil")
}
config.Domain = dns.Fqdn(config.Domain)
return nil
}
func shuffleRecords(records []dns.RR) {
for i := range records {
j := rand.Intn(i + 1)
records[i], records[j] = records[j], records[i]
}
}