aria2-core 0.3.14

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Build the reqwest clients used by the HTTP download engine.
//!
//! This module owns HTTP-specific proxy selection, source-address binding,
//! TLS identity, and range-client pool configuration. The standalone
//! `aria2_protocol::http::HttpClient` remains an independent public client.

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};

/// Resolved endpoints used to pin direct and proxied HTTP clients to the
/// already-selected DNS answers.
pub(crate) struct ResolvedNetworkAddresses {
    pub(crate) target: Option<Vec<std::net::SocketAddr>>,
    pub(crate) proxy: Option<Vec<std::net::SocketAddr>>,
    pub(crate) resolver: Option<Arc<dyn reqwest::dns::Resolve>>,
}

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;
    // A reqwest client already keeps multiple HTTP/1.1 connections alive, so
    // one client is only needed for each pool's worth of connections. HTTP/2
    // still needs independent clients to create independent TCP sessions.
    let connections_per_client = constants::HTTP_DEFAULT_POOL_MAX_IDLE_PER_HOST.max(1);
    let http1_clients = max_connections.div_ceil(connections_per_client).max(1);
    match options.http_version {
        crate::http::HttpVersion::Http2 => requested_sessions,
        crate::http::HttpVersion::Auto => requested_sessions.max(http1_clients),
        crate::http::HttpVersion::Http11 => http1_clients,
    }
}

/// Build the default client and the independent clients used for concurrent
/// range requests, applying the outbound address policy to the actual first
/// hop (the origin or its HTTP proxy).
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))
}

/// Return the actual HTTP proxy endpoint used for an HTTP(S) URI.
///
/// The endpoint, rather than the origin, determines the address family of the
/// first outbound socket. The command factory uses this to resolve proxy DNS
/// before the synchronous reqwest client is built.
pub(crate) fn http_proxy_origin(uri: &str, options: &DownloadOptions) -> Option<(String, u16)> {
    options.http_proxy_options().proxy_for_uri(uri)
}

/// Apply the task's HTTP proxy routes to a reqwest client.
///
/// Shared by ordinary HTTP downloads and BitTorrent WebSeeds so scheme
/// precedence, credentials, bypass rules, and invalid-proxy handling agree.
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_dns_resolver(builder, resolved_addresses.resolver.as_ref());
            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 || resolved_addresses.resolver.is_some() {
            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_dns_resolver(builder, resolved_addresses.resolver.as_ref());
            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}"
                    )))
                });
        }

        if resolved_addresses.resolver.is_none() {
            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)
        // Redirects are handled by SequentialDownloader so direct, DNS-pinned,
        // and proxied clients share one URI/retry seam.
        .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());
    builder = apply_dns_resolver(builder, resolved_addresses.resolver.as_ref());

    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}"
            )))
        })
}

fn apply_dns_resolver(
    builder: reqwest::ClientBuilder,
    resolver: Option<&Arc<dyn reqwest::dns::Resolve>>,
) -> reqwest::ClientBuilder {
    match resolver {
        Some(resolver) => builder.dns_resolver(Arc::clone(resolver)),
        None => builder,
    }
}

#[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 {
        // Reqwest's built-in NO_PROXY rules treat an unprefixed domain as a
        // suffix, while aria2's matcher treats it as an exact hostname. Use
        // the same matcher for proxy selection and actual request routing.
        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)?,
        }
    };

    // Preserve credentials embedded in the proxy URL unless an option
    // explicitly overrides them, matching AbstractCommand::makeProxyUri().
    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;