-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.go
110 lines (102 loc) · 2.82 KB
/
input.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
/*
* Copyright (c) 2017, 2019 Farsight Security, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package main
import (
"log"
"github.com/dnstap/golang-dnstap"
"github.com/golang/protobuf/proto"
"github.com/farsightsec/go-nmsg"
"github.com/farsightsec/go-nmsg/nmsg_base"
)
type dnstapInput string
func (i dnstapInput) run(ctx *Context) {
traceMsg(ctx, "Opening dnstap socket input at %s", i)
fsinput, err := dnstap.NewFrameStreamSockInputFromPath(string(i))
if err != nil {
log.Fatalf("Could not listen on %s: %s", i, err.Error())
}
ch := make(chan []byte, 100)
go i.publish(ctx, ch)
fsinput.ReadInto(ch)
log.Printf("input %s finished", i)
}
func dnstapUnmarshal(b []byte) (*nmsg_base.Dnstap, error) {
d := new(nmsg_base.Dnstap)
err := proto.Unmarshal(b, d)
if err != nil {
return nil, err
}
return d, nil
}
func (i dnstapInput) publish(ctx *Context, ch <-chan []byte) {
var outputs []nmsg.Output
if ctx.Client != nil {
output := nmsg.TimedBufferedOutput(
newPayloadWriter(ctx),
ctx.Config.Flush.Duration,
)
output.SetMaxSize(nmsg.MaxContainerSize, 2*nmsg.MaxContainerSize)
output.SetCompression(true)
outputs = append(outputs, output)
}
if ctx.Output != nil {
outputs = append(outputs, ctx.Output)
}
for b := range ch {
ctx.DnstapIn.Messages++
ctx.DnstapIn.Bytes += uint64(len(b))
tapm, err := dnstapUnmarshal(b)
if err != nil {
ctx.DnstapError.Messages++
ctx.DnstapError.Bytes += uint64(len(b))
traceMsg(ctx, "Error unmarshaling Dnstap message: %s", err)
continue
}
if tapm.GetMessage().GetType() != dnstap.Message_RESOLVER_RESPONSE {
ctx.DnstapFiltered.Messages++
ctx.DnstapFiltered.Bytes += uint64(len(b))
traceMsg(ctx, "Filtering message of type %s", tapm.GetMessage().GetType())
continue
}
ok, _ := ctx.Config.FilterQnames.FilterMsgQname(tapm.GetMessage().GetResponseMessage())
if ok {
ctx.QnameFiltered.Messages++
ctx.QnameFiltered.Bytes += uint64(len(b))
if ctx.Trace {
b, ok := dnstap.TextFormat(&tapm.Dnstap)
if ok {
traceMsg(ctx, "Qname filtered response: %s", string(b))
} else {
traceMsg(ctx, "Qname filtered response: formatting failed")
}
}
continue
}
p, err := nmsg.Payload(tapm)
if err != nil {
ctx.NmsgError.Messages++
ctx.NmsgError.Bytes += uint64(len(b))
traceMsg(ctx, "Error converting to NMSG: %s", err)
continue
}
if ctx.Trace {
b, ok := dnstap.TextFormat(&tapm.Dnstap)
if ok {
traceMsg(ctx, "Submitting response: %s", string(b))
} else {
traceMsg(ctx, "Submitting response: formatting failed")
}
}
for _, o := range outputs {
err = o.Send(p)
if err != nil {
log.Fatal("Output error: ", err)
}
}
}
}