ssh2_config/params/
remote_forward.rs1use std::fmt;
4use std::path::{Path, PathBuf};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct RemoteForward {
27 pub listen: RemoteForwardListen,
29 pub destination: Option<RemoteForwardDestination>,
31}
32
33impl RemoteForward {
34 pub fn new(listen: RemoteForwardListen, destination: Option<RemoteForwardDestination>) -> Self {
36 Self {
37 listen,
38 destination,
39 }
40 }
41}
42
43impl fmt::Display for RemoteForward {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 write!(f, "{listen}", listen = self.listen)?;
46 if let Some(destination) = &self.destination {
47 write!(f, " {destination}")?;
48 }
49 Ok(())
50 }
51}
52
53#[derive(Debug, Clone, PartialEq, Eq)]
55pub enum RemoteForwardListen {
56 Port(u16),
58 Host {
60 host: String,
62 port: u16,
64 },
65 UnixSocket(PathBuf),
67}
68
69impl fmt::Display for RemoteForwardListen {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 match self {
72 Self::Port(port) => write!(f, "{port}"),
73 Self::Host { host, port } => write_host_port(f, host, *port),
74 Self::UnixSocket(path) => write_socket_path(f, path),
75 }
76 }
77}
78
79#[derive(Debug, Clone, PartialEq, Eq)]
81pub enum RemoteForwardDestination {
82 Host {
84 host: String,
86 port: u16,
88 },
89 UnixSocket(PathBuf),
91}
92
93impl fmt::Display for RemoteForwardDestination {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 match self {
96 Self::Host { host, port } => write_host_port(f, host, *port),
97 Self::UnixSocket(path) => write_socket_path(f, path),
98 }
99 }
100}
101
102fn write_host_port(f: &mut fmt::Formatter<'_>, host: &str, port: u16) -> fmt::Result {
103 if host.contains(':') {
104 write!(f, "[{host}]:{port}")
105 } else {
106 write!(f, "{host}:{port}")
107 }
108}
109
110fn write_socket_path(f: &mut fmt::Formatter<'_>, path: &Path) -> fmt::Result {
111 let path = path.display().to_string();
112 if path.chars().any(char::is_whitespace) {
113 write!(
114 f,
115 "\"{path}\"",
116 path = path.replace('\\', "\\\\").replace('"', "\\\"")
117 )
118 } else {
119 write!(f, "{path}")
120 }
121}