|
| 1 | +//! Cross-platform SSH option types shared between different backends. |
| 2 | +//! |
| 3 | +//! Extracted from ssh.rs to allow macOS and Windows backends to share |
| 4 | +//! SSH option types without pulling in Linux-specific dependencies. |
| 5 | +
|
| 6 | +/// Common SSH options that can be shared between different SSH implementations |
| 7 | +#[derive(Debug, Clone)] |
| 8 | +#[allow(dead_code)] |
| 9 | +pub struct CommonSshOptions { |
| 10 | + /// Use strict host key checking |
| 11 | + pub strict_host_keys: bool, |
| 12 | + /// SSH connection timeout in seconds |
| 13 | + pub connect_timeout: u32, |
| 14 | + /// Server alive interval in seconds |
| 15 | + pub server_alive_interval: u32, |
| 16 | + /// SSH log level |
| 17 | + pub log_level: String, |
| 18 | + /// Additional SSH options as key-value pairs |
| 19 | + pub extra_options: Vec<(String, String)>, |
| 20 | +} |
| 21 | + |
| 22 | +impl Default for CommonSshOptions { |
| 23 | + fn default() -> Self { |
| 24 | + Self { |
| 25 | + strict_host_keys: false, |
| 26 | + connect_timeout: 1, |
| 27 | + server_alive_interval: 60, |
| 28 | + log_level: "ERROR".to_string(), |
| 29 | + extra_options: vec![], |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl CommonSshOptions { |
| 35 | + /// Apply these options to an SSH command |
| 36 | + #[allow(dead_code)] |
| 37 | + pub fn apply_to_command(&self, cmd: &mut std::process::Command) { |
| 38 | + // Basic security options |
| 39 | + cmd.args(["-o", "IdentitiesOnly=yes"]); |
| 40 | + cmd.args(["-o", "PasswordAuthentication=no"]); |
| 41 | + cmd.args(["-o", "KbdInteractiveAuthentication=no"]); |
| 42 | + cmd.args(["-o", "GSSAPIAuthentication=no"]); |
| 43 | + |
| 44 | + // Connection options |
| 45 | + cmd.args(["-o", &format!("ConnectTimeout={}", self.connect_timeout)]); |
| 46 | + cmd.args([ |
| 47 | + "-o", |
| 48 | + &format!("ServerAliveInterval={}", self.server_alive_interval), |
| 49 | + ]); |
| 50 | + cmd.args(["-o", &format!("LogLevel={}", self.log_level)]); |
| 51 | + |
| 52 | + // Host key checking |
| 53 | + if !self.strict_host_keys { |
| 54 | + cmd.args(["-o", "StrictHostKeyChecking=no"]); |
| 55 | + cmd.args(["-o", "UserKnownHostsFile=/dev/null"]); |
| 56 | + } |
| 57 | + |
| 58 | + // Add extra SSH options |
| 59 | + for (key, value) in &self.extra_options { |
| 60 | + cmd.args(["-o", &format!("{}={}", key, value)]); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// SSH connection configuration options |
| 66 | +#[derive(Debug, Clone)] |
| 67 | +#[allow(dead_code)] |
| 68 | +pub struct SshConnectionOptions { |
| 69 | + /// Common SSH options shared across implementations |
| 70 | + pub common: CommonSshOptions, |
| 71 | + /// Enable/disable TTY allocation (default: true) |
| 72 | + pub allocate_tty: bool, |
| 73 | + /// Suppress output to stdout/stderr (default: false) |
| 74 | + pub suppress_output: bool, |
| 75 | +} |
| 76 | + |
| 77 | +impl Default for SshConnectionOptions { |
| 78 | + fn default() -> Self { |
| 79 | + Self { |
| 80 | + common: CommonSshOptions::default(), |
| 81 | + allocate_tty: true, |
| 82 | + suppress_output: false, |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl SshConnectionOptions { |
| 88 | + /// Create options suitable for quick connectivity tests (short timeout, no TTY) |
| 89 | + #[allow(dead_code)] |
| 90 | + pub fn for_connectivity_test() -> Self { |
| 91 | + Self { |
| 92 | + common: CommonSshOptions { |
| 93 | + strict_host_keys: false, |
| 94 | + connect_timeout: 2, |
| 95 | + server_alive_interval: 60, |
| 96 | + log_level: "ERROR".to_string(), |
| 97 | + extra_options: vec![], |
| 98 | + }, |
| 99 | + allocate_tty: false, |
| 100 | + suppress_output: true, |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use super::*; |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn test_common_ssh_options_default() { |
| 111 | + let opts = CommonSshOptions::default(); |
| 112 | + assert!(!opts.strict_host_keys); |
| 113 | + assert_eq!(opts.connect_timeout, 1); |
| 114 | + assert_eq!(opts.server_alive_interval, 60); |
| 115 | + assert_eq!(opts.log_level, "ERROR"); |
| 116 | + assert!(opts.extra_options.is_empty()); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_ssh_connection_options() { |
| 121 | + // Test default options |
| 122 | + let default_opts = SshConnectionOptions::default(); |
| 123 | + assert_eq!(default_opts.common.connect_timeout, 1); |
| 124 | + assert!(default_opts.allocate_tty); |
| 125 | + assert_eq!(default_opts.common.log_level, "ERROR"); |
| 126 | + assert!(default_opts.common.extra_options.is_empty()); |
| 127 | + assert!(!default_opts.suppress_output); |
| 128 | + |
| 129 | + // Test connectivity test options |
| 130 | + let test_opts = SshConnectionOptions::for_connectivity_test(); |
| 131 | + assert_eq!(test_opts.common.connect_timeout, 2); |
| 132 | + assert!(!test_opts.allocate_tty); |
| 133 | + assert_eq!(test_opts.common.log_level, "ERROR"); |
| 134 | + assert!(test_opts.common.extra_options.is_empty()); |
| 135 | + assert!(test_opts.suppress_output); |
| 136 | + |
| 137 | + // Test custom options |
| 138 | + let mut custom_opts = SshConnectionOptions::default(); |
| 139 | + custom_opts.common.connect_timeout = 10; |
| 140 | + custom_opts.allocate_tty = false; |
| 141 | + custom_opts.common.log_level = "DEBUG".to_string(); |
| 142 | + custom_opts |
| 143 | + .common |
| 144 | + .extra_options |
| 145 | + .push(("ServerAliveInterval".to_string(), "30".to_string())); |
| 146 | + |
| 147 | + assert_eq!(custom_opts.common.connect_timeout, 10); |
| 148 | + assert!(!custom_opts.allocate_tty); |
| 149 | + assert_eq!(custom_opts.common.log_level, "DEBUG"); |
| 150 | + assert_eq!(custom_opts.common.extra_options.len(), 1); |
| 151 | + assert_eq!( |
| 152 | + custom_opts.common.extra_options[0], |
| 153 | + ("ServerAliveInterval".to_string(), "30".to_string()) |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn test_apply_to_command() { |
| 159 | + let opts = CommonSshOptions::default(); |
| 160 | + let mut cmd = std::process::Command::new("ssh"); |
| 161 | + opts.apply_to_command(&mut cmd); |
| 162 | + let args: Vec<_> = cmd |
| 163 | + .get_args() |
| 164 | + .map(|a| a.to_string_lossy().to_string()) |
| 165 | + .collect(); |
| 166 | + assert!(args.contains(&"IdentitiesOnly=yes".to_string())); |
| 167 | + assert!(args.contains(&"PasswordAuthentication=no".to_string())); |
| 168 | + assert!(args.contains(&"StrictHostKeyChecking=no".to_string())); |
| 169 | + assert!(args.contains(&"ConnectTimeout=1".to_string())); |
| 170 | + } |
| 171 | +} |
0 commit comments