-
Notifications
You must be signed in to change notification settings - Fork 1
/
zone.go
157 lines (135 loc) · 3.85 KB
/
zone.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
package zone
import (
"github.com/ennetech/consul-dns/pkg/common"
"github.com/ennetech/consul-dns/pkg/logger"
"github.com/miekg/dns"
"sort"
"strings"
)
type Zone struct {
text string
origin string
records []dns.RR
}
func NewZone(text, origin string) *Zone {
z := new(Zone)
z.text = text
z.origin = origin
z.updateRecords()
logger.Debug(z.String(), "Created zone (NewZone): "+origin)
return z
}
func (zone *Zone) String() string {
return zone.text
}
func (zone *Zone) Origin() string {
return zone.origin
}
func (zone *Zone) updateRecords() {
for x := range dns.ParseZone(strings.NewReader(zone.text), dns.Fqdn(zone.origin), "") {
if x.Error != nil {
// Ignore parsing error
// panic(x.Error)
} else {
zone.records = append(zone.records, x.RR)
}
}
}
func (zone *Zone) updateText() {
build := common.NewChainText()
for _, rr := range zone.records {
build.Append(rr.String())
}
sp := strings.Split(build.String(), "\n")
sort.Strings(sp)
zone.text = strings.Join(reverseStringsSlice(sp), "\n")
}
func (zone *Zone) QueryRR(question dns.Question) []dns.RR {
var answerRecords []dns.RR
// Scanning al record in the zone
for _, rr := range zone.records {
// Basic comparison
namematch := rr.Header().Name == question.Name
typematch := (rr.Header().Rrtype == question.Qtype) || (question.Qtype == dns.TypeA && rr.Header().Rrtype == dns.TypeCNAME)
// Perfect match
if namematch && typematch {
answerRecords = append(answerRecords, rr)
// Wildcard match
// The name does not match
// The scanned record starts with *
// I haven't found anything
} else if !namematch && strings.HasPrefix(rr.Header().Name, "*") && (len(answerRecords) == 0) {
// Remove first level on question
s := strings.Split(question.Name, ".")
copy(s, s[1:])
s = s[:len(s)-1]
builded := "*." + strings.Join(s, ".")
if (builded == rr.Header().Name) && typematch {
// We have to correct the name (so we don't expose we have a wildcard)
rr.Header().Name = question.Name
answerRecords = append(answerRecords, rr)
}
}
}
return answerRecords
}
func (zone *Zone) AddRR(input dns.RR) {
newRecord := true
for _, rr := range zone.records {
if input.String() == rr.String() {
newRecord = false
}
}
if newRecord {
zone.records = append(zone.records, input)
}
// Update the text version of the zone
zone.updateText()
logger.Debug(zone.String(), "Updated zone after AddRR")
}
func (zone *Zone) DeleteRR(input dns.RR) {
// We maybe need to delete more than one item
var deleteIndices []int
// Scan all the zone records
for i, rr := range zone.records {
// Uniform TTL
// TODO: i think this can be deleted
//input.Header().Ttl = 300
//rr.Header().Ttl = 300
// Additional check, goes false when a specific case happen
additional := true
// A record value must match
t1, o1 := input.(*dns.A)
t2, o2 := rr.(*dns.A)
if o1 && o2 && input.Header().Class != dns.ClassANY {
additional = t1.A.String() == t2.A.String()
}
// Type + Name + Additional check matching
if input.Header().Rrtype == rr.Header().Rrtype && input.Header().Name == rr.Header().Name && additional {
deleteIndices = append(deleteIndices, i)
}
}
// Delete in reverse order
deleteIndices = reverseIntegersSlice(deleteIndices)
for _, d := range deleteIndices {
zone.records = append(zone.records[:d], zone.records[d+1:]...)
}
// Update the text version of the zone
zone.updateText()
logger.Debug(zone.String(), "Updated zone after RemoveRR")
}
// Helper functions
func reverseIntegersSlice(numbers []int) []int {
for i := 0; i < len(numbers)/2; i++ {
j := len(numbers) - i - 1
numbers[i], numbers[j] = numbers[j], numbers[i]
}
return numbers
}
func reverseStringsSlice(numbers []string) []string {
for i := 0; i < len(numbers)/2; i++ {
j := len(numbers) - i - 1
numbers[i], numbers[j] = numbers[j], numbers[i]
}
return numbers
}