Allow FTPS/FTP; added secure param

This commit is contained in:
ChristianVisintin
2020-12-01 16:41:15 +01:00
parent c19416abdd
commit ca797558d7
4 changed files with 35 additions and 13 deletions

View File

@@ -87,6 +87,7 @@ 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,
@@ -94,6 +95,7 @@ impl ActivityManager {
protocol: protocol, protocol: protocol,
username: username, username: username,
password: password, password: password,
extra_flag_secure: secure,
}); });
} }
@@ -159,6 +161,7 @@ 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

@@ -63,6 +63,7 @@ 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(
@@ -114,12 +115,13 @@ 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)) => { Ok((addr, portn, proto, user, secure)) => {
// 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);
@@ -162,7 +164,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); manager.set_filetransfer_params(address, port, protocol, username, password, secure);
} }
// Run // Run
manager.run(start_activity); manager.run(start_activity);

View File

@@ -68,6 +68,7 @@ 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

View File

@@ -34,7 +34,7 @@ use std::time::SystemTime;
/// ### parse_remote_opt /// ### parse_remote_opt
/// ///
/// Parse remote option string. Returns in case of success a tuple made of (address, port, protocol, username) /// Parse remote option string. Returns in case of success a tuple made of (address, port, protocol, username, secure)
/// 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
@@ -53,12 +53,13 @@ use std::time::SystemTime;
/// ///
pub fn parse_remote_opt( pub fn parse_remote_opt(
remote: &String, remote: &String,
) -> Result<(String, u16, FileTransferProtocol, Option<String>), String> { ) -> Result<(String, u16, FileTransferProtocol, Option<String>, bool), 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;
let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp; let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp;
let mut username: Option<String> = None; let mut username: Option<String> = None;
let mut secure: bool = false;
// Split string by '://' // Split string by '://'
let tokens: Vec<&str> = wrkstr.split("://").collect(); let tokens: Vec<&str> = wrkstr.split("://").collect();
// If length is > 1, then token[0] is protocol // If length is > 1, then token[0] is protocol
@@ -73,12 +74,20 @@ pub fn parse_remote_opt(
// Set port to default (22) // Set port to default (22)
port = 22; port = 22;
} }
"ftp" | "ftps" => { "ftp" => {
// Set protocol to fpt // Set protocol to fpt
protocol = FileTransferProtocol::Ftp; protocol = FileTransferProtocol::Ftp;
// Set port to default (21) // Set port to default (21)
port = 21; port = 21;
} }
"ftps" => {
// Set protocol to fpt
protocol = FileTransferProtocol::Ftp;
// Set port to default (21)
port = 21;
// Set secure to true
secure = true;
}
_ => return Err(format!("Unknown protocol '{}'", tokens[0])), _ => return Err(format!("Unknown protocol '{}'", tokens[0])),
} }
wrkstr = String::from(tokens[1]); // Wrkstr becomes tokens[1] wrkstr = String::from(tokens[1]); // Wrkstr becomes tokens[1]
@@ -120,7 +129,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)) Ok((address, port, protocol, username, secure))
} }
/// ### instant_to_str /// ### instant_to_str
@@ -139,47 +148,54 @@ 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>) = parse_remote_opt(&String::from("172.26.104.1")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>, bool) = parse_remote_opt(&String::from("172.26.104.1")).ok().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, 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>) = parse_remote_opt(&String::from("root@172.26.104.1")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>, bool) = parse_remote_opt(&String::from("root@172.26.104.1")).ok().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, 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>) = parse_remote_opt(&String::from("root@172.26.104.1:8022")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>, bool) = parse_remote_opt(&String::from("root@172.26.104.1:8022")).ok().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, 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>) = parse_remote_opt(&String::from("172.26.104.1:4022")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>, bool) = parse_remote_opt(&String::from("172.26.104.1:4022")).ok().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, 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>) = parse_remote_opt(&String::from("ftp://172.26.104.1")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>, bool) = 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.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);
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>) = parse_remote_opt(&String::from("ftp://anon@172.26.104.1")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>, bool) = 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.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);
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>) = parse_remote_opt(&String::from("ftp://anon@172.26.104.1:8021")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>, bool) = 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.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);
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