-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultipass.go
More file actions
46 lines (38 loc) · 1.22 KB
/
multipass.go
File metadata and controls
46 lines (38 loc) · 1.22 KB
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
package multipass
import (
"context"
"net"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
type Multipass struct {
Next plugin.Handler
}
func (mp Multipass) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
req := request.Request{W: w, Req: r}
var log = clog.NewWithPlugin("multipass")
vms, err := vmList()
if err != nil {
log.Errorf("Error while getting VM list: %s", err)
}
labels := dns.SplitDomainName(req.Name())
if req.QType() == dns.TypeA && labels[0] != "" && len(vms[labels[0]]) > 0 {
log.Debugf("Got request of type %s, searching for VM with name %s", req.Type(), labels[0])
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
a := new(dns.A)
a.Hdr = dns.RR_Header{Name: req.Name(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600}
a.A = net.ParseIP(vms[labels[0]][0])
m.Answer = []dns.RR{a}
w.WriteMsg(m)
return dns.RcodeSuccess, nil
} else {
log.Debugf("Received unsupported request: %v", req)
}
// Call the next plugin in the chain
return plugin.NextOrFailure(mp.Name(), mp.Next, ctx, w, r)
}
func (mp Multipass) Name() string { return "multipass" }