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
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion mitmproxy-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ boringtun = "0.6"
tar = "0.4.43"
console-subscriber = { version = "0.4.1", optional = true }

[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.29.0", features = ["user"] }

[dev-dependencies]
env_logger = "0.11"
Expand All @@ -40,4 +42,4 @@ tracing = ["console-subscriber"]
[[test]]
name = "test_task"
path = "pytests/test_task.rs"
harness = false
harness = false
1 change: 1 addition & 0 deletions mitmproxy-rs/mitmproxy_rs/tun.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 Down
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, TunInterface};
use crate::server::{create_tun_interface, unavailable_reason, TunInterface};
}

#[pymodule]
Expand Down
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, TunInterface};
pub use tun::{create_tun_interface, unavailable_reason, TunInterface};
pub use udp::{start_udp_server, UdpServer};
pub use wireguard::{start_wireguard_server, WireGuardServer};
21 changes: 20 additions & 1 deletion mitmproxy-rs/src/server/tun.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::server::base::Server;
use pyo3::prelude::*;

#[cfg(target_os = "linux")]
use nix::unistd;

/// An open TUN interface.
///
/// A new tun interface can be created by calling `create_tun_interface`.
Expand Down Expand Up @@ -59,6 +62,22 @@ pub fn create_tun_interface(
}
#[cfg(not(target_os = "linux"))]
Err(pyo3::exceptions::PyNotImplementedError::new_err(
"TUN proxy mode is only available on Linux",
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"))
}
10 changes: 8 additions & 2 deletions src/network/virtual_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ impl VirtualDevice {
}

impl Device for VirtualDevice {
type RxToken<'a> = VirtualRxToken where Self: 'a;
type TxToken<'a> = VirtualTxToken<'a> where Self: 'a;
type RxToken<'a>
= VirtualRxToken
where
Self: 'a;
type TxToken<'a>
= VirtualTxToken<'a>
where
Self: 'a;

fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
if self.rx_buffer.is_empty() {
Expand Down
Loading