-
Notifications
You must be signed in to change notification settings - Fork 24
/
handler.go
143 lines (122 loc) · 3.59 KB
/
handler.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
package coredns_mysql
import (
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/request"
_ "github.com/go-sql-driver/mysql"
"github.com/miekg/dns"
"golang.org/x/net/context"
)
type CoreDNSMySql struct {
Next plugin.Handler
Dsn string
TablePrefix string
MaxLifetime time.Duration
MaxOpenConnections int
MaxIdleConnections int
Ttl uint32
tableName string
lastZoneUpdate time.Time
zoneUpdateTime time.Duration
zones []string
}
// ServeDNS implements the plugin.Handler interface.
func (handler *CoreDNSMySql) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
qName := state.Name()
qType := state.Type()
if time.Since(handler.lastZoneUpdate) > handler.zoneUpdateTime {
err := handler.loadZones()
if err != nil {
return handler.errorResponse(state, dns.RcodeServerFailure, err)
}
}
qZone := plugin.Zones(handler.zones).Matches(qName)
if qZone == "" {
return plugin.NextOrFailure(handler.Name(), handler.Next, ctx, w, r)
}
records, err := handler.findRecord(qZone, qName, qType)
if err != nil {
return handler.errorResponse(state, dns.RcodeServerFailure, err)
}
var recordNotFound bool
if len(records) == 0 {
recordNotFound = true
// no record found but we are going to return a SOA
recs, err := handler.findRecord(qZone, "", "SOA")
if err != nil {
return handler.errorResponse(state, dns.RcodeServerFailure, err)
}
records = append(records, recs...)
}
if qType == "SOA" {
recsNs, err := handler.findRecord(qZone, qName, "NS")
if err != nil {
return handler.errorResponse(state, dns.RcodeServerFailure, err)
}
records = append(records, recsNs...)
}
if qType == "AXFR" {
return handler.errorResponse(state, dns.RcodeNotImplemented, nil)
}
answers := make([]dns.RR, 0, 10)
extras := make([]dns.RR, 0, 10)
for _, record := range records {
var answer dns.RR
switch record.RecordType {
case "A":
answer, extras, err = record.AsARecord()
case "AAAA":
answer, extras, err = record.AsAAAARecord()
case "CNAME":
answer, extras, err = record.AsCNAMERecord()
case "SOA":
answer, extras, err = record.AsSOARecord()
case "SRV":
answer, extras, err = record.AsSRVRecord()
case "NS":
answer, extras, err = record.AsNSRecord()
case "MX":
answer, extras, err = record.AsMXRecord()
case "TXT":
answer, extras, err = record.AsTXTRecord()
case "CAA":
answer, extras, err = record.AsCAARecord()
default:
return handler.errorResponse(state, dns.RcodeNotImplemented, nil)
}
if err != nil {
return handler.errorResponse(state, dns.RcodeServerFailure, err)
}
if answer != nil {
answers = append(answers, answer)
}
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.RecursionAvailable = false
m.Compress = true
if !recordNotFound {
m.Answer = append(m.Answer, answers...)
} else {
m.Ns = append(m.Ns, answers...)
m.Rcode = dns.RcodeNameError
}
m.Extra = append(m.Extra, extras...)
state.SizeAndDo(m)
m = state.Scrub(m)
_ = w.WriteMsg(m)
return dns.RcodeSuccess, nil
}
// Name implements the Handler interface.
func (handler *CoreDNSMySql) Name() string { return "handler" }
func (handler *CoreDNSMySql) errorResponse(state request.Request, rCode int, err error) (int, error) {
m := new(dns.Msg)
m.SetRcode(state.Req, rCode)
m.Authoritative, m.RecursionAvailable, m.Compress = true, false, true
state.SizeAndDo(m)
_ = state.W.WriteMsg(m)
// Return success as the rCode to signal we have written to the client.
return dns.RcodeSuccess, err
}