diff --git a/Cargo.lock b/Cargo.lock index 54a9d13..2a629f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,6 +142,8 @@ dependencies = [ [[package]] name = "kaminari" version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac4230248f949ab9b97da55ad9f304c8c4d22ca347a0cdfbdf4930da32197f0a" dependencies = [ "lightws", "rcgen", @@ -153,10 +155,9 @@ dependencies = [ [[package]] name = "kaminari" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac4230248f949ab9b97da55ad9f304c8c4d22ca347a0cdfbdf4930da32197f0a" +version = "0.4.0" dependencies = [ + "lazy_static", "lightws", "rcgen", "rustls-pemfile", @@ -170,7 +171,7 @@ name = "kaminari-cmd" version = "0.1.1" dependencies = [ "anyhow", - "kaminari 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "kaminari 0.3.1", "tokio", ] diff --git a/cmd/src/client.rs b/cmd/src/client.rs index 3ef039b..5b22a2d 100644 --- a/cmd/src/client.rs +++ b/cmd/src/client.rs @@ -10,14 +10,13 @@ use kaminari::tls::TlsConnect; use kaminari_cmd::{Endpoint, parse_cmd, parse_env}; - #[tokio::main] async fn main() -> Result<()> { - let (Endpoint{local, remote}, options) = parse_env().or_else(|_| parse_cmd())?; + let (Endpoint { local, remote }, options) = parse_env().or_else(|_| parse_cmd())?; let ws = opt::get_ws_conf(&options); let tls = opt::get_tls_client_conf(&options); - + eprintln!("ws: {:?}", &ws); eprintln!("tls: {:?}", &tls); @@ -25,24 +24,28 @@ async fn main() -> Result<()> { 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 - ))), - (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))) + (None, None) => tokio::spawn(relay(stream, remote, NopConnect {})), + (Some(ws), None) => { + tokio::spawn(relay(stream, remote, WsConnect::new(NopConnect {}, ws))) + } + (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), + )), }; } Ok(()) } -async fn relay(mut local: TcpStream, remote: SocketAddr, client: T) -> Result<()> -where T:AsyncConnect{ +async fn relay(mut local: TcpStream, remote: SocketAddr, client: T) -> Result<()> +where + T: AsyncConnect, +{ let remote = TcpStream::connect(remote).await?; let mut remote = client.connect(remote).await?; diff --git a/cmd/src/lib.rs b/cmd/src/lib.rs index dd29ee7..9e5a30d 100644 --- a/cmd/src/lib.rs +++ b/cmd/src/lib.rs @@ -15,22 +15,29 @@ pub fn parse_env() -> Result<(Endpoint, String)> { let plugin_opts = env::var("SS_PLUGIN_OPTIONS")?; let local = format!("{}:{}", local_host, local_port) - .to_socket_addrs()?.next().unwrap(); + .to_socket_addrs()? + .next() + .unwrap(); let remote = format!("{}:{}", remote_host, remote_port) - .to_socket_addrs()?.next().unwrap(); - - Ok((Endpoint{local, remote}, plugin_opts)) + .to_socket_addrs()? + .next() + .unwrap(); + + Ok((Endpoint { local, remote }, plugin_opts)) } pub fn parse_cmd() -> Result<(Endpoint, String)> { let args: Vec = env::args().collect(); - anyhow::ensure!(args.len() == 4, "only allow 3 params: "); + anyhow::ensure!( + args.len() == 4, + "only allow 3 params: " + ); let local = args[1].to_socket_addrs()?.next().unwrap(); let remote = args[2].to_socket_addrs()?.next().unwrap(); let plugin_opts = args[3].clone(); - Ok((Endpoint{local, remote}, plugin_opts)) + Ok((Endpoint { local, remote }, plugin_opts)) } diff --git a/cmd/src/server.rs b/cmd/src/server.rs index 76b31f9..e2bac99 100644 --- a/cmd/src/server.rs +++ b/cmd/src/server.rs @@ -10,10 +10,9 @@ use kaminari::tls::TlsAccept; use kaminari_cmd::{Endpoint, parse_cmd, parse_env}; - #[tokio::main] -async fn main() -> Result<()>{ - let (Endpoint{local, remote}, options) = parse_env().or_else(|_| parse_cmd())?; +async fn main() -> Result<()> { + let (Endpoint { local, remote }, options) = parse_env().or_else(|_| parse_cmd())?; let ws = opt::get_ws_conf(&options); let tls = opt::get_tls_server_conf(&options); @@ -25,23 +24,29 @@ async fn main() -> Result<()>{ 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 - ))), - (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))) + (None, None) => tokio::spawn(relay(stream, remote, NopAccept {})), + (Some(ws), None) => { + tokio::spawn(relay(stream, remote, WsAccept::new(NopAccept {}, ws))) + } + (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), + )), }; } Ok(()) } -async fn relay>(local: TcpStream, remote: SocketAddr, server: T) -> Result<()> { +async fn relay>( + local: TcpStream, + remote: SocketAddr, + server: T, +) -> Result<()> { let mut local = server.accept(local).await?; let mut remote = TcpStream::connect(remote).await?;