From b4fbcd8d80821d4faf4e045bf2bd7ccd6547f59e Mon Sep 17 00:00:00 2001 From: "sijie.sun" Date: Sun, 4 Aug 2024 23:10:31 +0800 Subject: [PATCH] use random ip from dns record instead of only first one --- easytier/src/connector/manual.rs | 1 + easytier/src/tunnel/mod.rs | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/easytier/src/connector/manual.rs b/easytier/src/connector/manual.rs index 6e756d3..944e9fa 100644 --- a/easytier/src/connector/manual.rs +++ b/easytier/src/connector/manual.rs @@ -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 { diff --git a/easytier/src/tunnel/mod.rs b/easytier/src/tunnel/mod.rs index cad6568..16bc29c 100644 --- a/easytier/src/tunnel/mod.rs +++ b/easytier/src/tunnel/mod.rs @@ -205,7 +205,7 @@ impl FromUrl for SocketAddr { fn from_url(url: url::Url, ip_version: IpVersion) -> Result { 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::>(); - 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)) } }