Skip to content

Commit 0fcb1ec

Browse files
authored
Allow retrieving peer SSH Protocol Version String (#260)
Allow retrieving peer SSH Protocol Version String
1 parent f1985e1 commit 0fcb1ec

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

russh/src/client/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ where
697697
strict_kex: false,
698698
alive_timeouts: 0,
699699
received_data: false,
700+
remote_sshid: sshid.into(),
700701
},
701702
session_receiver,
702703
session_sender,

russh/src/client/session.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl Session {
268268
/// Requests a TCP/IP forwarding from the server
269269
///
270270
/// If `reply_channel` is not None, sets want_reply and returns the server's response via the channel,
271-
/// Some<u32> for a success message with port, or None for failure
271+
/// [`Some<u32>`] for a success message with port, or [`None`] for failure
272272
pub fn tcpip_forward(
273273
&mut self,
274274
reply_channel: Option<oneshot::Sender<Option<u32>>>,
@@ -395,4 +395,17 @@ impl Session {
395395
0
396396
}
397397
}
398+
399+
/// Returns the SSH ID (Protocol Version + Software Version) the server sent when connecting
400+
///
401+
/// This should contain only ASCII characters for implementations conforming to RFC4253, Section 4.2:
402+
///
403+
/// > Both the 'protoversion' and 'softwareversion' strings MUST consist of
404+
/// > printable US-ASCII characters, with the exception of whitespace
405+
/// > characters and the minus sign (-).
406+
///
407+
/// So it usually is fine to convert it to a `String` using `String::from_utf8_lossy`
408+
pub fn remote_sshid(&self) -> &[u8] {
409+
&self.common.remote_sshid
410+
}
398411
}

russh/src/server/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! # Writing servers
1717
//!
1818
//! There are two ways of accepting connections:
19-
//! * implement the [Server](server::Server) trait and let [run](server::run) handle everything
19+
//! * implement the [Server](server::Server) trait and let [run_on_socket](server::Server::run_on_socket)/[run_on_address](server::Server::run_on_address) handle everything
2020
//! * accept connections yourself and pass them to [run_stream](server::run_stream)
2121
//!
2222
//! In both cases, you'll first need to implement the [Handler](server::Handler) trait -
@@ -731,6 +731,7 @@ async fn read_ssh_id<R: AsyncRead + Unpin>(
731731
strict_kex: false,
732732
alive_timeouts: 0,
733733
received_data: false,
734+
remote_sshid: sshid.into(),
734735
})
735736
}
736737

russh/src/server/session.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,19 @@ impl Session {
10281028
}
10291029
}
10301030

1031+
/// Returns the SSH ID (Protocol Version + Software Version) the client sent when connecting
1032+
///
1033+
/// This should contain only ASCII characters for implementations conforming to RFC4253, Section 4.2:
1034+
///
1035+
/// > Both the 'protoversion' and 'softwareversion' strings MUST consist of
1036+
/// > printable US-ASCII characters, with the exception of whitespace
1037+
/// > characters and the minus sign (-).
1038+
///
1039+
/// So it usually is fine to convert it to a [`String`] using [`String::from_utf8_lossy`]
1040+
pub fn remote_sshid(&self) -> &[u8] {
1041+
&self.common.remote_sshid
1042+
}
1043+
10311044
pub(crate) fn maybe_send_ext_info(&mut self) {
10321045
if let Some(ref mut enc) = self.common.encrypted {
10331046
// If client sent a ext-info-c message in the kex list, it supports RFC 8308 extension negotiation.

russh/src/session.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub(crate) struct Encrypted {
5454

5555
pub(crate) struct CommonSession<Config> {
5656
pub auth_user: String,
57+
pub remote_sshid: Vec<u8>,
5758
pub config: Config,
5859
pub encrypted: Option<Encrypted>,
5960
pub auth_method: Option<auth::Method>,

0 commit comments

Comments
 (0)