ssh-channels-hub 0.2.0

A CLI tool for managing SSH port forwarding tunnels with auto-reconnect
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use crate::error::{AppError, Result};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::path::PathBuf;

/// SSH host definition (previously channel definition)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HostConfig {
    /// Host name/identifier (used by channels to reference)
    pub name: String,
    /// Remote host address
    pub host: String,
    /// SSH port (defaults to 22)
    #[serde(default = "default_ssh_port")]
    pub port: u16,
    /// SSH username
    pub username: String,
    /// Authentication method
    pub auth: AuthConfig,
}

fn default_ssh_port() -> u16 {
    22
}

/// Port forwarding configuration (local:dest format)
#[derive(Debug, Clone)]
pub struct PortForward {
    /// Local port to bind (required)
    pub local_port: Option<u16>,
    /// Destination port (required)
    pub dest_port: u16,
}

impl PortForward {
    /// Parse port forward string in format "local:dest"
    /// Both local and dest ports are required (e.g., "80:3923")
    fn parse(s: &str) -> Result<Self> {
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 2 {
            return Err(AppError::Config(format!(
                "Invalid port format '{}'. Expected format: 'local:dest' (e.g., '80:3923')",
                s
            )));
        }

        if parts[0].is_empty() {
            return Err(AppError::Config(format!(
                "Invalid port format '{}'. Local port cannot be empty. Expected format: 'local:dest' (e.g., '80:3923')",
                s
            )));
        }

        if parts[1].is_empty() {
            return Err(AppError::Config(format!(
                "Invalid port format '{}'. Destination port cannot be empty. Expected format: 'local:dest' (e.g., '80:3923')",
                s
            )));
        }

        let local_port = parts[0]
            .parse::<u16>()
            .map_err(|e| AppError::Config(format!("Invalid local port '{}': {}", parts[0], e)))?;

        let dest_port = parts[1].parse::<u16>().map_err(|e| {
            AppError::Config(format!("Invalid destination port '{}': {}", parts[1], e))
        })?;

        Ok(PortForward {
            local_port: Some(local_port),
            dest_port,
        })
    }
}

impl<'de> Deserialize<'de> for PortForward {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        PortForward::parse(&s).map_err(serde::de::Error::custom)
    }
}

impl Serialize for PortForward {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let local = self.local_port.expect("local_port must be set");
        let s = format!("{}:{}", local, self.dest_port);
        serializer.serialize_str(&s)
    }
}

/// Channel definition referencing a host
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionConfig {
    /// Channel name/identifier
    pub name: String,
    /// Host reference (must match hosts.name)
    pub hostname: String,
    /// Channel type: "direct-tcpip" (local forward, like ssh -L) or "forwarded-tcpip" (remote forward, like ssh -R).
    /// Default: "direct-tcpip"
    #[serde(default)]
    pub channel_type: Option<String>,
    /// Port forwarding configuration.
    /// For direct-tcpip: "local:dest" (local listen port : remote dest port). Example: "80:3923"
    /// For forwarded-tcpip: "remote:local" (remote bind port : local connect port). Example: "8022:80"
    pub ports: PortForward,
    /// For direct-tcpip: destination host on remote (defaults to 127.0.0.1).
    /// For forwarded-tcpip: local host to connect to (defaults to 127.0.0.1).
    #[serde(default = "default_destination_host")]
    pub dest_host: String,
    /// Local listen address for direct-tcpip (defaults to 127.0.0.1).
    /// Use "0.0.0.0" to accept connections from any interface.
    /// Ignored for forwarded-tcpip.
    #[serde(default = "default_listen_host")]
    pub listen_host: String,
}

fn default_listen_host() -> String {
    "127.0.0.1".to_string()
}

fn default_destination_host() -> String {
    "127.0.0.1".to_string()
}

/// SSH channel configuration (runtime)
#[derive(Debug, Clone)]
pub struct ChannelConfig {
    /// Channel name/identifier
    pub name: String,
    /// Remote host address
    pub host: String,
    /// SSH port (defaults to 22)
    pub port: u16,
    /// SSH username
    pub username: String,
    /// Authentication method
    pub auth: AuthConfig,
    /// Channel type string for logging and status display (e.g. "direct-tcpip", "forwarded-tcpip")
    #[allow(dead_code)]
    pub channel_type: String,
    /// Parameters specific to the channel type; semantics are explicit per variant
    pub params: ChannelTypeParams,
}

