forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edns0ede.go
68 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
67
68
package rdns
import (
"bytes"
"text/template"
"github.com/miekg/dns"
)
type EDNS0EDETemplate struct {
infoCode uint16
extraText string
textTemplate *template.Template
}
func NewEDNS0EDETemplate(infoCode uint16, extraText string) (*EDNS0EDETemplate, error) {
if infoCode == 0 && extraText == "" {
return nil, nil
}
textTemplate := template.New("EDNS0EDE")
textTemplate, err := textTemplate.Parse(extraText)
if err != nil {
return nil, err
}
return &EDNS0EDETemplate{
infoCode: infoCode,
extraText: extraText,
textTemplate: textTemplate,
}, nil
}
// Data that is passed to any templates.
type templateInput struct {
ID uint16
Question string
}
// 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, q *dns.Msg) error {
if t == nil {
return nil
}
var question string
if len(q.Question) > 0 {
question = q.Question[0].Name
}
input := templateInput{
ID: q.Id,
Question: question,
}
text := new(bytes.Buffer)
if err := t.textTemplate.Execute(text, input); err != nil {
return err
}
ede := &dns.EDNS0_EDE{
InfoCode: t.infoCode,
ExtraText: text.String(),
}
msg.SetEdns0(4096, false)
opt := msg.IsEdns0()
opt.Option = append(opt.Option, ede)
return nil
}