Skip to content

Commit 35f76e1

Browse files
committed
Add TransportErrorKind::ProxyError
Signed-off-by: Eval EXEC <execvy@gmail.com>
1 parent 999e855 commit 35f76e1

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

tentacle/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ pub enum TransportErrorKind {
99
/// IO error
1010
#[error("transport io error: `{0:?}`")]
1111
Io(#[from] IOError),
12+
/// Proxy server error
13+
#[error("proxy error: `{0:?}`")]
14+
ProxyError(IOError),
1215
/// Protocol not support
1316
#[error("multiaddr `{0:?}` is not supported")]
1417
NotSupported(Multiaddr),

tentacle/src/runtime/tokio_runtime/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,17 @@ where
154154
let socks5_config = socks5_config::parse(&proxy_url, onion_random_socks_auth)?;
155155

156156
let dial_addr: SocketAddr = socks5_config.proxy_url.parse().map_err(io::Error::other)?;
157-
let stream = connect_direct(dial_addr, socket_transformer).await?;
157+
let stream = connect_direct(dial_addr, socket_transformer)
158+
.await
159+
.map_err(|err| {
160+
io::Error::other(format!("connect to proxy:{}, failed: {:?}", dial_addr, err))
161+
})?;
158162

159163
super::proxy::socks5::establish_connection(stream, target_addr, socks5_config)
160164
.await
161-
.map_err(io::Error::other)
165+
.map_err(|err| {
166+
io::Error::other(format!("failed to establish connection to target:{}", err))
167+
})
162168
}
163169

164170
pub(crate) async fn connect(
@@ -174,7 +180,14 @@ pub(crate) async fn connect(
174180

175181
match proxy_url {
176182
Some(proxy_url) => {
177-
connect_by_proxy(target_addr, socket_transformer, proxy_url, false).await
183+
connect_by_proxy(target_addr, socket_transformer, proxy_url.clone(), false)
184+
.await
185+
.map_err(|err| {
186+
io::Error::new(
187+
io::ErrorKind::Other,
188+
format!("connect_by_proxy: {}, error: {}", proxy_url, err),
189+
)
190+
})
178191
}
179192
None => connect_direct(target_addr, socket_transformer).await,
180193
}

tentacle/src/transports/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,13 @@ mod os {
443443
) -> Result<TcpStream> {
444444
match crate::runtime::timeout(timeout, crate::runtime::connect(addr, tcp_config)).await {
445445
Err(_) => Err(TransportErrorKind::Io(io::ErrorKind::TimedOut.into())),
446-
Ok(res) => res.map_err(Into::into),
446+
Ok(res) => res.map_err(|err| {
447+
if err.to_string().contains("connect_by_proxy") {
448+
TransportErrorKind::ProxyError(err)
449+
} else {
450+
err.into()
451+
}
452+
}),
447453
}
448454
}
449455

@@ -461,7 +467,13 @@ mod os {
461467
.await
462468
{
463469
Err(_) => Err(TransportErrorKind::Io(io::ErrorKind::TimedOut.into())),
464-
Ok(res) => res.map_err(Into::into),
470+
Ok(res) => res.map_err(|err| {
471+
if err.to_string().contains("connect_by_proxy") {
472+
TransportErrorKind::ProxyError(err)
473+
} else {
474+
err.into()
475+
}
476+
}),
465477
}
466478
}
467479
}

0 commit comments

Comments
 (0)