From 792c648db3e555125ced70b9545e35b427197eef Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Mon, 16 Mar 2020 23:58:23 +0800 Subject: [PATCH] Add local-flow-stat to control flow statistic reports in local server ref #210 --- Cargo.toml | 3 +-- src/bin/local.rs | 38 +++++++++++++++++------------- src/config.rs | 2 ++ src/context.rs | 7 +++++- src/relay/local.rs | 22 ++++++++--------- src/relay/tcprelay/proxy_stream.rs | 18 +++++++++----- src/relay/udprelay/association.rs | 6 +++-- 7 files changed, 58 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5af087dc..81780449 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,7 @@ single-threaded = [] trust-dns = ["trust-dns-resolver"] openssl-vendored = ["openssl/vendored"] local-dns-relay = [] +local-flow-stat = [] [dependencies] log = "0.4" @@ -109,8 +110,6 @@ lazy_static = "1.4" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["mswsock", "winsock2"] } -[target.'cfg(target_os = "android")'.dependencies] - # Just for the ioctl call macro [target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))'.dependencies] nix = "0.17" diff --git a/src/bin/local.rs b/src/bin/local.rs index 6951d804..b8650f62 100644 --- a/src/bin/local.rs +++ b/src/bin/local.rs @@ -152,18 +152,21 @@ fn main() { ); if cfg!(target_os = "android") { - app = app - .arg( - Arg::with_name("VPN_MODE") - .long("vpn") - .help("Enable VPN mode (only for Android)"), - ) - .arg( - Arg::with_name("STAT_PATH") - .long("stat-path") - .takes_value(true) - .help("Specify stat_path for traffic stat (only for Android)"), - ); + app = app.arg( + Arg::with_name("VPN_MODE") + .long("vpn") + .help("Enable VPN mode (only for Android)"), + ); + } + + #[cfg(feature = "local-flow-stat")] + { + app = app.arg( + Arg::with_name("STAT_PATH") + .long("stat-path") + .takes_value(true) + .help("Specify stat_path for traffic stat (only for Android)"), + ); } #[cfg(feature = "local-dns-relay")] @@ -243,15 +246,18 @@ fn main() { if cfg!(target_os = "android") { config.local_dns_path = Some("local_dns_path".to_owned()); - if let Some(stat_path) = matches.value_of("STAT_PATH") { - config.stat_path = Some(stat_path.to_owned()); - } - if matches.is_present("VPN_MODE") { config.protect_path = Some("protect_path".to_owned()); } } + #[cfg(feature = "local-flow-stat")] + { + if let Some(stat_path) = matches.value_of("STAT_PATH") { + config.stat_path = Some(stat_path.to_owned()); + } + } + #[cfg(feature = "local-dns-relay")] { use std::net::SocketAddr; diff --git a/src/config.rs b/src/config.rs index f03e75bd..c8a2246b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -914,6 +914,7 @@ pub struct Config { /// UDP Transparent Proxy type pub udp_redir: RedirType, /// Android flow statistic report Unix socket path + #[cfg(feature = "local-flow-stat")] pub stat_path: Option, /// Path to protect callback unix address, only for Android pub protect_path: Option, @@ -1014,6 +1015,7 @@ impl Config { acl: None, tcp_redir: RedirType::tcp_default(), udp_redir: RedirType::udp_default(), + #[cfg(feature = "local-flow-stat")] stat_path: None, protect_path: None, local_dns_path: None, diff --git a/src/context.rs b/src/context.rs index d760c7a3..c826bea2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -22,9 +22,11 @@ use trust_dns_resolver::TokioAsyncResolver; #[cfg(feature = "trust-dns")] use crate::relay::dns_resolver::create_resolver; +#[cfg(feature = "local-flow-stat")] +use crate::relay::flow::ServerFlowStatistic; use crate::{ config::{Config, ConfigType, ServerConfig}, - relay::{dns_resolver::resolve, flow::ServerFlowStatistic, socks5::Address}, + relay::{dns_resolver::resolve, socks5::Address}, }; // Entries for server's bloom filter @@ -165,6 +167,7 @@ pub struct Context { nonce_ppbloom: Mutex, // For Android's flow stat report + #[cfg(feature = "local-flow-stat")] local_flow_statistic: ServerFlowStatistic, // For DNS relay's ACL domain name reverse lookup @@ -187,6 +190,7 @@ impl Context { server_state, server_running: AtomicBool::new(true), nonce_ppbloom, + #[cfg(feature = "local-flow-stat")] local_flow_statistic: ServerFlowStatistic::new(), #[cfg(feature = "local-dns-relay")] reverse_lookup_cache, @@ -327,6 +331,7 @@ impl Context { } /// Get client flow statistics + #[cfg(feature = "local-flow-stat")] pub fn local_flow_statistic(&self) -> &ServerFlowStatistic { &self.local_flow_statistic } diff --git a/src/relay/local.rs b/src/relay/local.rs index 3c8e5be9..f22f87c4 100644 --- a/src/relay/local.rs +++ b/src/relay/local.rs @@ -6,9 +6,11 @@ use futures::{future::select_all, FutureExt}; use log::{debug, error, trace, warn}; use tokio::runtime::Handle; +#[cfg(feature = "local-flow-stat")] +use crate::context::SharedContext; use crate::{ config::{Config, ConfigType}, - context::{Context, ServerState, SharedContext}, + context::{Context, ServerState}, plugin::{PluginMode, Plugins}, relay::{tcprelay::local::run as run_tcp, udprelay::local::run as run_udp, utils::set_nofile}, }; @@ -107,11 +109,14 @@ pub async fn run(mut config: Config, rt: Handle) -> io::Result<()> { } } - if cfg!(target_os = "android") && context.config().stat_path.is_some() { - // For Android's flow statistic + #[cfg(feature = "local-flow-stat")] + { + if context.config().stat_path.is_some() { + // For Android's flow statistic - let report_fut = flow_report_task(context.clone()); - vf.push(report_fut.boxed()); + let report_fut = flow_report_task(context.clone()); + vf.push(report_fut.boxed()); + } } let (res, ..) = select_all(vf.into_iter()).await; @@ -123,7 +128,7 @@ pub async fn run(mut config: Config, rt: Handle) -> io::Result<()> { Err(io::Error::new(io::ErrorKind::Other, "server exited unexpectly")) } -#[cfg(target_os = "android")] +#[cfg(feature = "local-flow-stat")] async fn flow_report_task(context: SharedContext) -> io::Result<()> { use std::{slice, time::Duration}; @@ -168,8 +173,3 @@ async fn flow_report_task(context: SharedContext) -> io::Result<()> { } Ok(()) } - -#[cfg(not(target_os = "android"))] -async fn flow_report_task(_context: SharedContext) -> io::Result<()> { - unimplemented!("only for android") -} diff --git a/src/relay/tcprelay/proxy_stream.rs b/src/relay/tcprelay/proxy_stream.rs index 2f6a69c0..29086c04 100644 --- a/src/relay/tcprelay/proxy_stream.rs +++ b/src/relay/tcprelay/proxy_stream.rs @@ -197,9 +197,12 @@ impl AsyncRead for ProxyStream { let p = forward_call!(self, poll_read, cx, buf); // Flow statistic for Android client - if cfg!(target_os = "android") && self.is_proxied() { - if let Poll::Ready(Ok(n)) = p { - self.context().local_flow_statistic().tcp().incr_tx(n as u64); + #[cfg(feature = "local-flow-stat")] + { + if self.is_proxied() { + if let Poll::Ready(Ok(n)) = p { + self.context().local_flow_statistic().tcp().incr_tx(n as u64); + } } } @@ -212,9 +215,12 @@ impl AsyncWrite for ProxyStream { let p = forward_call!(self, poll_write, cx, buf); // Flow statistic for Android client - if cfg!(target_os = "android") && self.is_proxied() { - if let Poll::Ready(Ok(n)) = p { - self.context().local_flow_statistic().tcp().incr_rx(n as u64); + #[cfg(feature = "local-flow-stat")] + { + if self.is_proxied() { + if let Poll::Ready(Ok(n)) = p { + self.context().local_flow_statistic().tcp().incr_rx(n as u64); + } } } diff --git a/src/relay/udprelay/association.rs b/src/relay/udprelay/association.rs index fafeadab..52944ed5 100644 --- a/src/relay/udprelay/association.rs +++ b/src/relay/udprelay/association.rs @@ -318,7 +318,8 @@ impl ProxyAssociation { ); } - if cfg!(target_os = "android") { + #[cfg(feature = "local-flow-stat")] + { context.local_flow_statistic().udp().incr_tx(send_len as u64); } @@ -420,7 +421,8 @@ impl ProxyAssociation { let mut payload = Vec::new(); cur.read_to_end(&mut payload)?; - if cfg!(target_os = "android") { + #[cfg(feature = "local-flow-stat")] + { context.local_flow_statistic().udp().incr_rx(recv_n as u64); }