Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ src-tauri/vendor/**/target/**.exe
# Tauri
target
/target

# Nix
shell.nix

# Dev tools
gen_pcap.py
test.pcap
3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ sysinfo = "0.38.3"
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-cli = "2"
tauri-plugin-global-shortcut = "2"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.59", features = ["Win32_Storage_FileSystem"] }
79 changes: 67 additions & 12 deletions src-tauri/src/commandes/import/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use log::{error, info};
use packet_parser::PacketFlow;
use pcap::Capture;
use std::fs::File;
#[cfg(unix)]
use std::os::unix::io::IntoRawFd;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

L'utilisation de std::os::unix::io::IntoRawFd rend ce code spécifique à Unix et ne compilera pas sur d'autres plateformes comme Windows. Pour maintenir la compatibilité multiplateforme, qui semble être un objectif du projet (présence de .exe dans .gitignore), il est préférable d'utiliser la compilation conditionnelle.

Suggested change
use std::os::unix::io::IntoRawFd;
#[cfg(unix)]
use std::os::unix::io::IntoRawFd;

use std::sync::{Arc, Mutex};
use tauri::{State, ipc::Channel};

Expand All @@ -13,13 +16,70 @@ use crate::{
},
};

fn open_capture(file_path: &str) -> Result<Capture<pcap::Offline>, CaptureStateError> {
#[cfg(unix)]
{
let file = File::open(file_path).map_err(|e| {
CaptureStateError::Import(PcapImportError::OpenFileError(
file_path.to_string(),
e.to_string(),
))
})?;
let fd = file.into_raw_fd();
// SAFETY: fd est valide et nous en sommes propriétaires. `from_raw_fd` en prend possession.
unsafe { Capture::from_raw_fd(fd) }.map_err(|e| {
CaptureStateError::Import(PcapImportError::OpenFileError(
file_path.to_string(),
e.to_string(),
))
})
}
#[cfg(windows)]
{
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Storage::FileSystem::GetShortPathNameW;

// Convertit le chemin en UTF-16 pour l'API Windows
let wide: Vec<u16> = OsStr::new(file_path)
.encode_wide()
.chain(std::iter::once(0))
.collect();

// Récupère la taille du buffer nécessaire pour le chemin court (8.3)
let len = unsafe { GetShortPathNameW(wide.as_ptr(), std::ptr::null_mut(), 0) };

let short_path = if len > 0 {
let mut buf = vec![0u16; len as usize];
unsafe { GetShortPathNameW(wide.as_ptr(), buf.as_mut_ptr(), len) };
// Retire le null-terminator
String::from_utf16_lossy(&buf[..len as usize - 1])
} else {
// Fallback si GetShortPathNameW échoue (ex: short names désactivés)
file_path.to_string()
};

Capture::from_file(&short_path).map_err(|e| {
CaptureStateError::Import(PcapImportError::OpenFileError(
file_path.to_string(),
e.to_string(),
))
})
}

#[cfg(not(any(unix, windows)))]
{
Capture::from_file(file_path).map_err(|e| {
CaptureStateError::Import(PcapImportError::OpenFileError(
file_path.to_string(),
e.to_string(),
))
})
}
}
Comment on lines +19 to +79

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Cette implémentation de open_capture est spécifique à Unix à cause de into_raw_fd et from_raw_fd. Cela va causer des erreurs de compilation sur Windows. Je suggère d'utiliser la compilation conditionnelle pour fournir une implémentation pour les systèmes Unix et de conserver l'ancienne méthode pour les autres systèmes (comme Windows). Cela permettra à l'application de continuer à compiler sur toutes les plateformes, même si le bug des chemins d'accès persiste sous Windows en attendant une solution dédiée.

fn open_capture(file_path: &str) -> Result<Capture<pcap::Offline>, CaptureStateError> {
    #[cfg(unix)]
    {
        let file = File::open(file_path).map_err(|e| {
            CaptureStateError::Import(PcapImportError::OpenFileError(
                file_path.to_string(),
                e.to_string(),
            ))
        })?;
        let fd = file.into_raw_fd();
        // SAFETY: fd est valide et nous en sommes propriétaires. `from_raw_fd` en prend possession.
        unsafe { Capture::from_raw_fd(fd) }.map_err(|e| {
            CaptureStateError::Import(PcapImportError::OpenFileError(
                file_path.to_string(),
                e.to_string(),
            ))
        })
    }
    #[cfg(not(unix))]
    {
        // Conserve l'ancienne implémentation pour les plateformes non-Unix.
        // Le bug des chemins existera toujours sur Windows, mais le code compilera.
        Capture::from_file(file_path).map_err(|e| {
            CaptureStateError::Import(PcapImportError::OpenFileError(
                file_path.to_string(),
                e.to_string(),
            ))
        })
    }
}


fn count_packets_in_pcap(file_path: &str) -> Result<usize, CaptureStateError> {
let mut cap = Capture::from_file(file_path).map_err(|e| {
CaptureStateError::Import(PcapImportError::OpenFileError(
file_path.to_string(),
e.to_string(),
))
})?;
let mut cap = open_capture(file_path)?;

let mut count: usize = 0;
while cap.next_packet().is_ok() {
Expand Down Expand Up @@ -89,12 +149,7 @@ fn handle_pcap_file(
file_path, total
);

let mut cap = Capture::from_file(file_path).map_err(|e| {
CaptureStateError::Import(PcapImportError::OpenFileError(
file_path.to_string(),
e.to_string(),
))
})?;
let mut cap = open_capture(file_path)?;

let mut packet_count: usize = 0;

Expand Down