This commit is contained in:
Y. T. Chung
2016-10-21 01:14:12 +08:00
parent d27d7414b7
commit bbfcec5326
4 changed files with 125 additions and 8 deletions
+17 -2
View File
@@ -39,6 +39,7 @@ use clap::{App, Arg};
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use std::env;
use std::time::Duration;
use coio::Scheduler;
@@ -109,7 +110,8 @@ fn main() {
let mut log_builder = LogBuilder::new();
log_builder.filter(None, LogLevelFilter::Info);
match matches.occurrences_of("VERBOSE") {
let debug_level = matches.occurrences_of("VERBOSE");
match debug_level {
0 => {
// Default filter
log_builder.format(|record: &LogRecord| {
@@ -235,7 +237,7 @@ fn main() {
config.server.push(sc);
has_provided_server_config = true;
} else if matches.value_of("SERVER_ADDR").is_none() && matches.value_of("SERVER_PORT").is_none() &&
matches.value_of("PASSWORD").is_none() && matches.value_of("ENCRYPT_METHOD").is_none() {
matches.value_of("PASSWORD").is_none() && matches.value_of("ENCRYPT_METHOD").is_none() {
// Does not provide server config
} else {
panic!("`server-addr`, `server-port`, `method` and `password` should be provided together");
@@ -295,6 +297,19 @@ fn main() {
.with_workers(threads)
.default_stack_size(stack_size)
.run(move || {
if debug_level > 0 && cfg!(debug_assertions) {
// Statistic coroutine
Scheduler::spawn(|| {
loop {
debug!("STAT Coroutines: {}, TCP work: {}, HTTP work: {}",
Scheduler::instance().unwrap().work_count(),
RelayLocal::global_tcp_work_count(),
RelayLocal::global_http_work_count());
coio::sleep(Duration::from_secs(1));
}
});
}
RelayLocal::new(config).run();
})
.unwrap();
+10
View File
@@ -84,6 +84,16 @@ impl RelayLocal {
enable_http: config.http_proxy.is_some(),
}
}
/// Global TCP work count
pub fn global_tcp_work_count() -> usize {
super::tcprelay::global_tcp_work_count()
}
/// Global HTTP work count
pub fn global_http_work_count() -> usize {
super::tcprelay::global_http_work_count()
}
}
impl Relay for RelayLocal {
+9 -5
View File
@@ -98,7 +98,6 @@ impl TcpRelayLocal {
server_addr: SocketAddr,
password: Vec<u8>,
encrypt_method: CipherType,
enable_udp: bool,
conf: Arc<Config>) {
let sockname = match stream.peer_addr() {
Ok(sockname) => sockname,
@@ -172,6 +171,8 @@ impl TcpRelayLocal {
let addr_cloned = addr.clone();
Scheduler::spawn(move || {
let _guard = super::TcpWorkCounter::new();
loop {
match ::relay::copy_once(&mut local_reader, &mut encrypt_stream) {
Ok(0) => {
@@ -235,7 +236,7 @@ impl TcpRelayLocal {
}
socks5::Command::UdpAssociate => {
info!("{} requests for UDP ASSOCIATE", sockname);
if cfg!(feature = "enable-udp") && enable_udp {
if cfg!(feature = "enable-udp") && conf.enable_udp {
TcpRelayLocal::handle_udp_associate_local(&mut local_writer, sockname, &addr, conf.local.unwrap())
.unwrap_or_else(|err| error!("Failed to write UDP ASSOCIATE response: {}", err));
} else {
@@ -286,6 +287,8 @@ impl TcpRelayLocal {
let addr_cloned = addr.clone();
Scheduler::spawn(move || {
let _guard = super::HttpWorkCounter::new();
loop {
match ::relay::copy_once(&mut local_reader, &mut encrypt_stream) {
Ok(0) => {
@@ -380,6 +383,8 @@ impl TcpRelayLocal {
let mut remain_len = content_len.saturating_sub(remain.len());
Scheduler::spawn(move || {
let _guard = super::HttpWorkCounter::new();
let mut buf = [0u8; 1024];
let mut content_len = content_len;
@@ -691,10 +696,9 @@ impl TcpRelayLocal {
pub fn run_tcp(&self) {
self.run_server(self.config.local.expect("Require local config"),
|stream, server_addr, pwd, encrypt_method, conf| {
let enable_udp = conf.enable_udp;
Scheduler::spawn(move || {
TcpRelayLocal::handle_tcp_client(stream, server_addr, pwd, encrypt_method, enable_udp, conf);
});
TcpRelayLocal::handle_tcp_client(stream, server_addr, pwd, encrypt_method, conf);
});
});
}
+89 -1
View File
@@ -68,7 +68,8 @@ fn connect_proxy_server(server_addr: &SocketAddr,
EncryptedWriter::new(remote_writer, encryptor)
};
trace!("Got encrypt stream and going to send addr: {:?}", relay_addr);
trace!("Got encrypt stream and going to send addr: {:?}",
relay_addr);
// Send relay address to remote
let mut addr_buf = Vec::new();
@@ -116,3 +117,90 @@ fn connect_proxy_server(server_addr: &SocketAddr,
trace!("Finished creating remote encrypt stream pair");
Ok((decrypt_stream, encrypt_stream))
}
#[cfg(debug_assertions)]
mod stat {
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
static GLOBAL_TCP_WORK_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
static GLOBAL_HTTP_WORK_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
pub fn global_tcp_work_count_add() {
GLOBAL_TCP_WORK_COUNT.fetch_add(1, Ordering::Relaxed);
}
pub fn global_tcp_work_count_sub() {
GLOBAL_TCP_WORK_COUNT.fetch_sub(1, Ordering::Relaxed);
}
pub fn global_tcp_work_count_get() -> usize {
GLOBAL_TCP_WORK_COUNT.load(Ordering::Relaxed)
}
pub fn global_http_work_count_add() {
GLOBAL_HTTP_WORK_COUNT.fetch_add(1, Ordering::Relaxed);
}
pub fn global_http_work_count_sub() {
GLOBAL_HTTP_WORK_COUNT.fetch_sub(1, Ordering::Relaxed);
}
pub fn global_http_work_count_get() -> usize {
GLOBAL_HTTP_WORK_COUNT.load(Ordering::Relaxed)
}
}
#[cfg(not(debug_assertions))]
mod stat {
pub fn global_tcp_work_count_add() {}
pub fn global_tcp_work_count_sub() {}
pub fn global_tcp_work_count_get() -> usize {
0
}
pub fn global_http_work_count_add() {}
pub fn global_http_work_count_sub() {}
pub fn global_http_work_count_get() -> usize {
0
}
}
struct TcpWorkCounter;
impl TcpWorkCounter {
fn new() -> TcpWorkCounter {
stat::global_tcp_work_count_add();
TcpWorkCounter
}
}
impl Drop for TcpWorkCounter {
fn drop(&mut self) {
stat::global_tcp_work_count_sub();
}
}
struct HttpWorkCounter;
impl HttpWorkCounter {
fn new() -> HttpWorkCounter {
stat::global_http_work_count_add();
HttpWorkCounter
}
}
impl Drop for HttpWorkCounter {
fn drop(&mut self) {
stat::global_http_work_count_sub();
}
}
/// Get total TCP relay work count
pub fn global_tcp_work_count() -> usize {
stat::global_tcp_work_count_get()
}
/// Get total HTTP relay work count
pub fn global_http_work_count() -> usize {
stat::global_http_work_count_get()
}