client: Fix an issue where non-IP proxy URLs didn’t resolve correctly (#32664)

If the proxy URL is in the form of `example.com` instead of a raw IP
address, and `example.com` isn't a well-known domain, then the default
URL resolution can fail.

The test setup:

A Linux machine runs a CoreDNS server with a custom entry: `10.254.7.38
example.com`. On a Windows machine, if the proxy URL is set to
`example.com`, the resolved address does **not** end up being
`10.254.7.38`.

Using `hickory_resolver` for more advanced DNS resolution fixes this
issue.


Release Notes:

- Fixed proxy URL resolution when using custom DNS entries.
This commit is contained in:
张小白 2025-06-13 15:17:49 +08:00 committed by GitHub
parent 20793fc251
commit bc68455320
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 154 additions and 8 deletions

View file

@ -1,5 +1,7 @@
//! socks proxy
use std::net::SocketAddr;
use anyhow::{Context as _, Result};
use http_client::Url;
use tokio::net::TcpStream;
@ -8,6 +10,8 @@ use tokio_socks::{
tcp::{Socks4Stream, Socks5Stream},
};
use crate::proxy::SYSTEM_DNS_RESOLVER;
use super::AsyncReadWrite;
/// Identification to a Socks V4 Proxy
@ -73,12 +77,14 @@ pub(super) async fn connect_socks_proxy_stream(
};
let rpc_host = match (rpc_host, local_dns) {
(TargetAddr::Domain(domain, port), true) => {
let ip_addr = tokio::net::lookup_host((domain.as_ref(), port))
let ip_addr = SYSTEM_DNS_RESOLVER
.lookup_ip(domain.as_ref())
.await
.with_context(|| format!("Failed to lookup domain {}", domain))?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("Failed to lookup domain {}", domain))?;
TargetAddr::Ip(ip_addr)
TargetAddr::Ip(SocketAddr::new(ip_addr, port))
}
(rpc_host, _) => rpc_host,
};