diff --git a/mitmproxy-rs/mitmproxy_rs/local.pyi b/mitmproxy-rs/mitmproxy_rs/local.pyi index d10514c0..a524cf46 100644 --- a/mitmproxy-rs/mitmproxy_rs/local.pyi +++ b/mitmproxy-rs/mitmproxy_rs/local.pyi @@ -15,3 +15,5 @@ class LocalRedirector: def set_intercept(self, spec: str) -> None: ... def close(self) -> None: ... async def wait_closed(self) -> None: ... + @staticmethod + def unavailable_reason() -> str | None: ... diff --git a/mitmproxy-rs/mitmproxy_rs/tun.pyi b/mitmproxy-rs/mitmproxy_rs/tun.pyi index 52d898e8..0b8cf979 100644 --- a/mitmproxy-rs/mitmproxy_rs/tun.pyi +++ b/mitmproxy-rs/mitmproxy_rs/tun.pyi @@ -4,7 +4,6 @@ from collections.abc import Awaitable, Callable from typing import final from . import Stream -def unavailable_reason() -> str | None: ... async def create_tun_interface( handle_tcp_stream: Callable[[Stream], Awaitable[None]], handle_udp_stream: Callable[[Stream], Awaitable[None]], @@ -16,3 +15,5 @@ class TunInterface: def close(self) -> None: ... async def wait_closed(self) -> None: ... def __repr__(self) -> str: ... + @staticmethod + def unavailable_reason() -> str | None: ... diff --git a/mitmproxy-rs/src/lib.rs b/mitmproxy-rs/src/lib.rs index fbe3cebb..a44c885d 100644 --- a/mitmproxy-rs/src/lib.rs +++ b/mitmproxy-rs/src/lib.rs @@ -62,7 +62,7 @@ mod mitmproxy_rs { #[pymodule] mod tun { #[pymodule_export] - use crate::server::{create_tun_interface, unavailable_reason, TunInterface}; + use crate::server::{create_tun_interface, TunInterface}; } #[pymodule] diff --git a/mitmproxy-rs/src/server/local_redirector.rs b/mitmproxy-rs/src/server/local_redirector.rs index 554e2d4d..dc0500d2 100644 --- a/mitmproxy-rs/src/server/local_redirector.rs +++ b/mitmproxy-rs/src/server/local_redirector.rs @@ -62,6 +62,25 @@ impl LocalRedirector { self.server.wait_closed(py) } + /// Returns a `str` describing why local redirect mode is unavailable, or `None` if it is available. + /// + /// Reasons for unavailability may be an unsupported platform, or missing privileges. + #[staticmethod] + pub fn unavailable_reason() -> Option { + #[cfg(any(windows, target_os = "macos"))] + return None; + + // #[cfg(target_os = "linux")] + // if !unistd::geteuid().is_root() { + // Some(String::from("mitmproxy is not running as root")) + // } else { + // None + // } + + #[cfg(not(any(windows, target_os = "macos")))] + Some(String::from("OS not supported for local redirect mode")) + } + pub fn __repr__(&self) -> String { format!("Local Redirector({})", self.spec) } @@ -135,6 +154,6 @@ pub fn start_local_redirector( } #[cfg(not(any(windows, target_os = "macos")))] Err(pyo3::exceptions::PyNotImplementedError::new_err( - "OS proxy mode is only available on Windows and macOS", + LocalRedirector::unavailable_reason(), )) } diff --git a/mitmproxy-rs/src/server/mod.rs b/mitmproxy-rs/src/server/mod.rs index 2b1aa8d8..cfc29981 100644 --- a/mitmproxy-rs/src/server/mod.rs +++ b/mitmproxy-rs/src/server/mod.rs @@ -5,6 +5,6 @@ mod udp; mod wireguard; pub use local_redirector::{start_local_redirector, LocalRedirector}; -pub use tun::{create_tun_interface, unavailable_reason, TunInterface}; +pub use tun::{create_tun_interface, TunInterface}; pub use udp::{start_udp_server, UdpServer}; pub use wireguard::{start_wireguard_server, WireGuardServer}; diff --git a/mitmproxy-rs/src/server/tun.rs b/mitmproxy-rs/src/server/tun.rs index 4525a4f9..261287bd 100644 --- a/mitmproxy-rs/src/server/tun.rs +++ b/mitmproxy-rs/src/server/tun.rs @@ -31,6 +31,22 @@ impl TunInterface { self.server.wait_closed(py) } + /// Returns a `str` describing why tun mode is unavailable, or `None` if TUN mode is available. + /// + /// Reasons for unavailability may be an unsupported platform, or missing privileges. + #[staticmethod] + pub fn unavailable_reason() -> Option { + #[cfg(target_os = "linux")] + if !unistd::geteuid().is_root() { + Some(String::from("mitmproxy is not running as root")) + } else { + None + } + + #[cfg(not(target_os = "linux"))] + Some(String::from("OS not supported for TUN proxy mode")) + } + pub fn __repr__(&self) -> String { format!("TunInterface({})", self.tun_name) } @@ -62,22 +78,6 @@ pub fn create_tun_interface( } #[cfg(not(target_os = "linux"))] Err(pyo3::exceptions::PyNotImplementedError::new_err( - unavailable_reason(), + TunInterface::unavailable_reason(), )) } - -/// Returns a `str` describing why tun mode is unavailable, or `None` if TUN mode is available. -/// -/// Reasons for unavailability may be an unsupported platform, or missing privileges. -#[pyfunction] -pub fn unavailable_reason() -> Option { - #[cfg(target_os = "linux")] - if !unistd::geteuid().is_root() { - Some(String::from("mitmproxy is not running as root")) - } else { - None - } - - #[cfg(not(target_os = "linux"))] - Some(String::from("OS not supported for TUN proxy mode")) -}