forked from StephanU/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
187 lines (154 loc) · 3.93 KB
/
publish.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
177
178
179
180
181
182
183
184
185
186
187
package publish
import (
"errors"
"fmt"
"sync"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/publisher"
)
type Transactions interface {
PublishTransaction(common.MapStr) bool
}
type PacketbeatPublisher struct {
pub *publisher.PublisherType
client publisher.Client
wg sync.WaitGroup
events chan common.MapStr
done chan struct{}
}
type ChanTransactions struct {
Channel chan common.MapStr
}
func (t *ChanTransactions) PublishTransaction(event common.MapStr) bool {
t.Channel <- event
return true
}
var debugf = logp.MakeDebug("publish")
func NewPublisher(pub *publisher.PublisherType, hwm int) *PacketbeatPublisher {
return &PacketbeatPublisher{
pub: pub,
client: pub.Client(),
done: make(chan struct{}),
events: make(chan common.MapStr, hwm),
}
}
func (t *PacketbeatPublisher) PublishTransaction(event common.MapStr) bool {
select {
case t.events <- event:
return true
default:
return false
}
}
func (t *PacketbeatPublisher) Start() {
t.wg.Add(1)
go func() {
defer t.wg.Done()
for {
select {
case event := <-t.events:
t.onEvent(event)
case <-t.done:
return
}
}
}()
}
func (t *PacketbeatPublisher) Stop() {
close(t.done)
t.wg.Wait()
}
func (t *PacketbeatPublisher) onEvent(event common.MapStr) {
if err := validateEvent(event); err != nil {
logp.Warn("Dropping invalid event: %v", err)
return
}
debugf("on event")
if !updateEventAddresses(t.pub, event) {
return
}
t.client.PublishEvent(event)
}
// filterEvent validates an event for common required fields with types.
// If event is to be filtered out the reason is returned as error.
func validateEvent(event common.MapStr) error {
ts, ok := event["@timestamp"]
if !ok {
return errors.New("missing '@timestamp' field from event")
}
_, ok = ts.(common.Time)
if !ok {
return errors.New("invalid '@timestamp' field from event")
}
err := event.EnsureCountField()
if err != nil {
return err
}
t, ok := event["type"]
if !ok {
return errors.New("missing 'type' field from event")
}
_, ok = t.(string)
if !ok {
return errors.New("invalid 'type' field from event")
}
return nil
}
func updateEventAddresses(pub *publisher.PublisherType, event common.MapStr) bool {
var srcServer, dstServer string
src, ok := event["src"].(*common.Endpoint)
if ok {
// check if it's outgoing transaction (as client)
isOutgoing := pub.IsPublisherIP(src.Ip)
if isOutgoing {
if pub.IgnoreOutgoing {
// duplicated transaction -> ignore it
debugf("Ignore duplicated transaction on: %s -> %s", srcServer, dstServer)
return false
}
//outgoing transaction
event["direction"] = "out"
}
srcServer = pub.GetServerName(src.Ip)
event["client_ip"] = src.Ip
event["client_port"] = src.Port
event["client_proc"] = src.Proc
event["client_server"] = srcServer
delete(event, "src")
}
dst, ok := event["dst"].(*common.Endpoint)
if ok {
dstServer = pub.GetServerName(dst.Ip)
event["ip"] = dst.Ip
event["port"] = dst.Port
event["proc"] = dst.Proc
event["server"] = dstServer
delete(event, "dst")
//check if it's incoming transaction (as server)
if pub.IsPublisherIP(dst.Ip) {
// incoming transaction
event["direction"] = "in"
}
}
event.EnsureCountField()
if pub.GeoLite != nil {
realIP, exists := event["real_ip"]
if exists && len(realIP.(common.NetString)) > 0 {
loc := pub.GeoLite.GetLocationByIP(string(realIP.(common.NetString)))
if loc != nil && loc.Latitude != 0 && loc.Longitude != 0 {
loc := fmt.Sprintf("%f, %f", loc.Latitude, loc.Longitude)
event["client_location"] = loc
}
} else {
if len(srcServer) == 0 && src != nil { // only for external IP addresses
loc := pub.GeoLite.GetLocationByIP(src.Ip)
if loc != nil && loc.Latitude != 0 && loc.Longitude != 0 {
loc := fmt.Sprintf("%f, %f", loc.Latitude, loc.Longitude)
event["client_location"] = loc
}
}
}
}
return true
}