Add local-flow-stat to control flow statistic reports in local server

ref #210
This commit is contained in:
zonyitoo
2020-03-17 22:24:21 +08:00
committed by Y. T. CHUNG
parent 05cdde3032
commit 792c648db3
7 changed files with 58 additions and 38 deletions
+1 -2
View File
@@ -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"
+22 -16
View File
@@ -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;
+2
View File
@@ -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<String>,
/// Path to protect callback unix address, only for Android
pub protect_path: Option<String>,
@@ -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,
+6 -1
View File
@@ -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<PingPongBloom>,
// 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
}
+11 -11
View File
@@ -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")
}
+12 -6
View File
@@ -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);
}
}
}
+4 -2
View File
@@ -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);
}