use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tracing::warn;
use crate::error::{Aria2Error, RecoverableError, Result};
use crate::ftp::connection::negotiation::control::FreshControl;
use crate::ftp::connection::negotiation::control::PooledControl;
pub(crate) fn percent_decode(s: &str) -> String {
let mut bytes = Vec::with_capacity(s.len());
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c == '%' {
let hex: String = chars.by_ref().take(2).collect();
if let Ok(byte) = u8::from_str_radix(&hex, 16) {
bytes.push(byte);
} else {
bytes.push(b'%');
bytes.extend_from_slice(hex.as_bytes());
}
} else {
bytes.push(c as u8);
}
}
String::from_utf8_lossy(&bytes).into_owned()
}
pub(super) fn extract_directory_part(remote_path: &str) -> String {
if remote_path.is_empty() {
return String::new();
}
let decoded = percent_decode(remote_path);
match decoded.rfind('/') {
Some(idx) => decoded[..idx].to_string(),
None => String::new(),
}
}
pub(super) fn extract_file_part(remote_path: &str) -> String {
if remote_path.is_empty() {
return String::new();
}
let decoded = percent_decode(remote_path);
match decoded.rfind('/') {
Some(idx) => decoded[idx + 1..].to_string(),
None => decoded,
}
}
pub(super) fn parse_pasv_response(response: &str) -> Option<(String, u16)> {
let start = response.find('(')?;
let end = response.rfind(')')?;
let inner = &response[start + 1..end];
let parts: Vec<&str> = inner.split(',').collect();
if parts.len() != 6 {
return None;
}
let h1: u8 = parts[0].trim().parse().ok()?;
let h2: u8 = parts[1].trim().parse().ok()?;
let h3: u8 = parts[2].trim().parse().ok()?;
let h4: u8 = parts[3].trim().parse().ok()?;
let p1: u8 = parts[4].trim().parse().ok()?;
let p2: u8 = parts[5].trim().parse().ok()?;
Some((
format!("{}.{}.{}.{}", h1, h2, h3, h4),
u16::from(p1) * 256 + u16::from(p2),
))
}
pub(super) fn parse_epsv_response(response: &str) -> Option<u16> {
let epsv_part = if let Some(open) = response.find('(') {
let close = response.rfind(')').filter(|&c| c > open)?;
&response[open + 1..close]
} else {
response
};
let parts: Vec<&str> = epsv_part.split('|').collect();
if parts.len() < 5 {
return None;
}
let port_str = parts[3];
let port: u16 = port_str.parse().ok()?;
if port == 0 {
return None;
}
Some(port)
}
pub(super) fn parse_mdtm_timestamp(s: &str) -> Option<SystemTime> {
if s.len() < 14 {
return None;
}
let year: i32 = s[0..4].parse().ok()?;
let month: u32 = s[4..6].parse().ok()?;
let day: u32 = s[6..8].parse().ok()?;
let hour: u32 = s[8..10].parse().ok()?;
let minute: u32 = s[10..12].parse().ok()?;
let second: u32 = s[12..14].parse().ok()?;
if !(1990..=2999).contains(&year)
|| !(1..=12).contains(&month)
|| !(1..=31).contains(&day)
|| hour > 23
|| minute > 59
|| second > 60
{
return None;
}
let days_since_epoch = days_from_civil(year, month, day)?;
let secs = days_since_epoch * 86400 + hour as u64 * 3600 + minute as u64 * 60 + second as u64;
Some(UNIX_EPOCH + Duration::from_secs(secs))
}
pub(super) fn days_from_civil(year: i32, month: u32, day: u32) -> Option<u64> {
let y = if month <= 2 { year - 1 } else { year };
let m = if month <= 2 { month + 9 } else { month - 3 };
let era = y.div_euclid(400);
let yoe = y.rem_euclid(400) as u64;
let doy = (153 * m as u64 + 2) / 5 + day as u64 - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
let days = era as u64 * 146097 + doe - 719468;
Some(days)
}
pub(super) async fn authenticate(
ctrl: &mut FreshControl,
username: &str,
password: &str,
) -> Result<()> {
use tracing::{debug, info};
debug!("Authenticating as user: {}", username);
let user_resp = ctrl.command(&format!("USER {}", username)).await?;
match user_resp.0 {
230 => {
info!("FTP login successful (no password required)");
}
331 | 332 => {
debug!("Password required, sending PASS command");
let pass_resp = ctrl.command(&format!("PASS {}", password)).await?;
if pass_resp.0 == 530 {
return Err(Aria2Error::Fatal(
crate::error::FatalError::PermissionDenied {
path: format!("FTP authentication failed: {} {}", pass_resp.0, pass_resp.1),
},
));
}
if !(200..300).contains(&pass_resp.0) {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("Login failed: {} {}", pass_resp.0, pass_resp.1),
},
));
}
info!("FTP login successful");
}
530 => {
return Err(Aria2Error::Fatal(
crate::error::FatalError::PermissionDenied {
path: format!("FTP USER rejected: {} {}", user_resp.0, user_resp.1),
},
));
}
_ => {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("Unexpected USER response: {} {}", user_resp.0, user_resp.1),
},
));
}
}
Ok(())
}
pub(super) async fn set_transfer_mode(ctrl: &mut FreshControl, binary: bool) -> Result<()> {
use tracing::debug;
let type_cmd = if binary { "TYPE I" } else { "TYPE A" };
debug!("Setting transfer mode: {}", type_cmd);
let resp = ctrl.command(type_cmd).await?;
if !(200..300).contains(&resp.0) {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("{} failed: {} {}", type_cmd, resp.0, resp.1),
},
));
}
Ok(())
}
#[allow(dead_code)] pub(super) async fn set_binary_mode(ctrl: &mut FreshControl) -> Result<()> {
set_transfer_mode(ctrl, true).await
}
pub(super) async fn query_pwd(ctrl: &mut FreshControl) -> Result<String> {
use tracing::debug;
debug!("Sending PWD command");
let resp = ctrl.command("PWD").await?;
if resp.0 != 257 {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("PWD failed: {} {}", resp.0, resp.1),
},
));
}
let msg = resp.1.trim();
if let Some(start) = msg.find('"')
&& let Some(end) = msg.rfind('"')
&& end > start
{
Ok(msg[start + 1..end].to_string())
} else {
Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("PWD response missing quoted path: {}", msg),
},
))
}
}
pub(super) async fn cwd_traversal(
ctrl: &mut FreshControl,
base_working_dir: &str,
dir_path: &str,
) -> Result<()> {
use tracing::{debug, info};
let mut dirs: Vec<&str> = Vec::new();
if base_working_dir != "/" && !base_working_dir.is_empty() {
dirs.push(base_working_dir);
}
for component in dir_path.split('/') {
if !component.is_empty() {
dirs.push(component);
}
}
debug!("CWD traversal: {} directories to traverse", dirs.len());
for dir in &dirs {
debug!("CWD {}", dir);
let (code, msg) = ctrl.command(&format!("CWD {}", dir)).await?;
if code == 550 {
return Err(Aria2Error::Fatal(crate::error::FatalError::FileNotFound {
path: dir.to_string(),
}));
}
if code != 250 {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("CWD {} failed: {} {}", dir, code, msg),
},
));
}
}
info!("CWD traversal completed successfully");
Ok(())
}
pub(super) async fn query_mdtm(
ctrl: &mut FreshControl,
file_path: &str,
) -> Result<Option<SystemTime>> {
use tracing::{debug, info};
debug!("Sending MDTM command for: {}", file_path);
let resp = ctrl.command(&format!("MDTM {}", file_path)).await?;
if resp.0 != 213 {
info!(
"MDTM command returned non-213 response: {} {}",
resp.0, resp.1
);
return Ok(None);
}
let msg = resp.1.trim();
let timestamp_str = if let Some(stripped) = msg.strip_prefix("213") {
stripped.trim()
} else {
msg
};
if timestamp_str.len() < 14 {
warn!("MDTM response too short to parse: {}", timestamp_str);
return Ok(None);
}
let ts = ×tamp_str[..14];
match parse_mdtm_timestamp(ts) {
Some(t) => {
debug!("MDTM parsed modification time: {:?}", t);
Ok(Some(t))
}
None => {
warn!("Failed to parse MDTM timestamp: {}", ts);
Ok(None)
}
}
}
pub(super) async fn query_size(ctrl: &mut FreshControl, file_path: &str) -> Result<Option<u64>> {
use tracing::{debug, info};
debug!("Sending SIZE command for: {}", file_path);
let resp = ctrl.command(&format!("SIZE {}", file_path)).await?;
if resp.0 == 213 {
let msg = resp.1.trim();
let size_str = if let Some(stripped) = msg.strip_prefix("213") {
stripped.trim()
} else {
msg
};
match size_str.parse::<u64>() {
Ok(size) => {
debug!("File size: {} bytes", size);
Ok(Some(size))
}
Err(_) => {
warn!("Failed to parse SIZE response: {}", size_str);
Ok(None)
}
}
} else {
info!(
"SIZE command returned non-213 response: {} {}",
resp.0, resp.1
);
Ok(None)
}
}
pub(super) async fn send_rest(ctrl: &mut FreshControl, offset: u64) -> Result<()> {
use tracing::{debug, warn};
debug!("Setting resume offset: {} bytes", offset);
let resp = ctrl.command(&format!("REST {}", offset)).await?;
if resp.0 != 350 {
warn!("REST command not accepted by server: {} {}", resp.0, resp.1);
if offset > 0 {
return Err(Aria2Error::Recoverable(RecoverableError::CannotResume));
}
debug!("REST 0 rejected, continuing (file pointer assumed at start)");
} else {
debug!("REST accepted by server");
}
Ok(())
}
pub(super) async fn send_retr(ctrl: &mut FreshControl, file_path: &str) -> Result<()> {
use tracing::debug;
debug!("Initiating file retrieval: {}", file_path);
let resp = ctrl.command(&format!("RETR {}", file_path)).await?;
if resp.0 == 150 || resp.0 == 125 {
Ok(())
} else if resp.0 == 550 {
Err(Aria2Error::Fatal(crate::error::FatalError::FileNotFound {
path: file_path.to_string(),
}))
} else {
Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("RETR unexpected response: {} {}", resp.0, resp.1),
},
))
}
}
pub(super) async fn cwd_traversal_pooled(
ctrl: &mut PooledControl,
base_working_dir: &str,
dir_path: &str,
) -> Result<()> {
use tracing::{debug, info};
let mut dirs: Vec<&str> = Vec::new();
if base_working_dir != "/" && !base_working_dir.is_empty() {
dirs.push(base_working_dir);
}
for component in dir_path.split('/') {
if !component.is_empty() {
dirs.push(component);
}
}
debug!("CWD traversal (pooled): {} directories", dirs.len());
for dir in &dirs {
debug!("CWD {}", dir);
let (code, msg) = ctrl.command(&format!("CWD {}", dir)).await?;
if code == 550 {
return Err(Aria2Error::Fatal(crate::error::FatalError::FileNotFound {
path: dir.to_string(),
}));
}
if code != 250 {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("CWD {} failed (pooled): {} {}", dir, code, msg),
},
));
}
}
info!("CWD traversal (pooled) completed successfully");
Ok(())
}
pub(super) async fn query_mdtm_pooled(
ctrl: &mut PooledControl,
file_path: &str,
) -> Result<Option<SystemTime>> {
use tracing::{debug, info};
debug!("Sending MDTM command (pooled) for: {}", file_path);
let resp = ctrl.command(&format!("MDTM {}", file_path)).await?;
if resp.0 != 213 {
info!("MDTM non-213: {} {}", resp.0, resp.1);
return Ok(None);
}
let msg = resp.1.trim();
let timestamp_str = if let Some(stripped) = msg.strip_prefix("213") {
stripped.trim()
} else {
msg
};
if timestamp_str.len() < 14 {
return Ok(None);
}
match parse_mdtm_timestamp(×tamp_str[..14]) {
Some(t) => Ok(Some(t)),
None => Ok(None),
}
}
pub(super) async fn query_size_pooled(
ctrl: &mut PooledControl,
file_path: &str,
) -> Result<Option<u64>> {
use tracing::debug;
debug!("Sending SIZE command (pooled) for: {}", file_path);
let resp = ctrl.command(&format!("SIZE {}", file_path)).await?;
if resp.0 == 213 {
let msg = resp.1.trim();
let size_str = if let Some(stripped) = msg.strip_prefix("213") {
stripped.trim()
} else {
msg
};
Ok(size_str.parse::<u64>().ok())
} else {
Ok(None)
}
}
pub(super) async fn send_rest_pooled(ctrl: &mut PooledControl, offset: u64) -> Result<()> {
use tracing::debug;
debug!("Setting resume offset (pooled): {} bytes", offset);
let resp = ctrl.command(&format!("REST {}", offset)).await?;
if resp.0 != 350 {
if offset > 0 {
return Err(Aria2Error::Recoverable(RecoverableError::CannotResume));
}
debug!("REST 0 rejected (pooled), continuing");
}
Ok(())
}
pub(super) async fn send_retr_pooled(ctrl: &mut PooledControl, file_path: &str) -> Result<()> {
use tracing::debug;
debug!("Initiating file retrieval (pooled): {}", file_path);
let resp = ctrl.command(&format!("RETR {}", file_path)).await?;
if resp.0 == 150 || resp.0 == 125 {
Ok(())
} else if resp.0 == 550 {
Err(Aria2Error::Fatal(crate::error::FatalError::FileNotFound {
path: file_path.to_string(),
}))
} else {
Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("RETR unexpected response (pooled): {} {}", resp.0, resp.1),
},
))
}
}