-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.go
180 lines (157 loc) · 4.87 KB
/
database.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
package database
import (
"context"
"errors"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/coredns/coredns/request"
"github.com/jinzhu/gorm"
"github.com/miekg/dns"
)
const (
name = "database"
priority = 10
ttl = 30
)
var errKeyNotFound = errors.New("key not found")
type DBBackend struct {
*gorm.DB
Zones []string
Upstream *upstream.Upstream
Next plugin.Handler
}
func (backend *DBBackend) Name() string { return name }
func (backend *DBBackend) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc()
opt := plugin.Options{}
state := request.Request{W: w, Req: r}
zone := plugin.Zones(backend.Zones).Matches(state.Name())
if zone == "" {
return plugin.NextOrFailure(backend.Name(), backend.Next, ctx, w, r)
}
var (
records, extra []dns.RR
err error
)
switch state.QType() {
case dns.TypeA:
records, err = plugin.A(ctx, backend, zone, state, nil, opt)
case dns.TypeAAAA:
records, err = plugin.AAAA(ctx, backend, zone, state, nil, opt)
case dns.TypeTXT:
records, err = plugin.TXT(ctx, backend, zone, state, opt)
case dns.TypeCNAME:
records, err = plugin.CNAME(ctx, backend, zone, state, opt)
case dns.TypePTR:
records, err = plugin.PTR(ctx, backend, zone, state, opt)
case dns.TypeMX:
records, extra, err = plugin.MX(ctx, backend, zone, state, opt)
case dns.TypeSRV:
records, extra, err = plugin.SRV(ctx, backend, zone, state, opt)
case dns.TypeSOA:
records, err = plugin.SOA(ctx, backend, zone, state, opt)
case dns.TypeNS:
if state.Name() == zone {
records, extra, err = plugin.NS(ctx, backend, zone, state, opt)
break
}
fallthrough
default:
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
_, err = plugin.A(ctx, backend, zone, state, nil, opt)
}
if err != nil && backend.IsNameError(err) {
//if backend.Fall.Through(state.Name()) {
// return plugin.NextOrFailure(backend.Name(), backend.Next, ctx, w, r)
//}
// Make err nil when returning here, so we don't log spam for NXDOMAIN.
return plugin.BackendError(ctx, backend, zone, dns.RcodeNameError, state, nil /* err */ , opt)
}
if err != nil {
return plugin.BackendError(ctx, backend, zone, dns.RcodeServerFailure, state, err, opt)
}
if len(records) == 0 {
return plugin.BackendError(ctx, backend, zone, dns.RcodeSuccess, state, err, opt)
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...)
w.WriteMsg(m)
return dns.RcodeSuccess, nil
//return plugin.NextOrFailure(backend.Name(), backend.Next, ctx, w, r)
}
func (backend *DBBackend) Services(ctx context.Context, state request.Request, exact bool, opt plugin.Options) ([]msg.Service, error) {
services, err := backend.Records(ctx, state, exact)
if err != nil {
return nil, err
}
return services, err
}
func (backend *DBBackend) Reverse(ctx context.Context, state request.Request, exact bool, opt plugin.Options) ([]msg.Service, error) {
return backend.Services(ctx, state, exact, opt)
}
func (backend *DBBackend) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
return backend.Upstream.Lookup(ctx, state, name, typ)
}
func (backend *DBBackend) IsNameError(err error) bool {
return err == errKeyNotFound
}
func (backend *DBBackend) Serial(state request.Request) uint32 {
return uint32(time.Now().Unix())
}
func (backend *DBBackend) MinTTL(state request.Request) uint32 {
return 30
}
func (backend *DBBackend) Transfer(ctx context.Context, state request.Request) (int, error) {
return dns.RcodeServerFailure, nil
}
func (backend *DBBackend) Records(ctx context.Context, state request.Request, exact bool) ([]msg.Service, error) {
name := state.Name()
serviceList, err := backend.get(name)
if err != nil {
return nil, err
}
var services []msg.Service
for _, service := range serviceList {
services = append(services, msg.Service{
Host: service.Host,
Port: service.Port,
Priority: setDefaultPriority(service.Priority),
Weight: service.Weight,
Text: service.Text,
Mail: service.Mail,
TTL: setDefaultTTL(service.TTL),
TargetStrip: 0,
Group: "",
Key: "",
})
}
return services, nil
}
func (backend *DBBackend) get(name string) ([]Service, error) {
var serviceList []Service
if err := backend.DB.Model(&Service{}).Where("name = ?", name).Scan(&serviceList).Error; err != nil {
return nil, err
}
if len(serviceList) == 0 {
return nil, errKeyNotFound
}
return serviceList, nil
}
func setDefaultPriority(p int) int {
if p == 0 {
return priority
}
return p
}
func setDefaultTTL(t uint32) uint32 {
if t == 0 {
return ttl
}
return t
}