Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macos processes #118

Closed
wants to merge 21 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/docs/
__pycache__/
*.xcuserstate
*.DS_Store
141 changes: 138 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ prost = "0.12.0"
tokio-util = { version = "0.7.8", features = ["codec"] }
futures-util = { version = "0.3.28", features = ["sink"] }

sysinfo = "0.29.10"
data-encoding = "2.4.0"

# [patch.crates-io]
# tokio = { path = "../tokio/tokio" }
# smoltcp = { git = 'https://github.com/mhils/smoltcp', rev = 'f65351adfa92db5193f368368cb668bac721fe43' }
Expand All @@ -68,6 +71,10 @@ features = [
[target.'cfg(target_os = "macos")'.dependencies]
apple-security-framework = "2.9.2"
nix = { version = "0.27.1", default-features = false, features = ["fs"] }
core-graphics = "0.23"
core-foundation = "0.9"
cocoa = "0.25"
objc = "0.2"

[dev-dependencies]
env_logger = "0.10"
Expand Down
23 changes: 23 additions & 0 deletions benches/process.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
#[cfg(target_os = "macos")]
use mitmproxy::macos::icons;
#[cfg(windows)]
use mitmproxy::windows::{icons, processes};

Expand Down Expand Up @@ -45,6 +47,27 @@ fn criterion_benchmark(c: &mut Criterion) {
b.iter(processes::active_executables)
});
}
#[cfg(target_os = "macos")]
{
let test_app = std::path::PathBuf::from(
"/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder",
);

c.bench_function("get_png", |b| {
b.iter(|| {
icons::IconCache::default()
.get_png(test_app.clone())
.unwrap();
})
});

let mut cache = icons::IconCache::default();
c.bench_function("get_png (cached)", |b| {
b.iter(|| {
cache.get_png(test_app.clone()).unwrap();
})
});
}
}

criterion_group!(benches, criterion_benchmark);
Expand Down
18 changes: 10 additions & 8 deletions mitmproxy-rs/src/process_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::path::{Path, PathBuf};
use anyhow::Result;
use pyo3::prelude::*;

#[cfg(target_os = "macos")]
use mitmproxy::macos::{icons, processes};
#[allow(unused_imports)]
use mitmproxy::processes::{image, ProcessList};
use mitmproxy::processes::image;
#[cfg(windows)]
use mitmproxy::windows;
use mitmproxy::windows::{icons, processes};

#[pyclass(module = "mitmproxy_rs", frozen)]
pub struct Process(mitmproxy::processes::ProcessInfo);
Expand Down Expand Up @@ -46,13 +48,13 @@ impl Process {
/// *Availability: Windows*
#[pyfunction]
pub fn active_executables() -> PyResult<Vec<Process>> {
#[cfg(windows)]
#[cfg(any(windows, target_os = "macos"))]
{
windows::processes::active_executables()
processes::active_executables()
.map(|p| p.into_iter().map(Process).collect())
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("{}", e)))
}
#[cfg(not(windows))]
#[cfg(not(any(windows, target_os = "macos")))]
Err(pyo3::exceptions::PyNotImplementedError::new_err(
"active_executables is only available on Windows",
))
Expand All @@ -61,15 +63,15 @@ pub fn active_executables() -> PyResult<Vec<Process>> {
#[pyfunction]
#[allow(unused_variables)]
pub fn executable_icon(path: PathBuf) -> Result<PyObject> {
#[cfg(windows)]
#[cfg(any(windows, target_os = "macos"))]
{
let mut icon_cache = windows::icons::ICON_CACHE.lock().unwrap();
let mut icon_cache = icons::ICON_CACHE.lock().unwrap();
let png_bytes = icon_cache.get_png(path)?;
Ok(Python::with_gil(|py| {
pyo3::types::PyBytes::new(py, png_bytes).to_object(py)
}))
}
#[cfg(not(windows))]
#[cfg(not(any(windows, target_os = "macos")))]
Err(pyo3::exceptions::PyNotImplementedError::new_err(
"executable_icon is only available on Windows",
)
Expand Down
4 changes: 2 additions & 2 deletions mitmproxy-rs/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn add_cert(py: Python<'_>, pem: String) -> PyResult<()> {
return Err(anyhow!("{} does not exist", executable_path.display()).into());
}
let der = BASE64.decode(pem_body.as_bytes()).unwrap();
match macos::add_cert(der, executable_path.to_str().unwrap()) {
match macos::certificates::add_cert(der, executable_path.to_str().unwrap()) {
Ok(_) => Ok(()),
Err(e) => Err(PyErr::new::<PyOSError, _>(format!(
"Failed to add certificate: {:?}",
Expand All @@ -109,7 +109,7 @@ pub fn add_cert(py: Python<'_>, pem: String) -> PyResult<()> {
pub fn remove_cert() -> PyResult<()> {
#[cfg(target_os = "macos")]
{
match macos::remove_cert() {
match macos::certificates::remove_cert() {
Ok(_) => Ok(()),
Err(e) => Err(PyErr::new::<PyOSError, _>(format!(
"Failed to remove certificate: {:?}",
Expand Down
File renamed without changes.
Loading
Loading