From 9f3571c8b89748ce8d760fb8910787a64193a9b7 Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Mon, 20 Nov 2023 18:32:11 +0800 Subject: [PATCH] ctrlc2 usage --- Cargo.toml | 7 ++++--- src/client.rs | 2 +- src/main.rs | 24 ++++-------------------- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 25748e9..c80084d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,13 +13,14 @@ base64 = "0.21" bytes = "1.5" chrono = "0.4" clap = { version = "4.4", features = ["derive"] } +ctrlc2 = { version = "3.5", features = ["tokio", "termination"] } dotenvy = "0.15" env_logger = "0.10" futures-util = { version = "0.3", default-features = false, features = [ "sink", "std", ] } -http = "0.2" +http = "1.0" httparse = "1.8" lazy_static = "1.4" log = "0.4" @@ -34,7 +35,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" socks5-impl = "0.5" thiserror = "1.0" -tokio = { version = "1.32", features = ["full"] } +tokio = { version = "1.34", features = ["full"] } tokio-rustls = "0.24" tokio-tungstenite = { version = "0.20", features = ["rustls-tls-webpki-roots"] } trust-dns-proto = "0.23" @@ -56,7 +57,7 @@ crossbeam = "0.8" jni = { version = "0.21", default-features = false } [target.'cfg(target_os="windows")'.dependencies] -windows = { version = "0.51", features = [ +windows = { version = "0.52", features = [ "Win32_NetworkManagement_IpHelper", "Win32_NetworkManagement_Ndis", "Win32_Networking_WinSock", diff --git a/src/client.rs b/src/client.rs index 3f5108c..0eaa1c1 100644 --- a/src/client.rs +++ b/src/client.rs @@ -245,7 +245,7 @@ pub(crate) async fn create_ws_stream( let accept_key = tungstenite::handshake::derive_accept_key(key.as_bytes()); - if accept_key.as_str() != remote_key.to_str()? { + if accept_key.as_str() != remote_key.to_str().map_err(|e| e.to_string())? { return Err(Error::from("accept key error")); } diff --git a/src/main.rs b/src/main.rs index 5f7145a..de263dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,23 +69,7 @@ async fn async_main(config: config::Config) -> Result<()> { let local_addr = config.listen_addr()?; - tokio::spawn(async move { - #[cfg(unix)] - { - use tokio::signal::unix::{signal, SignalKind}; - let mut kill_signal = signal(SignalKind::terminate())?; - tokio::select! { - _ = tokio::signal::ctrl_c() => log::info!("Ctrl-C received, shutting down..."), - _ = kill_signal.recv() => log::info!("Kill signal received, shutting down..."), - } - } - - #[cfg(not(unix))] - { - tokio::signal::ctrl_c().await?; - log::info!("Ctrl-C received, shutting down..."); - } - + ctrlc2::set_async_handler(async move { exiting_flag.store(true, std::sync::atomic::Ordering::Relaxed); let addr = if local_addr.is_ipv6() { @@ -94,9 +78,9 @@ async fn async_main(config: config::Config) -> Result<()> { SocketAddr::from((Ipv4Addr::LOCALHOST, local_addr.port())) }; let _ = std::net::TcpStream::connect(addr); - - Ok::<(), Error>(()) - }); + log::info!(""); + }) + .await; if let Err(e) = main_body.await { log::error!("{}", e);