Skip to content

Commit

Permalink
simplify ssh mod
Browse files Browse the repository at this point in the history
  • Loading branch information
haxjump committed Oct 17, 2024
1 parent d4c1134 commit 91a80ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ruc"
version = "7.6.0"
version = "7.7.0"
authors = ["[email protected]"]
edition = "2021"
description = "Rust Util Collections"
Expand Down
31 changes: 10 additions & 21 deletions src/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct RemoteHost<'a> {
/// The sshd listening port of the remote host.
pub port: Port,
/// Path list of the ssh secret keys(rsa/ed25519 key).
pub local_seckeys: Vec<&'a Path>,
pub local_sk: &'a Path,
}

impl<'a> RemoteHost<'a> {
Expand All @@ -43,16 +43,9 @@ impl<'a> RemoteHost<'a> {
.c(d!())?;
sess.set_tcp_stream(tcp);
sess.handshake().c(d!()).and_then(|_| {
for seckey in self.local_seckeys.iter() {
let ret =
sess.userauth_pubkey_file(self.user, None, seckey, None);
if ret.is_ok() {
return ret.c(d!());
} else {
info_omit!(ret);
}
}
Err(eg!("{:?}", self))
let p = PathBuf::from(self.local_sk);
sess.userauth_pubkey_file(self.user, None, p.as_path(), None)
.c(d!())
})?;

let timeout = env::var("RUC_SSH_TIMEOUT")
Expand Down Expand Up @@ -236,7 +229,7 @@ pub struct RemoteHostOwned {
/// The sshd listening port of the remote host.
pub port: Port,
/// Path list of the ssh secret keys(rsa/ed25519 key).
pub local_seckeys: Vec<PathBuf>,
pub local_sk: PathBuf,
}

impl RemoteHostOwned {
Expand All @@ -247,11 +240,11 @@ impl RemoteHostOwned {
let rsa_key_path = PathBuf::from(format!("{}/.ssh/id_rsa", &home));
let ed25519_key_path = PathBuf::from(home + "/.ssh/id_ed25519");

let mut local_seckeys = vec![];
let local_sk;
if ed25519_key_path.exists() {
local_seckeys.push(ed25519_key_path);
local_sk = ed25519_key_path;
} else if rsa_key_path.exists() {
local_seckeys.push(rsa_key_path);
local_sk = rsa_key_path;
} else {
return Err(eg!(
"Private key not found, neither RSA nor ED25519."
Expand All @@ -262,7 +255,7 @@ impl RemoteHostOwned {
addr,
user: remote_user,
port: 22,
local_seckeys,
local_sk,
})
}
}
Expand All @@ -273,11 +266,7 @@ impl<'a> From<&'a RemoteHostOwned> for RemoteHost<'a> {
addr: o.addr.as_str(),
user: o.user.as_str(),
port: o.port,
local_seckeys: o
.local_seckeys
.iter()
.map(|k| k.as_path())
.collect::<Vec<_>>(),
local_sk: o.local_sk.as_path(),
}
}
}

0 comments on commit 91a80ba

Please sign in to comment.