From 177d472d59ab590817d811a45a7650f4a0f49629 Mon Sep 17 00:00:00 2001 From: "Federico Pasqua (eisterman)" Date: Wed, 13 Mar 2024 13:04:29 +0100 Subject: [PATCH] Add first real SSH bridge --- bonknet_client/src/bin/client.rs | 24 +++++++++--------------- bonknet_server/src/bin/server.rs | 15 ++++++++++----- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/bonknet_client/src/bin/client.rs b/bonknet_client/src/bin/client.rs index 26eb7a0..9579210 100644 --- a/bonknet_client/src/bin/client.rs +++ b/bonknet_client/src/bin/client.rs @@ -2,7 +2,7 @@ use std::io::{Error, ErrorKind}; use std::sync::Arc; use std::time::Duration; use futures::{SinkExt, StreamExt}; -use tokio::net::TcpStream; +use tokio::net::{TcpStream, TcpListener}; use tokio_rustls::rustls::{ClientConfig, RootCertStore}; use tokio_rustls::rustls::pki_types::ServerName; use tokio_rustls::TlsConnector; @@ -78,22 +78,16 @@ async fn datastream(tlsconfig: ClientConfig, conn_id: Uuid) -> std::io::Result<( } } } - let (mut rx, mut tx) = tokio::io::split(transport.into_inner()); - let mut stdout = tokio::io::stdout(); - let mut stdin = tokio::io::stdin(); - let stdout_task = async move { - match tokio::io::copy(&mut rx, &mut stdout).await { - Ok(bytes_copied) => info!("{bytes_copied}"), + let mut outbound = transport.into_inner(); + let listener = TcpListener::bind("127.0.0.1:9919").await?; + if let Ok((mut inbound, _)) = listener.accept().await { + match tokio::io::copy_bidirectional(&mut inbound, &mut outbound).await { + Ok(bytes_copied) => info!("{bytes_copied:?}"), Err(e) => error!("Error during copy: {e}"), } - }; - let stdin_task = async move { - match tokio::io::copy(&mut stdin, &mut tx).await { - Ok(bytes_copied) => info!("{bytes_copied}"), - Err(e) => error!("Error during copy: {e}"), - } - }; - tokio::join!(stdout_task, stdin_task); + } else { + error!("Error"); + } Ok(()) } diff --git a/bonknet_server/src/bin/server.rs b/bonknet_server/src/bin/server.rs index f49dec5..c10cb00 100644 --- a/bonknet_server/src/bin/server.rs +++ b/bonknet_server/src/bin/server.rs @@ -77,9 +77,11 @@ async fn datastream(tlsconfig: Arc, conn_id: Uuid) -> std::io::Res } } } - let (mut rx, mut tx) = tokio::io::split(transport.into_inner()); - match tokio::io::copy(&mut rx, &mut tx).await { - Ok(bytes_copied) => info!("{bytes_copied}"), + // Initialize outbound stream + let mut inbound = transport.into_inner(); + let mut outbound = TcpStream::connect("127.0.0.1:22").await?; + match tokio::io::copy_bidirectional(&mut inbound, &mut outbound).await { + Ok(bytes_copied) => info!("{bytes_copied:?}"), Err(e) => error!("Error during copy: {e}"), } Ok(()) @@ -90,6 +92,8 @@ async fn main() -> std::io::Result<()> { // Tracing Subscriber let subscriber = tracing_subscriber::FmtSubscriber::new(); tracing::subscriber::set_global_default(subscriber).unwrap(); + // Server Name + let my_name = "cicciopizza"; // Root certs to verify the server is the right one let mut broker_root_cert_store = RootCertStore::empty(); let broker_root_cert_der = load_cert("certs/broker_root_cert.pem").unwrap(); @@ -114,8 +118,9 @@ async fn main() -> std::io::Result<()> { let stream = connector.connect(dnsname, stream).await?; let mut transport = Framed::new(stream, LengthDelimitedCodec::new()); - let msg = FromGuestServerMessage::Announce { name: "cicciopizza".into() }; + let msg = FromGuestServerMessage::Announce { name: my_name.into() }; transport.send(rmp_serde::to_vec(&msg).unwrap().into()).await.unwrap(); + // TODO: Remove this two mutable option let mut myserver_cert: Option = None; let mut myserver_prkey: Option = None; match transport.next().await { @@ -136,7 +141,7 @@ async fn main() -> std::io::Result<()> { myserver_prkey = Some(server_prkey); } FailedNameAlreadyOccupied => { - error!("Failed Announce"); + error!("Failed Announce, name already occupied"); return Ok(()); } }