This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
68 lines (54 loc) · 1.7 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
package threebot
import (
"context"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
// ServeDNS implements the plugin.Handler interface.
func (threebot *Threebot) 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()
zone := plugin.Zones(threebot.Zones).Matches(qname)
if zone == "" {
return errorResponse(state, zone, dns.RcodeBadName, nil)
}
location := threebot.findLocation(qname, zone)
answers := make([]dns.RR, 0, 10)
extras := make([]dns.RR, 0, 10)
record, err := threebot.get(location)
if err != nil {
return threebot.Next.ServeDNS(ctx, w, r)
//return errorResponse(state, zone, dns.RcodeBadName, nil)
}
switch qtype {
case "A":
answers, extras = threebot.A(qname, "", record)
case "AAAA":
answers, extras = threebot.AAAA(qname, "", record)
case "CNAME":
answers, extras = threebot.CNAME(qname, "", record)
case "CAA":
answers, extras = threebot.CAA(qname, "", record)
default:
return errorResponse(state, zone, dns.RcodeNotImplemented, nil)
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.Answer = append(m.Answer, answers...)
m.Extra = append(m.Extra, extras...)
w.WriteMsg(m)
return dns.RcodeSuccess, nil
}
// Name implements the Handler interface.
func (threebot *Threebot) Name() string { return "threebot" }
func errorResponse(state request.Request, zone string, 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 dns.RcodeSuccess, err
}