Added to FileTransferProtocol bool secure flag to Ftp

This commit is contained in:
ChristianVisintin
2020-12-03 08:16:07 +01:00
parent 9b26b5b99d
commit a328a4f751
6 changed files with 38 additions and 80 deletions

View File

@@ -87,7 +87,6 @@ impl ActivityManager {
protocol: FileTransferProtocol, protocol: FileTransferProtocol,
username: Option<String>, username: Option<String>,
password: Option<String>, password: Option<String>,
secure: bool
) { ) {
self.ftparams = Some(FileTransferParams { self.ftparams = Some(FileTransferParams {
address: address, address: address,
@@ -95,7 +94,6 @@ impl ActivityManager {
protocol: protocol, protocol: protocol,
username: username, username: username,
password: password, password: password,
extra_flag_secure: secure,
}); });
} }
@@ -161,7 +159,6 @@ impl ActivityManager {
_ => Some(activity.password.clone()), _ => Some(activity.password.clone()),
}, },
protocol: activity.protocol.clone(), protocol: activity.protocol.clone(),
extra_flag_secure: activity.secure,
}); });
break; break;
} }

View File

@@ -39,7 +39,7 @@ pub mod sftp_transfer;
#[derive(std::cmp::PartialEq, std::fmt::Debug, std::clone::Clone)] #[derive(std::cmp::PartialEq, std::fmt::Debug, std::clone::Clone)]
pub enum FileTransferProtocol { pub enum FileTransferProtocol {
Sftp, Sftp,
Ftp, Ftp(bool), // Bool is for secure (true => ftps)
} }
/// ## FileTransferError /// ## FileTransferError

View File

