mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Allow FTPS/FTP; added secure param
This commit is contained in:
38
src/utils.rs
38
src/utils.rs
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user