-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathrecords.go
50 lines (43 loc) · 1.25 KB
/
records.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
package main
import "github.com/miekg/dns"
func NewCustomDNSRecordsFromText(recordsText []string) []CustomDNSRecords {
customRecordsMap := make(map[string][]dns.RR)
for _, recordText := range recordsText {
answer, answerErr := dns.NewRR(recordText)
if answerErr != nil {
logger.Errorf("Cannot parse custom record: %s", answerErr)
}
name := answer.Header().Name
if len(name) > 0 {
if customRecordsMap[name] == nil {
customRecordsMap[name] = []dns.RR{}
}
customRecordsMap[name] = append(customRecordsMap[name], answer)
} else {
logger.Errorf("Cannot parse custom record (invalid name): '%s'", recordText)
}
}
return NewCustomDNSRecords(customRecordsMap)
}
func NewCustomDNSRecords(from map[string][]dns.RR) []CustomDNSRecords {
var records []CustomDNSRecords
for name, rrs := range from {
records = append(records, CustomDNSRecords{
name: name,
answer: rrs,
})
}
return records
}
type CustomDNSRecords struct {
name string
answer []dns.RR
}
func (c CustomDNSRecords) serve(server *DNSHandler) (handler func(dns.ResponseWriter, *dns.Msg)) {
return func(writer dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Answer = append(m.Answer, c.answer...)
server.WriteReplyMsg(writer, m)
}
}