This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin.go
73 lines (61 loc) · 1.53 KB
/
plugin.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
package plugin
import (
"net"
"strings"
"github.com/coredns/coredns/request"
"github.com/gophercloud/gophercloud"
"github.com/miekg/dns"
"golang.org/x/net/context"
)
// OpenStack is a plugin to talk to an OpenStack API.
type OpenStack struct {
Zone string
Entries *DNSEntries
AuthOptions *gophercloud.AuthOptions
Region string
EnableWildcard bool
}
// Name implements the plugin.Handler interface.
func (os OpenStack) Name() string {
return "openstack"
}
// ServeDNS implements the plugin.Handler interface.
func (os OpenStack) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
a := new(dns.Msg)
a.SetReply(r)
a.Compress = true
a.Authoritative = true
// Only answer type A queries
if state.QType() != dns.TypeA {
a.SetRcode(r, dns.RcodeNameError)
state.SizeAndDo(a)
w.WriteMsg(a)
return 0, nil
}
qn := strings.TrimSuffix(state.QName(), "."+os.Zone)
var entry []string
if os.EnableWildcard {
parts := strings.Split(qn, ".")
for i := len(parts) - 2; i >= 0; i-- {
name := strings.Join(parts[i:len(parts)], ".")
if e := (*os.Entries)[name]; e != nil {
entry = e
continue
}
}
} else {
entry = (*os.Entries)[qn]
}
if entry != nil {
rr := new(dns.A)
rr.Hdr = dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeA, Class: state.QClass()}
rr.A = net.ParseIP(entry[0]).To4()
a.Answer = []dns.RR{rr}
} else {
a.SetRcode(r, dns.RcodeNameError)
}
state.SizeAndDo(a)
w.WriteMsg(a)
return 0, nil
}