Skip to content

Commit

Permalink
Logging: Add a Verbose Flag
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Mar 10, 2024
1 parent 56e94ed commit 9b54a0c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(version, about, long_about = "A QUIC Proxy for SSH")]
pub(crate) struct Cli {
#[arg(short, long, default_value = "false")]
pub(crate) verbose: bool,
#[command(subcommand)]
pub(crate) command: Commands,
}
Expand Down
17 changes: 11 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
use crate::server::ALPN_QUIC_HTTP;
use crate::verifier::SkipServerVerification;
use quinn::VarInt;
use std::sync::Arc;
use std::{error::Error, net::SocketAddr};
use quinn::VarInt;
use tokio::io::{stdin, stdout};
use crate::verifier::SkipServerVerification;
use tracing::{debug, info};

pub(crate) async fn invoke(addr: SocketAddr) -> Result<(), Box<dyn Error>> {
let mut client_crypto = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(SkipServerVerification::new())
.with_no_client_auth();
client_crypto.alpn_protocols = ALPN_QUIC_HTTP.iter().map(|&x| x.into()).collect();

info!(
"Connecting to {} using client config: {:?}",
addr, client_crypto
);
let client_config = quinn::ClientConfig::new(Arc::new(client_crypto));
let mut endpoint = quinn::Endpoint::client("[::]:0".parse().unwrap())?;
endpoint.set_default_client_config(client_config);
let conn = endpoint.connect(addr, "localhost").unwrap().await?;

info!("Connected to {}", conn.remote_address());
while let Ok((mut writer, mut reader)) = conn.open_bi().await {
println!("Established Connection to {}", conn.remote_address());
let mut server = tokio::io::join(&mut reader, &mut writer);
let (inp, out) = (stdin(), stdout());
let mut localhost = tokio::io::join(inp, out);
if let Ok((read, wrote)) = tokio::io::copy_bidirectional(&mut server, &mut localhost).await {
println!("Read {read}bytes. Wrote: {wrote}bytes");
if let Ok((read, wrote)) = tokio::io::copy_bidirectional(&mut server, &mut localhost).await
{
debug!("Read {read}bytes. Wrote: {wrote}bytes");
continue;
};
break;
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ mod verifier;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt::init();
let args = cli::Cli::parse();

if args.verbose {
tracing_subscriber::fmt::init();
}
tracing::info!("Verbose mode enabled");
match args.command {
cli::Commands::Server { bind } => server::invoke(bind).await,
cli::Commands::Client { addr } => client::invoke(addr).await,
Expand Down
21 changes: 13 additions & 8 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;
use std::{error::Error, net::SocketAddr};
use std::sync::{Arc};
use tokio::net::{TcpStream};
use tokio::net::TcpStream;
use tracing::{debug, info};

pub const ALPN_QUIC_HTTP: &[&[u8]] = &[b"hq-29"];

Expand All @@ -24,25 +25,29 @@ pub(crate) async fn invoke(bind: SocketAddr) -> Result<(), Box<dyn Error>> {
server_config.use_retry(true);

let endpoint = quinn::Endpoint::server(server_config, bind)?;
eprintln!("Listening for connections on {}", endpoint.local_addr()?);
info!("Listening for connections on {}", endpoint.local_addr()?);
while let Some(connecting) = endpoint.accept().await {
println!("Got Connection from {}", connecting.remote_address());
info!("Got Connection from {}", connecting.remote_address());
tokio::spawn(async move {
let connection = connecting.await.unwrap();
while let Ok((mut writer, mut reader)) = connection.accept_bi().await {
let mut stream = TcpStream::connect("127.0.0.1:22").await.unwrap();
let mut stream = TcpStream::connect("127.0.0.1:22")
.await
.expect("Failed to connect to sshd");
tokio::spawn(async move {
loop {
let mut joined = tokio::io::join(&mut reader, &mut writer);
if let Ok((read, wrote)) = tokio::io::copy_bidirectional(&mut stream, &mut joined).await {
println!("Read from SSH: {read}bytes. Wrote to SSH: {wrote}bytes.");
if let Ok((read, wrote)) =
tokio::io::copy_bidirectional(&mut stream, &mut joined).await
{
debug!("Read from SSH: {read}bytes. Wrote to SSH: {wrote}bytes.");
continue;
};
break;
}
});
}
});
};
}
Ok(())
}

0 comments on commit 9b54a0c

Please sign in to comment.