Skip to content

Commit feae9e9

Browse files
committed
refactor: reduce duplicated code, unwrap
1 parent 8192f3b commit feae9e9

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum FluereError {
1313
// Add new variants
1414
FileNotFound(PathBuf),
1515
ParameterMissing(String),
16+
PluginError(String),
1617
InvalidValue { field: String, value: String },
1718
}
1819

@@ -31,6 +32,7 @@ impl std::fmt::Display for FluereError {
3132
Self::InvalidValue { field, value } => {
3233
write!(f, "Invalid value '{}' for field '{}'", value, field)
3334
}
35+
Self::PluginError(e) => write!(f, "Plugin error: {}", e),
3436
}
3537
}
3638
}

src/net/live_fluereflow.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
types::{Args, UDFlowKey},
1313
utils::{cur_time_file, fluere_exporter},
1414
FluereError,
15+
error::OptionExt,
1516
};
1617
use std::{
1718
borrow::Cow,
@@ -65,16 +66,14 @@ struct FlowSummary {
6566
// It takes the command line arguments as input, which specify the network interface to capture from and other parameters.
6667
// The function runs indefinitely, capturing packets and updating the terminal user interface with the captured data.
6768
pub async fn online_packet_capture(arg: Args) -> Result<(), FluereError> {
68-
let csv_file = arg
69-
.files
70-
.csv
71-
.ok_or_else(|| FluereError::ConfigError("CSV file not specified".to_string()))?;
72-
let use_mac = arg.parameters.use_mac.unwrap();
73-
let interface_name = arg.interface.expect("interface not found");
74-
let duration = arg.parameters.duration.unwrap();
75-
let interval = arg.parameters.interval.unwrap();
76-
let flow_timeout = arg.parameters.timeout.unwrap();
77-
let _sleep_windows = arg.parameters.sleep_windows.unwrap();
69+
let csv_file = arg.files.csv.required("this should be defaulted to `output` on construction")?;
70+
//let enable_ipv6
71+
let use_mac = arg.parameters.use_mac.required("this should be defaulted to `false` on construction")?;
72+
let interface_name = arg.interface.required("interface should be provided")?;
73+
let duration = arg.parameters.duration.required("this should be defaulted to `0(infinite)` on construction")?;
74+
let interval = arg.parameters.interval.required("this should be defaulted to `30 minutes` on construction")?;
75+
let flow_timeout = arg.parameters.timeout.required("this should be defaulted to `10 minutes` on construction")?;
76+
let _sleep_windows = arg.parameters.sleep_windows.required("this should be defaulted to `false`, and now deprecated")?;
7877
let config = Config::new();
7978
let plugin_manager = PluginManager::new().expect("Failed to create plugin manager");
8079
let plugin_worker = plugin_manager.start_worker();
@@ -276,7 +275,7 @@ pub async fn online_packet_capture(arg: Args) -> Result<(), FluereError> {
276275
if is_reverse { "reverse" } else { "forward" }
277276
);
278277

279-
if flags.fin == 1 || flags.rst == 1 {
278+
if flags.is_finished() {
280279
trace!("flow finished");
281280
plugin_manager.process_flow_data(*flow).await.unwrap();
282281
records.push(*flow);

src/net/offline_fluereflows.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ use pcap::Capture;
2424
use tokio::task;
2525

2626
pub async fn fluereflow_fileparse(arg: Args) -> Result<(), FluereError> {
27-
let _csv_file = arg.files.csv.required("csv file")?;
28-
let file_name = arg.files.file.required("input file")?;
29-
let use_mac = arg.parameters.use_mac.required("use_mac parameter")?;
30-
let flow_timeout = arg.parameters.timeout.required("timeout parameter")?;
27+
let _csv_file = arg.files.csv.required("this should be defaulted to `output` on construction")?;
28+
let file_name = arg.files.file.required("pcap file path should be provided")?;
29+
let use_mac = arg.parameters.use_mac.required("this should be defaulted to `false` on construction")?;
30+
let flow_timeout = arg.parameters.timeout.required("this should be defaulted to `10 minutes` on construction")?;
3131

3232
let mut cap = Capture::from_file(file_name.clone())?;
3333

@@ -137,7 +137,7 @@ pub async fn fluereflow_fileparse(arg: Args) -> Result<(), FluereError> {
137137
if is_reverse { "reverse" } else { "forward" }
138138
);
139139

140-
if flags.fin == 1 || flags.rst == 1 {
140+
if flags.is_finished() {
141141
trace!("Flow finished");
142142
trace!("Flow data: {:?}", flow);
143143
records.push(*flow);

src/net/online_fluereflow.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
types::{Args, UDFlowKey},
2121
utils::{cur_time_file, fluere_exporter},
2222
FluereError,
23+
error::OptionExt,
2324
};
2425

2526
use fluere_config::Config;
@@ -37,14 +38,14 @@ use tokio::{task, task::JoinHandle};
3738
// It takes the command line arguments as input, which specify the network interface to capture from and other parameters.
3839
// The function runs indefinitely, capturing packets and exporting the captured data to a CSV file.
3940
pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
40-
let csv_file = arg.files.csv.expect("this should be defaulted to `output` on construction");
41+
let csv_file = arg.files.csv.required("this should be defaulted to `output` on construction")?;
4142
//let enable_ipv6
42-
let use_mac = arg.parameters.use_mac.expect("this should be defaulted to `false` on construction");
43-
let interface_name = arg.interface.expect("interface not found");
44-
let duration = arg.parameters.duration.expect("this should be defaulted to `0(infinite)` on construction");
45-
let interval = arg.parameters.interval.expect("this should be defaulted to `30 minutes` on construction");
46-
let flow_timeout = arg.parameters.timeout.expect("this should be defaulted to `10 minutes` on construction");
47-
let _sleep_windows = arg.parameters.sleep_windows.expect("this should be defaulted to `false`, and now deprecated");
43+
let use_mac = arg.parameters.use_mac.required("this should be defaulted to `false` on construction")?;
44+
let interface_name = arg.interface.required("interface should be provided")?;
45+
let duration = arg.parameters.duration.required("this should be defaulted to `0(infinite)` on construction")?;
46+
let interval = arg.parameters.interval.required("this should be defaulted to `30 minutes` on construction")?;
47+
let flow_timeout = arg.parameters.timeout.required("this should be defaulted to `10 minutes` on construction")?;
48+
let _sleep_windows = arg.parameters.sleep_windows.required("this should be defaulted to `false`, and now deprecated")?;
4849
let config = Config::new();
4950
let plugin_manager = PluginManager::new().expect("Failed to create plugin manager");
5051
let plugin_worker = plugin_manager.start_worker();
@@ -81,7 +82,10 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
8182

8283
loop {
8384
match cap.next_packet() {
84-
Err(_) => continue,
85+
Err(e) => {
86+
trace!("Error capturing packet: {}", e);
87+
continue;
88+
}
8589
Ok(packet) => {
8690
trace!("received packet");
8791

@@ -172,11 +176,11 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
172176
trace!("flow key detail: {:?}", flow_key);
173177

174178
// Check if the flow has finished
175-
if flags.fin == 1 || flags.rst == 1 {
179+
if flags.is_finished() {
176180
trace!("flow finished");
177181
trace!("flow data: {:?}", flow);
178182

179-
plugin_manager.process_flow_data(*flow).await.unwrap();
183+
plugin_manager.process_flow_data(*flow).await.map_err(|e| FluereError::PluginError(e.to_string()))?;
180184
records.push(*flow);
181185

182186
active_flow.remove(flow_key);
@@ -203,7 +207,7 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
203207
for key in keys {
204208
if let Some(flow) = active_flow.remove(&key) {
205209
trace!("flow expired");
206-
plugin_manager.process_flow_data(flow).await.unwrap();
210+
plugin_manager.process_flow_data(flow).await.map_err(|e| FluereError::PluginError(e.to_string()))?;
207211
records.push(flow);
208212
}
209213
}
@@ -246,15 +250,15 @@ pub async fn packet_capture(arg: Args) -> Result<(), FluereError> {
246250
for (_exp_time, keys) in flow_expirations.iter() {
247251
for key in keys {
248252
if let Some(flow) = active_flow.remove(key) {
249-
plugin_manager.process_flow_data(flow).await.unwrap();
253+
plugin_manager.process_flow_data(flow).await.map_err(|e| FluereError::PluginError(e.to_string()))?;
250254
records.push(flow);
251255
}
252256
}
253257
}
254258

255259
debug!("Captured in {:?}", start.elapsed());
256260
for (_key, flow) in active_flow.iter() {
257-
plugin_manager.process_flow_data(*flow).await.unwrap();
261+
plugin_manager.process_flow_data(*flow).await.map_err(|e| FluereError::PluginError(e.to_string()))?;
258262
records.push(*flow);
259263
}
260264
for task in tasks {

src/net/types/flags.rs

+3
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ impl TcpFlags {
2525
ns: flags[8],
2626
}
2727
}
28+
pub fn is_finished(&self) -> bool {
29+
self.fin == 1 || self.rst == 1
30+
}
2831
}

0 commit comments

Comments
 (0)