5
5
use std:: {
6
6
collections:: HashMap ,
7
7
fs,
8
+ mem:: take,
8
9
time:: { Duration , Instant } ,
9
10
} ;
10
11
@@ -29,9 +30,8 @@ use fluere_config::Config;
29
30
use fluere_plugin:: PluginManager ;
30
31
use fluereflow:: FluereRecord ;
31
32
32
- use tokio:: task;
33
-
34
33
use log:: { debug, info, trace} ;
34
+ use tokio:: task;
35
35
36
36
// This function captures packets from a network interface and converts them into NetFlow data.
37
37
// It takes the command line arguments as input, which specify the network interface to capture from and other parameters.
@@ -53,17 +53,13 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
53
53
. await
54
54
. expect ( "Failed to load plugins" ) ;
55
55
56
- let interface = find_device ( interface_name. as_str ( ) ) ?;
56
+ let interface = find_device ( & interface_name) ?;
57
57
let mut cap_device = CaptureDevice :: new ( interface. clone ( ) ) . map_err ( NetError :: from) ?;
58
58
let cap = & mut cap_device. capture ;
59
59
60
60
let file_dir = "./output" ;
61
- match fs:: create_dir_all ( <& str >:: clone ( & file_dir) ) {
62
- Ok ( _) => {
63
- trace ! ( "Created directory: {}" , file_dir)
64
- }
65
- Err ( error) => panic ! ( "Problem creating directory: {:?}" , error) ,
66
- } ;
61
+ fs:: create_dir_all ( file_dir)
62
+ . unwrap_or_else ( |error| panic ! ( "Problem creating directory: {:?}" , error) ) ;
67
63
68
64
let start = Instant :: now ( ) ;
69
65
let mut last_export = Instant :: now ( ) ;
@@ -78,16 +74,12 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
78
74
let mut active_flow: HashMap < Key , FluereRecord > = HashMap :: new ( ) ;
79
75
let mut tasks = vec ! [ ] ;
80
76
let mut export_tasks = vec ! [ ] ;
81
- // let mut packet_count = 0;
82
- // let export_rt =
77
+
83
78
loop {
84
79
match cap. next_packet ( ) {
85
- Err ( _) => {
86
- continue ;
87
- }
80
+ Err ( _) => continue ,
88
81
Ok ( packet) => {
89
82
trace ! ( "received packet" ) ;
90
- // trace!("packet: {:?}", );
91
83
92
84
let ( mut key_value, mut reverse_key) = match parse_keys ( packet. clone ( ) ) {
93
85
Ok ( keys) => keys,
@@ -149,14 +141,14 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
149
141
packet. header . ts . tv_sec as u64 ,
150
142
packet. header . ts . tv_usec as u64 ,
151
143
) ;
152
- //println!("time: {:?}", time);
153
144
let pkt = flowdata. min_pkt ;
154
145
let ttl = flowdata. min_ttl ;
155
146
// trace!(
156
147
// "current inputed flow{:?}",
157
148
// active_flow.get(&key_value).unwrap()
158
149
// );
159
150
let flow_key = if is_reverse { & reverse_key } else { & key_value } ;
151
+
160
152
if let Some ( flow) = active_flow. get_mut ( flow_key) {
161
153
let update_key = UDFlowKey {
162
154
doctets,
@@ -190,7 +182,7 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
190
182
if last_export. elapsed ( ) >= Duration :: from_millis ( interval) && interval != 0 {
191
183
let mut expired_flows = vec ! [ ] ;
192
184
let mut expired_flow_data = vec ! [ ] ;
193
- // packet_count = 0;
185
+
194
186
debug ! ( "Calculating timeout start" ) ;
195
187
for ( key, flow) in active_flow. iter ( ) {
196
188
if flow_timeout > 0 && flow. last < ( time - ( flow_timeout * 1000 ) ) {
@@ -204,19 +196,14 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
204
196
}
205
197
}
206
198
207
- // Send the expired flows to the plugins
208
199
debug ! (
209
200
"Sending {} expired flows to plugins start" ,
210
201
expired_flows. len( )
211
202
) ;
212
-
213
- let cloned_plugin_manager = plugin_manager. clone ( ) ;
203
+ let plugin_manager_clone = plugin_manager. clone ( ) ;
214
204
tasks. push ( task:: spawn ( async move {
215
205
for flow in & expired_flow_data {
216
- cloned_plugin_manager
217
- . process_flow_data ( * flow)
218
- . await
219
- . unwrap ( ) ;
206
+ plugin_manager_clone. process_flow_data ( * flow) . await . unwrap ( ) ;
220
207
}
221
208
debug ! (
222
209
"Sending {} expired flows to plugins done" ,
@@ -225,21 +212,18 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
225
212
} ) ) ;
226
213
227
214
active_flow. retain ( |key, _| !expired_flows. contains ( key) ) ;
228
- let cloned_records = records. clone ( ) ;
229
- records. clear ( ) ;
215
+ let records_to_export = take ( & mut records) ;
230
216
debug ! ( "Calculating timeout done" ) ;
231
217
232
218
let file_path_clone = file_path. clone ( ) ;
233
- //let file = fs::File::create(file_path_clone).unwrap();
234
219
info ! ( "Export {} Started" , file_path_clone) ;
235
220
export_tasks. push ( task:: spawn ( async move {
236
- fluere_exporter ( cloned_records , file) . await ;
221
+ fluere_exporter ( records_to_export , file) . await ;
237
222
info ! ( "Export {} Finished" , file_path_clone) ;
238
223
} ) ) ;
239
224
240
- // let result = tasks.await;
241
- info ! ( "running without blockng" ) ;
242
- file_path = cur_time_file ( csv_file. as_str ( ) , file_dir, ".csv" ) ;
225
+ info ! ( "running without blocking" ) ;
226
+ file_path = cur_time_file ( & csv_file, file_dir, ".csv" ) ;
243
227
file = fs:: File :: create ( file_path. as_ref ( ) ) . unwrap ( ) ;
244
228
last_export = Instant :: now ( ) ;
245
229
}
@@ -270,9 +254,9 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
270
254
let _ = task. await ;
271
255
}
272
256
273
- let cloned_records = records . clone ( ) ;
257
+ let records_to_export = take ( & mut records ) ;
274
258
export_tasks. push ( task:: spawn ( async {
275
- fluere_exporter ( cloned_records , file) . await ;
259
+ fluere_exporter ( records_to_export , file) . await ;
276
260
} ) ) ;
277
261
plugin_manager. await_completion ( plugin_worker) . await ;
278
262
drop ( plugin_manager) ;
0 commit comments