Skip to main content

prt_core/platform/
mod.rs

1//! Platform-specific port scanning.
2//!
3//! - **macOS**: parses `lsof -F` output + batch `ps` calls (2 per cycle)
4//! - **Linux**: reads `/proc/net/{tcp,tcp6,udp,udp6}` via `procfs` crate
5
6#[cfg(target_os = "linux")]
7mod linux;
8#[cfg(target_os = "macos")]
9mod macos;
10
11use crate::model::PortEntry;
12use anyhow::Result;
13
14/// Scan all visible network ports (unprivileged).
15pub fn scan_ports() -> Result<Vec<PortEntry>> {
16    #[cfg(target_os = "linux")]
17    {
18        linux::scan()
19    }
20    #[cfg(target_os = "macos")]
21    {
22        macos::scan()
23    }
24    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
25    {
26        anyhow::bail!("unsupported platform")
27    }
28}
29
30/// Scan with cached sudo credentials (`sudo -n`).
31pub fn scan_ports_elevated() -> Result<Vec<PortEntry>> {
32    #[cfg(target_os = "macos")]
33    {
34        macos::scan_elevated()
35    }
36    #[cfg(target_os = "linux")]
37    {
38        linux::scan()
39    }
40    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
41    {
42        anyhow::bail!("unsupported platform")
43    }
44}
45
46/// Scan with explicit sudo password piped via stdin (`sudo -S`).
47pub fn scan_ports_with_sudo(password: &str) -> Result<Vec<PortEntry>> {
48    #[cfg(target_os = "macos")]
49    {
50        macos::scan_with_sudo(password)
51    }
52    #[cfg(target_os = "linux")]
53    {
54        let _ = password;
55        linux::scan()
56    }
57    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
58    {
59        let _ = password;
60        anyhow::bail!("unsupported platform")
61    }
62}