-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdns.go
93 lines (87 loc) · 2.21 KB
/
dns.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
/*
* Copyright 2017 LinkedIn Corporation. All rights reserved. Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
package main
import (
"github.com/satori/go.uuid"
"github.com/miekg/dns"
)
// DNSAnswer represents a answer to a DNS question
type DNSAnswer struct {
TTL uint32
Address *Address
}
// ServeDefaultDNS handles unknown DNS requests, returning the default HTTP bind
func (m *RebindManager) ServeDefaultDNS(w dns.ResponseWriter, req *dns.Msg) {
r := new(dns.Msg)
r.SetReply(req)
// Refuse the request
r.Rcode = dns.RcodeRefused
err := w.WriteMsg(r)
if err != nil {
log.Error(err)
}
}
// ServeDNS handles DNS requests, either returning the matching
func (m *RebindManager) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
log.Debugf("Got DNS Request: %s", req.Question[0].String())
// Extract out the UUID
matches := SubdomainRegex.FindStringSubmatch(req.Question[0].Name)
if len(matches) != 2 {
m.ServeDefaultDNS(w, req)
return
}
u, err := uuid.FromString(matches[1])
if err != nil {
m.ServeDefaultDNS(w, req)
return
}
// Check if we have a known rebind method for it
m.RebindsLock.RLock()
rebind, exists := m.Rebinds[u]
m.RebindsLock.RUnlock()
if !exists {
m.ServeDefaultDNS(w, req)
return
}
// Create a reply message
r := new(dns.Msg)
r.SetReply(req)
// Call the "Handle" method to get our answers
answers := rebind.HandleDNS(req.Question[0].Qtype)
// Iterate over each answer and add to the dns message
r.Answer = make([]dns.RR, len(answers))
for idx, answer := range answers {
// Switch based on the request type
hdr := dns.RR_Header{
Name: req.Question[0].Name,
Rrtype: req.Question[0].Qtype,
Class: dns.ClassINET,
Ttl: answer.TTL,
}
switch req.Question[0].Qtype {
case dns.TypeA:
r.Answer[idx] = &dns.A{
Hdr: hdr,
A: answer.Address.IP(),
}
case dns.TypeAAAA:
r.Answer[idx] = &dns.AAAA{
Hdr: hdr,
AAAA: answer.Address.IP(),
}
case dns.TypeCNAME:
r.Answer[idx] = &dns.CNAME{
Hdr: hdr,
Target: answer.Address.Host,
}
}
}
//log.Debugf("Answered DNS Request: %s", r)
// Send the actual DNS response
err = w.WriteMsg(r)
if err != nil {
log.Error(err)
}
}