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 redirector rust side #69

Closed

Conversation

emanuele-em
Copy link
Member

macos-redirector rust side without ipc protobuf

Comment on lines 21 to 32
#[allow(dead_code)]
pub struct PipeServer {
ip_rx: pipe::Receiver,
ip_tx: pipe::Sender,
net_rx: pipe::Receiver,
net_tx: pipe::Sender,
filter_rx: pipe::Receiver,
filter_tx: pipe::Sender,
ip_path: PathBuf,
net_path: PathBuf,
filter_path: PathBuf,
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may sound strange but we have to save both ends of the pipe somewhere because otherwise rust compiler notices that the variable is not used and it make it out of scope, and if it makes it out of scope it makes the pipe unreadable or unwritable and swift can't read or write from it

Comment on lines 41 to 43
let ip_path = Path::new(&home_dir).join(format!("Downloads/{}.pipe", &ip_pipe));
let net_path = Path::new(&home_dir).join(format!("Downloads/{}.pipe", &net_pipe));
let filter_path = Path::new(&home_dir).join(format!("Downloads/{}.pipe", &filter_pipe));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only Downloads or Movies or Pictures dir are avalaible to be written or read from swift (for now)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should eventually take a closer look at https://developer.apple.com/documentation/security/app_sandbox/accessing_files_from_the_macos_app_sandbox#4144035, but let's get things working first. :)

net_rx: Receiver<NetworkCommand>,
sd_watcher: broadcast::Receiver<()>,
) -> Result<(MacosTask, Self::Data)> {
let executable_path = "/Applications/MitmproxyAppleTunnel.app/";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The app works only if we launch it from /Applications/ folder

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, when/how are we moving it there?

src/packet_sources/macos.rs Outdated Show resolved Hide resolved
@emanuele-em emanuele-em requested a review from mhils August 2, 2023 08:58
Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🚀🚀

src/messages.rs Outdated
@@ -15,6 +15,9 @@ pub enum TunnelInfo {
pid: u32,
process_name: Option<PathBuf>,
},
Macos {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: MacOS?

net_rx: Receiver<NetworkCommand>,
sd_watcher: broadcast::Receiver<()>,
) -> Result<(MacosTask, Self::Data)> {
let executable_path = "/Applications/MitmproxyAppleTunnel.app/";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, when/how are we moving it there?

src/packet_sources/macos.rs Outdated Show resolved Hide resolved
let tx = match pipe::OpenOptions::new().unchecked(true).open_sender(path) {
Ok(tx) => tx,
Err(e) => Err(anyhow!("Failed to open fifo transmitter: {:?}", e))?,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we opening all pipes both for reading and for sending? I would have expected that we open one for reading, and one for sending, and the Swift side does the same (just opposite).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that drove me crazy, too. It seems in swift these pipes are handled as normal input and output files, however, if they are not already open (and saved, otherwise rust destroys the variables) then swift returns an error

Copy link
Member

@mhils mhils Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to replicate these issues at https://github.com/mhils/rust-swift-fifo, things seem to work as expected here.
The one tricky part is waiting on the Rust side until the pipe becomes readable, see my example here. Can we check this again please? :)

Comment on lines 41 to 43
let ip_path = Path::new(&home_dir).join(format!("Downloads/{}.pipe", &ip_pipe));
let net_path = Path::new(&home_dir).join(format!("Downloads/{}.pipe", &net_pipe));
let filter_path = Path::new(&home_dir).join(format!("Downloads/{}.pipe", &filter_pipe));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should eventually take a closer look at https://developer.apple.com/documentation/security/app_sandbox/accessing_files_from_the_macos_app_sandbox#4144035, but let's get things working first. :)

src/packet_sources/macos.rs Outdated Show resolved Hide resolved
src/packet_sources/macos.rs Show resolved Hide resolved
src/packet_sources/macos.rs Outdated Show resolved Hide resolved
ffi/src/server.rs Outdated Show resolved Hide resolved
fs::copy(&src_path, &dst_path).expect("Failed to copy {src_path} to {dst_path}");
}
}
}
Copy link
Member

@mhils mhils Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return a result instead of a blind panic (and not use expect/unwrap internally). :)

In contrast to the build.rs script (where panic is an ok user experience), this happens on the user's machine, so we should have a proper error message.

#[cfg(target_os = "macos")]
{
copy_dir(
Path::new("mitmproxy_rs/MitmProxyAppleTunnel.app/"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This here won't work depending on your current working directory. Take a look at how we do it for Windows above with py.import(...).filename(). :)

@emanuele-em emanuele-em deleted the macos_redirector_rust_side branch August 18, 2023 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants