26
26
# Amount of time to wait before dropping an undecodable ExportPacket
27
27
PACKET_TIMEOUT = 60 * 60
28
28
29
- # TODO: Add source IP
30
- RawPacket = namedtuple ('RawPacket' , ['ts' , 'data' ])
29
+ RawPacket = namedtuple ('RawPacket' , ['ts' , 'client' , 'data' ])
31
30
32
31
33
32
class QueuingRequestHandler (socketserver .BaseRequestHandler ):
34
33
def handle (self ):
35
- data = self .request [0 ]
36
- self .server .queue .put (RawPacket (time .time (), data ))
34
+ data = self .request [0 ] # get content, [1] would be the socket
35
+ self .server .queue .put (RawPacket (time .time (), self . client_address , data ))
37
36
logger .debug (
38
- "Received %d bytes of data from %s" , len (data ), self .client_address [ 0 ]
37
+ "Received %d bytes of data from %s" , len (data ), self .client_address
39
38
)
40
39
41
40
@@ -107,7 +106,7 @@ def run(self):
107
106
while not self ._shutdown .is_set ():
108
107
try :
109
108
# 0.5s delay to limit CPU usage while waiting for new packets
110
- pkt = self .input .get (block = True , timeout = 0.5 )
109
+ pkt : RawPacket = self .input .get (block = True , timeout = 0.5 )
111
110
except queue .Empty :
112
111
continue
113
112
@@ -130,15 +129,15 @@ def run(self):
130
129
131
130
# If any new templates were discovered, dump the unprocessable
132
131
# data back into the queue and try to decode them again
133
- if ( export .header .version == 9 and export .contains_new_templates and to_retry ) :
132
+ if export .header .version == 9 and export .contains_new_templates and to_retry :
134
133
logger .debug ("Received new template(s)" )
135
134
logger .debug ("Will re-attempt to decode %d old v9 ExportPackets" ,
136
135
len (to_retry ))
137
136
for p in to_retry :
138
137
self .input .put (p )
139
138
to_retry .clear ()
140
139
141
- self .output .put ((pkt .ts , export ))
140
+ self .output .put ((pkt .ts , pkt . client , export ))
142
141
finally :
143
142
self .server .shutdown ()
144
143
self .server .server_close ()
@@ -198,8 +197,11 @@ def get_export_packets(host, port):
198
197
# 3. the disk usage of files with JSON and its full strings as keys is reduced by using gzipped files
199
198
# This also means that the files have to be handled differently, because they are gzipped and not formatted as
200
199
# one single big JSON dump, but rather many little JSON dumps, separated by line breaks.
201
- for ts , export in get_export_packets (args .host , args .port ):
202
- entry = {ts : [flow .data for flow in export .flows ]}
200
+ for ts , client , export in get_export_packets (args .host , args .port ):
201
+ entry = {ts : {
202
+ "client" : client ,
203
+ "flows" : [flow .data for flow in export .flows ]}
204
+ }
203
205
line = json .dumps (entry ).encode () + b"\n " # byte encoded line
204
206
with gzip .open (args .output_file , "ab" ) as fh : # open as append, not reading the whole file
205
207
fh .write (line )
0 commit comments