-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathresolver.go
172 lines (140 loc) · 4.07 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package dns
import (
"errors"
"net"
"strings"
"sync"
"github.com/gravitl/netclient/config"
"github.com/miekg/dns"
"golang.org/x/exp/slog"
)
const (
ttlTimeout = 3600
)
var dnsMapMutex = sync.Mutex{} // used to mutex functions of the DNS
var (
ErrNXDomain = errors.New("non existent domain")
ErrNoQTypeRecord = errors.New("domain exists but no record matching the question type")
)
type DNSResolver struct {
DnsEntriesCacheStore map[string]dns.RR
DnsEntriesCacheMap map[string][]dnsRecord
}
var DnsResolver *DNSResolver
func init() {
DnsResolver = &DNSResolver{
DnsEntriesCacheStore: make(map[string]dns.RR),
DnsEntriesCacheMap: make(map[string][]dnsRecord),
}
}
// GetInstance
func GetDNSResolverInstance() *DNSResolver {
return DnsResolver
}
func isInternetGW() bool {
for _, v := range config.GetNodes() {
if v.IsIngressGateway {
return true
}
}
return false
}
// ServeDNS handles a DNS request
func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
slog.Info("receiving DNS query request", "Info", r.Question[0])
reply := &dns.Msg{}
reply.SetReply(r)
reply.RecursionAvailable = true
reply.RecursionDesired = true
reply.Rcode = dns.RcodeSuccess
reply.Authoritative = true
resp, err := GetDNSResolverInstance().Lookup(r)
if err != nil && errors.Is(err, ErrNXDomain) {
nslist := config.Netclient().NameServers
if config.Netclient().CurrGwNmIP != nil {
nslist = []string{}
nslist = append(nslist, config.Netclient().CurrGwNmIP.String())
} else if isInternetGW() {
nslist = []string{}
nslist = append(nslist, "8.8.8.8")
nslist = append(nslist, "8.8.4.4")
nslist = append(nslist, "2001:4860:4860::8888")
nslist = append(nslist, "2001:4860:4860::8844")
}
gotResult := false
client := &dns.Client{}
for _, v := range nslist {
if strings.Contains(v, ":") {
v = "[" + v + "]"
}
resp, _, err := client.Exchange(r, v+":53")
if err != nil {
continue
}
if resp.Rcode != dns.RcodeSuccess {
continue
}
if len(resp.Answer) > 0 {
reply.Answer = append(reply.Answer, resp.Answer...)
gotResult = true
break
}
}
if !gotResult {
reply.Rcode = dns.RcodeNameError
}
}
if resp != nil {
reply.Answer = append(reply.Answer, resp)
}
err = w.WriteMsg(reply)
if err != nil {
slog.Error("write DNS response message error: ", "error", err.Error())
}
}
// Register A record
func (d *DNSResolver) RegisterA(record dnsRecord) error {
dnsMapMutex.Lock()
defer dnsMapMutex.Unlock()
r := new(dns.A)
r.Hdr = dns.RR_Header{Name: dns.Fqdn(record.Name), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttlTimeout}
r.A = net.ParseIP(record.RData)
d.DnsEntriesCacheStore[buildDNSEntryKey(record.Name, record.Type)] = r
slog.Debug("registering A record successfully", "Info", d.DnsEntriesCacheStore[buildDNSEntryKey(record.Name, record.Type)])
return nil
}
// Register AAAA record
func (d *DNSResolver) RegisterAAAA(record dnsRecord) error {
dnsMapMutex.Lock()
defer dnsMapMutex.Unlock()
r := new(dns.AAAA)
r.Hdr = dns.RR_Header{Name: dns.Fqdn(record.Name), Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: ttlTimeout}
r.AAAA = net.ParseIP(record.RData)
d.DnsEntriesCacheStore[buildDNSEntryKey(record.Name, record.Type)] = r
slog.Debug("registering AAAA record successfully", "Info", d.DnsEntriesCacheStore[buildDNSEntryKey(record.Name, record.Type)])
return nil
}
// Lookup DNS entry in local directory
func (d *DNSResolver) Lookup(m *dns.Msg) (dns.RR, error) {
dnsMapMutex.Lock()
defer dnsMapMutex.Unlock()
q := m.Question[0]
r, ok := d.DnsEntriesCacheStore[buildDNSEntryKey(strings.TrimSuffix(q.Name, "."), q.Qtype)]
if !ok {
if q.Qtype == dns.TypeA {
_, ok = d.DnsEntriesCacheStore[buildDNSEntryKey(strings.TrimSuffix(q.Name, "."), dns.TypeAAAA)]
if ok {
// aware but no ipv6 address
return nil, ErrNoQTypeRecord
}
} else if q.Qtype == dns.TypeAAAA {
_, ok = d.DnsEntriesCacheStore[buildDNSEntryKey(strings.TrimSuffix(q.Name, "."), dns.TypeA)]
if ok {
// aware but no ipv4 address
return nil, ErrNoQTypeRecord
}
}
return nil, ErrNXDomain
}
return r, nil
}