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

[QUESTION] - Passive mode with custom provided TcpStream #89

Closed
markhaehnel opened this issue Oct 9, 2024 · 6 comments · Fixed by #91
Closed

[QUESTION] - Passive mode with custom provided TcpStream #89

markhaehnel opened this issue Oct 9, 2024 · 6 comments · Fixed by #91
Assignees
Labels
new feature A new feature request question Further information is requested

Comments

@markhaehnel
Copy link

markhaehnel commented Oct 9, 2024

Hi, i'm trying to connect to a FTP server over a SOCKS5 proxy. Connecting works fine.

I just connect with FtpStream::connect_with_stream(stream.into_inner())? where stream is my SOCKS5 TcpStream.

The problem is now: In passive mode, when i want to retrieve a file, the connection to the server is not done via the proxy (provided TcpStream).
It instantiates a new TcpStream internally.

Is there a way to achieve connecting over a custom provided TcpStream when doing retr_as_stream?

Best regards,
Mark

@markhaehnel markhaehnel added the question Further information is requested label Oct 9, 2024
@veeso
Copy link
Owner

veeso commented Oct 14, 2024

Hi,

I'm not sure how it should work though. The TcpStream is instantiated using the addr returned by pasv, so how can you know the address prior to this? If you could provide more details with a full flow it would be great for me to understand exactly how it should be implemented.

@markhaehnel
Copy link
Author

markhaehnel commented Oct 14, 2024

The current flow is as follows:

let stream = Socks5Stream::connect("localhost:1080", address)?;
let mut client = FtpStream::connect_with_stream(stream.into_inner())?;

client.set_mode(Mode::Passive);
client.login(username, password)?;

let mut file_to_write = std::fs::File::create(format!("dl/{}", filename))?;
let mut retr_stream = client.retr_as_stream(filename)?;
std::io::copy(&mut retr_stream, &mut file_to_write)?;
client.finalize_retr_stream(retr_stream)?;

client.quit()?;

For solving this my idea would be that one could provide a optional function that creates a TcpStream for the client.
Kind of like this:

let stream = Socks5Stream::connect("localhost:1080", address)?;
let mut client = FtpStream::connect_with_stream(stream.into_inner())?;

client.stream_builder(|address| {
    let stream = TcpStream::connect(address)?;
    Ok(stream)

    // or if you want to use a socks5 proxy
    let stream = Socks5Stream::connect("localhost:1080", address)?;
    Ok(stream.into_inner())
});

client.set_mode(Mode::Passive);
client.login(username, password)?;

let mut file_to_write = std::fs::File::create(format!("dl/{}", filename))?;
let mut retr_stream = client.retr_as_stream(filename)?;
std::io::copy(&mut retr_stream, &mut file_to_write)?;
client.finalize_retr_stream(retr_stream)?;

client.quit()?;

This function would be called whenever the client needs to create a new TcpStream for the data connection. For example here.

What do you think of this?

BTW, i would be happy to provide the implementation in a PR once we agree on a solution.

@veeso
Copy link
Owner

veeso commented Oct 14, 2024

I guess it could work. I'll try to implement this.

@veeso veeso added the new feature A new feature request label Oct 14, 2024
@markhaehnel
Copy link
Author

Mind if I submit a Pull Request with my proposed changes?

@veeso
Copy link
Owner

veeso commented Oct 14, 2024

I actually was already implementing it 😅

@markhaehnel
Copy link
Author

No problem, go on! 😄

@veeso veeso linked a pull request Oct 14, 2024 that will close this issue
@veeso veeso closed this as completed in #91 Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature request question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants