Skip to main content

kget/ftp/
client.rs

1//! FTP client implementation.
2
3use std::error::Error;
4use std::io::Write;
5use url::Url;
6use suppaftp::FtpStream;
7use crate::progress::create_progress_bar;
8use crate::config::ProxyConfig;
9use crate::optimization::Optimizer;
10use crate::utils::print;
11
12/// FTP file downloader with progress tracking.
13///
14/// Supports:
15/// - Anonymous FTP (use "anonymous" as username)
16/// - Authenticated FTP with username/password in URL
17/// - Binary transfer mode (safe for all file types)
18/// - SOCKS5 proxy connections
19///
20/// # URL Format
21///
22/// ```text
23/// ftp://[user[:password]@]host[:port]/path/to/file
24/// ```
25///
26/// # Example
27///
28/// ```rust,no_run
29/// use kget::ftp::FtpDownloader;
30/// use kget::{ProxyConfig, Optimizer};
31///
32/// // Anonymous download
33/// let dl = FtpDownloader::new(
34///     "ftp://ftp.gnu.org/gnu/emacs/emacs-28.2.tar.gz".to_string(),
35///     "emacs-28.2.tar.gz".to_string(),
36///     false,
37///     ProxyConfig::default(),
38///     Optimizer::new(),
39/// );
40/// dl.download().expect("FTP download failed");
41///
42/// // Authenticated download
43/// let dl = FtpDownloader::new(
44///     "ftp://user:pass@private-server.com/file.zip".to_string(),
45///     "file.zip".to_string(),
46///     false,
47///     ProxyConfig::default(),
48///     Optimizer::new(),
49/// );
50/// dl.download().expect("FTP download failed");
51/// ```
52pub struct FtpDownloader {
53    url: String,
54    output_path: String,
55    quiet_mode: bool,
56    proxy: ProxyConfig,
57    #[allow(dead_code)]
58    optimizer: Optimizer,
59}
60
61impl FtpDownloader {
62    /// Create a new FTP downloader.
63    ///
64    /// # Arguments
65    ///
66    /// * `url` - FTP URL including path to file
67    /// * `output_path` - Local path to save the file
68    /// * `quiet_mode` - Suppress console output
69    /// * `proxy` - Proxy configuration (SOCKS5 only)
70    /// * `optimizer` - Optimizer instance
71    pub fn new(
72        url: String,
73        output_path: String,
74        quiet_mode: bool,
75        proxy: ProxyConfig,
76        optimizer: Optimizer,
77    ) -> Self {
78        Self {
79            url,
80            output_path,
81            quiet_mode,
82            proxy,
83            optimizer,
84        }
85    }
86
87    pub fn download(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
88        let url = Url::parse(&self.url)?;
89        let host = url.host_str().ok_or("Invalid host")?;
90        let port = url.port().unwrap_or(21);
91        let path = url.path();
92
93        print(&format!("Connecting to FTP server {}:{}...", host, port), self.quiet_mode);
94
95        let mut ftp = if self.proxy.enabled {
96            self.connect_via_proxy(host, port)?
97        } else {
98            FtpStream::connect((host, port))?
99        };
100
101        let username = url.username();
102        let password = url.password().unwrap_or("anonymous");
103
104        print(&format!("Logging in as {}...", username), self.quiet_mode);
105        ftp.login(username, password)?;
106
107        ftp.transfer_type(suppaftp::types::FileType::Binary)?;
108
109        let size = ftp.size(path)? as u64;
110        
111        let progress = create_progress_bar(
112            self.quiet_mode,
113            format!("Downloading {}", path),
114            Some(size),
115            false
116        );
117
118        let mut file = std::fs::File::create(&self.output_path)?;
119
120        // Download file
121        let mut downloaded = 0;
122        ftp.retr(path, |reader| {
123            let mut buffer = vec![0; 8192];
124            loop {
125                match reader.read(&mut buffer) {
126                    Ok(0) => break,
127                    Ok(n) => {
128                        file.write_all(&buffer[..n]).map_err(|e| suppaftp::FtpError::ConnectionError(e))?;
129                        downloaded += n;
130                        progress.set_position(downloaded as u64);
131                    }
132                    Err(e) => return Err(suppaftp::FtpError::ConnectionError(e)),
133                }
134            }
135            Ok(())
136        })?;
137
138        progress.finish();
139        print("Download completed successfully!", self.quiet_mode);
140
141        Ok(())
142    }
143
144    fn connect_via_proxy(&self, host: &str, port: u16) -> Result<FtpStream, Box<dyn Error + Send + Sync>> {
145        match self.proxy.proxy_type {
146            crate::config::ProxyType::Http | crate::config::ProxyType::Https => {
147                Err("HTTP/HTTPS proxy not supported for FTP".into())
148            }
149            crate::config::ProxyType::Socks5 => {
150                if let Some(proxy_url) = &self.proxy.url {
151                    let proxy = Url::parse(proxy_url)?;
152                    let proxy_host = proxy.host_str().ok_or("Invalid proxy host")?;
153                    let proxy_port = proxy.port().unwrap_or(1080);
154
155                    let stream = socks::Socks5Stream::connect(
156                        (proxy_host, proxy_port),
157                        (host, port),
158                    )?;
159
160                    Ok(FtpStream::connect_with_stream(stream.into_inner())?)
161                } else {
162                    Err("Proxy URL not configured".into())
163                }
164            }
165        }
166    }
167}