Skip to content

Commit

Permalink
clean up log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Dec 30, 2024
1 parent a61ff20 commit 0f38f84
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
24 changes: 11 additions & 13 deletions mitmproxy-linux/src/main2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ async fn main() -> anyhow::Result<()> {

bump_memlock_rlimit();

info!("Creating tun device...");
debug!("Creating tun device...");
let (mut device, name) = create_tun_device(None)?;
let device_index = device.tun_index().context("failed to get tun device index")? as u32;
info!("Tun device created: {name} (id={device_index})");
debug!("Tun device created: {name} (id={device_index})");

info!("Loading BPF program...");
debug!("Loading BPF program...");
let mut ebpf = EbpfLoader::new()
.btf(Btf::from_sys_fs().ok().as_ref())
.set_global("INTERFACE_ID", &device_index, true)
Expand All @@ -65,22 +65,22 @@ async fn main() -> anyhow::Result<()> {
warn!("failed to initialize eBPF logger: {}", e);
}

info!("Attaching BPF_CGROUP_INET_SOCK_CREATE program...");
debug!("Attaching BPF_CGROUP_INET_SOCK_CREATE program...");
let prog: &mut CgroupSock = ebpf.program_mut("cgroup_sock_create").context("failed to get cgroup_sock_create")?.try_into()?;
// root cgroup to get all events.
let cgroup = std::fs::File::open("/sys/fs/cgroup/")?;
prog.load()?;
prog.attach(&cgroup, CgroupAttachMode::Single)?;

info!("Getting INTERCEPT_CONF map...");
debug!("Getting INTERCEPT_CONF map...");
let mut intercept_conf = {
let map = ebpf.map_mut("INTERCEPT_CONF")
.context("couldn't get INTERCEPT_CONF map")?;
Array::<_, ActionWrapper>::try_from(map)
.context("Cannot cast INTERCEPT_CONF to Array")?
};

info!("Connecting to {}...", mitmproxy_addr.display());
debug!("Connecting to {}...", mitmproxy_addr.display());
let ipc = UnixDatagram::bind(&redirector_addr)
.with_context(|| format!("failed to bind to {}", redirector_addr.display()))?;
ipc.connect(&mitmproxy_addr)
Expand All @@ -97,21 +97,19 @@ async fn main() -> anyhow::Result<()> {
r = ipc.recv_buf(&mut ipc_buf) => {
match r {
Ok(len) if len > 0 => {
info!("Received IPC message len: {}", ipc_buf.len());

let Ok(FromProxy { message: Some(message)}) = FromProxy::decode(&mut ipc_buf) else {
return Err(anyhow!("Received invalid IPC message: {:?}", &ipc_buf[..len]));
};
assert_eq!(ipc_buf.len(), 0);
info!("Received IPC message: {message:?}");
// debug!("Received IPC message: {message:?}");

match message {
from_proxy::Message::Packet(packet) => {
info!("Forwarding Packet to device: {}", packet.data.len());
// debug!("Forwarding Packet to device: {}", packet.data.len());
device.send(&packet.data).await.context("failed to send packet")?;
}
from_proxy::Message::InterceptConf(conf) => {
info!("Received InterceptConf: {conf:?}");
debug!("Updating ebpf intercept conf: {conf:?}");
if conf.actions.len() > INTERCEPT_CONF_LEN as usize {
error!("Truncating intercept conf to {INTERCEPT_CONF_LEN} elements.");
}
Expand Down Expand Up @@ -145,7 +143,7 @@ async fn main() -> anyhow::Result<()> {
packet.encode(&mut ipc_buf)?;
let encoded = ipc_buf.split();

info!("Sending packet to proxy: {} {:?}", encoded.len(), &encoded);
// debug!("Sending packet to proxy: {} {:?}", encoded.len(), &encoded);
ipc.send(&encoded).await?;
},
}
Expand All @@ -161,6 +159,6 @@ fn bump_memlock_rlimit() {
};
let ret = unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) };
if ret != 0 {
debug!("remove limit on locked memory failed, ret is: {}", ret);
info!("remove limit on locked memory failed, ret is: {}", ret);
}
}
21 changes: 12 additions & 9 deletions mitmproxy-rs/src/server/local_redirector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ impl LocalRedirector {
#[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"))
#[cfg(target_os = "linux")]
if nix::unistd::geteuid().is_root() {
None
} else {
Some("mitmproxy is not running as root.".to_string())
}

#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
Some(format!(
"Local redirect mode is not supported on {}",
std::env::consts::OS
))
}

pub fn __repr__(&self) -> String {
Expand Down
18 changes: 11 additions & 7 deletions src/network/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ pub(super) fn handle_icmpv6_echo_request(
}
};

// Checking that it is an ICMP Echo Request.
if input_icmpv6_packet.msg_type() != Icmpv6Message::EchoRequest {
log::debug!(
"Unsupported ICMPv6 packet of type: {}",
input_icmpv6_packet.msg_type()
);
return None;
match input_icmpv6_packet.msg_type() {
Icmpv6Message::EchoRequest => (),
Icmpv6Message::RouterSolicit => {
// These happen in Linux local redirect mode, not investigated any further.
log::debug!("Ignoring ICMPv6 router solicitation.");
return None;
}
other => {
log::debug!("Unsupported ICMPv6 packet of type: {other}");
return None;
}
}

// Creating fake response packet.
Expand Down
10 changes: 4 additions & 6 deletions src/packet_sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::messages::{
use crate::network::add_network_layer;
use crate::{ipc, MAX_PACKET_SIZE};
use anyhow::{anyhow, Context, Result};
use log::{info, warn};
use prost::bytes::{Bytes, BytesMut};
use prost::Message;
use std::future::Future;
Expand Down Expand Up @@ -70,7 +69,7 @@ async fn forward_packets<T: AsyncRead + AsyncWrite + Unpin>(
};
msg.encode(&mut buf)?;

info!("Sending IPC message to redirector: {} {:?}", buf.len(), buf);
// debug!("Sending IPC message to redirector: {} {:?}", buf.len(), buf);
channel.write_all_buf(&mut buf).await.context("failed to propagate interception config update")?;
},
// read packets from the IPC pipe into our network stack.
Expand Down Expand Up @@ -99,7 +98,7 @@ async fn forward_packets<T: AsyncRead + AsyncWrite + Unpin>(
continue;
};

warn!("Receiving packet: {:?}", &packet);
// debug!("Receiving packet: {:?}", &packet);

// WinDivert packets do not have correct IP checksums yet, we need fix that here
// otherwise smoltcp will be unhappy with us.
Expand All @@ -120,12 +119,11 @@ async fn forward_packets<T: AsyncRead + AsyncWrite + Unpin>(
// write packets from the network stack to the IPC pipe to be reinjected.
Some(e) = net_rx.recv() => {
match e {
NetworkCommand::SendPacket(mut packet) => {
packet.fill_ip_checksum(); // FIXME probably unnecessary.
NetworkCommand::SendPacket(packet) => {
let packet = ipc::FromProxy { message: Some(ipc::from_proxy::Message::Packet( ipc::Packet { data: Bytes::from(packet.into_inner()) }))};
assert!(buf.is_empty());
packet.encode(&mut buf)?;
warn!("Sending packet: {} {:?}", buf.len(), &packet.message.as_ref().unwrap());
// debug!("Sending packet: {} {:?}", buf.len(), &packet.message.as_ref().unwrap());
channel.write_all_buf(&mut buf).await.context("failed to send packet")?;
}
}
Expand Down

0 comments on commit 0f38f84

Please sign in to comment.