Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mode not updating for udp traffic #149

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ hyper-boring = { git = "https://github.com/Watfaq/boring.git", rev = "24c006f" }
tokio-boring = { git = "https://github.com/Watfaq/boring.git", rev = "24c006f" }
ip_network_table-deps-treebitmap = "0.5.0"
once_cell = "1.18.0"
arc-swap = "1.6.0"

# opentelemetry
opentelemetry = "0.20"
Expand Down
16 changes: 8 additions & 8 deletions clash_lib/src/app/dispatcher/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::config::internal::proxy::PROXY_GLOBAL;
use crate::proxy::datagram::UdpPacket;
use crate::proxy::AnyInboundDatagram;
use crate::session::Session;
use arc_swap::ArcSwap;
use futures::SinkExt;
use futures::StreamExt;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::net::SocketAddr;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use std::time::Instant;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
Expand All @@ -35,7 +35,7 @@ pub struct Dispatcher {
outbound_manager: ThreadSafeOutboundManager,
router: ThreadSafeRouter,
resolver: ThreadSafeDNSResolver,
mode: ArcSwap<RunMode>,
mode: Arc<Mutex<RunMode>>,

manager: Arc<Manager>,
}
Expand All @@ -59,19 +59,19 @@ impl Dispatcher {
outbound_manager,
router,
resolver,
mode: Arc::new(mode).into(),
mode: Arc::new(Mutex::new(mode)),
manager: statistics_manager,
}
}

pub async fn set_mode(&self, mode: RunMode) {
info!("run mode switched to {}", mode);

self.mode.store(Arc::new(mode));
*self.mode.lock().unwrap() = mode;
}

pub async fn get_mode(&self) -> RunMode {
**self.mode.load()
*self.mode.lock().unwrap()
}

#[instrument(skip(lhs))]
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Dispatcher {
sess
};

let mode = **self.mode.load();
let mode = *self.mode.lock().unwrap();
let (outbound_name, rule) = match mode {
RunMode::Global => (PROXY_GLOBAL, None),
RunMode::Rule => self.router.match_route(&sess).await,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl Dispatcher {
let router = self.router.clone();
let outbound_manager = self.outbound_manager.clone();
let resolver = self.resolver.clone();
let mode = **self.mode.load();
let mode = self.mode.clone();
let manager = self.manager.clone();

let (mut local_w, mut local_r) = udp_inbound.split();
Expand Down Expand Up @@ -242,7 +242,7 @@ impl Dispatcher {
let mut packet = packet;
packet.dst_addr = sess.destination.clone();

let mode = mode.clone();
let mode = *mode.lock().unwrap();

let (outbound_name, rule) = match mode {
RunMode::Global => (PROXY_GLOBAL, None),
Expand Down
2 changes: 2 additions & 0 deletions clash_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::io;
use std::path::PathBuf;
use tokio::task::JoinHandle;
use tracing::error;
use tracing::info;

use std::sync::Arc;
use thiserror::Error;
Expand Down Expand Up @@ -265,6 +266,7 @@ async fn start_async(opts: Options) -> Result<(), Error> {
}

runners.push(Box::pin(async move {
info!("receive shutdown signal");
shutdown_rx.recv().await;
}));

Expand Down
Loading