Skip to content

Commit

Permalink
macos: improve redirector error messages (#186)
Browse files Browse the repository at this point in the history
* macos: improve redirector error messages

* [autofix.ci] apply automated fixes

* adapt to security_framework api changes

* fixups

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
mhils and autofix-ci[bot] authored Oct 17, 2024
1 parent a59d333 commit fc23896
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
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

0 comments on commit fc23896

Please sign in to comment.