Skip to content

Commit e3f39d1

Browse files
add LocalRedirector.unavailable_reason() (#202)
* add LocalRedirector.unavailable_reason() * [autofix.ci] apply automated fixes * add methods to .pyi file * [autofix.ci] apply automated fixes * mark method as static * comment out linux stuff --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 5f7480a commit e3f39d1

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

mitmproxy-rs/mitmproxy_rs/local.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ class LocalRedirector:
1515
def set_intercept(self, spec: str) -> None: ...
1616
def close(self) -> None: ...
1717
async def wait_closed(self) -> None: ...
18+
@staticmethod
19+
def unavailable_reason() -> str | None: ...

mitmproxy-rs/mitmproxy_rs/tun.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ from collections.abc import Awaitable, Callable
44
from typing import final
55
from . import Stream
66

7-
def unavailable_reason() -> str | None: ...
87
async def create_tun_interface(
98
handle_tcp_stream: Callable[[Stream], Awaitable[None]],
109
handle_udp_stream: Callable[[Stream], Awaitable[None]],
@@ -16,3 +15,5 @@ class TunInterface:
1615
def close(self) -> None: ...
1716
async def wait_closed(self) -> None: ...
1817
def __repr__(self) -> str: ...
18+
@staticmethod
19+
def unavailable_reason() -> str | None: ...

mitmproxy-rs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod mitmproxy_rs {
6262
#[pymodule]
6363
mod tun {
6464
#[pymodule_export]
65-
use crate::server::{create_tun_interface, unavailable_reason, TunInterface};
65+
use crate::server::{create_tun_interface, TunInterface};
6666
}
6767

6868
#[pymodule]

mitmproxy-rs/src/server/local_redirector.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ impl LocalRedirector {
6262
self.server.wait_closed(py)
6363
}
6464

65+
/// Returns a `str` describing why local redirect mode is unavailable, or `None` if it is available.
66+
///
67+
/// Reasons for unavailability may be an unsupported platform, or missing privileges.
68+
#[staticmethod]
69+
pub fn unavailable_reason() -> Option<String> {
70+
#[cfg(any(windows, target_os = "macos"))]
71+
return None;
72+
73+
// #[cfg(target_os = "linux")]
74+
// if !unistd::geteuid().is_root() {
75+
// Some(String::from("mitmproxy is not running as root"))
76+
// } else {
77+
// None
78+
// }
79+
80+
#[cfg(not(any(windows, target_os = "macos")))]
81+
Some(String::from("OS not supported for local redirect mode"))
82+
}
83+
6584
pub fn __repr__(&self) -> String {
6685
format!("Local Redirector({})", self.spec)
6786
}
@@ -135,6 +154,6 @@ pub fn start_local_redirector(
135154
}
136155
#[cfg(not(any(windows, target_os = "macos")))]
137156
Err(pyo3::exceptions::PyNotImplementedError::new_err(
138-
"OS proxy mode is only available on Windows and macOS",
157+
LocalRedirector::unavailable_reason(),
139158
))
140159
}

mitmproxy-rs/src/server/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ mod udp;
55
mod wireguard;
66

77
pub use local_redirector::{start_local_redirector, LocalRedirector};
8-
pub use tun::{create_tun_interface, unavailable_reason, TunInterface};
8+
pub use tun::{create_tun_interface, TunInterface};
99
pub use udp::{start_udp_server, UdpServer};
1010
pub use wireguard::{start_wireguard_server, WireGuardServer};

mitmproxy-rs/src/server/tun.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ impl TunInterface {
3131
self.server.wait_closed(py)
3232
}
3333

34+
/// Returns a `str` describing why tun mode is unavailable, or `None` if TUN mode is available.
35+
///
36+
/// Reasons for unavailability may be an unsupported platform, or missing privileges.
37+
#[staticmethod]
38+
pub fn unavailable_reason() -> Option<String> {
39+
#[cfg(target_os = "linux")]
40+
if !unistd::geteuid().is_root() {
41+
Some(String::from("mitmproxy is not running as root"))
42+
} else {
43+
None
44+
}
45+
46+
#[cfg(not(target_os = "linux"))]
47+
Some(String::from("OS not supported for TUN proxy mode"))
48+
}
49+
3450
pub fn __repr__(&self) -> String {
3551
format!("TunInterface({})", self.tun_name)
3652
}
@@ -62,22 +78,6 @@ pub fn create_tun_interface(
6278
}
6379
#[cfg(not(target_os = "linux"))]
6480
Err(pyo3::exceptions::PyNotImplementedError::new_err(
65-
unavailable_reason(),
81+
TunInterface::unavailable_reason(),
6682
))
6783
}
68-
69-
/// Returns a `str` describing why tun mode is unavailable, or `None` if TUN mode is available.
70-
///
71-
/// Reasons for unavailability may be an unsupported platform, or missing privileges.
72-
#[pyfunction]
73-
pub fn unavailable_reason() -> Option<String> {
74-
#[cfg(target_os = "linux")]
75-
if !unistd::geteuid().is_root() {
76-
Some(String::from("mitmproxy is not running as root"))
77-
} else {
78-
None
79-
}
80-
81-
#[cfg(not(target_os = "linux"))]
82-
Some(String::from("OS not supported for TUN proxy mode"))
83-
}

0 commit comments

Comments
 (0)