-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomainCache.go
241 lines (143 loc) · 3.65 KB
/
DomainCache.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package structs
import "tholian-endpoint/protocols/dns"
import "bytes"
import "encoding/json"
import "os"
import "path/filepath"
import "strings"
func readDomainRecords(file string) []dns.Record {
var records []dns.Record
stat, err1 := os.Stat(file)
if err1 == nil && !stat.IsDir() {
buffer, err2 := os.ReadFile(file)
if err2 == nil {
json.Unmarshal(buffer, &records)
}
}
return records
}
func writeDomainRecords(file string, records []dns.Record) bool {
var result bool = false
buffer, err1 := json.MarshalIndent(records, "", "\t")
if err1 == nil {
dir := filepath.Dir(file)
stat, err2 := os.Stat(dir)
if err2 == nil && stat.IsDir() {
err3 := os.WriteFile(file, buffer, 0666)
if err3 == nil {
result = true
}
} else {
err3 := os.Mkdir(dir, 0750)
if err3 == nil {
err4 := os.WriteFile(file, buffer, 0666)
if err4 == nil {
result = true
}
}
}
}
return result
}
type DomainCache struct {
Folder string `json:"folder"`
}
func NewDomainCache(folder string) DomainCache {
var cache DomainCache
if strings.HasSuffix(folder, "/") {
folder = folder[0:len(folder)-1]
}
stat, err1 := os.Stat(folder)
if err1 == nil && stat.IsDir() {
cache.Folder = folder
} else {
err2 := os.MkdirAll(folder, 0750)
if err2 == nil {
cache.Folder = folder
}
}
return cache
}
func (cache *DomainCache) Exists(query dns.Packet) bool {
var result bool = false
if len(query.Questions) > 0 {
result = true
for q := 0; q < len(query.Questions); q++ {
question := query.Questions[q]
resolved := question.Name + "/" + question.Type.String() + ".json"
found := false
if resolved != "" {
stat, err := os.Stat(cache.Folder + "/" + resolved)
if err == nil && !stat.IsDir() {
found = true
}
}
if found == false {
result = false
}
}
}
return result
}
func (cache *DomainCache) Read(query dns.Packet) dns.Packet {
var response dns.Packet
if query.Type == "query" && len(query.Questions) > 0 {
response.SetIdentifier(query.Identifier)
response.SetType("response")
response.Flags.AuthorativeAnswer = false
response.Flags.Truncated = false
if query.Flags.RecursionDesired == true {
response.Flags.RecursionAvailable = true
response.Flags.RecursionDesired = true
}
for q := 0; q < len(query.Questions); q++ {
question := query.Questions[q]
resolved := question.Name + "/" + question.Type.String() + ".json"
if resolved != "" {
records := readDomainRecords(cache.Folder + "/" + resolved)
if len(records) > 0 {
response.AddQuestion(question)
for r := 0; r < len(records); r++ {
response.AddAnswer(records[r])
}
}
}
}
}
return response
}
func (cache *DomainCache) Write(response dns.Packet) bool {
var result bool = false
if response.Type == "response" && len(response.Answers) > 0 {
for a := 0; a < len(response.Answers); a++ {
record := response.Answers[a]
resolved := record.Name + "/" + record.Type.String() + ".json"
if resolved != "" {
records := readDomainRecords(cache.Folder + "/" + resolved)
changed := false
if len(records) > 0 {
found := false
for r := 0; r < len(records); r++ {
if bytes.Equal(records[r].Data, record.Data) {
found = true
break
}
}
if found == false {
record.TTL = uint32(0)
records = append(records, record)
changed = true
}
} else {
record.TTL = uint32(0)
records = append(records, record)
changed = true
}
if changed == true {
writeDomainRecords(cache.Folder + "/" + resolved, records)
}
}
}
}
return result
}