-
Notifications
You must be signed in to change notification settings - Fork 6
/
fstrm.go
159 lines (139 loc) · 3.91 KB
/
fstrm.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
package pub
import (
"bytes"
"sync"
dnstap "github.com/dnstap/golang-dnstap"
framestream "github.com/farsightsec/golang-framestream"
"github.com/mimuret/dtap/v2/pkg/types"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
const FormatDNSTAP Format = "DNSTAP"
const FormatDtapFrame Format = "DTAPFrame"
// FSContentType is the FrameStream content type for dnstap protobuf data.
var DtapFrameFSContentType = []byte("protobuf:dtap.DtapFrame")
const (
DnstapFstrmControlHeaderSize = 42
DnstapFstrmMsgHeaderSize = 4
)
var _ Publisher = &FstrmPublisher{}
type FstrmPublisherMarshaler func(*types.DnstapMessage) ([]byte, error)
type FstrmPublisher struct {
sync.Mutex
buf *bytes.Buffer
handler PublisherHandler
maxSize int
contentType []byte
intervalFlusher *intervalFlusher
marshaler FstrmPublisherMarshaler
writer *framestream.Writer
writeSize int
writeState writeState
writeCount int
}
func NewFstrmPublisher(maxSize int, intervalSec uint, handler PublisherHandler) *FstrmPublisher {
buf := make([]byte, 0, maxSize)
return &FstrmPublisher{
buf: bytes.NewBuffer(buf),
handler: handler,
maxSize: maxSize,
intervalFlusher: newIntervalFlusher(intervalSec),
}
}
func NewFstrmDNSTAPPublisher(maxSize int, intervalSec uint, handler PublisherHandler) Publisher {
publisher := NewFstrmPublisher(maxSize, intervalSec, handler)
publisher.marshaler = func(dm *types.DnstapMessage) ([]byte, error) {
return dm.GetRaw(), nil
}
publisher.contentType = dnstap.FSContentType
return publisher
}
func NewFstrmDtapFramePublisher(maxSize int, intervalSec uint, handler PublisherHandler) Publisher {
publisher := NewFstrmPublisher(maxSize, intervalSec, handler)
publisher.marshaler = func(dm *types.DnstapMessage) ([]byte, error) {
return proto.Marshal(dm.ToDtapFrame())
}
publisher.contentType = DtapFrameFSContentType
return publisher
}
func (f *FstrmPublisher) Start() {
f.intervalFlusher.Start(f)
}
func (f *FstrmPublisher) reset() {
f.buf.Reset()
f.writeState = writeStateInit
f.writeSize = 0
f.writeCount = 0
}
func (f *FstrmPublisher) Write(dm *types.DnstapMessage) error {
f.Lock()
defer f.Unlock()
if err := f.write(dm); err != nil {
f.reset()
return err
}
return nil
}
func (f *FstrmPublisher) write(dm *types.DnstapMessage) error {
var err error
data, err := f.marshaler(dm)
if err != nil {
return nil
}
if f.writeSize+4+len(data)+DnstapFstrmControlHeaderSize > f.maxSize {
if err := f.publish(); err != nil {
return errors.Wrap(err, "failed to publish message")
}
}
if f.writeState == writeStateInit {
f.writer, err = framestream.NewWriter(f.buf, &framestream.WriterOptions{
ContentTypes: [][]byte{f.contentType},
Bidirectional: false,
})
if err != nil {
return errors.Wrap(err, "failed to create writer")
}
f.writeState = writeStateActive
f.writeSize = DnstapFstrmControlHeaderSize
f.writeCount = 0
}
n, err := f.writer.WriteFrame(data)
f.writeSize += n + DnstapFstrmMsgHeaderSize
f.writeCount++
return errors.Wrap(err, "failed to write message")
}
func (f *FstrmPublisher) Publish() error {
f.Lock()
defer f.Unlock()
return f.publish()
}
func (f *FstrmPublisher) publish() error {
if f.writeState != writeStateActive {
return nil
}
if err := f.writer.Flush(); err != nil {
return errors.Wrap(err, "failed to flush fstrm")
}
if err := f.writer.Close(); err != nil {
return errors.Wrap(err, "failed to close fstrm")
}
f.writeSize += DnstapFstrmControlHeaderSize
if f.writeCount == 0 {
return nil
}
data := f.buf.Bytes()
err := f.handler.Publish(data[:f.writeSize])
if err != nil {
return errors.Wrap(err, "publish error")
}
f.reset()
return nil
}
func (f *FstrmPublisher) Close() error {
f.intervalFlusher.Close()
return f.Publish()
}
func init() {
RegisterPublisher(FormatDNSTAP, NewFstrmDNSTAPPublisher)
RegisterPublisher(FormatDtapFrame, NewFstrmDtapFramePublisher)
}