Skip to content
Merged
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
2 changes: 2 additions & 0 deletions mitmproxy-rs/mitmproxy_rs/local.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
3 changes: 2 additions & 1 deletion mitmproxy-rs/mitmproxy_rs/tun.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand All @@ -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: ...
2 changes: 1 addition & 1 deletion mitmproxy-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 20 additions & 1 deletion mitmproxy-rs/src/server/local_redirector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
#[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)
}
Expand Down Expand Up @@ -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(),
))
}
2 changes: 1 addition & 1 deletion mitmproxy-rs/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
34 changes: 17 additions & 17 deletions mitmproxy-rs/src/server/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
#[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)
}
Expand Down Expand Up @@ -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<String> {
#[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"))
}
Loading