mirror of
https://github.com/shadowsocks/shadowsocks-rust.git
synced 2026-02-09 01:59:16 +08:00
use server's timeout setting as connect() timeout
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
//! Shadowsocks TCP server
|
||||
|
||||
use std::{io, net::SocketAddr, sync::Arc, time::Duration};
|
||||
use std::{
|
||||
io::{self, ErrorKind},
|
||||
net::SocketAddr,
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use futures::future::{self, Either};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
@@ -60,6 +65,7 @@ impl TcpServer {
|
||||
method: svr_cfg.method(),
|
||||
peer_addr,
|
||||
stream: local_stream,
|
||||
timeout: svr_cfg.timeout(),
|
||||
};
|
||||
|
||||
tokio::spawn(async move {
|
||||
@@ -76,6 +82,7 @@ struct TcpServerClient {
|
||||
method: CipherKind,
|
||||
peer_addr: SocketAddr,
|
||||
stream: ProxyServerStream<MonProxyStream<TokioTcpStream>>,
|
||||
timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
impl TcpServerClient {
|
||||
@@ -109,12 +116,37 @@ impl TcpServerClient {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut remote_stream = OutboundTcpStream::connect_remote_with_opts(
|
||||
self.context.context_ref(),
|
||||
&target_addr,
|
||||
self.context.connect_opts_ref(),
|
||||
)
|
||||
.await?;
|
||||
let mut remote_stream = match self.timeout {
|
||||
Some(d) => {
|
||||
match time::timeout(
|
||||
d,
|
||||
OutboundTcpStream::connect_remote_with_opts(
|
||||
self.context.context_ref(),
|
||||
&target_addr,
|
||||
self.context.connect_opts_ref(),
|
||||
),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Ok(s)) => s,
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(..) => {
|
||||
return Err(io::Error::new(
|
||||
ErrorKind::TimedOut,
|
||||
format!("connect {} timeout", target_addr),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
OutboundTcpStream::connect_remote_with_opts(
|
||||
self.context.context_ref(),
|
||||
&target_addr,
|
||||
self.context.connect_opts_ref(),
|
||||
)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
||||
let (mut lr, mut lw) = self.stream.into_split();
|
||||
let (mut rr, mut rw) = remote_stream.split();
|
||||
|
||||
@@ -86,7 +86,7 @@ pub async fn tcp_stream_connect(saddr: &SocketAddr, config: &ConnectOpts) -> io:
|
||||
match time::timeout(Duration::from_secs(3), protect(path, socket.as_raw_fd())).await {
|
||||
Ok(Ok(..)) => {}
|
||||
Ok(Err(err)) => return Err(err),
|
||||
Err(..) => return Err(ErrorKind::TimedOut.into()),
|
||||
Err(..) => return Err(Error::new(ErrorKind::TimedOut, "protect() timeout")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! TCP stream for communicating with shadowsocks' proxy server
|
||||
|
||||
use std::{
|
||||
io,
|
||||
io::{self, ErrorKind},
|
||||
pin::Pin,
|
||||
task::{self, Poll},
|
||||
};
|
||||
@@ -13,6 +13,7 @@ use log::trace;
|
||||
use tokio::{
|
||||
io::{AsyncRead, AsyncWrite, ReadBuf},
|
||||
net::TcpStream,
|
||||
time,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -95,7 +96,26 @@ where
|
||||
A: Into<Address>,
|
||||
F: FnOnce(TcpStream) -> S,
|
||||
{
|
||||
let stream = OutboundTcpStream::connect_server_with_opts(&context, svr_cfg.external_addr(), opts).await?;
|
||||
let stream = match svr_cfg.timeout() {
|
||||
Some(d) => {
|
||||
match time::timeout(
|
||||
d,
|
||||
OutboundTcpStream::connect_server_with_opts(&context, svr_cfg.external_addr(), opts),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Ok(s)) => s,
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(..) => {
|
||||
return Err(io::Error::new(
|
||||
ErrorKind::TimedOut,
|
||||
format!("connect {} timeout", svr_cfg.addr()),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
None => OutboundTcpStream::connect_server_with_opts(&context, svr_cfg.external_addr(), opts).await?,
|
||||
};
|
||||
|
||||
trace!(
|
||||
"connected tcp remote {} (outbound: {}) with {:?}",
|
||||
|
||||
Reference in New Issue
Block a user