Skip to content

Commit 6d03c38

Browse files
committed
fix: try all resolved socket addrs for connection
For the dealer connection, only the first address was tried. For example when using a VPN, which doesn't support IPv6, the first address might still be an IPv6 address and thus fail. The IPv4 address later in the list is never tried. This relies on the tokio default to try all resolved addresses and use the first that was successful.
1 parent ea81314 commit 6d03c38

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

core/src/socket.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
1-
use std::{io, net::ToSocketAddrs};
1+
use std::io;
22

33
use tokio::net::TcpStream;
44
use url::Url;
55

66
use crate::proxytunnel;
77

88
pub async fn connect(host: &str, port: u16, proxy: Option<&Url>) -> io::Result<TcpStream> {
9-
let socket = if let Some(proxy_url) = proxy {
9+
if let Some(proxy_url) = proxy {
1010
info!("Using proxy \"{proxy_url}\"");
1111

12-
let socket_addr = proxy_url.socket_addrs(|| None).and_then(|addrs| {
13-
addrs.into_iter().next().ok_or_else(|| {
14-
io::Error::new(
15-
io::ErrorKind::NotFound,
16-
"Can't resolve proxy server address",
17-
)
18-
})
19-
})?;
20-
let socket = TcpStream::connect(&socket_addr).await?;
12+
let socket_addrs = proxy_url.socket_addrs(|| None)?;
13+
let socket = TcpStream::connect(&*socket_addrs).await?;
2114

22-
proxytunnel::proxy_connect(socket, host, &port.to_string()).await?
15+
proxytunnel::proxy_connect(socket, host, &port.to_string()).await
2316
} else {
24-
let socket_addr = (host, port).to_socket_addrs()?.next().ok_or_else(|| {
25-
io::Error::new(
26-
io::ErrorKind::NotFound,
27-
"Can't resolve access point address",
28-
)
29-
})?;
30-
31-
TcpStream::connect(&socket_addr).await?
32-
};
33-
Ok(socket)
17+
TcpStream::connect((host, port)).await
18+
}
3419
}

0 commit comments

Comments
 (0)