mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Don't prompt for password if a ssh key is set for that host (#186)
* feat: don't ask password if a ssh key is set for that host * fix: User from ssh config was not used as it should * fix: forgot a file
This commit is contained in:
committed by
GitHub
parent
b7369162d2
commit
044f2436f8
@@ -9,6 +9,7 @@ pub mod fmt;
|
||||
pub mod parser;
|
||||
pub mod path;
|
||||
pub mod random;
|
||||
pub mod ssh;
|
||||
pub mod string;
|
||||
pub mod tty;
|
||||
pub mod ui;
|
||||
|
||||
@@ -196,16 +196,7 @@ fn parse_generic_remote_opt(
|
||||
match REMOTE_GENERIC_OPT_REGEX.captures(s) {
|
||||
Some(groups) => {
|
||||
// Match user
|
||||
let username: Option<String> = match groups.get(1) {
|
||||
Some(group) => Some(group.as_str().to_string()),
|
||||
None => match protocol {
|
||||
// If group is empty, set to current user
|
||||
FileTransferProtocol::Scp | FileTransferProtocol::Sftp => {
|
||||
Some(whoami::username())
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
};
|
||||
let username = groups.get(1).map(|x| x.as_str().to_string());
|
||||
// Get address
|
||||
let address: String = match groups.get(2) {
|
||||
Some(group) => group.as_str().to_string(),
|
||||
@@ -437,7 +428,7 @@ mod tests {
|
||||
assert_eq!(result.protocol, FileTransferProtocol::Sftp);
|
||||
assert_eq!(params.address, String::from("172.26.104.1"));
|
||||
assert_eq!(params.port, 22);
|
||||
assert!(params.username.is_some());
|
||||
assert!(params.username.is_none());
|
||||
// User case
|
||||
let result: FileTransferParams = parse_remote_opt(&String::from("root@172.26.104.1"))
|
||||
.ok()
|
||||
@@ -472,7 +463,7 @@ mod tests {
|
||||
assert_eq!(result.protocol, FileTransferProtocol::Sftp);
|
||||
assert_eq!(params.address, String::from("172.26.104.1"));
|
||||
assert_eq!(params.port, 4022);
|
||||
assert!(params.username.is_some());
|
||||
assert!(params.username.is_none());
|
||||
assert!(result.entry_directory.is_none());
|
||||
// Protocol
|
||||
let result: FileTransferParams = parse_remote_opt(&String::from("ftp://172.26.104.1"))
|
||||
@@ -492,7 +483,7 @@ mod tests {
|
||||
assert_eq!(result.protocol, FileTransferProtocol::Sftp);
|
||||
assert_eq!(params.address, String::from("172.26.104.1"));
|
||||
assert_eq!(params.port, 22); // Fallback to sftp default
|
||||
assert!(params.username.is_some()); // Doesn't fall back
|
||||
assert!(params.username.is_none()); // Doesn't fall back
|
||||
assert!(result.entry_directory.is_none());
|
||||
let result: FileTransferParams = parse_remote_opt(&String::from("scp://172.26.104.1"))
|
||||
.ok()
|
||||
@@ -501,7 +492,7 @@ mod tests {
|
||||
assert_eq!(result.protocol, FileTransferProtocol::Scp);
|
||||
assert_eq!(params.address, String::from("172.26.104.1"));
|
||||
assert_eq!(params.port, 22); // Fallback to scp default
|
||||
assert!(params.username.is_some()); // Doesn't fall back
|
||||
assert!(params.username.is_none()); // Doesn't fall back
|
||||
assert!(result.entry_directory.is_none());
|
||||
// Protocol + user
|
||||
let result: FileTransferParams =
|
||||
@@ -539,7 +530,7 @@ mod tests {
|
||||
assert_eq!(result.protocol, FileTransferProtocol::Sftp);
|
||||
assert_eq!(params.address, String::from("172.26.104.1"));
|
||||
assert_eq!(params.port, 22);
|
||||
assert!(params.username.is_some());
|
||||
assert!(params.username.is_none());
|
||||
assert_eq!(result.entry_directory.unwrap(), PathBuf::from("home"));
|
||||
// All together now
|
||||
let result: FileTransferParams =
|
||||
|
||||
45
src/utils/ssh.rs
Normal file
45
src/utils/ssh.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use ssh2_config::{ParseRule, SshConfig};
|
||||
|
||||
pub fn parse_ssh2_config(path: &str) -> Result<SshConfig, String> {
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
|
||||
let mut reader = File::open(path)
|
||||
.map_err(|e| format!("failed to open {path}: {e}"))
|
||||
.map(BufReader::new)?;
|
||||
SshConfig::default()
|
||||
.parse(&mut reader, ParseRule::ALLOW_UNKNOWN_FIELDS)
|
||||
.map_err(|e| format!("Failed to parse ssh2 config: {e}"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
use crate::utils::{ssh::parse_ssh2_config, test_helpers};
|
||||
|
||||
#[test]
|
||||
fn should_parse_ssh2_config() {
|
||||
let rsa_key = test_helpers::create_sample_file_with_content("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDErJhQxEI0+VvhlXVUyh+vMCm7aXfCA/g633AG8ezD/5EylwchtAr2JCoBWnxn4zV8nI9dMqOgm0jO4IsXpKOjQojv+0VOH7I+cDlBg0tk4hFlvyyS6YviDAfDDln3jYUM+5QNDfQLaZlH2WvcJ3mkDxLVlI9MBX1BAeSmChLxwAvxALp2ncImNQLzDO9eHcig3dtMrEKkzXQowRW5Y7eUzg2+vvVq4H2DOjWwUndvB5sJkhEfTUVE7ID8ZdGJo60kUb/02dZYj+IbkAnMCsqktk0cg/4XFX82hEfRYFeb1arkysFisPU1DOb6QielL/axeTebVplaouYcXY0pFdJt root@8c50fd4c345a");
|
||||
let ssh_config_file = test_helpers::create_sample_file_with_content(format!(
|
||||
r#"
|
||||
Host test
|
||||
HostName 127.0.0.1
|
||||
Port 2222
|
||||
User test
|
||||
IdentityFile {}
|
||||
StrictHostKeyChecking no
|
||||
UserKnownHostsFile /dev/null
|
||||
"#,
|
||||
rsa_key.path().display()
|
||||
));
|
||||
|
||||
assert!(parse_ssh2_config(
|
||||
ssh_config_file
|
||||
.path()
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
.as_str()
|
||||
)
|
||||
.is_ok());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user