Skip to content

Commit

Permalink
avoid clone
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Apr 10, 2022
1 parent 8f898a1 commit e418f71
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "kaminari-cmd"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
authors = ["zephyr <[email protected]>"]
repository = "https://github.com/zephyrchien/kaminari/cmd"
license = "GPLv3"

[dependencies]
kaminari = "0.3.1"
kaminari = "0.4.1"
tokio = {version = "1", features = ["full"] }
anyhow = "1"

Expand Down
39 changes: 25 additions & 14 deletions cmd/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use kaminari::AsyncConnect;
use kaminari::nop::NopConnect;
use kaminari::ws::WsConnect;
use kaminari::tls::TlsConnect;
use kaminari::trick::Ref;

use kaminari_cmd::{Endpoint, parse_cmd, parse_env};

Expand All @@ -22,27 +23,37 @@ async fn main() -> Result<()> {

let lis = TcpListener::bind(local).await?;

while let Ok((stream, _)) = lis.accept().await {
match (ws.clone(), tls.clone()) {
(None, None) => tokio::spawn(relay(stream, remote, NopConnect {})),
(Some(ws), None) => {
tokio::spawn(relay(stream, remote, WsConnect::new(NopConnect {}, ws)))
macro_rules! run {
($cc: expr) => {
while let Ok((stream, _)) = lis.accept().await {
tokio::spawn(relay(stream, remote, $cc));
}
(None, Some(tls)) => {
tokio::spawn(relay(stream, remote, TlsConnect::new(NopConnect {}, tls)))
}
(Some(ws), Some(tls)) => tokio::spawn(relay(
stream,
remote,
WsConnect::new(TlsConnect::new(NopConnect {}, tls), ws),
)),
};
}

match (ws, tls) {
(None, None) => {
let client = NopConnect {};
run!(Ref::new(&client));
}
(Some(ws), None) => {
let client = WsConnect::new(NopConnect {}, ws);
run!(Ref::new(&client));
}
(None, Some(tls)) => {
let client = TlsConnect::new(NopConnect {}, tls);
run!(Ref::new(&client));
}
(Some(ws), Some(tls)) => {
let client = WsConnect::new(TlsConnect::new(NopConnect {}, tls), ws);
run!(Ref::new(&client));
}
};

Ok(())
}

async fn relay<T>(mut local: TcpStream, remote: SocketAddr, client: T) -> Result<()>
async fn relay<T>(mut local: TcpStream, remote: SocketAddr, client: Ref<T>) -> Result<()>
where
T: AsyncConnect<TcpStream>,
{
Expand Down
39 changes: 25 additions & 14 deletions cmd/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use kaminari::AsyncAccept;
use kaminari::nop::NopAccept;
use kaminari::ws::WsAccept;
use kaminari::tls::TlsAccept;
use kaminari::trick::Ref;

use kaminari_cmd::{Endpoint, parse_cmd, parse_env};

Expand All @@ -22,30 +23,40 @@ async fn main() -> Result<()> {

let lis = TcpListener::bind(local).await?;

while let Ok((stream, _)) = lis.accept().await {
match (ws.clone(), tls.clone()) {
(None, None) => tokio::spawn(relay(stream, remote, NopAccept {})),
(Some(ws), None) => {
tokio::spawn(relay(stream, remote, WsAccept::new(NopAccept {}, ws)))
macro_rules! run {
($ac: expr) => {
while let Ok((stream, _)) = lis.accept().await {
tokio::spawn(relay(stream, remote, $ac));
}
(None, Some(tls)) => {
tokio::spawn(relay(stream, remote, TlsAccept::new(NopAccept {}, tls)))
}
(Some(ws), Some(tls)) => tokio::spawn(relay(
stream,
remote,
WsAccept::new(TlsAccept::new(NopAccept {}, tls), ws),
)),
};
}

match (ws, tls) {
(None, None) => {
let server = NopAccept {};
run!(Ref::new(&server));
}
(Some(ws), None) => {
let server = WsAccept::new(NopAccept {}, ws);
run!(Ref::new(&server));
}
(None, Some(tls)) => {
let server = TlsAccept::new(NopAccept {}, tls);
run!(Ref::new(&server));
}
(Some(ws), Some(tls)) => {
let server = WsAccept::new(TlsAccept::new(NopAccept {}, tls), ws);
run!(Ref::new(&server));
}
};

Ok(())
}

async fn relay<T: AsyncAccept<TcpStream>>(
local: TcpStream,
remote: SocketAddr,
server: T,
server: Ref<T>,
) -> Result<()> {
let mut local = server.accept(local).await?;

Expand Down

0 comments on commit e418f71

Please sign in to comment.