Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/commands/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ pub async fn download_file(
) -> Result<()> {
// Security: Validate the remote source path
let validated_source = crate::security::validate_remote_path(source)
.with_context(|| format!("Invalid source path: {}", source))?;
.with_context(|| format!("Invalid source path: {source}"))?;

// Security: Validate the local destination path
let validated_destination = crate::security::validate_local_path(destination)
.with_context(|| format!("Invalid destination path: {:?}", destination))?;
.with_context(|| format!("Invalid destination path: {destination:?}"))?;

// Create destination directory if it doesn't exist
if !validated_destination.exists() {
Expand Down Expand Up @@ -66,10 +66,7 @@ pub async fn download_file(
// Check if source is a directory (for recursive download)
let is_directory = if params.recursive && !has_glob {
// Use a test command to check if source is a directory
let test_cmd = format!(
"test -d '{}' && echo 'dir' || echo 'file'",
validated_source
);
let test_cmd = format!("test -d '{validated_source}' && echo 'dir' || echo 'file'");
let test_results = executor.execute(&test_cmd).await?;
test_results.iter().any(|r| {
r.result
Expand Down Expand Up @@ -166,7 +163,7 @@ pub async fn download_file(
.nodes
.first()
.ok_or_else(|| anyhow::anyhow!("No nodes available"))?;
let glob_command = format!("ls -1 {} 2>/dev/null || true", validated_source);
let glob_command = format!("ls -1 {validated_source} 2>/dev/null || true");

let mut test_client = SshClient::new(
test_node.host.clone(),
Expand All @@ -192,7 +189,7 @@ pub async fn download_file(
.collect();

if remote_files.is_empty() {
anyhow::bail!("No files found matching pattern: {}", validated_source);
anyhow::bail!("No files found matching pattern: {validated_source}");
}

println!(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub async fn ping_nodes(
);
if let Err(e) = &result.result {
// Display the full error chain for better debugging
let error_chain = format!("{:#}", e);
let error_chain = format!("{e:#}");
// Split by newlines and indent each line
for (i, line) in error_chain.lines().enumerate() {
if i == 0 {
Expand Down
22 changes: 11 additions & 11 deletions src/commands/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ pub async fn upload_file(
) -> Result<()> {
// Security: Validate the local source path
let validated_source = crate::security::validate_local_path(source)
.with_context(|| format!("Invalid source path: {:?}", source))?;
.with_context(|| format!("Invalid source path: {source:?}"))?;

// Security: Validate the remote destination path
let validated_destination = crate::security::validate_remote_path(destination)
.with_context(|| format!("Invalid destination path: {}", destination))?;
.with_context(|| format!("Invalid destination path: {destination}"))?;

// Collect all files matching the pattern
let files = resolve_source_files(&validated_source, params.recursive)?;

if files.is_empty() {
anyhow::bail!("No files found matching pattern: {:?}", source);
anyhow::bail!("No files found matching pattern: {source:?}");
}

// Determine destination handling based on file count
Expand Down Expand Up @@ -123,32 +123,32 @@ pub async fn upload_file(
}

if validated_destination.ends_with('/') {
format!("{}{remote_relative}", validated_destination)
format!("{validated_destination}{remote_relative}")
} else {
format!("{}/{remote_relative}", validated_destination)
format!("{validated_destination}/{remote_relative}")
}
} else {
// No base dir, just use filename
let filename = file
.file_name()
.ok_or_else(|| anyhow::anyhow!("Failed to get filename from {:?}", file))?
.ok_or_else(|| anyhow::anyhow!("Failed to get filename from {file:?}"))?
.to_string_lossy();
if validated_destination.ends_with('/') {
format!("{}{filename}", validated_destination)
format!("{validated_destination}{filename}")
} else {
format!("{}/{filename}", validated_destination)
format!("{validated_destination}/{filename}")
}
}
} else {
// Non-recursive: just append filename
let filename = file
.file_name()
.ok_or_else(|| anyhow::anyhow!("Failed to get filename from {:?}", file))?
.ok_or_else(|| anyhow::anyhow!("Failed to get filename from {file:?}"))?
.to_string_lossy();
if validated_destination.ends_with('/') {
format!("{}{filename}", validated_destination)
format!("{validated_destination}{filename}")
} else {
format!("{}/{filename}", validated_destination)
format!("{validated_destination}/{filename}")
}
}
} else {
Expand Down
30 changes: 12 additions & 18 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl ParallelExecutor {

let style = ProgressStyle::default_bar()
.template("{prefix:.bold} {spinner:.cyan} {msg}")
.map_err(|e| anyhow::anyhow!("Failed to create progress bar template: {}", e))?
.map_err(|e| anyhow::anyhow!("Failed to create progress bar template: {e}"))?
.tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷ ");

let tasks: Vec<_> = self
Expand Down Expand Up @@ -176,7 +176,7 @@ impl ParallelExecutor {
));
return ExecutionResult {
node,
result: Err(anyhow::anyhow!("Semaphore acquisition failed: {}", e)),
result: Err(anyhow::anyhow!("Semaphore acquisition failed: {e}")),
};
}
};
Expand Down Expand Up @@ -213,7 +213,7 @@ impl ParallelExecutor {
}
Err(e) => {
// Get the most specific error message from the chain
let error_msg = format!("{:#}", e);
let error_msg = format!("{e:#}");
// Take the first line which is usually the most specific error
let first_line = error_msg.lines().next().unwrap_or("Unknown error");
let short_error = if first_line.len() > 50 {
Expand Down Expand Up @@ -256,9 +256,7 @@ impl ParallelExecutor {

let style = ProgressStyle::default_bar()
.template("{prefix:.bold} {spinner:.cyan} {msg}")
.map_err(|e| {
anyhow::anyhow!("Failed to create progress bar template for upload: {}", e)
})?
.map_err(|e| anyhow::anyhow!("Failed to create progress bar template for upload: {e}"))?
.tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷ ");

let tasks: Vec<_> = self
Expand Down Expand Up @@ -301,7 +299,7 @@ impl ParallelExecutor {
));
return UploadResult {
node,
result: Err(anyhow::anyhow!("Semaphore acquisition failed: {}", e)),
result: Err(anyhow::anyhow!("Semaphore acquisition failed: {e}")),
};
}
};
Expand Down Expand Up @@ -330,7 +328,7 @@ impl ParallelExecutor {
}
Err(e) => {
// Get the most specific error message from the chain
let error_msg = format!("{:#}", e);
let error_msg = format!("{e:#}");
// Take the first line which is usually the most specific error
let first_line = error_msg.lines().next().unwrap_or("Unknown error");
let short_error = if first_line.len() > 50 {
Expand Down Expand Up @@ -374,7 +372,7 @@ impl ParallelExecutor {
let style = ProgressStyle::default_bar()
.template("{prefix:.bold} {spinner:.cyan} {msg}")
.map_err(|e| {
anyhow::anyhow!("Failed to create progress bar template for download: {}", e)
anyhow::anyhow!("Failed to create progress bar template for download: {e}")
})?
.tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷ ");

Expand Down Expand Up @@ -418,7 +416,7 @@ impl ParallelExecutor {
));
return DownloadResult {
node,
result: Err(anyhow::anyhow!("Semaphore acquisition failed: {}", e)),
result: Err(anyhow::anyhow!("Semaphore acquisition failed: {e}")),
};
}
};
Expand Down Expand Up @@ -493,10 +491,7 @@ impl ParallelExecutor {
let style = ProgressStyle::default_bar()
.template("{prefix:.bold} {spinner:.cyan} {msg}")
.map_err(|e| {
anyhow::anyhow!(
"Failed to create progress bar template for multi-download: {}",
e
)
anyhow::anyhow!("Failed to create progress bar template for multi-download: {e}")
})?
.tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷ ");

Expand Down Expand Up @@ -541,8 +536,7 @@ impl ParallelExecutor {
return DownloadResult {
node,
result: Err(anyhow::anyhow!(
"Semaphore acquisition failed: {}",
e
"Semaphore acquisition failed: {e}"
)),
};
}
Expand Down Expand Up @@ -780,7 +774,7 @@ impl UploadResult {
"Failed to upload file".red()
);
// Show full error chain
let error_chain = format!("{:#}", e);
let error_chain = format!("{e:#}");
for line in error_chain.lines() {
println!(" {}", line.dimmed());
}
Expand Down Expand Up @@ -819,7 +813,7 @@ impl DownloadResult {
"Failed to download file".red()
);
// Show full error chain
let error_chain = format!("{:#}", e);
let error_chain = format!("{e:#}");
for line in error_chain.lines() {
println!(" {}", line.dimmed());
}
Expand Down
18 changes: 6 additions & 12 deletions src/forwarding/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,15 @@ impl DynamicForwarder {
// Send failure response
let response = [0, 0x5B, 0, 0, 0, 0, 0, 0]; // 0x5B = request rejected
tcp_stream.write_all(&response).await?;
return Err(anyhow::anyhow!("Invalid SOCKS4 version: {}", version));
return Err(anyhow::anyhow!("Invalid SOCKS4 version: {version}"));
}

// Only support CONNECT command (0x01)
if command != 0x01 {
debug!("Unsupported SOCKS4 command: {} from {}", command, peer_addr);
let response = [0, 0x5C, 0, 0, 0, 0, 0, 0]; // 0x5C = request failed
tcp_stream.write_all(&response).await?;
return Err(anyhow::anyhow!("Unsupported SOCKS4 command: {}", command));
return Err(anyhow::anyhow!("Unsupported SOCKS4 command: {command}"));
}

// Read USERID (until NULL byte)
Expand Down Expand Up @@ -541,7 +541,7 @@ impl DynamicForwarder {
let nmethods = auth_request[1];

if version != 5 {
return Err(anyhow::anyhow!("Invalid SOCKS5 version: {}", version));
return Err(anyhow::anyhow!("Invalid SOCKS5 version: {version}"));
}

// Read authentication methods
Expand Down Expand Up @@ -574,18 +574,15 @@ impl DynamicForwarder {
let address_type = request_header[3];

if version != 5 {
return Err(anyhow::anyhow!(
"Invalid SOCKS5 request version: {}",
version
));
return Err(anyhow::anyhow!("Invalid SOCKS5 request version: {version}"));
}

// Only support CONNECT command (0x01)
if command != 0x01 {
// Send error response
let response = [5, 0x07, 0, 1, 0, 0, 0, 0, 0, 0]; // Command not supported
tcp_stream.write_all(&response).await?;
return Err(anyhow::anyhow!("Unsupported SOCKS5 command: {}", command));
return Err(anyhow::anyhow!("Unsupported SOCKS5 command: {command}"));
}

// Parse destination address based on address type
Expand Down Expand Up @@ -626,10 +623,7 @@ impl DynamicForwarder {
_ => {
let response = [5, 0x08, 0, 1, 0, 0, 0, 0, 0, 0]; // Address type not supported
tcp_stream.write_all(&response).await?;
return Err(anyhow::anyhow!(
"Unsupported address type: {}",
address_type
));
return Err(anyhow::anyhow!("Unsupported address type: {address_type}"));
}
};

Expand Down
13 changes: 6 additions & 7 deletions src/forwarding/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,13 @@ impl ForwardingManager {
let sessions = self.sessions.read().await;
let session_arc = sessions
.get(&id)
.ok_or_else(|| anyhow::anyhow!("Forwarding session {} not found", id))?;
.ok_or_else(|| anyhow::anyhow!("Forwarding session {id} not found"))?;

let mut session = session_arc.lock().await;

if session.task_handle.is_some() {
return Err(anyhow::anyhow!(
"Forwarding session {} is already started",
id
"Forwarding session {id} is already started"
));
}

Expand Down Expand Up @@ -353,7 +352,7 @@ impl ForwardingManager {
let sessions = self.sessions.read().await;
let session_arc = sessions
.get(&id)
.ok_or_else(|| anyhow::anyhow!("Forwarding session {} not found", id))?;
.ok_or_else(|| anyhow::anyhow!("Forwarding session {id} not found"))?;

let mut session = session_arc.lock().await;

Expand Down Expand Up @@ -392,7 +391,7 @@ impl ForwardingManager {
let sessions = self.sessions.read().await;
let session_arc = sessions
.get(&id)
.ok_or_else(|| anyhow::anyhow!("Forwarding session {} not found", id))?;
.ok_or_else(|| anyhow::anyhow!("Forwarding session {id} not found"))?;

let session = session_arc.lock().await;
Ok(session.status.clone())
Expand All @@ -403,7 +402,7 @@ impl ForwardingManager {
let sessions = self.sessions.read().await;
let session_arc = sessions
.get(&id)
.ok_or_else(|| anyhow::anyhow!("Forwarding session {} not found", id))?;
.ok_or_else(|| anyhow::anyhow!("Forwarding session {id} not found"))?;

let session = session_arc.lock().await;
Ok(session.stats.clone())
Expand Down Expand Up @@ -431,7 +430,7 @@ impl ForwardingManager {
let mut sessions = self.sessions.write().await;
sessions
.remove(&id)
.ok_or_else(|| anyhow::anyhow!("Forwarding session {} not found", id))?;
.ok_or_else(|| anyhow::anyhow!("Forwarding session {id} not found"))?;

tracing::info!("Removed forwarding session {}", id);
Ok(())
Expand Down
3 changes: 1 addition & 2 deletions src/forwarding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ impl SocksVersion {
"4" | "v4" | "socks4" => Ok(SocksVersion::V4),
"5" | "v5" | "socks5" => Ok(SocksVersion::V5),
_ => Err(anyhow::anyhow!(
"Invalid SOCKS version: {}. Expected 4 or 5",
s
"Invalid SOCKS version: {s}. Expected 4 or 5"
)),
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/forwarding/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ impl ForwardingSpec {
})
}
_ => Err(anyhow::anyhow!(
"Invalid local forwarding specification: '{}'. Expected format: [bind_address:]port:host:hostport",
spec
"Invalid local forwarding specification: '{spec}'. Expected format: [bind_address:]port:host:hostport"
)),
}
}
Expand Down Expand Up @@ -120,8 +119,7 @@ impl ForwardingSpec {
})
}
_ => Err(anyhow::anyhow!(
"Invalid remote forwarding specification: '{}'. Expected format: [bind_address:]port:host:hostport",
spec
"Invalid remote forwarding specification: '{spec}'. Expected format: [bind_address:]port:host:hostport"
)),
}
}
Expand Down Expand Up @@ -163,8 +161,7 @@ impl ForwardingSpec {
"remote" | "r" | "-r" => Self::parse_remote(spec),
"dynamic" | "d" | "-d" => Self::parse_dynamic(spec),
_ => Err(anyhow::anyhow!(
"Unknown forwarding type: '{}'. Expected: local, remote, or dynamic",
forward_type
"Unknown forwarding type: '{forward_type}'. Expected: local, remote, or dynamic"
)),
}
}
Expand Down
Loading