/// Parameters for each channel type. Makes intent explicit and type-safe.
#[derive(Debug, Clone)]
pub enum ChannelTypeParams {
    /// Local port forwarding (ssh -L): listen locally, forward to remote dest.
    DirectTcpIp {
        listen_host: String,
        local_port: u16,
        dest_host: String,
        dest_port: u16,
    },
    /// Remote port forwarding (ssh -R): server binds port, we connect to local and bridge.
    ForwardedTcpIp {
        remote_bind_port: u16,
        local_connect_host: String,
        local_connect_port: u16,
    },
    /// Session channel (e.g. shell or single command).
    Session { command: Option<String> },
}

/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AuthConfig {
    /// Password authentication
    #[serde(rename = "password")]
    Password { password: String },
    /// Private key authentication
    #[serde(rename = "key")]
    Key {
        /// Path to private key file
        key_path: PathBuf,
        /// Optional passphrase for the key
        passphrase: Option<String>,
    },
}

/// Application configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    /// SSH hosts definition (replaces channels)
    pub hosts: Vec<HostConfig>,
    /// Channels referencing hosts
    #[serde(default)]
    pub channels: Vec<ConnectionConfig>,
    /// Reconnection settings
    #[serde(default)]
    pub reconnection: ReconnectionConfig,
}

/// Reconnection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconnectionConfig {
    /// Maximum retry attempts (0 = unlimited)
    #[serde(default = "default_max_retries")]
    pub max_retries: u32,
    /// Initial delay in seconds before retry
    #[serde(default = "default_initial_delay")]
    pub initial_delay_secs: u64,
    /// Maximum delay in seconds between retries
    #[serde(default = "default_max_delay")]
    pub max_delay_secs: u64,
    /// Use exponential backoff (true) or fixed interval (false)
    #[serde(default = "default_use_exponential")]
    pub use_exponential_backoff: bool,
}

fn default_max_retries() -> u32 {
    0 // Unlimited by default
}

fn default_initial_delay() -> u64 {
    1
}

fn default_max_delay() -> u64 {
    30
}

fn default_use_exponential() -> bool {
    true
}

impl Default for ReconnectionConfig {
    fn default() -> Self {
        Self {
            max_retries: default_max_retries(),
            initial_delay_secs: default_initial_delay(),
            max_delay_secs: default_max_delay(),
            use_exponential_backoff: default_use_exponential(),
        }
    }
}

impl AppConfig {
    /// Load configuration from a TOML file
    pub fn from_file(path: impl AsRef<std::path::Path>) -> Result<Self> {
        let content = std::fs::read_to_string(path.as_ref())
            .map_err(|e| AppError::Config(format!("Failed to read config file: {}", e)))?;

        let config: AppConfig = toml::from_str(&content)
            .map_err(|e| AppError::Config(format!("Failed to parse config: {}", e)))?;

        Ok(config)
    }

    /// Default config file candidates (first existing wins; if none exist, first is used).
    /// Order: current directory `configs.toml`, then platform config dir `config.toml`.
    pub fn default_path_candidates() -> Vec<PathBuf> {
        let current_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
        let mut candidates = vec![current_dir.join("configs.toml")];
        if let Some(mut path) = dirs::config_dir() {
            path.push("ssh-channels-hub");
            path.push("config.toml");
            candidates.push(path);
        }
        candidates
    }

    /// Get default configuration file path: first candidate that exists, or first candidate.
    pub fn default_path() -> PathBuf {
        for path in Self::default_path_candidates() {
            if path.exists() {
                return path;
            }
        }
        Self::default_path_candidates()
            .into_iter()
            .next()
            .unwrap_or_else(|| PathBuf::from("configs.toml"))
    }

    /// Generate configuration from SSH config entries
    pub fn from_ssh_config_entries(entries: Vec<crate::ssh_config::SshConfigEntry>) -> Self {
        let mut hosts = Vec::new();

        for entry in entries.into_iter() {
            // Skip entries without required fields
            let hostname = match entry.hostname {
                Some(h) => h,
                None => continue,
            };
            let username = match entry.user {
                Some(u) => u,
                None => continue,
            };

            // Determine authentication method
            let auth = if let Some(key_path) = entry.identity_file {
                AuthConfig::Key {
                    key_path,
                    passphrase: None, // Passphrase not available from SSH config
                }
            } else {
                // If no identity file, we'll use password auth as placeholder
                // User will need to fill in the password manually
                AuthConfig::Password {
                    password: "CHANGE_ME".to_string(),
                }
            };

            let host_cfg = HostConfig {
                name: entry.host.clone(),
                host: hostname,
                port: entry.port.unwrap_or(22), // Use port from SSH config or default to 22
                username,
                auth,
            };

            hosts.push(host_cfg);
        }

        Self {
            hosts,
            channels: Vec::new(), // Generate command doesn't create channels
            reconnection: ReconnectionConfig::default(),
        }
    }

