Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macos: improve redirector error messages #186

Merged
merged 5 commits into from
Oct 17, 2024
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
10 changes: 4 additions & 6 deletions src/certificates/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ use anyhow::{anyhow, Result};
use security_framework::{
certificate::SecCertificate,
item::{
add_item, AddRef, ItemAddOptions, ItemAddValue, ItemClass, ItemSearchOptions, Reference,
SearchResult,
AddRef, ItemAddOptions, ItemAddValue, ItemClass, ItemSearchOptions, Reference, SearchResult,
},
};
use tokio::process::Command;

pub fn add_cert(der: Vec<u8>, path: &str) -> Result<()> {
let cert = SecCertificate::from_der(&der)?;
let add_ref = AddRef::Certificate(cert);
let add_option = ItemAddOptions::new(ItemAddValue::Ref(add_ref))
.set_label("mitmproxy")
.to_dictionary();
let mut add_option = ItemAddOptions::new(ItemAddValue::Ref(add_ref));
add_option.set_label("mitmproxy");

let search_result = ItemSearchOptions::new()
.class(ItemClass::certificate())
Expand All @@ -26,7 +24,7 @@ pub fn add_cert(der: Vec<u8>, path: &str) -> Result<()> {
cert.delete()?;
}

add_item(add_option)?;
add_option.add()?;

Command::new("open")
.arg(path)
Expand Down
15 changes: 11 additions & 4 deletions src/packet_sources/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,16 @@ impl ConnectionTask {
match new_flow {
NewFlow {
message: Some(ipc::new_flow::Message::Tcp(tcp_flow)),
} => self.handle_tcp(tcp_flow).await,
} => self
.handle_tcp(tcp_flow)
.await
.context("failed to handle TCP stream"),
NewFlow {
message: Some(ipc::new_flow::Message::Udp(udp_flow)),
} => self.handle_udp(udp_flow).await,
} => self
.handle_udp(udp_flow)
.await
.context("failed to handle UDP stream"),
_ => bail!("Received invalid IPC message: {:?}", new_flow),
}
}
Expand All @@ -228,7 +234,8 @@ impl ConnectionTask {
let Some(addr) = &flow.local_address else {
bail!("no local address")
};
SocketAddr::try_from(addr)?
SocketAddr::try_from(addr)
.with_context(|| format!("invalid local_address: {:?}", addr))?
};
let mut remote_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0);
let (command_tx, mut command_rx) = unbounded_channel();
Expand All @@ -246,7 +253,7 @@ impl ConnectionTask {
).context("invalid IPC message")?;
let dst_addr = {
let Some(dst_addr) = &packet.remote_address else { bail!("no remote addr") };
SocketAddr::try_from(dst_addr).context("invalid socket address")?
SocketAddr::try_from(dst_addr).with_context(|| format!("invalid remote_address: {:?}", dst_addr))?
};

// We can only send ConnectionEstablished once we know the destination address.
Expand Down
Loading