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

View file

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

View file

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

View file

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

View file

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

View file

@ -35,7 +35,7 @@ use std::time::{Duration, SystemTime};
/// ### 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.
/// In case of error, message is returned
/// 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(
remote: &String,
) -> Result<(String, u16, FileTransferProtocol, Option<String>, bool), String> {
) -> Result<(String, u16, FileTransferProtocol, Option<String>), String> {
let mut wrkstr: String = remote.clone();
let address: String;
let mut port: u16 = 22;
@ -77,17 +77,15 @@ pub fn parse_remote_opt(
}
"ftp" => {
// Set protocol to fpt
protocol = FileTransferProtocol::Ftp;
protocol = FileTransferProtocol::Ftp(false);
// Set port to default (21)
port = 21;
}
"ftps" => {
// Set protocol to fpt
protocol = FileTransferProtocol::Ftp;
protocol = FileTransferProtocol::Ftp(true);
// Set port to default (21)
port = 21;
// Set secure to true
secure = true;
}
_ => 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...
}
Ok((address, port, protocol, username, secure))
Ok((address, port, protocol, username))
}
/// ### instant_to_str
@ -193,7 +191,7 @@ mod tests {
#[test]
fn test_utils_parse_remote_opt() {
// 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"))
.ok()
.unwrap();
@ -201,9 +199,8 @@ mod tests {
assert_eq!(result.1, 22);
assert_eq!(result.2, FileTransferProtocol::Sftp);
assert!(result.3.is_some());
assert_eq!(result.4, false);
// 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"))
.ok()
.unwrap();
@ -211,9 +208,8 @@ mod tests {
assert_eq!(result.1, 22);
assert_eq!(result.2, FileTransferProtocol::Sftp);
assert_eq!(result.3.unwrap(), String::from("root"));
assert_eq!(result.4, false);
// 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"))
.ok()
.unwrap();
@ -221,9 +217,8 @@ mod tests {
assert_eq!(result.1, 8022);
assert_eq!(result.2, FileTransferProtocol::Sftp);
assert_eq!(result.3.unwrap(), String::from("root"));
assert_eq!(result.4, false);
// 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"))
.ok()
.unwrap();
@ -231,37 +226,33 @@ mod tests {
assert_eq!(result.1, 4022);
assert_eq!(result.2, FileTransferProtocol::Sftp);
assert!(result.3.is_some());
assert_eq!(result.4, false);
// 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"))
.ok()
.unwrap();
assert_eq!(result.0, String::from("172.26.104.1"));
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_eq!(result.4, false);
// 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"))
.ok()
.unwrap();
assert_eq!(result.0, String::from("172.26.104.1"));
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.4, true);
// 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"))
.ok()
.unwrap();
assert_eq!(result.0, String::from("172.26.104.1"));
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.4, false);
// bad syntax
assert!(parse_remote_opt(&String::from("://172.26.104.1")).is_err()); // Missing protocol