    /// Build runtime channel configs by combining hosts and channels
    pub fn build_channels(&self) -> Result<Vec<ChannelConfig>> {
        let mut channels = Vec::new();

        for conn in &self.channels {
            let host_cfg = self
                .hosts
                .iter()
                .find(|h| h.name == conn.hostname)
                .ok_or_else(|| {
                    AppError::Config(format!(
                        "Channel '{}' references unknown host '{}'",
                        conn.name, conn.hostname
                    ))
                })?;

            let channel_type = conn
                .channel_type
                .as_deref()
                .unwrap_or("direct-tcpip")
                .to_string();

            let params = match channel_type.as_str() {
                "forwarded-tcpip" => {
                    let local_connect_port = conn.ports.local_port.ok_or_else(|| {
                        AppError::Config(format!(
                            "Channel '{}': forwarded-tcpip requires ports local:remote (e.g. 80:8022)",
                            conn.name
                        ))
                    })?;
                    ChannelTypeParams::ForwardedTcpIp {
                        remote_bind_port: conn.ports.dest_port,
                        local_connect_host: conn.dest_host.clone(),
                        local_connect_port,
                    }
                }
                "session" => ChannelTypeParams::Session { command: None },
                "direct-tcpip" => {
                    let local_port = conn.ports.local_port.ok_or_else(|| {
                        AppError::Config(format!(
                            "Channel '{}': direct-tcpip requires ports local:remote (e.g. 8080:80)",
                            conn.name
                        ))
                    })?;
                    ChannelTypeParams::DirectTcpIp {
                        listen_host: conn.listen_host.clone(),
                        local_port,
                        dest_host: conn.dest_host.clone(),
                        dest_port: conn.ports.dest_port,
                    }
                }
                unknown => {
                    return Err(AppError::Config(format!(
                        "Channel '{}': unknown channel_type '{}', expected 'direct-tcpip', 'forwarded-tcpip', or 'session'",
                        conn.name, unknown
                    )));
                }
            };

            channels.push(ChannelConfig {
                name: conn.name.clone(),
                host: host_cfg.host.clone(),
                port: host_cfg.port,
                username: host_cfg.username.clone(),
                auth: host_cfg.auth.clone(),
                channel_type,
                params,
            });
        }

        Ok(channels)
    }

    /// Save configuration to a TOML file
    pub fn to_file(&self, path: impl AsRef<std::path::Path>) -> Result<()> {
        let content = toml::to_string_pretty(self)
            .map_err(|e| AppError::Config(format!("Failed to serialize config: {}", e)))?;

        // Add comments before each [[hosts]] entry
        let content_with_comments = self.add_host_comments(&content);

        std::fs::write(path.as_ref(), content_with_comments)
            .map_err(|e| AppError::Config(format!("Failed to write config file: {}", e)))?;

        Ok(())
    }

    /// Add comments before each [[hosts]] entry
    fn add_host_comments(&self, content: &str) -> String {
        let mut result = String::new();
        let lines: Vec<&str> = content.lines().collect();
        let mut i = 0;

        while i < lines.len() {
            let line = lines[i];

            // Check if this is a [[hosts]] line
            if line.trim() == "[[hosts]]" {
                // Find the corresponding host config to get its name
                if let Some(host_idx) = self.find_host_index(&lines, i) {
                    let host = &self.hosts[host_idx];

                    // Check if there's already a blank line before this entry
                    let has_blank_before = i > 0 && lines[i - 1].trim().is_empty();

                    // Add a blank line if there isn't one already
                    if !has_blank_before && !result.trim().is_empty() {
                        result.push('\n');
                    }

                    // Add comment with host information
                    result.push_str(&format!("# Host: {} ({})\n", host.name, host.host));
                }
            }

            result.push_str(line);
            result.push('\n');
            i += 1;
        }

        result
    }

    /// Find which host index corresponds to a [[hosts]] line at a given position
    fn find_host_index(&self, lines: &[&str], start_pos: usize) -> Option<usize> {
        // Count how many [[hosts]] entries appear before this position
        let host_count = lines
            .iter()
            .take(start_pos)
            .filter(|line| line.trim() == "[[hosts]]")
            .count();

        if host_count < self.hosts.len() {
            Some(host_count)
        } else {
            None
        }
    }
}