use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;
use tracing::warn;
use crate::constants;
use crate::error::{Aria2Error, Result};
use crate::http::socks_connector::NoProxyMatcher;
use crate::network::OutboundNetworkPolicy;
use crate::request::request_group::{DownloadOptions, HttpProxyOptions};
use aria2_protocol::proxy::{ProxyProtocol, ProxyUrl};
pub(crate) struct ResolvedNetworkAddresses {
pub(crate) target: Option<Vec<std::net::SocketAddr>>,
pub(crate) proxy: Option<Vec<std::net::SocketAddr>>,
}
pub(crate) fn range_client_pool_count(options: &DownloadOptions) -> usize {
let max_connections = options
.max_connection_per_server
.unwrap_or(constants::DEFAULT_MAX_CONNECTION_PER_SERVER as u16)
.clamp(1, constants::MAX_CONNECTION_PER_SERVER as u16) as usize;
let requested_sessions = options
.max_http2_sessions_per_server
.unwrap_or(constants::DEFAULT_HTTP2_SESSIONS_PER_SERVER as u16)
.clamp(1, constants::MAX_HTTP2_SESSIONS_PER_SERVER as u16)
as usize;
match options.http_version {
crate::http::HttpVersion::Http2 => requested_sessions,
crate::http::HttpVersion::Auto => max_connections.max(requested_sessions),
crate::http::HttpVersion::Http11 => max_connections,
}
}
pub(super) fn build_download_clients(
uri: &str,
options: &DownloadOptions,
client_tls: &crate::http::client_identity::ClientTlsConfig,
resolved_addresses: &ResolvedNetworkAddresses,
outbound_network_policy: &OutboundNetworkPolicy,
) -> Result<(Arc<reqwest::Client>, Arc<Vec<reqwest::Client>>)> {
let proxy_disabled = ![
options.http_proxy.as_deref(),
options.https_proxy.as_deref(),
options.all_proxy.as_deref(),
]
.into_iter()
.flatten()
.any(|proxy| !proxy.is_empty());
let proxy_origin = http_proxy_origin(uri, options);
let target_remotes = resolved_addresses
.target
.as_deref()
.filter(|addresses| !addresses.is_empty())
.map(<[_]>::to_vec)
.or_else(|| {
uri_host(uri).and_then(|host| {
host.parse::<IpAddr>().ok().map(|ip| {
vec![std::net::SocketAddr::new(
ip,
url::Url::parse(uri)
.ok()
.and_then(|url| url.port_or_known_default())
.unwrap_or(80),
)]
})
})
});
let local_address = if proxy_origin.is_some() {
let literal_proxy_remote = proxy_origin.as_ref().and_then(|(host, port)| {
host.parse::<IpAddr>()
.ok()
.map(|address| std::net::SocketAddr::new(address, *port))
});
let proxy_remotes = resolved_addresses
.proxy
.as_deref()
.filter(|addresses| !addresses.is_empty())
.map(<[_]>::to_vec)
.or_else(|| literal_proxy_remote.map(|remote| vec![remote]));
source_for_remotes(
outbound_network_policy,
proxy_remotes.as_deref().unwrap_or_default(),
)
.map_err(|error| {
Aria2Error::Fatal(crate::error::FatalError::Config(format!(
"Outbound network policy cannot serve HTTP proxy for {uri}: {error}"
)))
})?
} else {
let source = source_for_remotes(
outbound_network_policy,
target_remotes.as_deref().unwrap_or_default(),
)
.map_err(|error| {
Aria2Error::Fatal(crate::error::FatalError::Config(format!(
"Outbound network policy cannot serve {uri}: {error}"
)))
})?;
source.or_else(|| outbound_network_policy.addresses().into_iter().next())
};
let client = build_download_client(
uri,
options,
client_tls,
proxy_disabled,
local_address,
resolved_addresses,
)?;
let connect_timeout = http_connect_timeout(options);
let has_custom_tls = client_tls.requires_custom_client();
let has_resolved_target = resolved_addresses
.target
.as_deref()
.is_some_and(|addresses| !addresses.is_empty());
let session_count = range_client_pool_count(options);
let range_clients = if proxy_disabled && !has_resolved_target && !has_custom_tls {
crate::http::client_pool::get_bound_download_clients_with_version_and_timeout(
local_address,
options.http_accept_gzip,
session_count,
options.http_version,
connect_timeout,
)
} else {
let mut clients = Vec::with_capacity(session_count);
clients.push(client.as_ref().clone());
for _ in 1..session_count {
clients.push(
build_download_client(
uri,
options,
client_tls,
proxy_disabled,
local_address,
resolved_addresses,
)?
.as_ref()
.clone(),
);
}
Arc::new(clients)
};
Ok((client, range_clients))
}
pub(crate) fn http_proxy_origin(uri: &str, options: &DownloadOptions) -> Option<(String, u16)> {
options.http_proxy_options().proxy_for_uri(uri)
}
pub(crate) fn apply_proxy_options(
mut builder: reqwest::ClientBuilder,
options: &HttpProxyOptions,
) -> reqwest::ClientBuilder {
let no_proxy = options.no_proxy.as_deref();
if let Some(proxy) = options
.http_proxy
.as_deref()
.filter(|proxy| !proxy.is_empty())
{
builder = add_reqwest_proxy(
builder,
ProxyTarget::Http,
proxy,
options.credentials_for_scheme("http"),
no_proxy,
);
}
if let Some(proxy) = options
.https_proxy
.as_deref()
.filter(|proxy| !proxy.is_empty())
{
builder = add_reqwest_proxy(
builder,
ProxyTarget::Https,
proxy,
options.credentials_for_scheme("https"),
no_proxy,
);
}
if let Some(proxy) = options
.all_proxy
.as_deref()
.filter(|proxy| !proxy.is_empty())
{
match ProxyUrl::parse(proxy) {
Ok(parsed) => match parsed.protocol {
ProxyProtocol::Http
| ProxyProtocol::Https
| ProxyProtocol::Socks4
| ProxyProtocol::Socks5 => {
builder = add_reqwest_proxy(
builder,
ProxyTarget::All,
proxy,
options.credentials_for_scheme("all"),
no_proxy,
);
}
},
Err(_) => warn!(
proxy = %proxy_log_target(proxy),
"Ignoring invalid all-proxy configuration"
),
}
}
builder
}
pub(super) fn uri_host(uri: &str) -> Option<String> {
reqwest::Url::parse(uri).ok()?.host_str().map(str::to_owned)
}
pub(crate) fn http_connect_timeout(options: &DownloadOptions) -> Duration {
Duration::from_secs(
options
.connect_timeout
.unwrap_or(constants::HTTP_DEFAULT_CONNECT_TIMEOUT_SECS),
)
}
pub(super) fn source_for_remotes(
policy: &OutboundNetworkPolicy,
remotes: &[std::net::SocketAddr],
) -> std::io::Result<Option<IpAddr>> {
let mut last_error = None;
for remote in remotes {
match policy.source_for(*remote) {
Ok(source) => return Ok(source),
Err(error) => last_error = Some(error),
}
}
if let Some(error) = last_error {
return Err(error);
}
Ok(policy.addresses().into_iter().next())
}
fn apply_local_address(
builder: reqwest::ClientBuilder,
local_address: Option<IpAddr>,
) -> reqwest::ClientBuilder {
match local_address {
Some(address) => builder.local_address(address),
None => builder,
}
}
pub(super) fn build_download_client(
uri: &str,
options: &DownloadOptions,
client_tls: &crate::http::client_identity::ClientTlsConfig,
proxy_disabled: bool,
local_address: Option<IpAddr>,
resolved_addresses: &ResolvedNetworkAddresses,
) -> Result<Arc<reqwest::Client>> {
let has_custom_tls = client_tls.requires_custom_client();
let connect_timeout = http_connect_timeout(options);
if proxy_disabled {
if let Some(addresses) = resolved_addresses
.target
.as_deref()
.filter(|addresses| !addresses.is_empty())
{
let host = uri_host(uri).ok_or_else(|| {
Aria2Error::Fatal(crate::error::FatalError::Config(
"Unable to extract HTTP hostname for DNS cache override".to_string(),
))
})?;
let builder = reqwest::Client::builder()
.connect_timeout(connect_timeout)
.gzip(options.http_accept_gzip)
.user_agent(constants::USER_AGENT)
.redirect(reqwest::redirect::Policy::none())
.resolve_to_addrs(&host, addresses);
let builder = apply_local_address(builder, local_address);
let builder = crate::http::client_identity::apply(builder, client_tls)?;
return options
.http_version
.configure(crate::http::client_pool::configure_http2_download_client(
builder,
))
.build()
.map(Arc::new)
.map_err(|error| {
Aria2Error::Fatal(crate::error::FatalError::Config(format!(
"Failed to build HTTP client with DNS cache: {error}"
)))
});
}
if has_custom_tls {
let builder = reqwest::Client::builder()
.connect_timeout(connect_timeout)
.gzip(options.http_accept_gzip)
.user_agent(constants::USER_AGENT)
.redirect(reqwest::redirect::Policy::none())
.pool_max_idle_per_host(constants::HTTP_CLIENT_POOL_MAX_IDLE_PER_HOST)
.pool_idle_timeout(Some(Duration::from_secs(
constants::HTTP_CLIENT_POOL_IDLE_TIMEOUT_SECS,
)))
.tcp_keepalive(Some(Duration::from_secs(
constants::HTTP_DEFAULT_TCP_KEEPALIVE_SECS,
)));
let builder = apply_local_address(builder, local_address);
let builder = crate::http::client_identity::apply(builder, client_tls)?;
return options
.http_version
.configure(crate::http::client_pool::configure_http2_download_client(
builder,
))
.build()
.map(Arc::new)
.map_err(|error| {
Aria2Error::Fatal(crate::error::FatalError::Config(format!(
"Failed to build HTTP client: {error}"
)))
});
}
return Ok(
crate::http::client_pool::get_bound_client_with_version_and_timeout(
local_address,
options.http_accept_gzip,
options.http_version,
connect_timeout,
),
);
}
let mut builder = reqwest::Client::builder()
.connect_timeout(connect_timeout)
.gzip(options.http_accept_gzip)
.user_agent(constants::USER_AGENT)
.redirect(reqwest::redirect::Policy::none())
.pool_max_idle_per_host(constants::HTTP_DEFAULT_POOL_MAX_IDLE_PER_HOST)
.pool_idle_timeout(Some(Duration::from_secs(
constants::HTTP_DEFAULT_POOL_IDLE_TIMEOUT_SECS,
)))
.tcp_keepalive(Some(Duration::from_secs(
constants::HTTP_DEFAULT_TCP_KEEPALIVE_SECS,
)));
builder = apply_proxy_options(builder, &options.http_proxy_options());
if let (Some((proxy_host, _)), Some(proxy_addresses)) = (
http_proxy_origin(uri, options),
resolved_addresses
.proxy
.as_deref()
.filter(|addresses| !addresses.is_empty()),
) {
builder = builder.resolve_to_addrs(&proxy_host, proxy_addresses);
}
let builder = apply_local_address(builder, local_address);
let builder = crate::http::client_identity::apply(builder, client_tls)?;
options
.http_version
.configure(crate::http::client_pool::configure_http2_download_client(
builder,
))
.build()
.map(Arc::new)
.map_err(|error| {
Aria2Error::Fatal(crate::error::FatalError::Config(format!(
"Failed to build HTTP client: {error}"
)))
})
}
#[derive(Clone, Copy, Debug)]
pub(crate) enum ProxyTarget {
Http,
Https,
All,
}
pub(crate) fn build_reqwest_proxy(
target: ProxyTarget,
proxy_url: &str,
username: Option<&str>,
password: Option<&str>,
no_proxy: Option<&str>,
) -> std::result::Result<reqwest::Proxy, reqwest::Error> {
let mut proxy = if let Some(no_proxy) = no_proxy {
match target {
ProxyTarget::Http => reqwest::Proxy::http(proxy_url)?,
ProxyTarget::Https => reqwest::Proxy::https(proxy_url)?,
ProxyTarget::All => reqwest::Proxy::all(proxy_url)?,
};
let no_proxy = NoProxyMatcher::from_env_value(no_proxy);
let proxy_url = proxy_url.to_owned();
reqwest::Proxy::custom(move |url| {
let scheme_matches = match target {
ProxyTarget::Http => url.scheme() == "http",
ProxyTarget::Https => url.scheme() == "https",
ProxyTarget::All => matches!(url.scheme(), "http" | "https"),
};
let bypass = url
.host_str()
.is_some_and(|host| no_proxy.should_bypass_hostname(host));
(scheme_matches && !bypass).then(|| proxy_url.clone())
})
} else {
match target {
ProxyTarget::Http => reqwest::Proxy::http(proxy_url)?,
ProxyTarget::Https => reqwest::Proxy::https(proxy_url)?,
ProxyTarget::All => reqwest::Proxy::all(proxy_url)?,
}
};
if username.is_some() || password.is_some() {
let embedded = ProxyUrl::parse(proxy_url).ok();
let embedded_user = embedded
.as_ref()
.and_then(|proxy| proxy.username.as_deref())
.map(str::to_string);
let embedded_password = embedded.and_then(|proxy| proxy.password);
let effective_user = username.map(str::to_owned).or(embedded_user);
let effective_password = password
.map(str::to_owned)
.or(embedded_password)
.unwrap_or_default();
if let Some(user) = effective_user {
proxy = proxy.basic_auth(&user, &effective_password);
}
}
Ok(proxy)
}
pub(crate) fn add_reqwest_proxy(
builder: reqwest::ClientBuilder,
target: ProxyTarget,
proxy_url: &str,
credentials: (Option<String>, Option<String>),
no_proxy: Option<&str>,
) -> reqwest::ClientBuilder {
match build_reqwest_proxy(
target,
proxy_url,
credentials.0.as_deref(),
credentials.1.as_deref(),
no_proxy,
) {
Ok(proxy) => builder.proxy(proxy),
Err(error) => {
warn!(
proxy = %proxy_log_target(proxy_url),
?target,
error = %error.without_url(),
"Ignoring invalid HTTP proxy configuration"
);
builder
}
}
}
fn proxy_log_target(proxy_url: &str) -> String {
let Ok(proxy) = ProxyUrl::parse(proxy_url) else {
return "<invalid proxy URL>".to_string();
};
let scheme = match proxy.protocol {
ProxyProtocol::Http => "http",
ProxyProtocol::Https => "https",
ProxyProtocol::Socks4 => "socks4",
ProxyProtocol::Socks5 => "socks5",
};
let host = if proxy.host.parse::<std::net::Ipv6Addr>().is_ok() {
format!("[{}]", proxy.host)
} else {
proxy.host
};
format!("{scheme}://{host}:{}", proxy.port)
}
#[cfg(test)]
mod tests;