From c599aa4e6153948767d305b52f0b38d2cb8013a2 Mon Sep 17 00:00:00 2001 From: Yuwei Ba Date: Mon, 1 Jan 2024 01:09:48 +1100 Subject: [PATCH] config api compatibility (#227) * config api compatibility * reload api too * Bump thiserror from 1.0.51 to 1.0.52 (#231) Bumps [thiserror](https://github.com/dtolnay/thiserror) from 1.0.51 to 1.0.52. - [Release notes](https://github.com/dtolnay/thiserror/releases) - [Commits](https://github.com/dtolnay/thiserror/compare/1.0.51...1.0.52) --- updated-dependencies: - dependency-name: thiserror dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump anyhow from 1.0.76 to 1.0.77 (#230) Bumps [anyhow](https://github.com/dtolnay/anyhow) from 1.0.76 to 1.0.77. - [Release notes](https://github.com/dtolnay/anyhow/releases) - [Commits](https://github.com/dtolnay/anyhow/compare/1.0.76...1.0.77) --- updated-dependencies: - dependency-name: anyhow dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * don't verify at all (#232) * enable ipv6 on wireguard (#234) * enable ipv6 on wg * clippy * cleanup some useless prints (#236) * wait a bit * clippy * fmt --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- Cargo.lock | 6 +-- Cargo.toml | 2 +- clash_lib/src/app/api/handlers/config.rs | 15 +++++-- clash_lib/src/app/dispatcher/mod.rs | 2 - clash_lib/src/app/dns/config.rs | 2 +- clash_lib/src/app/logging.rs | 3 +- .../providers/rule_provider/mod.rs | 2 +- clash_lib/src/lib.rs | 45 +++++++++++++++---- clash_lib/src/proxy/transport/mod.rs | 3 -- clash_lib/src/proxy/vmess/vmess_impl/mod.rs | 3 -- 10 files changed, 55 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e1ae9bff..653446a95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -802,7 +802,7 @@ checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" [[package]] name = "clash" -version = "0.1.11" +version = "0.1.12" dependencies = [ "clap 4.4.11", "clash_lib", @@ -810,14 +810,14 @@ dependencies = [ [[package]] name = "clash_doc" -version = "0.1.11" +version = "0.1.12" dependencies = [ "clash_lib", ] [[package]] name = "clash_lib" -version = "0.1.11" +version = "0.1.12" dependencies = [ "aes-gcm", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index fe1135fc1..e5aae74c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ members = [ [workspace.package] -version = "0.1.11" +version = "0.1.12" repository = "https://github.com/Watfaq/clash-rs.git" edition = "2021" diff --git a/clash_lib/src/app/api/handlers/config.rs b/clash_lib/src/app/api/handlers/config.rs index b43af7c88..c25c5ce3f 100644 --- a/clash_lib/src/app/api/handlers/config.rs +++ b/clash_lib/src/app/api/handlers/config.rs @@ -96,13 +96,17 @@ async fn update_configs( State(state): State, Json(req): Json, ) -> impl IntoResponse { + let (done, wait) = tokio::sync::oneshot::channel(); let g = state.global_state.lock().await; match (req.path, req.payload) { (_, Some(payload)) => { let msg = "config reloading from payload".to_string(); let cfg = crate::Config::Str(payload); - match g.reload_tx.send(cfg).await { - Ok(_) => (StatusCode::ACCEPTED, msg).into_response(), + match g.reload_tx.send((cfg, done)).await { + Ok(_) => { + wait.await.unwrap(); + (StatusCode::NO_CONTENT, msg).into_response() + } Err(_) => ( StatusCode::INTERNAL_SERVER_ERROR, "could not signal config reload", @@ -127,8 +131,11 @@ async fn update_configs( let msg = format!("config reloading from file {}", path); let cfg: crate::Config = crate::Config::File(path); - match g.reload_tx.send(cfg).await { - Ok(_) => (StatusCode::ACCEPTED, msg).into_response(), + match g.reload_tx.send((cfg, done)).await { + Ok(_) => { + wait.await.unwrap(); + (StatusCode::NO_CONTENT, msg).into_response() + } Err(_) => ( StatusCode::INTERNAL_SERVER_ERROR, diff --git a/clash_lib/src/app/dispatcher/mod.rs b/clash_lib/src/app/dispatcher/mod.rs index 4eb015f4e..cf7c3e491 100644 --- a/clash_lib/src/app/dispatcher/mod.rs +++ b/clash_lib/src/app/dispatcher/mod.rs @@ -10,5 +10,3 @@ pub use tracked::ChainedDatagram; pub use tracked::ChainedDatagramWrapper; pub use tracked::ChainedStream; pub use tracked::ChainedStreamWrapper; -pub use tracked::TrackedDatagram; -pub use tracked::TrackedStream; diff --git a/clash_lib/src/app/dns/config.rs b/clash_lib/src/app/dns/config.rs index 5f8deca80..15e7bf7cb 100644 --- a/clash_lib/src/app/dns/config.rs +++ b/clash_lib/src/app/dns/config.rs @@ -167,7 +167,7 @@ impl Config { pub fn parse_fallback_ip_cidr(ipcidr: &[String]) -> anyhow::Result> { let mut output = vec![]; - for (_i, ip) in ipcidr.iter().enumerate() { + for ip in ipcidr.iter() { let net: ipnet::IpNet = ip .parse() .map_err(|x: AddrParseError| Error::InvalidConfig(x.to_string()))?; diff --git a/clash_lib/src/app/logging.rs b/clash_lib/src/app/logging.rs index fa7e5b26c..ff6338ed8 100644 --- a/clash_lib/src/app/logging.rs +++ b/clash_lib/src/app/logging.rs @@ -140,7 +140,8 @@ pub fn setup_logging( .with( tracing_subscriber::fmt::Layer::new() .with_ansi(std::io::stdout().is_terminal()) - .pretty() + .compact() + .with_target(false) .with_file(true) .with_line_number(true) .with_writer(move || -> Box { Box::new(W(appender.clone())) }) diff --git a/clash_lib/src/app/remote_content_manager/providers/rule_provider/mod.rs b/clash_lib/src/app/remote_content_manager/providers/rule_provider/mod.rs index 695019b32..1cec3bbdf 100644 --- a/clash_lib/src/app/remote_content_manager/providers/rule_provider/mod.rs +++ b/clash_lib/src/app/remote_content_manager/providers/rule_provider/mod.rs @@ -2,4 +2,4 @@ mod cidr_trie; mod provider; pub use provider::ThreadSafeRuleProvider; -pub use provider::{RuleProvider, RuleProviderImpl, RuleSetBehavior}; +pub use provider::{RuleProviderImpl, RuleSetBehavior}; diff --git a/clash_lib/src/lib.rs b/clash_lib/src/lib.rs index 8e36113af..389d6f337 100644 --- a/clash_lib/src/lib.rs +++ b/clash_lib/src/lib.rs @@ -22,7 +22,7 @@ use std::io; use std::path::PathBuf; use std::sync::{Arc, OnceLock}; use thiserror::Error; -use tokio::sync::{broadcast, mpsc, Mutex}; +use tokio::sync::{broadcast, mpsc, oneshot, Mutex}; use tokio::task::JoinHandle; use tracing::debug; use tracing::error; @@ -96,7 +96,7 @@ pub struct GlobalState { tunnel_listener_handle: Option>>, api_listener_handle: Option>>, dns_listener_handle: Option>>, - reload_tx: mpsc::Sender, + reload_tx: mpsc::Sender<(Config, oneshot::Sender<()>)>, cwd: String, } @@ -269,7 +269,7 @@ async fn start_async(opts: Options) -> Result<(), Error> { let api_runner = app::api::get_api_runner( config.general.controller, - log_tx, + log_tx.clone(), inbound_manager.clone(), dispatcher, global_state.clone(), @@ -301,7 +301,7 @@ async fn start_async(opts: Options) -> Result<(), Error> { })); tasks.push(Box::pin(async move { - while let Some(config) = reload_rx.recv().await { + while let Some((config, done)) = reload_rx.recv().await { info!("reloading config"); let config = match config.try_parse() { Ok(c) => c, @@ -395,6 +395,9 @@ async fn start_async(opts: Options) -> Result<(), Error> { authenticator, )?)); + done.send(()).unwrap(); + + debug!("stopping listeners"); let mut g = global_state.lock().await; if let Some(h) = g.inbound_listener_handle.take() { h.abort(); @@ -405,21 +408,45 @@ async fn start_async(opts: Options) -> Result<(), Error> { if let Some(h) = g.dns_listener_handle.take() { h.abort(); } + if let Some(h) = g.api_listener_handle.take() { + h.abort(); + } - let inbound_runner = inbound_manager.lock().await.get_runner()?; - let inbound_listener_handle = tokio::spawn(inbound_runner); + let inbound_listener_handle = inbound_manager + .lock() + .await + .get_runner() + .map(tokio::spawn)?; - let tun_runner = get_tun_runner(config.tun, dispatcher.clone(), dns_resolver.clone())?; - let tun_runner_handle = tun_runner.map(tokio::spawn); + let tun_runner_handle = + get_tun_runner(config.tun, dispatcher.clone(), dns_resolver.clone())? + .map(tokio::spawn); - debug!("initializing dns listener"); + debug!("reloading dns listener"); let dns_listener_handle = dns::get_dns_listener(config.dns, dns_resolver.clone()) .await .map(tokio::spawn); + debug!("reloading api listener"); + let api_listener_handle = app::api::get_api_runner( + config.general.controller, + log_tx.clone(), + inbound_manager.clone(), + dispatcher, + global_state.clone(), + dns_resolver, + outbound_manager, + statistics_manager, + cache_store, + router, + cwd.to_string_lossy().to_string(), + ) + .map(tokio::spawn); + g.inbound_listener_handle = Some(inbound_listener_handle); g.tunnel_listener_handle = tun_runner_handle; g.dns_listener_handle = dns_listener_handle; + g.api_listener_handle = api_listener_handle; } Ok(()) })); diff --git a/clash_lib/src/proxy/transport/mod.rs b/clash_lib/src/proxy/transport/mod.rs index ee1e50aab..79bef5574 100644 --- a/clash_lib/src/proxy/transport/mod.rs +++ b/clash_lib/src/proxy/transport/mod.rs @@ -4,11 +4,8 @@ mod h2; mod internal_tls; mod ws; -pub use ws::WebsocketConn; -pub use ws::WebsocketEarlyDataConn; pub use ws::WebsocketStreamBuilder; -pub use grpc::GrpcStream; pub use grpc::GrpcStreamBuilder; pub use self::h2::Http2Config; diff --git a/clash_lib/src/proxy/vmess/vmess_impl/mod.rs b/clash_lib/src/proxy/vmess/vmess_impl/mod.rs index 14f4a4043..0b98ba4a0 100644 --- a/clash_lib/src/proxy/vmess/vmess_impl/mod.rs +++ b/clash_lib/src/proxy/vmess/vmess_impl/mod.rs @@ -28,6 +28,3 @@ const MAX_CHUNK_SIZE: usize = 17 * 1024; pub use client::Builder; pub use client::VmessOption; pub use datagram::OutboundDatagramVmess; -pub use stream::VmessStream; -pub use user::new_alter_id_list; -pub use user::new_id;