-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
176 lines (145 loc) · 3.99 KB
/
log.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
package egresseddie
import (
"strings"
"github.com/google/gopacket/layers"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// dnsFields returns a list of fields for a zap logger that describes
// a DNS packet.
func dnsFields(dns *layers.DNS, fullDNSLogging bool) []zap.Field {
var (
fields []zap.Field
flags []string
)
if fullDNSLogging {
fields = append(fields, zap.Uint8("opcode", uint8(dns.OpCode)))
if dns.AA {
flags = append(flags, "aa")
}
if dns.TC {
flags = append(flags, "tc")
}
if dns.RD {
flags = append(flags, "rd")
}
if dns.RA {
flags = append(flags, "ra")
}
fields = append(fields, zap.Strings("flags", flags))
if dns.QR {
fields = append(fields, zap.Uint8("resp-code", uint8(dns.ResponseCode)))
}
}
if dns.QDCount > 0 {
fields = append(fields, zap.Array("questions", dnsQuestions(dns.Questions)))
}
stringify := func(records []layers.DNSResourceRecord, key string) {
if len(records) == 0 {
return
}
// skip additionals containing empty OPTs
if len(records) == 1 && records[0].Type == layers.DNSTypeOPT && len(records[0].OPT) == 0 {
return
}
fields = append(fields, zap.Array(key, dnsRecords(records)))
}
stringify(dns.Answers, "answers")
if fullDNSLogging {
stringify(dns.Authorities, "authorities")
stringify(dns.Additionals, "additionals")
}
return fields
}
type dnsQuestions []layers.DNSQuestion
func (q dnsQuestions) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for i := range q {
if err := enc.AppendObject(dnsQuestion(q[i])); err != nil {
return err
}
}
return nil
}
type dnsQuestion layers.DNSQuestion
func (q dnsQuestion) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddByteString("name", q.Name)
enc.AddString("class", strings.ToLower(q.Class.String()))
enc.AddString("type", strings.ToLower(q.Type.String()))
return nil
}
type dnsRecords []layers.DNSResourceRecord
func (r dnsRecords) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for i := range r {
if err := enc.AppendObject(dnsRecord(r[i])); err != nil {
return err
}
}
return nil
}
type dnsRecord layers.DNSResourceRecord
func (r dnsRecord) MarshalLogObject(enc zapcore.ObjectEncoder) error {
switch r.Type {
case layers.DNSTypeA, layers.DNSTypeAAAA:
enc.AddString("ip", r.IP.String())
case layers.DNSTypeCNAME:
enc.AddByteString("name", r.CNAME)
case layers.DNSTypeNS:
enc.AddByteString("name", r.NS)
case layers.DNSTypeMX:
enc.AddUint16("pref", r.MX.Preference)
enc.AddByteString("name", r.MX.Name)
case layers.DNSTypeOPT:
err := enc.AddArray("opts", dnsOpts(r.OPT))
if err != nil {
return err
}
case layers.DNSTypePTR:
enc.AddByteString("name", r.PTR)
case layers.DNSTypeSOA:
enc.AddByteString("mname", r.SOA.MName)
enc.AddByteString("rname", r.SOA.RName)
enc.AddUint32("serial", r.SOA.Serial)
enc.AddUint32("refresh", r.SOA.Refresh)
enc.AddUint32("retry", r.SOA.Retry)
enc.AddUint32("expire", r.SOA.Expire)
enc.AddUint32("min", r.SOA.Minimum)
case layers.DNSTypeSRV:
enc.AddUint16("priority", r.SRV.Priority)
enc.AddUint16("weight", r.SRV.Weight)
enc.AddUint16("port", r.SRV.Port)
enc.AddByteString("name", r.SRV.Name)
case layers.DNSTypeTXT:
err := enc.AddArray("data", dnsTXTs(r.TXTs))
if err != nil {
return err
}
case layers.DNSTypeURI:
enc.AddUint16("priority", r.URI.Priority)
enc.AddUint16("weight", r.URI.Weight)
enc.AddByteString("name", r.URI.Target)
}
enc.AddString("type", strings.ToLower(r.Type.String()))
return nil
}
type dnsOpts []layers.DNSOPT
func (o dnsOpts) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for i := range o {
if err := enc.AppendObject(dnsOpt(o[i])); err != nil {
return err
}
}
return nil
}
type dnsOpt layers.DNSOPT
func (o dnsOpt) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("code", o.Code.String())
enc.AddBinary("data", o.Data)
return nil
}
type dnsTXTs [][]byte
func (t dnsTXTs) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for i := range t {
enc.AppendByteString(t[i])
}
return nil
}