From ed84bb636dae3dd6cac4e89e6ec31926aa76e816 Mon Sep 17 00:00:00 2001 From: Yuwei Ba Date: Mon, 15 Jul 2024 00:10:00 +1000 Subject: [PATCH] enable feature flags for outbounds (#492) --- clash_lib/Cargo.toml | 12 +++++++----- clash_lib/src/app/outbound/manager.rs | 3 ++- .../providers/proxy_provider/proxy_set_provider.rs | 2 ++ clash_lib/src/config/internal/proxy.rs | 6 ++++++ clash_lib/src/proxy/converters/mod.rs | 2 ++ clash_lib/src/proxy/mod.rs | 1 + clash_lib/src/proxy/relay/mod.rs | 1 + clash_lib/src/proxy/utils/test_utils/consts.rs | 3 +++ 8 files changed, 24 insertions(+), 6 deletions(-) diff --git a/clash_lib/Cargo.toml b/clash_lib/Cargo.toml index e30db2ed9..c87fc24e5 100644 --- a/clash_lib/Cargo.toml +++ b/clash_lib/Cargo.toml @@ -5,7 +5,9 @@ version = { workspace = true } edition = { workspace = true } [features] -default = ["shadowsocks"] +default = [] +shadowsocks = ["dep:shadowsocks"] +tuic = ["dep:tuic", "dep:tuic-quinn", "dep:quinn", "dep:register-count"] tracing = [] bench = ["criterion"] onion = ["arti-client/onion-service-client"] @@ -112,10 +114,10 @@ arti-client = { version = "0.20.0", default-features = false, features = ["tokio tor-rtcompat = { version = "0.20.0" } # tuic -tuic = { rev = "82fab62", git = "https://github.com/Itsusinn/tuic.git" } -tuic-quinn = { rev = "82fab62", git = "https://github.com/Itsusinn/tuic.git" } -quinn = { version = "0.10", default-features = false, features = ["futures-io", "runtime-tokio", "tls-rustls"] } -register-count = "0.1.0" +tuic = { rev = "82fab62", optional = true, git = "https://github.com/Itsusinn/tuic.git" } +tuic-quinn = { rev = "82fab62", optional = true, git = "https://github.com/Itsusinn/tuic.git" } +quinn = { version = "0.10", optional = true, default-features = false, features = ["futures-io", "runtime-tokio", "tls-rustls"] } +register-count = { version = "0.1.0", optional = true } console-subscriber = { version = "0.3.0" } tracing-timing = { version = "0.6.0" } diff --git a/clash_lib/src/app/outbound/manager.rs b/clash_lib/src/app/outbound/manager.rs index e3f1a3459..20929759a 100644 --- a/clash_lib/src/app/outbound/manager.rs +++ b/clash_lib/src/app/outbound/manager.rs @@ -198,7 +198,7 @@ impl OutboundManager { handlers .insert(PROXY_REJECT.to_string(), reject::Handler::new()); } - + #[cfg(feature = "shadowsocks")] OutboundProxyProtocol::Ss(s) => { handlers.insert(s.name.clone(), s.try_into()?); } @@ -223,6 +223,7 @@ impl OutboundManager { OutboundProxyProtocol::Tor(tor) => { handlers.insert(tor.name.clone(), tor.try_into()?); } + #[cfg(feature = "tuic")] OutboundProxyProtocol::Tuic(tuic) => { handlers.insert(tuic.name.clone(), tuic.try_into()?); } diff --git a/clash_lib/src/app/remote_content_manager/providers/proxy_provider/proxy_set_provider.rs b/clash_lib/src/app/remote_content_manager/providers/proxy_provider/proxy_set_provider.rs index f6b2d75b9..81a4281e1 100644 --- a/clash_lib/src/app/remote_content_manager/providers/proxy_provider/proxy_set_provider.rs +++ b/clash_lib/src/app/remote_content_manager/providers/proxy_provider/proxy_set_provider.rs @@ -113,12 +113,14 @@ impl ProxySetProvider { OutboundProxyProtocol::Reject => { Ok(reject::Handler::new()) } + #[cfg(feature = "shadowsocks")] OutboundProxyProtocol::Ss(s) => s.try_into(), OutboundProxyProtocol::Socks5(s) => s.try_into(), OutboundProxyProtocol::Trojan(tr) => tr.try_into(), OutboundProxyProtocol::Vmess(vm) => vm.try_into(), OutboundProxyProtocol::Wireguard(wg) => wg.try_into(), OutboundProxyProtocol::Tor(tor) => tor.try_into(), + #[cfg(feature = "tuic")] OutboundProxyProtocol::Tuic(tuic) => tuic.try_into(), }) .collect::, _>>(); diff --git a/clash_lib/src/config/internal/proxy.rs b/clash_lib/src/config/internal/proxy.rs index 43613ec53..06b12da89 100644 --- a/clash_lib/src/config/internal/proxy.rs +++ b/clash_lib/src/config/internal/proxy.rs @@ -55,6 +55,7 @@ pub enum OutboundProxyProtocol { Direct, #[serde(skip)] Reject, + #[cfg(feature = "shadowsocks")] #[serde(rename = "ss")] Ss(OutboundShadowsocks), #[serde(rename = "socks5")] @@ -67,6 +68,7 @@ pub enum OutboundProxyProtocol { Wireguard(OutboundWireguard), #[serde(rename = "tor")] Tor(OutboundTor), + #[cfg(feature = "tuic")] #[serde(rename = "tuic")] Tuic(OutboundTuic), } @@ -76,12 +78,14 @@ impl OutboundProxyProtocol { match &self { OutboundProxyProtocol::Direct => PROXY_DIRECT, OutboundProxyProtocol::Reject => PROXY_REJECT, + #[cfg(feature = "shadowsocks")] OutboundProxyProtocol::Ss(ss) => &ss.name, OutboundProxyProtocol::Socks5(socks5) => &socks5.name, OutboundProxyProtocol::Trojan(trojan) => &trojan.name, OutboundProxyProtocol::Vmess(vmess) => &vmess.name, OutboundProxyProtocol::Wireguard(wireguard) => &wireguard.name, OutboundProxyProtocol::Tor(tor) => &tor.name, + #[cfg(feature = "tuic")] OutboundProxyProtocol::Tuic(tuic) => &tuic.name, } } @@ -106,6 +110,7 @@ impl TryFrom> for OutboundProxyProtocol { impl Display for OutboundProxyProtocol { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { + #[cfg(feature = "shadowsocks")] OutboundProxyProtocol::Ss(_) => write!(f, "Shadowsocks"), OutboundProxyProtocol::Socks5(_) => write!(f, "Socks5"), OutboundProxyProtocol::Direct => write!(f, "{}", PROXY_DIRECT), @@ -114,6 +119,7 @@ impl Display for OutboundProxyProtocol { OutboundProxyProtocol::Vmess(_) => write!(f, "Vmess"), OutboundProxyProtocol::Wireguard(_) => write!(f, "Wireguard"), OutboundProxyProtocol::Tor(_) => write!(f, "Tor"), + #[cfg(feature = "tuic")] OutboundProxyProtocol::Tuic(_) => write!(f, "Tuic"), } } diff --git a/clash_lib/src/proxy/converters/mod.rs b/clash_lib/src/proxy/converters/mod.rs index b02ade789..25b3ddfb3 100644 --- a/clash_lib/src/proxy/converters/mod.rs +++ b/clash_lib/src/proxy/converters/mod.rs @@ -1,7 +1,9 @@ +#[cfg(feature = "shadowsocks")] pub mod shadowsocks; pub mod socks5; pub mod tor; pub mod trojan; +#[cfg(feature = "tuic")] pub mod tuic; pub mod vmess; pub mod wireguard; diff --git a/clash_lib/src/proxy/mod.rs b/clash_lib/src/proxy/mod.rs index d66baa304..e567f2ade 100644 --- a/clash_lib/src/proxy/mod.rs +++ b/clash_lib/src/proxy/mod.rs @@ -38,6 +38,7 @@ pub mod shadowsocks; pub mod socks; pub mod tor; pub mod trojan; +#[cfg(feature = "tuic")] pub mod tuic; pub mod tun; pub mod utils; diff --git a/clash_lib/src/proxy/relay/mod.rs b/clash_lib/src/proxy/relay/mod.rs index 19adbc27f..adab1bd68 100644 --- a/clash_lib/src/proxy/relay/mod.rs +++ b/clash_lib/src/proxy/relay/mod.rs @@ -171,6 +171,7 @@ impl OutboundHandler for Handler { } } +#[cfg(feature = "shadowsocks")] #[cfg(all(test, not(ci)))] mod tests { diff --git a/clash_lib/src/proxy/utils/test_utils/consts.rs b/clash_lib/src/proxy/utils/test_utils/consts.rs index 9a5fd3da1..6fe2011f7 100644 --- a/clash_lib/src/proxy/utils/test_utils/consts.rs +++ b/clash_lib/src/proxy/utils/test_utils/consts.rs @@ -1,8 +1,11 @@ pub const LOCAL_ADDR: &str = "127.0.0.1"; pub const IMAGE_WG: &str = "lscr.io/linuxserver/wireguard:1.0.20210914-legacy"; +#[cfg(feature = "shadowsocks")] pub const IMAGE_SS_RUST: &str = "ghcr.io/shadowsocks/ssserver-rust:latest"; +#[cfg(feature = "shadowsocks")] pub const IMAGE_SHADOW_TLS: &str = "ghcr.io/ihciah/shadow-tls:latest"; +#[cfg(feature = "shadowsocks")] pub const IMAGE_OBFS: &str = "liaohuqiu/simple-obfs:latest"; pub const IMAGE_TROJAN_GO: &str = "p4gefau1t/trojan-go:latest"; pub const IMAGE_VMESS: &str = "v2fly/v2fly-core:v4.45.2";