This commit is contained in:
Eugene Pankov 2022-09-03 23:19:47 +02:00
parent 6f8fbed25f
commit 804bdad01e
No known key found for this signature in database
GPG key ID: 5896FCBBDD1CF4F4
5 changed files with 63 additions and 17 deletions

2
russh

@ -1 +1 @@
Subproject commit 8cfa869c3dfb051ccc99f06bc911a074674cc636
Subproject commit e65dd27f2102b2cb7018981f7689525a9824a8a6

View file

@ -128,6 +128,7 @@ class Test:
'-v',
*common_args,
'-L', f'{local_port}:neverssl.com:80',
'-N',
password='123',
)

View file

@ -19,6 +19,7 @@ pub enum ClientHandlerEvent {
HostKeyReceived(PublicKey),
HostKeyUnknown(PublicKey, oneshot::Sender<bool>),
ForwardedTcpIp(Channel<Msg>, ForwardedTcpIpParams),
X11(Channel<Msg>, String, u32),
Disconnect,
}
@ -126,7 +127,7 @@ impl russh::client::Handler for ClientHandler {
.boxed()
}
fn channel_open_forwarded_tcpip(
fn server_channel_open_forwarded_tcpip(
self,
channel: Channel<Msg>,
connected_address: &str,
@ -151,6 +152,25 @@ impl russh::client::Handler for ClientHandler {
}
.boxed()
}
fn server_channel_open_x11(
self,
channel: Channel<Msg>,
originator_address: &str,
originator_port: u32,
session: Session,
) -> Self::FutureUnit {
let originator_address = originator_address.to_string();
async move {
let _ = self.event_tx.send(ClientHandlerEvent::X11(
channel,
originator_address,
originator_port,
));
Ok((self, session))
}
.boxed()
}
}
impl Drop for ClientHandler {

View file

@ -92,6 +92,7 @@ pub enum RCEvent {
HostKeyReceived(PublicKey),
HostKeyUnknown(PublicKey, oneshot::Sender<bool>),
ForwardedTcpIp(Uuid, ForwardedTcpIpParams),
X11(Uuid, String, u32),
}
pub type RCCommandReply = oneshot::Sender<Result<(), SshClientError>>;
@ -309,21 +310,16 @@ impl RemoteClient {
}
ClientHandlerEvent::ForwardedTcpIp(channel, params) => {
info!("New forwarded connection: {params:?}");
let id = Uuid::new_v4();
let (tx, rx) = unbounded_channel();
self.channel_pipes.lock().await.insert(id, tx);
let session_channel =
SessionChannel::new(channel, id, rx, self.tx.clone(), self.id);
self.child_tasks.push(
tokio::task::Builder::new()
.name(&format!("SSH {} {:?} ops", self.id, id))
.spawn(session_channel.run()),
);
let id = self.setup_server_initiated_channel(channel).await;
let _ = self.tx.send(RCEvent::ForwardedTcpIp(id, params));
}
ClientHandlerEvent::X11(channel, originator_address, originator_port) => {
info!("New X11 connection from {originator_address}:{originator_port:?}");
let id = self.setup_server_initiated_channel(channel).await;
let _ = self
.tx
.send(RCEvent::X11(id, originator_address, originator_port));
}
event => {
error!(?event, "Unhandled client handler event");
}
@ -333,6 +329,23 @@ impl RemoteClient {
Ok(false)
}
async fn setup_server_initiated_channel(&mut self, channel: russh::Channel<russh::client::Msg>) -> Uuid {
let id = Uuid::new_v4();
let (tx, rx) = unbounded_channel();
self.channel_pipes.lock().await.insert(id, tx);
let session_channel = SessionChannel::new(channel, id, rx, self.tx.clone(), self.id);
self.child_tasks.push(
tokio::task::Builder::new()
.name(&format!("SSH {} {:?} ops", self.id, id))
.spawn(session_channel.run()),
);
id
}
async fn handle_command(&mut self, cmd: RCCommand) -> Result<bool, SshClientError> {
match cmd {
RCCommand::Connect(options) => match self.connect(options).await {

View file

@ -725,8 +725,6 @@ impl ServerSession {
self.handle_unknown_host_key(key, reply).await?;
}
RCEvent::ForwardedTcpIp(id, params) => {
println!("{:?} {:?}", id, params);
if let Some(session) = &mut self.session_handle {
let server_channel = session
.channel_open_forwarded_tcpip(
@ -742,6 +740,20 @@ impl ServerSession {
self.all_channels.push(id);
}
}
RCEvent::X11(id, originator_address, originator_port) =>{
if let Some(session) = &mut self.session_handle {
let server_channel = session
.channel_open_x11(
originator_address,
originator_port,
)
.await?;
self.channel_map
.insert(ServerChannelId(server_channel.id()), id);
self.all_channels.push(id);
}
}
}
Ok(())
}