From e1cbd07d1fd4eda02bc9b0a30b9dfd2eae92c71c Mon Sep 17 00:00:00 2001 From: Chenx Dust Date: Tue, 3 Feb 2026 11:12:33 +0800 Subject: [PATCH] feat: separate `zstd` and `faketcp` into features (#1861) * feat: separate faketcp into a feature * fix: no need to initialize out_len * feat: separate zstd into a feature * clippy: remove unnecessary cast, because for unix size_t always equals usize --- easytier/Cargo.toml | 64 +++++++++++++++---- easytier/src/common/compressor.rs | 9 ++- easytier/src/connector/mod.rs | 8 ++- easytier/src/instance/listeners.rs | 7 +- easytier/src/proto/common.rs | 2 + .../tunnel/fake_tcp/netfilter/macos_bpf.rs | 6 +- easytier/src/tunnel/mod.rs | 4 +- easytier/src/tunnel/packet_def.rs | 2 + 8 files changed, 79 insertions(+), 23 deletions(-) diff --git a/easytier/Cargo.toml b/easytier/Cargo.toml index 4b33ee5f..8a1acdad 100644 --- a/easytier/Cargo.toml +++ b/easytier/Cargo.toml @@ -94,7 +94,13 @@ tun = { package = "tun-easytier", git = "https://github.com/EasyTier/rust-tun", "async", ], optional = true } # for net ns -nix = { version = "0.29.0", features = ["sched", "socket", "ioctl", "net", "fs"] } +nix = { version = "0.29.0", features = [ + "sched", + "socket", + "ioctl", + "net", + "fs", +] } uuid = { version = "1.5.0", features = [ "v4", @@ -202,7 +208,7 @@ async-ringbuf = "0.3.1" service-manager = { git = "https://github.com/EasyTier/service-manager-rs.git", branch = "main" } -zstd = { version = "0.13" } +zstd = { version = "0.13", optional = true } kcp-sys = { git = "https://github.com/EasyTier/kcp-sys", rev = "94964794caaed5d388463137da59b97499619e5f", optional = true } @@ -221,7 +227,9 @@ hickory-proto = "0.25.2" # for magic dns hickory-client = { version = "0.25.2", optional = true } -hickory-server = { version = "0.25.2", features = ["resolver"], optional = true } +hickory-server = { version = "0.25.2", features = [ + "resolver", +], optional = true } derive_builder = "0.20.2" humantime-serde = "1.1.1" @@ -232,7 +240,7 @@ sha2 = "0.10.8" shellexpand = "3.1.1" # for fake tcp -flume = "0.12" +flume = { version = "0.12", optional = true } cfg-if = "1.0" [target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "freebsd"))'.dependencies] @@ -249,7 +257,9 @@ dbus = { version = "0.9.7", features = ["vendored"] } which = "7.0.3" [target.'cfg(all(windows, any(target_arch = "x86_64", target_arch = "x86")))'.dependencies] -windivert = { git = "https://github.com/EasyTier/windivert-rust.git", rev = "adcc56d1550f7b5377ec2b3429f413ee24a77375", features = ["static"] } +windivert = { git = "https://github.com/EasyTier/windivert-rust.git", rev = "adcc56d1550f7b5377ec2b3429f413ee24a77375", features = [ + "static", +] } [target.'cfg(windows)'.dependencies] windows = { version = "0.52.0", features = [ @@ -269,16 +279,16 @@ windows-sys = { version = "0.52", features = [ "Win32_NetworkManagement_IpHelper", "Win32_NetworkManagement_Ndis", "Win32_Networking_WinSock", - "Win32_Foundation" + "Win32_Foundation", ] } winapi = { version = "0.3.9", features = ["impl-default"] } [target.'cfg(not(windows))'.dependencies] jemallocator = { package = "tikv-jemallocator", version = "0.6.0", optional = true, features = [ - "unprefixed_malloc_on_supported_platforms" + "unprefixed_malloc_on_supported_platforms", ] } jemalloc-ctl = { package = "tikv-jemalloc-ctl", version = "0.6.0", optional = true, features = [ - "use_std" + "use_std", ] } [target.'cfg(not(target_os = "macos"))'.dependencies] @@ -296,7 +306,9 @@ tonic-build = "0.12" globwalk = "0.8.1" regex = "1" prost-build = "0.13.2" -easytier-rpc-build = { path = "../easytier-rpc-build", features = [ "internal-namespace" ] } +easytier-rpc-build = { path = "../easytier-rpc-build", features = [ + "internal-namespace", +] } prost-reflect-build = { version = "0.14.0" } [target.'cfg(windows)'.build-dependencies] @@ -305,10 +317,14 @@ zip = "4.0.0" # enable thunk-rs when compiling for x86_64 or i686 windows [target.x86_64-pc-windows-msvc.build-dependencies] -thunk-rs = { git = "https://github.com/easytier/thunk.git", default-features = false, features = ["win7"] } +thunk-rs = { git = "https://github.com/easytier/thunk.git", default-features = false, features = [ + "win7", +] } [target.i686-pc-windows-msvc.build-dependencies] -thunk-rs = { git = "https://github.com/easytier/thunk.git", default-features = false, features = ["win7"] } +thunk-rs = { git = "https://github.com/easytier/thunk.git", default-features = false, features = [ + "win7", +] } [dev-dependencies] @@ -324,7 +340,18 @@ tokio-socks = "0.5.2" [features] -default = ["wireguard", "websocket", "smoltcp", "tun", "socks5", "kcp", "quic", "magic-dns"] +default = [ + "wireguard", + "websocket", + "smoltcp", + "tun", + "socks5", + "kcp", + "quic", + "faketcp", + "magic-dns", + "zstd", +] full = [ "websocket", "wireguard", @@ -333,8 +360,11 @@ full = [ "smoltcp", "tun", "socks5", + "kcp", "quic", + "faketcp", "magic-dns", + "zstd", ] wireguard = ["dep:boringtun", "dep:ring"] quic = ["dep:quinn", "dep:quinn-plaintext", "dep:rustls", "dep:rcgen"] @@ -353,6 +383,14 @@ websocket = [ smoltcp = ["dep:smoltcp"] socks5 = ["smoltcp"] jemalloc = ["dep:jemallocator", "dep:jemalloc-sys"] -jemalloc-prof = ["jemalloc", "dep:jemalloc-ctl", "jemalloc-ctl/stats", "jemalloc-sys/profiling", "jemalloc-sys/stats"] +jemalloc-prof = [ + "jemalloc", + "dep:jemalloc-ctl", + "jemalloc-ctl/stats", + "jemalloc-sys/profiling", + "jemalloc-sys/stats", +] tracing = ["tokio/tracing", "dep:console-subscriber"] magic-dns = ["dep:hickory-client", "dep:hickory-server"] +faketcp = ["dep:flume"] +zstd = ["dep:zstd"] diff --git a/easytier/src/common/compressor.rs b/easytier/src/common/compressor.rs index 6e8d0143..a5201852 100644 --- a/easytier/src/common/compressor.rs +++ b/easytier/src/common/compressor.rs @@ -1,6 +1,10 @@ +#[cfg(feature = "zstd")] use anyhow::Context; +#[cfg(feature = "zstd")] use dashmap::DashMap; +#[cfg(feature = "zstd")] use std::cell::RefCell; +#[cfg(feature = "zstd")] use zstd::bulk; use zerocopy::{AsBytes as _, FromBytes as _}; @@ -38,6 +42,7 @@ impl DefaultCompressor { compress_algo: CompressorAlgo, ) -> Result, Error> { match compress_algo { + #[cfg(feature = "zstd")] CompressorAlgo::ZstdDefault => CTX_MAP.with(|map_cell| { let map = map_cell.borrow(); let mut ctx_entry = map.entry(compress_algo).or_default(); @@ -58,6 +63,7 @@ impl DefaultCompressor { compress_algo: CompressorAlgo, ) -> Result, Error> { match compress_algo { + #[cfg(feature = "zstd")] CompressorAlgo::ZstdDefault => DCTX_MAP.with(|map_cell| { let map = map_cell.borrow(); let mut ctx_entry = map.entry(compress_algo).or_default(); @@ -169,12 +175,13 @@ impl Compressor for DefaultCompressor { } } +#[cfg(feature = "zstd")] thread_local! { static CTX_MAP: RefCell>> = RefCell::new(DashMap::new()); static DCTX_MAP: RefCell>> = RefCell::new(DashMap::new()); } -#[cfg(test)] +#[cfg(all(test, feature = "zstd"))] pub mod tests { use super::*; diff --git a/easytier/src/connector/mod.rs b/easytier/src/connector/mod.rs index 2ded7406..e85d0099 100644 --- a/easytier/src/connector/mod.rs +++ b/easytier/src/connector/mod.rs @@ -5,6 +5,8 @@ use std::{ use http_connector::HttpTunnelConnector; +#[cfg(feature = "faketcp")] +use crate::tunnel::fake_tcp::FakeTcpTunnelConnector; #[cfg(feature = "quic")] use crate::tunnel::quic::QUICTunnelConnector; #[cfg(unix)] @@ -14,9 +16,8 @@ use crate::tunnel::wireguard::{WgConfig, WgTunnelConnector}; use crate::{ common::{error::Error, global_ctx::ArcGlobalCtx, idn, network::IPCollector}, tunnel::{ - check_scheme_and_get_socket_addr, fake_tcp::FakeTcpTunnelConnector, - ring::RingTunnelConnector, tcp::TcpTunnelConnector, udp::UdpTunnelConnector, IpVersion, - TunnelConnector, + check_scheme_and_get_socket_addr, ring::RingTunnelConnector, tcp::TcpTunnelConnector, + udp::UdpTunnelConnector, IpVersion, TunnelConnector, }, }; @@ -165,6 +166,7 @@ pub async fn create_connector_by_url( let connector = dns_connector::DNSTunnelConnector::new(url, global_ctx.clone()); Box::new(connector) } + #[cfg(feature = "faketcp")] "faketcp" => { let dst_addr = check_scheme_and_get_socket_addr::(&url, "faketcp", ip_version).await?; diff --git a/easytier/src/instance/listeners.rs b/easytier/src/instance/listeners.rs index 0cdc3bf0..542d4300 100644 --- a/easytier/src/instance/listeners.rs +++ b/easytier/src/instance/listeners.rs @@ -9,6 +9,8 @@ use anyhow::Context; use async_trait::async_trait; use tokio::task::JoinSet; +#[cfg(feature = "faketcp")] +use crate::tunnel::fake_tcp::FakeTcpTunnelListener; #[cfg(feature = "quic")] use crate::tunnel::quic::QUICTunnelListener; #[cfg(feature = "wireguard")] @@ -21,8 +23,8 @@ use crate::{ }, peers::peer_manager::PeerManager, tunnel::{ - fake_tcp::FakeTcpTunnelListener, ring::RingTunnelListener, tcp::TcpTunnelListener, - udp::UdpTunnelListener, Tunnel, TunnelListener, + ring::RingTunnelListener, tcp::TcpTunnelListener, udp::UdpTunnelListener, Tunnel, + TunnelListener, }, }; @@ -49,6 +51,7 @@ pub fn get_listener_by_url( use crate::tunnel::websocket::WSTunnelListener; Box::new(WSTunnelListener::new(l.clone())) } + #[cfg(feature = "faketcp")] "faketcp" => Box::new(FakeTcpTunnelListener::new(l.clone())), #[cfg(unix)] "unix" => { diff --git a/easytier/src/proto/common.rs b/easytier/src/proto/common.rs index e655c0c5..7c05292e 100644 --- a/easytier/src/proto/common.rs +++ b/easytier/src/proto/common.rs @@ -330,6 +330,7 @@ impl TryFrom for CompressorAlgo { fn try_from(value: CompressionAlgoPb) -> Result { match value { + #[cfg(feature = "zstd")] CompressionAlgoPb::Zstd => Ok(CompressorAlgo::ZstdDefault), CompressionAlgoPb::None => Ok(CompressorAlgo::None), _ => Err(anyhow::anyhow!("Invalid CompressionAlgoPb")), @@ -342,6 +343,7 @@ impl TryFrom for CompressionAlgoPb { fn try_from(value: CompressorAlgo) -> Result { match value { + #[cfg(feature = "zstd")] CompressorAlgo::ZstdDefault => Ok(CompressionAlgoPb::Zstd), CompressorAlgo::None => Ok(CompressionAlgoPb::None), } diff --git a/easytier/src/tunnel/fake_tcp/netfilter/macos_bpf.rs b/easytier/src/tunnel/fake_tcp/netfilter/macos_bpf.rs index ca252257..17278088 100644 --- a/easytier/src/tunnel/fake_tcp/netfilter/macos_bpf.rs +++ b/easytier/src/tunnel/fake_tcp/netfilter/macos_bpf.rs @@ -716,7 +716,7 @@ fn open_bpf_device() -> io::Result { fn set_ifreq_name(ifr: &mut libc::ifreq, interface_name: &str) -> io::Result<()> { let bytes = interface_name.as_bytes(); - let ifnamsiz = libc::IFNAMSIZ as usize; + let ifnamsiz = libc::IFNAMSIZ; if bytes.len() >= ifnamsiz { return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -932,7 +932,7 @@ impl MacosBpfTun { let pkt_start = off + pkt_range.start; let pkt_end = off + pkt_range.end; - let shift = (pkt_range.start as usize).saturating_sub(hdr_len as usize); + let shift = pkt_range.start.saturating_sub(hdr_len as usize); if shift != 0 && shifted_record_logs_left > 0 { shifted_record_logs_left -= 1; warn!( @@ -1039,7 +1039,7 @@ impl stack::Tun for MacosBpfTun { Ok(()) }; - let mut out_len = 0usize; + let mut out_len: usize; let res = match self.link_type { LinkType::En10Mb => { out_len = packet.len(); diff --git a/easytier/src/tunnel/mod.rs b/easytier/src/tunnel/mod.rs index f1efa9f1..fdc23ca3 100644 --- a/easytier/src/tunnel/mod.rs +++ b/easytier/src/tunnel/mod.rs @@ -15,7 +15,6 @@ use self::packet_def::ZCPacket; pub mod buf; pub mod common; -pub mod fake_tcp; pub mod filter; pub mod mpsc; pub mod packet_def; @@ -33,6 +32,9 @@ pub const PROTO_PORT_OFFSET: &[(&str, u16)] = &[ ("faketcp", 3), ]; +#[cfg(feature = "faketcp")] +pub mod fake_tcp; + #[cfg(feature = "wireguard")] pub mod wireguard; diff --git a/easytier/src/tunnel/packet_def.rs b/easytier/src/tunnel/packet_def.rs index 101ef1d7..a59403d6 100644 --- a/easytier/src/tunnel/packet_def.rs +++ b/easytier/src/tunnel/packet_def.rs @@ -288,6 +288,7 @@ pub const AES_GCM_ENCRYPTION_RESERVED: usize = std::mem::size_of::() #[repr(u8)] pub enum CompressorAlgo { None = 0, + #[cfg(feature = "zstd")] ZstdDefault = 1, } @@ -301,6 +302,7 @@ pub const COMPRESSOR_TAIL_SIZE: usize = std::mem::size_of::(); impl CompressorTail { pub fn get_algo(&self) -> Option { match self.algo { + #[cfg(feature = "zstd")] 1 => Some(CompressorAlgo::ZstdDefault), _ => None, }