Skip to content

Commit

Permalink
config api compatibility (#227)
Browse files Browse the repository at this point in the history
* 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](dtolnay/thiserror@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] <[email protected]>
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](dtolnay/anyhow@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] <[email protected]>
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] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
ibigbug and dependabot[bot] authored Dec 31, 2023
1 parent 3ead55e commit c599aa4
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
15 changes: 11 additions & 4 deletions clash_lib/src/app/api/handlers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ async fn update_configs(
State(state): State<ConfigState>,
Json(req): Json<UpdateConfigRequest>,
) -> 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",
Expand All @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions clash_lib/src/app/dispatcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion clash_lib/src/app/dns/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Config {
pub fn parse_fallback_ip_cidr(ipcidr: &[String]) -> anyhow::Result<Vec<ipnet::IpNet>> {
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()))?;
Expand Down
3 changes: 2 additions & 1 deletion clash_lib/src/app/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::io::Write> { Box::new(W(appender.clone())) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod cidr_trie;
mod provider;

pub use provider::ThreadSafeRuleProvider;
pub use provider::{RuleProvider, RuleProviderImpl, RuleSetBehavior};
pub use provider::{RuleProviderImpl, RuleSetBehavior};
45 changes: 36 additions & 9 deletions clash_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,7 +96,7 @@ pub struct GlobalState {
tunnel_listener_handle: Option<JoinHandle<Result<(), Error>>>,
api_listener_handle: Option<JoinHandle<Result<(), Error>>>,
dns_listener_handle: Option<JoinHandle<Result<(), Error>>>,
reload_tx: mpsc::Sender<Config>,
reload_tx: mpsc::Sender<(Config, oneshot::Sender<()>)>,
cwd: String,
}

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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(())
}));
Expand Down
3 changes: 0 additions & 3 deletions clash_lib/src/proxy/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions clash_lib/src/proxy/vmess/vmess_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit c599aa4

Please sign in to comment.