@@ -64,7 +64,6 @@ fn main() {
let mut password: Option<String> = None; // Default password let mut password: Option<String> = None; // Default password
let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp; // Default protocol let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp; // Default protocol
let mut ticks: Duration = Duration::from_millis(10); let mut ticks: Duration = Duration::from_millis(10);
let mut secure: bool = false;
//Process options //Process options
let mut opts = Options::new(); let mut opts = Options::new();
opts.optopt( opts.optopt(
@@ -116,13 +115,12 @@ fn main() {
if let Some(remote) = extra_args.get(0) { if let Some(remote) = extra_args.get(0) {
// Parse address // Parse address
match utils::parse_remote_opt(remote) { match utils::parse_remote_opt(remote) {
Ok((addr, portn, proto, user, secure)) => { Ok((addr, portn, proto, user)) => {
// Set params // Set params
address = Some(addr); address = Some(addr);
port = portn; port = portn;
protocol = proto; protocol = proto;
username = user; username = user;
secure = secure;
} }
Err(err) => { Err(err) => {
eprintln!("Bad address option: {}", err); eprintln!("Bad address option: {}", err);
@@ -165,7 +163,7 @@ fn main() {
} }
// In this case the first activity will be FileTransfer // In this case the first activity will be FileTransfer
start_activity = NextActivity::FileTransfer; start_activity = NextActivity::FileTransfer;
manager.set_filetransfer_params(address, port, protocol, username, password, secure); manager.set_filetransfer_params(address, port, protocol, username, password);
} }
// Run // Run
manager.run(start_activity); manager.run(start_activity);

View File

@@ -44,12 +44,6 @@ use tui::{
}; };
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
enum InputProtocol {
Sftp,
Ftp,
Ftps,
}
/// ### InputField /// ### InputField
/// ///
/// InputField describes the current input field to edit /// InputField describes the current input field to edit
@@ -81,13 +75,11 @@ pub struct AuthActivity {
pub protocol: FileTransferProtocol, pub protocol: FileTransferProtocol,
pub username: String, pub username: String,
pub password: String, pub password: String,
pub secure: bool, // Special option used by some protocols (such as FTP)
pub submit: bool, // becomes true after user has submitted fields pub submit: bool, // becomes true after user has submitted fields
pub quit: bool, // Becomes true if user has pressed esc pub quit: bool, // Becomes true if user has pressed esc
context: Option<Context>, context: Option<Context>,
selected_field: InputField, selected_field: InputField,
input_mode: InputMode, input_mode: InputMode,
input_protocol: InputProtocol,
popup_message: Option<String>, popup_message: Option<String>,
password_placeholder: String, password_placeholder: String,
redraw: bool, // Should ui actually be redrawned? redraw: bool, // Should ui actually be redrawned?
@@ -104,8 +96,6 @@ impl AuthActivity {
protocol: FileTransferProtocol::Sftp, protocol: FileTransferProtocol::Sftp,
username: String::new(), username: String::new(),
password: String::new(), password: String::new(),
input_protocol: InputProtocol::Sftp,
secure: false,
submit: false, submit: false,
quit: false, quit: false,
context: None, context: None,
@@ -117,27 +107,6 @@ impl AuthActivity {
} }
} }
/// ### change_opt_based_on_protocol
///
/// Change current options based on the selected protocol
fn change_opt_based_on_protocol(&mut self) {
// Change options based on current protocol
match self.input_protocol {
InputProtocol::Sftp => {
self.secure = false;
self.protocol = FileTransferProtocol::Sftp;
}
InputProtocol::Ftp => {
self.secure = false;
self.protocol = FileTransferProtocol::Ftp;
}
InputProtocol::Ftps => {
self.secure = true;
self.protocol = FileTransferProtocol::Ftp;
}
}
}
/// ### set_input_mode /// ### set_input_mode
/// ///
/// Update input mode based on current parameters /// Update input mode based on current parameters
@@ -259,23 +228,25 @@ impl AuthActivity {
KeyCode::Left => { KeyCode::Left => {
// If current field is Protocol handle event... (move element left) // If current field is Protocol handle event... (move element left)
if self.selected_field == InputField::Protocol { if self.selected_field == InputField::Protocol {
self.input_protocol = match self.input_protocol { self.protocol = match self.protocol {
InputProtocol::Sftp => InputProtocol::Ftps, // End of list (wrap) FileTransferProtocol::Sftp => FileTransferProtocol::Ftp(true), // End of list (wrap)
InputProtocol::Ftp => InputProtocol::Sftp, FileTransferProtocol::Ftp(ftps) => match ftps {
InputProtocol::Ftps => InputProtocol::Ftp, true => FileTransferProtocol::Sftp,
false => FileTransferProtocol::Ftp(true),
}
}; };
self.change_opt_based_on_protocol();
} }
} }
KeyCode::Right => { KeyCode::Right => {
// If current field is Protocol handle event... ( move element right ) // If current field is Protocol handle event... ( move element right )
if self.selected_field == InputField::Protocol { if self.selected_field == InputField::Protocol {
self.input_protocol = match self.input_protocol { self.protocol = match self.protocol {
InputProtocol::Sftp => InputProtocol::Ftp, FileTransferProtocol::Sftp => FileTransferProtocol::Ftp(false),
InputProtocol::Ftp => InputProtocol::Ftps, FileTransferProtocol::Ftp(ftps) => match ftps {
InputProtocol::Ftps => InputProtocol::Sftp, // End of list (wrap) false => FileTransferProtocol::Ftp(true),
true => FileTransferProtocol::Sftp, // End of list (wrap)
}
}; };
self.change_opt_based_on_protocol();
} }
} }
_ => { /* Nothing to do */ } _ => { /* Nothing to do */ }
@@ -337,10 +308,12 @@ impl AuthActivity {
fn draw_protocol_select(&self) -> Tabs { fn draw_protocol_select(&self) -> Tabs {
let protocols: Vec<Spans> = let protocols: Vec<Spans> =
vec![Spans::from("SFTP"), Spans::from("FTP"), Spans::from("FTPS")]; vec![Spans::from("SFTP"), Spans::from("FTP"), Spans::from("FTPS")];
let index: usize = match self.input_protocol { let index: usize = match self.protocol {
InputProtocol::Sftp => 0, FileTransferProtocol::Sftp => 0,
InputProtocol::Ftp => 1, FileTransferProtocol::Ftp(ftps) => match ftps {
InputProtocol::Ftps => 2, false => 1,
true => 2,
}
}; };
Tabs::new(protocols) Tabs::new(protocols)
.block(Block::default().borders(Borders::ALL).title("Protocol")) .block(Block::default().borders(Borders::ALL).title("Protocol"))

View File

@@ -69,7 +69,6 @@ pub struct FileTransferParams {
pub protocol: FileTransferProtocol, pub protocol: FileTransferProtocol,
pub username: Option<String>, pub username: Option<String>,
pub password: Option<String>, pub password: Option<String>,
pub extra_flag_secure: bool,
} }
/// ### InputField /// ### InputField
@@ -240,8 +239,8 @@ impl FileTransferActivity {
context: None, context: None,
client: match protocol { client: match protocol {
FileTransferProtocol::Sftp => Box::new(SftpFileTransfer::new()), FileTransferProtocol::Sftp => Box::new(SftpFileTransfer::new()),
FileTransferProtocol::Ftp => { FileTransferProtocol::Ftp(ftps) => {
Box::new(FtpFileTransfer::new(params.extra_flag_secure)) Box::new(FtpFileTransfer::new(ftps))
} }
}, },
params: params, params: params,

View File

@@ -35,7 +35,7 @@ use std::time::{Duration, SystemTime};
/// ### parse_remote_opt /// ### parse_remote_opt
/// ///
/// Parse remote option string. Returns in case of success a tuple made of (address, port, protocol, username, secure) /// Parse remote option string. Returns in case of success a tuple made of (address, port, protocol, username)
/// For ssh if username is not provided, current user will be used. /// For ssh if username is not provided, current user will be used.
/// In case of error, message is returned /// In case of error, message is returned
/// If port is missing default port will be used for each protocol /// If port is missing default port will be used for each protocol
@@ -54,7 +54,7 @@ use std::time::{Duration, SystemTime};
/// ///
pub fn parse_remote_opt( pub fn parse_remote_opt(
remote: &String, remote: &String,
) -> Result<(String, u16, FileTransferProtocol, Option<String>, bool), String> { ) -> Result<(String, u16, FileTransferProtocol, Option<String>), String> {
let mut wrkstr: String = remote.clone(); let mut wrkstr: String = remote.clone();
let address: String; let address: String;
let mut port: u16 = 22; let mut port: u16 = 22;
@@ -77,17 +77,15 @@ pub fn parse_remote_opt(
} }
"ftp" => { "ftp" => {
// Set protocol to fpt // Set protocol to fpt
protocol = FileTransferProtocol::Ftp; protocol = FileTransferProtocol::Ftp(false);
// Set port to default (21) // Set port to default (21)
port = 21; port = 21;
} }
"ftps" => { "ftps" => {
// Set protocol to fpt // Set protocol to fpt
protocol = FileTransferProtocol::Ftp; protocol = FileTransferProtocol::Ftp(true);
// Set port to default (21) // Set port to default (21)
port = 21; port = 21;
// Set secure to true
secure = true;
} }
_ => return Err(format!("Unknown protocol '{}'", tokens[0])), _ => return Err(format!("Unknown protocol '{}'", tokens[0])),
} }
@@ -135,7 +133,7 @@ pub fn parse_remote_opt(
} }
_ => return Err(String::from("Bad syntax")), // Too many tokens... _ => return Err(String::from("Bad syntax")), // Too many tokens...
} }
Ok((address, port, protocol, username, secure)) Ok((address, port, protocol, username))
} }
/// ### instant_to_str /// ### instant_to_str
@@ -193,7 +191,7 @@ mod tests {
#[test] #[test]
fn test_utils_parse_remote_opt() { fn test_utils_parse_remote_opt() {
// Base case // Base case
let result: (String, u16, FileTransferProtocol, Option<String>, bool) = let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("172.26.104.1")) parse_remote_opt(&String::from("172.26.104.1"))
.ok() .ok()
.unwrap(); .unwrap();
@@ -201,9 +199,8 @@ mod tests {
assert_eq!(result.1, 22); assert_eq!(result.1, 22);
assert_eq!(result.2, FileTransferProtocol::Sftp); assert_eq!(result.2, FileTransferProtocol::Sftp);
assert!(result.3.is_some()); assert!(result.3.is_some());
assert_eq!(result.4, false);
// User case // User case
let result: (String, u16, FileTransferProtocol, Option<String>, bool) = let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("root@172.26.104.1")) parse_remote_opt(&String::from("root@172.26.104.1"))
.ok() .ok()
.unwrap(); .unwrap();
@@ -211,9 +208,8 @@ mod tests {
assert_eq!(result.1, 22); assert_eq!(result.1, 22);
assert_eq!(result.2, FileTransferProtocol::Sftp); assert_eq!(result.2, FileTransferProtocol::Sftp);
assert_eq!(result.3.unwrap(), String::from("root")); assert_eq!(result.3.unwrap(), String::from("root"));
assert_eq!(result.4, false);
// User + port // User + port
let result: (String, u16, FileTransferProtocol, Option<String>, bool) = let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("root@172.26.104.1:8022")) parse_remote_opt(&String::from("root@172.26.104.1:8022"))
.ok() .ok()
.unwrap(); .unwrap();
@@ -221,9 +217,8 @@ mod tests {
assert_eq!(result.1, 8022); assert_eq!(result.1, 8022);
assert_eq!(result.2, FileTransferProtocol::Sftp); assert_eq!(result.2, FileTransferProtocol::Sftp);
assert_eq!(result.3.unwrap(), String::from("root")); assert_eq!(result.3.unwrap(), String::from("root"));
assert_eq!(result.4, false);
// Port only // Port only
let result: (String, u16, FileTransferProtocol, Option<String>, bool) = let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("172.26.104.1:4022")) parse_remote_opt(&String::from("172.26.104.1:4022"))
.ok() .ok()
.unwrap(); .unwrap();
@@ -231,37 +226,33 @@ mod tests {
assert_eq!(result.1, 4022); assert_eq!(result.1, 4022);
assert_eq!(result.2, FileTransferProtocol::Sftp); assert_eq!(result.2, FileTransferProtocol::Sftp);
assert!(result.3.is_some()); assert!(result.3.is_some());
assert_eq!(result.4, false);
// Protocol // Protocol
let result: (String, u16, FileTransferProtocol, Option<String>, bool) = let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("ftp://172.26.104.1")) parse_remote_opt(&String::from("ftp://172.26.104.1"))
.ok() .ok()
.unwrap(); .unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 21); // Fallback to ftp default assert_eq!(result.1, 21); // Fallback to ftp default
assert_eq!(result.2, FileTransferProtocol::Ftp); assert_eq!(result.2, FileTransferProtocol::Ftp(false));
assert!(result.3.is_none()); // Doesn't fall back assert!(result.3.is_none()); // Doesn't fall back
assert_eq!(result.4, false);
// Protocol + user // Protocol + user
let result: (String, u16, FileTransferProtocol, Option<String>, bool) = let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("ftps://anon@172.26.104.1")) parse_remote_opt(&String::from("ftps://anon@172.26.104.1"))
.ok() .ok()
.unwrap(); .unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 21); // Fallback to ftp default assert_eq!(result.1, 21); // Fallback to ftp default
assert_eq!(result.2, FileTransferProtocol::Ftp); assert_eq!(result.2, FileTransferProtocol::Ftp(true));
assert_eq!(result.3.unwrap(), String::from("anon")); assert_eq!(result.3.unwrap(), String::from("anon"));
assert_eq!(result.4, true);
// All together now // All together now
let result: (String, u16, FileTransferProtocol, Option<String>, bool) = let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("ftp://anon@172.26.104.1:8021")) parse_remote_opt(&String::from("ftp://anon@172.26.104.1:8021"))
.ok() .ok()
.unwrap(); .unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 8021); // Fallback to ftp default assert_eq!(result.1, 8021); // Fallback to ftp default
assert_eq!(result.2, FileTransferProtocol::Ftp); assert_eq!(result.2, FileTransferProtocol::Ftp(false));
assert_eq!(result.3.unwrap(), String::from("anon")); assert_eq!(result.3.unwrap(), String::from("anon"));
assert_eq!(result.4, false);
// bad syntax // bad syntax
assert!(parse_remote_opt(&String::from("://172.26.104.1")).is_err()); // Missing protocol assert!(parse_remote_opt(&String::from("://172.26.104.1")).is_err()); // Missing protocol