diff --git a/Cargo.lock b/Cargo.lock index 2a629f9..3bf3095 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -141,10 +141,9 @@ dependencies = [ [[package]] name = "kaminari" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac4230248f949ab9b97da55ad9f304c8c4d22ca347a0cdfbdf4930da32197f0a" +version = "0.4.1" dependencies = [ + "lazy_static", "lightws", "rcgen", "rustls-pemfile", @@ -155,7 +154,9 @@ dependencies = [ [[package]] name = "kaminari" -version = "0.4.0" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df2ca3de88447dc338247c2c71c9b710c83b056dbc11fcf87b3a3fa05db455e0" dependencies = [ "lazy_static", "lightws", @@ -168,10 +169,10 @@ dependencies = [ [[package]] name = "kaminari-cmd" -version = "0.1.1" +version = "0.2.0" dependencies = [ "anyhow", - "kaminari 0.3.1", + "kaminari 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio", ] diff --git a/cmd/Cargo.toml b/cmd/Cargo.toml index 20c1d64..4a7a403 100644 --- a/cmd/Cargo.toml +++ b/cmd/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "kaminari-cmd" -version = "0.1.1" +version = "0.2.0" edition = "2021" authors = ["zephyr "] 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" diff --git a/cmd/src/client.rs b/cmd/src/client.rs index 5b22a2d..fa802c5 100644 --- a/cmd/src/client.rs +++ b/cmd/src/client.rs @@ -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}; @@ -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(mut local: TcpStream, remote: SocketAddr, client: T) -> Result<()> +async fn relay(mut local: TcpStream, remote: SocketAddr, client: Ref) -> Result<()> where T: AsyncConnect, { diff --git a/cmd/src/server.rs b/cmd/src/server.rs index e2bac99..8d40f85 100644 --- a/cmd/src/server.rs +++ b/cmd/src/server.rs @@ -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}; @@ -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>( local: TcpStream, remote: SocketAddr, - server: T, + server: Ref, ) -> Result<()> { let mut local = server.accept(local).await?;