forked from arvancloud/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
133 lines (110 loc) · 3.17 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
package redis
import (
"fmt"
"github.com/klovercloud-dev/redis/enums"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
"golang.org/x/net/context"
)
// ServeDNS implements the plugin.Handler interface.
func (redis *Redis) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
//go checkHealth(redis)
fmt.Println("This is local redis plugin - v2")
fmt.Println(w.RemoteAddr(), redis)
qname := state.Name()
qtype := state.Type()
if time.Since(redis.LastZoneUpdate) > enums.ZoneUpdateTime {
redis.LoadZones()
}
zone := plugin.Zones(redis.Zones).Matches(qname)
if zone == "" {
return plugin.NextOrFailure(qname, redis.Next, ctx, w, r)
}
fmt.Println("loading zone")
z := redis.load(zone)
if z == nil {
return redis.errorResponse(state, zone, dns.RcodeServerFailure, nil)
}
fmt.Println("zone loaded: ", z)
if qtype == "AXFR" {
records := redis.AXFR(z)
ch := make(chan *dns.Envelope)
tr := new(dns.Transfer)
tr.TsigSecret = nil
go func(ch chan *dns.Envelope) {
j, l := 0, 0
for i, r := range records {
l += dns.Len(r)
if l > enums.TransferLength {
ch <- &dns.Envelope{RR: records[j:i]}
l = 0
j = i
}
}
if j < len(records) {
ch <- &dns.Envelope{RR: records[j:]}
}
close(ch)
}(ch)
err := tr.Out(w, r, ch)
if err != nil {
fmt.Println(err)
}
w.Hijack()
return dns.RcodeSuccess, nil
}
fmt.Println("finding location", qname, qtype, z)
location := redis.findLocation(qname, z)
if len(location) == 0 {
fmt.Println("No location found")
return redis.errorResponse(state, zone, dns.RcodeNameError, nil)
}
answers := make([]dns.RR, 0, 10)
extras := make([]dns.RR, 0, 10)
record := redis.get(location, z)
switch qtype {
case "A":
answers, extras = redis.A(qname, z, record, w)
case "AAAA":
answers, extras = redis.AAAA(qname, z, record)
case "CNAME":
answers, extras = redis.CNAME(qname, z, record)
case "TXT":
answers, extras = redis.TXT(qname, z, record)
case "NS":
answers, extras = redis.NS(qname, z, record)
case "MX":
answers, extras = redis.MX(qname, z, record)
case "SRV":
answers, extras = redis.SRV(qname, z, record)
case "SOA":
answers, extras = redis.SOA(qname, z, record)
case "CAA":
answers, extras = redis.CAA(qname, z, record)
default:
return redis.errorResponse(state, zone, dns.RcodeNotImplemented, nil)
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative, m.RecursionAvailable, m.Compress = true, false, true
m.Answer = append(m.Answer, answers...)
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 (redis *Redis) Name() string { return enums.REDIS }
func (redis *Redis) 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 success as the rcode to signal we have written to the client.
return dns.RcodeSuccess, err
}