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

View File

@@ -63,6 +63,7 @@ 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(
@@ -114,12 +115,13 @@ fn main() {
if let Some(remote) = extra_args.get(0) {
// Parse address
match utils::parse_remote_opt(remote) {
Ok((addr, portn, proto, user)) => {
Ok((addr, portn, proto, user, secure)) => {
// Set params
address = Some(addr);
port = portn;
protocol = proto;
username = user;
secure = secure;
}
Err(err) => {
eprintln!("Bad address option: {}", err);
@@ -162,7 +164,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);
manager.set_filetransfer_params(address, port, protocol, username, password, secure);
}
// Run
manager.run(start_activity);

View File

@@ -68,6 +68,7 @@ pub struct FileTransferParams {
pub protocol: FileTransferProtocol,
pub username: Option<String>,
pub password: Option<String>,
pub extra_flag_secure: bool,
}
/// ### InputField

View File

@@ -34,7 +34,7 @@ use std::time::SystemTime;
/// ### 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.
/// In case of error, message is returned
/// 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(
remote: &String,
) -> Result<(String, u16, FileTransferProtocol, Option<String>), String> {
) -> Result<(String, u16, FileTransferProtocol, Option<String>, bool), String> {
let mut wrkstr: String = remote.clone();
let address: String;
let mut port: u16 = 22;
let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp;
let mut username: Option<String> = None;
let mut secure: bool = false;
// Split string by '://'
let tokens: Vec<&str> = wrkstr.split("://").collect();
// If length is > 1, then token[0] is protocol
@@ -73,12 +74,20 @@ pub fn parse_remote_opt(
// Set port to default (22)
port = 22;
}
"ftp" | "ftps" => {
"ftp" => {
// Set protocol to fpt
protocol = FileTransferProtocol::Ftp;
// Set port to default (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])),
}
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...
}
Ok((address, port, protocol, username))
Ok((address, port, protocol, username, secure))
}
/// ### instant_to_str
@@ -139,47 +148,54 @@ mod tests {
#[test]
fn test_utils_parse_remote_opt() {
// 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.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>) = 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.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>) = 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.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>) = 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.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>) = 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.1, 21); // Fallback to ftp default
assert_eq!(result.2, FileTransferProtocol::Ftp);
assert!(result.3.is_none()); // Doesn't fall back
assert_eq!(result.4, false);
// 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.1, 21); // Fallback to ftp default
assert_eq!(result.2, FileTransferProtocol::Ftp);
assert_eq!(result.3.unwrap(), String::from("anon"));
assert_eq!(result.4, true);
// 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.1, 8021); // Fallback to ftp default
assert_eq!(result.2, FileTransferProtocol::Ftp);
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