-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathedns0ede.go
66 lines (59 loc) · 1.36 KB
/
edns0ede.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
package rdns
import (
"github.com/miekg/dns"
)
type EDNS0EDETemplate struct {
infoCode uint16
textTemplate *Template
}
type EDNS0EDEInput struct {
*dns.Msg
*BlocklistMatch
}
func NewEDNS0EDETemplate(infoCode uint16, extraText string) (*EDNS0EDETemplate, error) {
if infoCode == 0 && extraText == "" {
return nil, nil
}
tpl, err := NewTemplate(extraText)
if err != nil {
return nil, err
}
return &EDNS0EDETemplate{
infoCode: infoCode,
textTemplate: tpl,
}, nil
}
// Apply executes the template for the EDNS0-EDE record text, e.g. replacing
// placeholders in the Text with Query names, then adding the EDE record to
// the given msg.
func (t *EDNS0EDETemplate) Apply(msg *dns.Msg, in EDNS0EDEInput) error {
if t == nil {
return nil
}
var question dns.Question
if len(in.Question) > 0 {
question = in.Question[0]
}
input := templateInput{
ID: in.Id,
Question: question.Name,
QuestionClass: dns.ClassToString[question.Qclass],
QuestionType: dns.TypeToString[question.Qtype],
}
if in.BlocklistMatch != nil {
input.BlocklistRule = in.Rule
input.Blocklist = in.List
}
extraText, err := t.textTemplate.Apply(input)
if err != nil {
return err
}
ede := &dns.EDNS0_EDE{
InfoCode: t.infoCode,
ExtraText: extraText,
}
msg.SetEdns0(4096, false)
opt := msg.IsEdns0()
opt.Option = append(opt.Option, ede)
return nil
}