Skip to content

Commit

Permalink
fix tun exit sometimes (#148)
Browse files Browse the repository at this point in the history
* fix tun exit sometimes

* fix tests

* skip 1 test
  • Loading branch information
ibigbug authored Nov 1, 2023
1 parent 22e5298 commit d67052f
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 26 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.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ members = [
"clash_doc",
]


[workspace.package]
version = "0.1.5"
repository = "https://github.com/Watfaq/clash-rs.git"
edition = "2021"

[profile.release]
opt-level = "s"
codegen-units = 1
Expand Down
5 changes: 3 additions & 2 deletions clash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "clash"
version = "0.1.0"
edition = "2021"
repository = { workspace = true }
version = { workspace = true }
edition = { workspace = true }

[[bin]]
name = "clash"
Expand Down
5 changes: 3 additions & 2 deletions clash_doc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "clash_doc"
version = "0.1.0"
edition = "2021"
repository = { workspace = true }
version = { workspace = true }
edition = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
5 changes: 3 additions & 2 deletions clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "clash_lib"
version = "0.1.0"
edition = "2021"
repository = { workspace = true }
version = { workspace = true }
edition = { workspace = true }

[features]
default = ["shadowsocks"]
Expand Down
27 changes: 15 additions & 12 deletions clash_lib/src/app/dispatcher/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ impl Dispatcher {
};

let mode = **self.mode.load();
debug!("dispatching {} with mode {}", sess, mode);
let (outbound_name, rule) = match mode {
RunMode::Global => (PROXY_GLOBAL, None),
RunMode::Rule => self.router.match_route(&sess).await,
RunMode::Direct => (PROXY_DIRECT, None),
};

debug!("dispatching {} to {}[{}]", sess, outbound_name, mode);

let mgr = self.outbound_manager.clone();
let handler = mgr.get_outbound(outbound_name).unwrap_or_else(|| {
debug!("unknown rule: {}, fallback to direct", outbound_name);
Expand Down Expand Up @@ -224,7 +225,7 @@ impl Dispatcher {
}
None => {
error!("failed to reverse lookup fake ip: {}", ip);
return;
continue;
}
}
} else {
Expand All @@ -242,15 +243,16 @@ impl Dispatcher {
packet.dst_addr = sess.destination.clone();

let mode = mode.clone();
trace!("dispatching {} with mode {}", sess, mode);

let (outbound_name, rule) = match mode {
RunMode::Global => (PROXY_GLOBAL, None),
RunMode::Rule => router.match_route(&sess).await,
RunMode::Direct => (PROXY_DIRECT, None),
};

let outbound_name = outbound_name.to_string();
debug!("dispatching {} to {}", sess, outbound_name);

debug!("dispatching {} to {}[{}]", sess, outbound_name, mode);

let remote_receiver_w = remote_receiver_w.clone();

Expand All @@ -274,7 +276,7 @@ impl Dispatcher {
Ok(v) => v,
Err(err) => {
error!("failed to connect outbound: {}", err);
return;
continue;
}
};

Expand Down Expand Up @@ -312,10 +314,8 @@ impl Dispatcher {
// local -> remote
let w_handle = tokio::spawn(async move {
while let Some(packet) = remote_forwarder.recv().await {
match remote_w.send(packet.clone()).await {
Ok(_) => {
debug!("{} sent to remote", packet);
}
match remote_w.send(packet).await {
Ok(_) => {}
Err(err) => {
warn!("failed to send packet to remote: {}", err);
}
Expand All @@ -333,7 +333,7 @@ impl Dispatcher {
)
.await;

match remote_sender.send(packet.clone()).await {
match remote_sender.send(packet).await {
Ok(_) => {}
Err(err) => {
error!("failed to send packet to remote: {}", err);
Expand Down Expand Up @@ -406,10 +406,13 @@ impl TimeoutUdpSessionManager {
let map_cloned = map.clone();

let cleaner = tokio::spawn(async move {
trace!("timeout udp session cleaner scanning");
let mut interval = tokio::time::interval(timeout);

loop {
tokio::time::sleep(Duration::from_secs(10)).await;
interval.tick().await;
trace!("timeout udp session cleaner ticking");

trace!("timeout udp session cleaner scanning");
let mut g = map_cloned.write().await;
let mut alived = 0;
let mut expired = 0;
Expand Down
3 changes: 2 additions & 1 deletion clash_lib/src/app/dns/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "figure out failure on CI"]
#[ignore = "network unstable on CI"]
async fn test_dot_resolve() {
let c = DnsClient::new(Opts {
r: Some(Arc::new(Resolver::new_default().await)),
Expand All @@ -645,6 +645,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "network unstable on CI"]
async fn test_doh_resolve() {
let default_resolver = Arc::new(Resolver::new_default().await);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod tests {

#[tokio::test]
async fn test_http_vehicle() {
let u = "http://mockbin.org/bin/db6924ba-6b95-4766-b926-e609e1ce49d2"
let u = "https://httpbin.yba.dev/base64/SFRUUEJJTiBpcyBhd2Vzb21l"
.parse::<Uri>()
.unwrap();
let r = Arc::new(Resolver::new_default().await);
Expand All @@ -83,6 +83,6 @@ mod tests {
);

let data = v.read().await.unwrap();
assert_eq!(str::from_utf8(&data).unwrap(), "ok");
assert_eq!(str::from_utf8(&data).unwrap(), "HTTPBIN is awesome");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl ProxySetProvider {
OutboundProxyProtocol::Reject => Ok(reject::Handler::new()),
OutboundProxyProtocol::Ss(s) => s.try_into(),
OutboundProxyProtocol::Socks5(_) => todo!("socks5 not supported yet"),
OutboundProxyProtocol::Trojan(_) => todo!("trojan not supported yet"),
OutboundProxyProtocol::Trojan(tr) => tr.try_into(),
OutboundProxyProtocol::Vmess(vm) => vm.try_into(),
})
.collect::<Result<Vec<_>, _>>();
Expand Down
7 changes: 6 additions & 1 deletion clash_lib/src/app/router/rules/geoip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::sync::Arc;

use tracing::debug;

use crate::{common::mmdb, session::Session};

use super::RuleMatcher;
Expand All @@ -24,7 +26,10 @@ impl RuleMatcher for GeoIP {
.unwrap_or_default()
== self.country_code
}
Err(_) => todo!(),
Err(e) => {
debug!("GeoIP lookup failed: {}", e);
false
}
},
crate::session::SocksAddr::Domain(_, _) => false,
}
Expand Down
7 changes: 7 additions & 0 deletions clash_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use state::InitCell;
use std::io;
use std::path::PathBuf;
use tokio::task::JoinHandle;
use tracing::error;

use std::sync::Arc;
use thiserror::Error;
Expand Down Expand Up @@ -144,6 +145,12 @@ async fn start_async(opts: Options) -> Result<(), Error> {
)
.map_err(|x| Error::InvalidConfig(format!("failed to setup logging: {}", x.to_string())))?;

let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic(info);
error!("panic hook: {:?}", info);
}));

let mut tasks = Vec::<Runner>::new();
let mut runners = Vec::new();

Expand Down

0 comments on commit d67052f

Please sign in to comment.