Skip to content

Commit

Permalink
fixes & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Aug 4, 2024
1 parent 2b3a565 commit 89d2600
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 152 deletions.
16 changes: 2 additions & 14 deletions russh/src/client/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,19 +860,7 @@ impl Session {
let _ = return_channel.send(true);
}
Some(GlobalRequestResponse::StreamLocalForward(return_channel)) => {
let mut r = buf.reader(1);
let socket_path: Option<String> = match r.read_string() {
Ok(socket_path) => Some(
std::str::from_utf8(socket_path)
.map_err(crate::Error::from)?
.into(),
),
Err(e) => {
error!("Error parsing socket path for StreamLocalForward request: {e:?}");
None
}
};
let _ = return_channel.send(socket_path);
let _ = return_channel.send(true);
}
Some(GlobalRequestResponse::CancelStreamLocalForward(return_channel)) => {
let _ = return_channel.send(true);
Expand All @@ -896,7 +884,7 @@ impl Session {
let _ = return_channel.send(false);
}
Some(GlobalRequestResponse::StreamLocalForward(return_channel)) => {
let _ = return_channel.send(None);
let _ = return_channel.send(false);
}
Some(GlobalRequestResponse::CancelStreamLocalForward(return_channel)) => {
let _ = return_channel.send(false);
Expand Down
8 changes: 4 additions & 4 deletions russh/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub enum Msg {
},
StreamLocalForward {
/// Provide a channel for the reply result to request a reply from the server
reply_channel: Option<oneshot::Sender<Option<String>>>,
reply_channel: Option<oneshot::Sender<bool>>,
socket_path: String,
},
CancelStreamLocalForward {
Expand Down Expand Up @@ -611,7 +611,7 @@ impl<H: Handler> Handle<H> {
pub async fn streamlocal_forward<A: Into<String>>(
&mut self,
socket_path: A,
) -> Result<String, crate::Error> {
) -> Result<(), crate::Error> {
let (reply_send, reply_recv) = oneshot::channel();
self.sender
.send(Msg::StreamLocalForward {
Expand All @@ -622,8 +622,8 @@ impl<H: Handler> Handle<H> {
.map_err(|_| crate::Error::SendError)?;

match reply_recv.await {
Ok(Some(returned_socket_path)) => Ok(returned_socket_path),
Ok(None) => Err(crate::Error::RequestDenied),
Ok(true) => Ok(()),
Ok(false) => Err(crate::Error::RequestDenied),
Err(e) => {
error!("Unable to receive StreamLocalForward result: {e:?}");
Err(crate::Error::Disconnect)
Expand Down
8 changes: 4 additions & 4 deletions russh/src/client/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl Session {

/// Requests cancellation of TCP/IP forwarding from the server
///
/// If `want_reply` is `true`, returns a oneshot receiving the server's reply:
/// If `reply_channel` is not None, sets want_reply and returns the server's response via the channel,
/// `true` for a success message, or `false` for failure
pub fn cancel_tcpip_forward(
&mut self,
Expand Down Expand Up @@ -321,10 +321,10 @@ impl Session {
/// Requests a UDS forwarding from the server, `socket path` being the server side socket path.
///
/// If `reply_channel` is not None, sets want_reply and returns the server's response via the channel,
/// [`Some<String>`] for a success message with the client side socket path, [`None`] for failure.
/// `true` for a success message, or `false` for failure
pub fn streamlocal_forward(
&mut self,
reply_channel: Option<oneshot::Sender<Option<String>>>,
reply_channel: Option<oneshot::Sender<bool>>,
socket_path: &str,
) {
if let Some(ref mut enc) = self.common.encrypted {
Expand All @@ -346,7 +346,7 @@ impl Session {

/// Requests cancellation of UDS forwarding from the server
///
/// If `want_reply` is true, returns a oneshot receiving the server's reply:
/// If `reply_channel` is not None, sets want_reply and returns the server's response via the channel,
/// `true` for a success message and `false` for failure.
pub fn cancel_streamlocal_forward(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion russh/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl OpenChannelMessage {
}
b"direct-tcpip" => ChannelType::DirectTcpip(TcpChannelInfo::new(r)?),
b"forwarded-tcpip" => ChannelType::ForwardedTcpIp(TcpChannelInfo::new(r)?),
b"forwarded-streamlocal" => {
b"forwarded-streamlocal@openssh.com" => {
ChannelType::ForwardedStreamLocal(StreamLocalChannelInfo::new(r)?)
}
b"auth-agent@openssh.com" => ChannelType::AgentForward,
Expand Down
28 changes: 2 additions & 26 deletions russh/src/server/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,25 +1122,7 @@ impl Session {
Some(GlobalRequestResponse::CancelTcpIpForward(return_channel)) => {
let _ = return_channel.send(true);
}
Some(GlobalRequestResponse::StreamLocalForward(return_channel)) => {
let mut r = buf.reader(1);
let socket_path: Option<String> = match r.read_string() {
Ok(socket_path) => Some(
std::str::from_utf8(socket_path)
.map_err(crate::Error::from)?
.into(),
),
Err(e) => {
error!("Error parsing socket path for StreamLocalForward request: {e:?}");
None
}
};
let _ = return_channel.send(socket_path);
}
Some(GlobalRequestResponse::CancelStreamLocalForward(return_channel)) => {
let _ = return_channel.send(true);
}
None => {
_ => {
error!("Received global request failure for unknown request!")
}
}
Expand All @@ -1158,13 +1140,7 @@ impl Session {
Some(GlobalRequestResponse::CancelTcpIpForward(return_channel)) => {
let _ = return_channel.send(false);
}
Some(GlobalRequestResponse::StreamLocalForward(return_channel)) => {
let _ = return_channel.send(None);
}
Some(GlobalRequestResponse::CancelStreamLocalForward(return_channel)) => {
let _ = return_channel.send(false);
}
None => {
_ => {
error!("Received global request failure for unknown request!")
}
}
Expand Down
102 changes: 0 additions & 102 deletions russh/src/server/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ pub enum Msg {
address: String,
port: u32,
},
StreamLocalForward {
// Provide a channel for the reply result to request a reply from the server
reply_channel: Option<oneshot::Sender<Option<String>>>,
socket_path: String,
},
CancelStreamLocalForward {
// Provide a channel for the reply result to request a reply from the server
reply_channel: Option<oneshot::Sender<bool>>,
socket_path: String,
},
Disconnect {
reason: crate::Disconnect,
description: String,
Expand Down Expand Up @@ -195,48 +185,6 @@ impl Handle {
}
}

// Notifies the client that it can open UDS forwarding channels for a given UDS
pub async fn forward_streamlocal(&self, socket_path: String) -> Result<String, ()> {
let (reply_send, reply_recv) = oneshot::channel();
self.sender
.send(Msg::StreamLocalForward {
reply_channel: Some(reply_send),
socket_path,
})
.await
.map_err(|_| ())?;

match reply_recv.await {
Ok(Some(socket_path)) => Ok(socket_path),
Ok(None) => Err(()),
Err(e) => {
error!("Unable to receive StreamLocalForward result: {e:?}");
Err(())
}
}
}

/// Notifies the client that it can no longer open TCP/IP forwarding channel for a port.
pub async fn cancel_forward_tcpip(&self, address: String, port: u32) -> Result<(), ()> {
let (reply_send, reply_recv) = oneshot::channel();
self.sender
.send(Msg::CancelTcpIpForward {
reply_channel: Some(reply_send),
address,
port,
})
.await
.map_err(|_| ())?;
match reply_recv.await {
Ok(true) => Ok(()),
Ok(false) => Err(()), // crate::Error::RequestDenied
Err(e) => {
error!("Unable to receive CancelTcpIpForward result: {e:?}");
Err(()) // crate::Error::Disconnect
}
}
}

/// Request a session channel (the most basic type of
/// channel). This function returns `Ok(..)` immediately if the
/// connection is authenticated, but the channel only becomes
Expand Down Expand Up @@ -594,12 +542,6 @@ impl Session {
Some(Msg::CancelTcpIpForward { address, port, reply_channel }) => {
self.cancel_tcpip_forward(&address, port, reply_channel);
}
Some(Msg::StreamLocalForward { socket_path, reply_channel }) => {
self.streamlocal_forward(&socket_path, reply_channel);
}
Some(Msg::CancelStreamLocalForward { socket_path, reply_channel }) => {
self.cancel_streamlocal_forward(&socket_path, reply_channel);
}
Some(Msg::Disconnect {reason, description, language_tag}) => {
self.common.disconnect(reason, &description, &language_tag);
}
Expand Down Expand Up @@ -1105,50 +1047,6 @@ impl Session {
}
}

pub fn streamlocal_forward(
&mut self,
socket_path: &str,
reply_channel: Option<oneshot::Sender<Option<String>>>,
) {
if let Some(ref mut enc) = self.common.encrypted {
let want_reply = reply_channel.is_some();
if let Some(reply_channel) = reply_channel {
self.open_global_requests.push_back(
crate::session::GlobalRequestResponse::StreamLocalForward(reply_channel),
);
}
push_packet!(enc.write, {
enc.write.push(msg::GLOBAL_REQUEST);
enc.write
.extend_ssh_string(b"streamlocal-forward@openssh.com");
enc.write.push(want_reply as u8);
enc.write.extend_ssh_string(socket_path.as_bytes());
})
}
}

pub fn cancel_streamlocal_forward(
&mut self,
socket_path: &str,
reply_channel: Option<oneshot::Sender<bool>>,
) {
if let Some(ref mut enc) = self.common.encrypted {
let want_reply = reply_channel.is_some();
if let Some(reply_channel) = reply_channel {
self.open_global_requests.push_back(
crate::session::GlobalRequestResponse::CancelStreamLocalForward(reply_channel),
);
}
push_packet!(enc.write, {
enc.write.push(msg::GLOBAL_REQUEST);
enc.write
.extend_ssh_string(b"cancel-streamlocal-forward@openssh.com");
enc.write.push(want_reply as u8);
enc.write.extend_ssh_string(socket_path.as_bytes());
});
}
}

/// Returns the SSH ID (Protocol Version + Software Version) the client sent when connecting
///
/// This should contain only ASCII characters for implementations conforming to RFC4253, Section 4.2:
Expand Down
3 changes: 2 additions & 1 deletion russh/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ pub(crate) enum GlobalRequestResponse {
TcpIpForward(oneshot::Sender<Option<u32>>),
/// request was for CancelTcpIpForward, sends true for success or false for failure
CancelTcpIpForward(oneshot::Sender<bool>),
StreamLocalForward(oneshot::Sender<Option<String>>),
/// request was for StreamLocalForward, sends true for success or false for failure
StreamLocalForward(oneshot::Sender<bool>),
CancelStreamLocalForward(oneshot::Sender<bool>),
}

0 comments on commit 89d2600

Please sign in to comment.