ssh/config/
mod.rs

1pub(crate) mod algorithm;
2pub(crate) mod auth;
3pub(crate) mod version;
4use crate::algorithm::PubKey as PubKeyAlgs;
5use std::time::Duration;
6
7fn insert_or_move_first(v: &mut Vec<PubKeyAlgs>, alg: PubKeyAlgs) {
8    if let Some(i) = v.iter().position(|each| *each == alg) {
9        v.swap(0, i)
10    } else {
11        v.insert(0, alg)
12    }
13}
14
15#[derive(Clone)]
16pub(crate) struct Config {
17    pub ver: version::SshVersion,
18    pub auth: auth::AuthInfo,
19    pub algs: algorithm::AlgList,
20    pub timeout: Option<Duration>,
21    auto_tune: bool,
22}
23
24impl Default for Config {
25    fn default() -> Self {
26        Self {
27            algs: algorithm::AlgList::client_default(),
28            auth: auth::AuthInfo::default(),
29            ver: version::SshVersion::default(),
30            timeout: Some(Duration::from_secs(30)),
31            auto_tune: true,
32        }
33    }
34}
35
36impl Config {
37    // use an empty client algorithm list
38    pub fn disable_default() -> Self {
39        Self {
40            algs: algorithm::AlgList::default(),
41            auth: auth::AuthInfo::default(),
42            ver: version::SshVersion::default(),
43            timeout: Some(Duration::from_secs(30)),
44            auto_tune: false,
45        }
46    }
47
48    pub(crate) fn tune_alglist_on_private_key(&mut self) {
49        if !self.auto_tune {
50            return;
51        }
52
53        if let Some(ref key_pair) = self.auth.key_pair {
54            match key_pair.key_type {
55                auth::KeyType::PemRsa | auth::KeyType::SshRsa => {
56                    let pubkeys = &mut self.algs.public_key;
57                    insert_or_move_first(pubkeys, PubKeyAlgs::RsaSha2_256);
58                    insert_or_move_first(pubkeys, PubKeyAlgs::RsaSha2_512);
59                }
60                auth::KeyType::SshEd25519 => {
61                    let pubkeys = &mut self.algs.public_key;
62                    insert_or_move_first(pubkeys, PubKeyAlgs::SshEd25519);
63                }
64            }
65        }
66    }
67}