diff --git a/Cargo.toml b/Cargo.toml index 6a620d5e..ce347e4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,4 +63,5 @@ lru-cache = "0.0.7" libc = "^0.2.7" [dependencies.coio] -git = "https://github.com/zonyitoo/coio-rs.git" +# git = "https://github.com/zonyitoo/coio-rs.git" +path = "../coio-rs" diff --git a/src/config.rs b/src/config.rs index 6e8f01b1..4a870048 100644 --- a/src/config.rs +++ b/src/config.rs @@ -75,6 +75,7 @@ use std::default::Default; use std::fmt::{self, Debug, Formatter}; use std::path::Path; use std::collections::HashSet; +use std::time::Duration; use ip::IpAddr; @@ -90,7 +91,7 @@ pub struct ServerConfig { pub port: u16, pub password: String, pub method: CipherType, - pub timeout: Option, + pub timeout: Option, pub dns_cache_capacity: usize, } @@ -116,7 +117,7 @@ impl json::ToJson for ServerConfig { obj.insert("password".to_owned(), Json::String(self.password.clone())); obj.insert("method".to_owned(), Json::String(self.method.to_string())); if let Some(t) = self.timeout { - obj.insert("timeout".to_owned(), Json::U64(t as u64)); + obj.insert("timeout".to_owned(), Json::U64(t.as_secs())); } obj.insert("dns_cache_capacity".to_owned(), Json::U64(self.dns_cache_capacity as u64)); @@ -140,7 +141,7 @@ pub struct Config { pub server: Vec, pub local: Option, pub enable_udp: bool, - pub timeout: Option, + pub timeout: Option, pub forbidden_ip: HashSet, } @@ -201,10 +202,11 @@ impl Config { config.timeout = match o.get("timeout") { Some(t_str) => { - Some(try!(t_str.as_u64() - .ok_or(Error::new(ErrorKind::Malformed, - "`timeout` should be an integer", - None))) as u32) + let val = try!(t_str.as_u64() + .ok_or(Error::new(ErrorKind::Malformed, + "`timeout` should be an integer", + None))); + Some(Duration::from_secs(val)) } None => None, }; @@ -262,10 +264,13 @@ impl Config { .to_string(), method: method, timeout: match server.find("timeout") { - Some(t) => Some(try!(t.as_u64() - .ok_or(Error::new(ErrorKind::Malformed, - "`timeout` should be an integer", - None))) as u32), + Some(t) => { + let val = try!(t.as_u64() + .ok_or(Error::new(ErrorKind::Malformed, + "`timeout` should be an integer", + None))); + Some(Duration::from_secs(val)) + } None => None, }, dns_cache_capacity: match server.find("dns_cache_capacity") { @@ -333,10 +338,11 @@ impl Config { method: method, timeout: match o.get("timeout") { Some(t) => { - Some(try!(t.as_u64() - .ok_or(Error::new(ErrorKind::Malformed, - "`timeout` should be an integer", - None))) as u32) + let val = try!(t.as_u64() + .ok_or(Error::new(ErrorKind::Malformed, + "`timeout` should be an integer", + None))); + Some(Duration::from_secs(val)) } None => None, }, @@ -508,7 +514,7 @@ impl json::ToJson for Config { obj.insert("method".to_owned(), Json::String(self.server[0].method.to_string())); if let Some(t) = self.server[0].timeout { - obj.insert("timeout".to_owned(), Json::U64(t as u64)); + obj.insert("timeout".to_owned(), Json::U64(t.as_secs())); } } else { let arr: json::Array = self.server.iter().map(|s| s.to_json()).collect(); diff --git a/src/relay/tcprelay/local.rs b/src/relay/tcprelay/local.rs index 6ae5a1ff..8e230260 100644 --- a/src/relay/tcprelay/local.rs +++ b/src/relay/tcprelay/local.rs @@ -25,6 +25,7 @@ use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; use std::net::lookup_host; use std::io::{self, BufWriter, BufReader, ErrorKind, Read, Write}; use std::collections::BTreeMap; +use std::sync::Arc; use coio::Scheduler; use coio::net::{TcpListener, TcpStream, Shutdown}; @@ -41,7 +42,7 @@ use crypto::CryptoMode; #[derive(Clone)] pub struct TcpRelayLocal { - config: Config, + config: Arc, } impl TcpRelayLocal { @@ -51,7 +52,7 @@ impl TcpRelayLocal { } TcpRelayLocal { - config: c, + config: Arc::new(c), } } @@ -95,7 +96,7 @@ impl TcpRelayLocal { password: Vec, encrypt_method: CipherType, enable_udp: bool, - local_conf: ClientConfig) { + conf: Arc) { let sockname = match stream.peer_addr() { Ok(sockname) => sockname, Err(err) => { @@ -167,6 +168,11 @@ impl TcpRelayLocal { Ok(s) => { s }, }; + if let Err(err) = remote_stream.set_read_timeout(conf.timeout) { + error!("Failed to set read timeout: {:?}", err); + return; + } + // Send header to client { let header = socks5::TcpResponseHeader::new(socks5::Reply::Succeeded, @@ -303,7 +309,7 @@ impl TcpRelayLocal { socks5::Command::UdpAssociate => { info!("{} requests for UDP ASSOCIATE", sockname); if cfg!(feature = "enable-udp") && enable_udp { - TcpRelayLocal::handle_udp_associate_local(&mut local_writer, sockname, &addr, local_conf) + 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 { warn!("UDP ASSOCIATE is disabled"); @@ -334,7 +340,7 @@ impl TcpRelayLocal { let mut cached_proxy: BTreeMap = BTreeMap::new(); for s in acceptor.incoming() { - let stream = match s { + let mut stream = match s { Ok((s, addr)) => { debug!("Got connection from client {:?}", addr); s @@ -344,8 +350,9 @@ impl TcpRelayLocal { continue; } }; - if let Err(err) = stream.set_keepalive(self.config.timeout) { - error!("Failed to set keep alive: {:?}", err); + + if let Err(err) = stream.set_read_timeout(self.config.timeout) { + error!("Failed to set read timeout: {:?}", err); continue; } @@ -405,15 +412,15 @@ impl TcpRelayLocal { let encrypt_method = server_cfg.method.clone(); let pwd = encrypt_method.bytes_to_key(server_cfg.password.as_bytes()); let enable_udp = self.config.enable_udp; - let local_conf = local_conf.clone(); + let conf = self.config.clone(); Scheduler::spawn(move || TcpRelayLocal::handle_client(stream, server_addr, pwd, encrypt_method, enable_udp, - local_conf)); + conf)); succeed = true; break; } diff --git a/src/relay/tcprelay/server.rs b/src/relay/tcprelay/server.rs index fc883bbd..517840e6 100644 --- a/src/relay/tcprelay/server.rs +++ b/src/relay/tcprelay/server.rs @@ -74,8 +74,8 @@ impl TcpRelayServer { } }; - if let Err(err) = stream.set_keepalive(timeout) { - error!("Failed to set keep alive: {}", err); + if let Err(err) = stream.set_read_timeout(timeout) { + error!("Failed to set read timeout: {:?}", err); continue; }