use random ip from dns record instead of only first one

This commit is contained in:
sijie.sun
2024-08-04 23:10:31 +08:00
committed by Sijie.Sun
parent 2415cb211e
commit b4fbcd8d80
2 changed files with 9 additions and 2 deletions

View File

@@ -316,6 +316,7 @@ impl ManualConnectorManager {
ip_versions.push(IpVersion::Both);
} else {
let addrs = u.socket_addrs(|| Some(1000))?;
tracing::info!(?addrs, ?dead_url, "get ip from url done");
let mut has_ipv4 = false;
let mut has_ipv6 = false;
for addr in addrs {

View File

@@ -205,7 +205,7 @@ impl FromUrl for SocketAddr {
fn from_url(url: url::Url, ip_version: IpVersion) -> Result<Self, TunnelError> {
let addrs = url.socket_addrs(|| None)?;
tracing::debug!(?addrs, ?ip_version, ?url, "convert url to socket addrs");
let mut addrs = addrs
let addrs = addrs
.into_iter()
.filter(|addr| match ip_version {
IpVersion::V4 => addr.is_ipv4(),
@@ -213,7 +213,13 @@ impl FromUrl for SocketAddr {
IpVersion::Both => true,
})
.collect::<Vec<_>>();
addrs.pop().ok_or(TunnelError::NoDnsRecordFound(ip_version))
use rand::seq::SliceRandom;
// randomly select one address
addrs
.choose(&mut rand::thread_rng())
.copied()
.ok_or(TunnelError::NoDnsRecordFound(ip_version))
}
}