From fa36779acab973824d2d7b222724d4a0ab986fe6 Mon Sep 17 00:00:00 2001 From: Shimomura Shunsuke Date: Mon, 27 May 2024 16:22:43 +0900 Subject: [PATCH 1/5] Created TCP exec --- Cargo.lock | 19 +++++++++++++++++++ Cargo.toml | 1 + kble-tcp/Cargo.toml | 26 ++++++++++++++++++++++++++ kble-tcp/src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 kble-tcp/Cargo.toml create mode 100644 kble-tcp/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index d9024e0..573ca70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -845,6 +845,25 @@ dependencies = [ "tokio-tungstenite", ] +[[package]] +name = "kble-tcp" +version = "0.3.0" +dependencies = [ + "anyhow", + "bytes", + "clap", + "eb90", + "futures", + "kble-socket", + "notalawyer", + "notalawyer-build", + "notalawyer-clap", + "tokio", + "tokio-util", + "tracing", + "tracing-subscriber", +] + [[package]] name = "lazy_static" version = "1.4.0" diff --git a/Cargo.toml b/Cargo.toml index 643787e..6dab281 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ members = [ "kble-socket", "kble-c2a", "kble-eb90", + "kble-tcp", ] [workspace.dependencies] diff --git a/kble-tcp/Cargo.toml b/kble-tcp/Cargo.toml new file mode 100644 index 0000000..d31738a --- /dev/null +++ b/kble-tcp/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "kble-tcp" +version.workspace = true +repository.workspace = true +license.workspace = true +edition.workspace = true +readme.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[build-dependencies] +notalawyer-build.workspace = true + +[dependencies] +anyhow.workspace = true +futures.workspace = true +tokio = { workspace = true, features = ["full"] } +kble-socket = { workspace = true, features = ["stdio", "tungstenite"] } +tokio-util.workspace = true +bytes.workspace = true +tracing.workspace = true +tracing-subscriber.workspace = true +clap.workspace = true +eb90 = "0.1.1" +notalawyer.workspace = true +notalawyer-clap.workspace = true \ No newline at end of file diff --git a/kble-tcp/src/main.rs b/kble-tcp/src/main.rs new file mode 100644 index 0000000..ec93ae2 --- /dev/null +++ b/kble-tcp/src/main.rs @@ -0,0 +1,43 @@ +use anyhow::Result; +use futures::{SinkExt, StreamExt}; +use std::env::args; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpStream; + +#[tokio::main] +async fn main() -> Result<()> { + let args: Vec = args().collect(); + let addr = format!("{}:{}", args.get(1).unwrap(), args.get(2).unwrap()); + + let tcp_stream = TcpStream::connect(addr).await?; + let (mut tcp_upstream, mut tcp_downstream) = tokio::io::split(tcp_stream); + let (mut tx, mut rx) = kble_socket::from_stdio().await; + let to_tcp = async { + while let Some(body) = rx.next().await { + let body = body?; + tcp_downstream.write_all(&body).await?; + } + anyhow::Ok(()) + }; + let from_tcp = async { + let mut buffer = [0; 8192]; + loop { + match tcp_upstream.read(&mut buffer).await { + Ok(0) => break, + Ok(n) => { + tx.send(buffer[..n].to_vec().into()).await?; + } + Err(e) => { + eprintln!("failed to read from socket; error = {:?}", e); + break; + } + } + } + anyhow::Ok(()) + }; + + tokio::select! { + _ = to_tcp => Ok(()), + _ = from_tcp => Ok(()) + } +} From 345fe51976e97b8fd86294b058927e6edf36c4fd Mon Sep 17 00:00:00 2001 From: Shimomura Shunsuke Date: Mon, 27 May 2024 18:00:26 +0900 Subject: [PATCH 2/5] Added argument handling using clap, error display using tracing_subscriber, and modified Cargo.toml --- Cargo.lock | 1 - kble-tcp/Cargo.toml | 3 +-- kble-tcp/src/main.rs | 32 ++++++++++++++++++++++---------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 573ca70..bb2afb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -852,7 +852,6 @@ dependencies = [ "anyhow", "bytes", "clap", - "eb90", "futures", "kble-socket", "notalawyer", diff --git a/kble-tcp/Cargo.toml b/kble-tcp/Cargo.toml index d31738a..8d9e5fe 100644 --- a/kble-tcp/Cargo.toml +++ b/kble-tcp/Cargo.toml @@ -21,6 +21,5 @@ bytes.workspace = true tracing.workspace = true tracing-subscriber.workspace = true clap.workspace = true -eb90 = "0.1.1" notalawyer.workspace = true -notalawyer-clap.workspace = true \ No newline at end of file +notalawyer-clap.workspace = true diff --git a/kble-tcp/src/main.rs b/kble-tcp/src/main.rs index ec93ae2..9a519b7 100644 --- a/kble-tcp/src/main.rs +++ b/kble-tcp/src/main.rs @@ -1,13 +1,29 @@ use anyhow::Result; use futures::{SinkExt, StreamExt}; -use std::env::args; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; +use clap::Parser; +use tracing_subscriber::{prelude::*, EnvFilter}; + +#[derive(Debug, Parser)] +#[clap(author, version, about, long_about = None)] +struct Args { + host: String, + port: String, +} #[tokio::main] async fn main() -> Result<()> { - let args: Vec = args().collect(); - let addr = format!("{}:{}", args.get(1).unwrap(), args.get(2).unwrap()); + tracing_subscriber::registry() + .with( + tracing_subscriber::fmt::layer() + .with_ansi(false) + .with_writer(std::io::stderr), + ) + .with(EnvFilter::from_default_env()) + .init(); + let args = Args::parse(); + let addr = format!("{}:{}", args.host, args.port); let tcp_stream = TcpStream::connect(addr).await?; let (mut tcp_upstream, mut tcp_downstream) = tokio::io::split(tcp_stream); @@ -22,15 +38,11 @@ async fn main() -> Result<()> { let from_tcp = async { let mut buffer = [0; 8192]; loop { - match tcp_upstream.read(&mut buffer).await { - Ok(0) => break, - Ok(n) => { + match tcp_upstream.read(&mut buffer).await? { + 0 => break, + n => { tx.send(buffer[..n].to_vec().into()).await?; } - Err(e) => { - eprintln!("failed to read from socket; error = {:?}", e); - break; - } } } anyhow::Ok(()) From 2b8ace95cb27339ee76a4752fa8c4be6ea5a87b7 Mon Sep 17 00:00:00 2001 From: Shimomura Shunsuke Date: Mon, 27 May 2024 18:02:35 +0900 Subject: [PATCH 3/5] cargo fmt --- kble-tcp/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kble-tcp/src/main.rs b/kble-tcp/src/main.rs index 9a519b7..af92a6b 100644 --- a/kble-tcp/src/main.rs +++ b/kble-tcp/src/main.rs @@ -1,8 +1,8 @@ use anyhow::Result; +use clap::Parser; use futures::{SinkExt, StreamExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; -use clap::Parser; use tracing_subscriber::{prelude::*, EnvFilter}; #[derive(Debug, Parser)] From 109ee1481f25cb74c57f4e3d70362edb11938fe4 Mon Sep 17 00:00:00 2001 From: Shimomura Shunsuke Date: Tue, 28 May 2024 02:30:37 +0900 Subject: [PATCH 4/5] Changed arguments from String to SocketAddr --- kble-tcp/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kble-tcp/src/main.rs b/kble-tcp/src/main.rs index af92a6b..d23b671 100644 --- a/kble-tcp/src/main.rs +++ b/kble-tcp/src/main.rs @@ -1,3 +1,5 @@ +use std::net::SocketAddr; + use anyhow::Result; use clap::Parser; use futures::{SinkExt, StreamExt}; @@ -8,8 +10,7 @@ use tracing_subscriber::{prelude::*, EnvFilter}; #[derive(Debug, Parser)] #[clap(author, version, about, long_about = None)] struct Args { - host: String, - port: String, + addr: SocketAddr } #[tokio::main] @@ -23,9 +24,8 @@ async fn main() -> Result<()> { .with(EnvFilter::from_default_env()) .init(); let args = Args::parse(); - let addr = format!("{}:{}", args.host, args.port); - let tcp_stream = TcpStream::connect(addr).await?; + let tcp_stream = TcpStream::connect(args.addr).await?; let (mut tcp_upstream, mut tcp_downstream) = tokio::io::split(tcp_stream); let (mut tx, mut rx) = kble_socket::from_stdio().await; let to_tcp = async { From 838a571931d6808b36648e11a5e0485475d86f10 Mon Sep 17 00:00:00 2001 From: Shimomura Shunsuke Date: Tue, 28 May 2024 02:37:30 +0900 Subject: [PATCH 5/5] fmt --- kble-tcp/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kble-tcp/src/main.rs b/kble-tcp/src/main.rs index d23b671..361ddd9 100644 --- a/kble-tcp/src/main.rs +++ b/kble-tcp/src/main.rs @@ -10,7 +10,7 @@ use tracing_subscriber::{prelude::*, EnvFilter}; #[derive(Debug, Parser)] #[clap(author, version, about, long_about = None)] struct Args { - addr: SocketAddr + addr: SocketAddr, } #[tokio::main]