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
use chrono::{DateTime, Utc};
use log::debug;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;

pub enum DumpType {
    ClientRequest,
    // Header
}

impl core::fmt::Display for DumpType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            DumpType::ClientRequest => f.write_str("client_request"),
        }
    }
}

/// Dumps the bytes of a given vector to a templated packet
pub async fn dump_bytes(bytes: Vec<u8>, dump_type: DumpType) {
    let now: DateTime<Utc> = Utc::now();

    debug!("bytes: {:?}", bytes);
    let filename = format!(
        "./captures/{}-{}.cap",
        dump_type,
        now.format("%Y-%m-%dT%H%M%SZ")
    );
    let mut fh = match File::create(&filename).await {
        Ok(value) => value,
        Err(error) => panic!("couldn't open {} for writing: {:?}", filename, error),
    };

    match fh.write_all(&bytes).await {
        Ok(_) => debug!("Successfully wrote packet to {}", &filename),
        Err(error) => debug!("Failed to write to {}: {:?}", filename, error),
    };
}