From 24a44fa1574d03f28568796448448652699d3dd1 Mon Sep 17 00:00:00 2001 From: zephyr Date: Sat, 20 Aug 2022 23:25:17 +0900 Subject: [PATCH] make tls optional --- Cargo.lock | 2 +- cmd/Cargo.toml | 9 +++++++-- cmd/src/client.rs | 15 ++++++++++++++- cmd/src/server.rs | 16 +++++++++++++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0222c66..f41d5fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,7 +143,7 @@ dependencies = [ [[package]] name = "kaminari-cmd" -version = "0.5.6" +version = "0.5.7" dependencies = [ "anyhow", "kaminari 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/cmd/Cargo.toml b/cmd/Cargo.toml index 5aa535f..ec2acc5 100644 --- a/cmd/Cargo.toml +++ b/cmd/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kaminari-cmd" -version = "0.5.6" +version = "0.5.7" edition = "2021" authors = ["zephyr "] repository = "https://github.com/zephyrchien/kaminari/cmd" @@ -11,7 +11,7 @@ license = "GPL-3.0" [dependencies] anyhow = "1" realm_io = "0.3.2" -kaminari = { version = "0.9.2", features = ["ws", "tls"] } +kaminari = { version = "0.9.2", features = ["ws"] } tokio = { version = "1.9", features = ["rt", "net", "macros"] } [[bin]] @@ -21,3 +21,8 @@ path = "src/client.rs" [[bin]] name = "kaminaris" path = "src/server.rs" + +[features] +default = ["tls-rustls"] +tls-rustls = ["kaminari/tls"] +tls-openssl = [] diff --git a/cmd/src/client.rs b/cmd/src/client.rs index 59023a2..f8de4a8 100644 --- a/cmd/src/client.rs +++ b/cmd/src/client.rs @@ -5,11 +5,12 @@ use tokio::net::{TcpListener, TcpStream}; use realm_io::{CopyBuffer, bidi_copy_buf}; use kaminari::opt; +use kaminari::trick::Ref; use kaminari::AsyncConnect; use kaminari::nop::NopConnect; use kaminari::ws::WsConnect; +#[cfg(any(feature="tls-rustls", feature="tls-openssl"))] use kaminari::tls::TlsConnect; -use kaminari::trick::Ref; use kaminari_cmd::{Endpoint, parse_cmd, parse_env}; @@ -18,6 +19,7 @@ async fn main() -> Result<()> { let (Endpoint { local, remote }, options) = parse_env().or_else(|_| parse_cmd())?; let ws = opt::get_ws_conf(&options); + #[cfg(any(feature="tls-rustls", feature="tls-openssl"))] let tls = opt::get_tls_client_conf(&options); eprintln!("listen: {}", &local); @@ -27,6 +29,7 @@ async fn main() -> Result<()> { eprintln!("ws: {}", ws) } + #[cfg(any(feature="tls-rustls", feature="tls-openssl"))] if let Some(tls) = &tls { eprintln!("tls: {}", &tls); } @@ -72,6 +75,7 @@ async fn main() -> Result<()> { } } + #[cfg(any(feature="tls-rustls", feature="tls-openssl"))] match (ws, tls) { (None, None) => { let client = NopConnect {}; @@ -91,6 +95,15 @@ async fn main() -> Result<()> { } }; + #[cfg(not(any(feature="tls-rustls", feature="tls-openssl")))] + if let Some(ws) = ws { + let client = WsConnect::new(NopConnect {}, ws); + run_ws_each!(client); + } else { + let client = NopConnect {}; + run!(Ref::new(&client)); + } + Ok(()) } diff --git a/cmd/src/server.rs b/cmd/src/server.rs index 2450b84..b18206c 100644 --- a/cmd/src/server.rs +++ b/cmd/src/server.rs @@ -5,11 +5,12 @@ use tokio::net::{TcpListener, TcpStream}; use realm_io::{CopyBuffer, bidi_copy_buf}; use kaminari::opt; +use kaminari::trick::Ref; use kaminari::AsyncAccept; use kaminari::nop::NopAccept; use kaminari::ws::WsAccept; +#[cfg(any(feature="tls-rustls", feature="tls-openssl"))] use kaminari::tls::TlsAccept; -use kaminari::trick::Ref; use kaminari_cmd::{Endpoint, parse_cmd, parse_env}; @@ -28,6 +29,8 @@ async fn main() -> Result<()> { .or_else(|_| parse_cmd())?; let ws = opt::get_ws_conf(&options); + + #[cfg(any(feature="tls-rustls", feature="tls-openssl"))] let tls = opt::get_tls_server_conf(&options); eprintln!("listen: {}", &local); @@ -37,6 +40,7 @@ async fn main() -> Result<()> { eprintln!("ws: {}", ws) } + #[cfg(any(feature="tls-rustls", feature="tls-openssl"))] if let Some(tls) = &tls { eprintln!("tls: {}", &tls); } @@ -60,6 +64,7 @@ async fn main() -> Result<()> { }; } + #[cfg(any(feature="tls-rustls", feature="tls-openssl"))] match (ws, tls) { (None, None) => { let server = NopAccept {}; @@ -79,6 +84,15 @@ async fn main() -> Result<()> { } }; + #[cfg(not(any(feature="tls-rustls", feature="tls-openssl")))] + if let Some(ws) = ws { + let server = WsAccept::new(NopAccept {}, ws); + run!(Ref::new(&server)); + } else { + let server = NopAccept {}; + run!(Ref::new(&server)); + } + Ok(()) }