-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from arkedge/add-tcp-exec
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ members = [ | |
"kble-socket", | ||
"kble-c2a", | ||
"kble-eb90", | ||
"kble-tcp", | ||
] | ||
|
||
[workspace.dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[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 | ||
notalawyer.workspace = true | ||
notalawyer-clap.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::net::SocketAddr; | ||
|
||
use anyhow::Result; | ||
use clap::Parser; | ||
use futures::{SinkExt, StreamExt}; | ||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
use tokio::net::TcpStream; | ||
use tracing_subscriber::{prelude::*, EnvFilter}; | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(author, version, about, long_about = None)] | ||
struct Args { | ||
addr: SocketAddr, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
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 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 { | ||
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? { | ||
0 => break, | ||
n => { | ||
tx.send(buffer[..n].to_vec().into()).await?; | ||
} | ||
} | ||
} | ||
anyhow::Ok(()) | ||
}; | ||
|
||
tokio::select! { | ||
_ = to_tcp => Ok(()), | ||
_ = from_tcp => Ok(()) | ||
} | ||
} |