aria2-core 0.3.13

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
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use super::DownloadEngine;
use crate::config::OptionValue;
use crate::dns::dns_cache::DnsCache;
use crate::network::OutboundNetworkPolicy;
use crate::rate_limiter::RateLimiterConfig;
#[cfg(feature = "bittorrent")]
use crate::request::request_group::DownloadOptions;
use crate::request::request_group_man::RequestGroupMan;

impl DownloadEngine {
    /// Construct an engine with process-wide settings applied before its loop
    /// starts and before protocol-owned sockets are created.
    ///
    /// The CLI, C API, and Rust library callers use this boundary to apply
    /// protocol-facing transports, resolver settings, scheduler limits, and
    /// server statistics before any process-owned services start.
    pub async fn from_process_options(
        options: &HashMap<String, OptionValue>,
        request_man: Arc<RequestGroupMan>,
    ) -> Self {
        let outbound_spec = option_string(options, "interface")
            .or_else(|| option_string(options, "multiple-interface"));
        let outbound_policy = match outbound_spec {
            Some(spec) => match OutboundNetworkPolicy::resolve_spec(spec).await {
                Ok(policy) => Arc::new(policy),
                Err(error) => {
                    tracing::warn!(%error, %spec, "Ignoring invalid network interface configuration");
                    Arc::new(OutboundNetworkPolicy::direct())
                }
            },
            None => Arc::new(OutboundNetworkPolicy::direct()),
        };
        #[cfg(feature = "bittorrent")]
        let mut engine = Self::with_lpd_manager(build_lpd_manager(options, &outbound_policy));
        #[cfg(not(feature = "bittorrent"))]
        let mut engine = Self::new();
        engine.set_outbound_network_policy(Arc::clone(&outbound_policy));

        let dns_timeout = option_i64(options, "dns-timeout")
            .filter(|seconds| *seconds > 0)
            .map(|seconds| Duration::from_secs(seconds as u64))
            .unwrap_or(Duration::from_secs(30));
        let dns_servers = option_string(options, "async-dns-server").and_then(|value| {
            match DnsCache::parse_dns_server_list(value) {
                Ok(servers) => Some(servers),
                Err(error) => {
                    tracing::warn!(%error, "Ignoring invalid async-dns-server configuration");
                    None
                }
            }
        });
        engine.set_dns_config(dns_timeout, dns_servers);

        #[cfg(feature = "bittorrent")]
        engine.configure_bittorrent_process_options(options);

        let server_stat_timeout = option_i64(options, "server-stat-timeout")
            .filter(|seconds| *seconds >= 0)
            .map(|seconds| seconds as u64)
            .unwrap_or(24 * 60 * 60);
        let server_stat_input = option_string(options, "server-stat-if").map(PathBuf::from);
        let server_stat_output = option_string(options, "server-stat-of").map(PathBuf::from);
        let server_stat_interval = option_i64(options, "save-server-stat-interval")
            .filter(|seconds| *seconds > 0)
            .map(|seconds| Duration::from_secs(seconds as u64));
        engine.set_server_stat_timeout(server_stat_timeout);
        engine.set_server_stat_persistence(
            server_stat_input,
            server_stat_output,
            server_stat_interval,
        );

        if let Some(max) =
            option_i64(options, "max-concurrent-downloads").filter(|value| *value >= 0)
            && let Ok(max) = u32::try_from(max)
        {
            request_man.set_max_concurrent(max);
        }
        let download_limit =
            option_i64(options, "max-overall-download-limit").and_then(non_zero_limit);
        let upload_limit = option_i64(options, "max-overall-upload-limit").and_then(non_zero_limit);
        if download_limit.is_some() || upload_limit.is_some() {
            engine.set_global_rate_limiter(RateLimiterConfig::new(download_limit, upload_limit));
            request_man.set_global_speed_limit(download_limit, upload_limit);
        }
        engine.set_request_group_man(request_man);
        engine
    }

    #[cfg(feature = "bittorrent")]
    fn configure_bittorrent_process_options(&mut self, options: &HashMap<String, OptionValue>) {
        use crate::engine::bittorrent::tracker::source_fetcher::CoreTrackerSourceFetcher;
        use aria2_protocol::bittorrent::tracker::public_list::{
            DEFAULT_TRACKER_SOURCE_TIMEOUT, DEFAULT_TRACKER_UPDATE_INTERVAL,
            MAX_TRACKER_SOURCE_CONCURRENCY, TrackerCatalogConfig,
        };

        if let Some(max_open_files) = option_i64(options, "bt-max-open-files")
            .and_then(|value| usize::try_from(value).ok())
            .filter(|value| *value > 0)
        {
            self.open_file_budget.set_max_open_files(max_open_files);
        }

        self.set_utp_socket_options(crate::config::utp_socket_options_from_values(
            option_i64(options, "bt-utp-max-connections"),
            option_i64(options, "bt-utp-connect-timeout"),
            option_i64(options, "bt-utp-idle-timeout"),
            option_i64(options, "bt-utp-keepalive-interval"),
        ));
        if let Some(seconds) = option_i64(options, "bt-incoming-peer-handshake-timeout")
            .and_then(|seconds| u64::try_from(seconds).ok())
        {
            self.set_incoming_peer_handshake_timeout(Duration::from_secs(seconds));
        }

        let sources = match options.get("bt-tracker-source") {
            Some(OptionValue::List(values)) => values.clone(),
            Some(OptionValue::Str(value)) => value
                .split([',', '\n'])
                .map(str::trim)
                .filter(|value| !value.is_empty())
                .map(str::to_string)
                .collect(),
            _ => Vec::new(),
        };
        let update_interval = option_i64(options, "bt-tracker-update-interval")
            .filter(|seconds| *seconds > 0)
            .map(|seconds| Duration::from_secs(seconds as u64))
            .unwrap_or(DEFAULT_TRACKER_UPDATE_INTERVAL);
        let source_timeout = option_i64(options, "bt-tracker-source-timeout")
            .and_then(|seconds| u64::try_from(seconds).ok())
            .filter(|seconds| *seconds > 0)
            .map(Duration::from_secs)
            .unwrap_or(DEFAULT_TRACKER_SOURCE_TIMEOUT);
        let tracker_config = TrackerCatalogConfig {
            enabled: option_bool(options, "enable-public-trackers").unwrap_or(true),
            sources,
            update_interval,
        };
        let option_strings = options
            .iter()
            .map(|(name, value)| (name.clone(), value.to_string()))
            .collect();
        let download_options = DownloadOptions::from_option_strings(&option_strings);
        let mut dht_options =
            crate::engine::bittorrent::dht::config::DhtEngineOptions::from(&download_options);
        let default_peer_storage = aria2_protocol::bittorrent::dht::DhtPeerStorageConfig::default();
        dht_options.dht_max_peer_info_hashes = option_usize(options, "dht-max-peer-info-hashes")
            .unwrap_or(default_peer_storage.max_info_hashes);
        dht_options.dht_max_peers_per_info_hash =
            option_usize(options, "dht-max-peers-per-info-hash")
                .unwrap_or(default_peer_storage.max_peers_per_info_hash);
        dht_options.dht_max_items = option_usize(options, "dht-max-items")
            .unwrap_or(aria2_protocol::bittorrent::dht::DEFAULT_DHT_MAX_ITEMS);
        self.bt_registry
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .set_global_dht_options(Arc::new(dht_options));
        let source_fetcher = Arc::new(CoreTrackerSourceFetcher::new(
            Arc::clone(&self.dns_cache),
            Arc::clone(&self.outbound_network_policy),
            download_options,
        ));
        let tracker_catalog = Arc::new(
            aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList::new_with_config_and_fetcher(
                tracker_config,
                source_fetcher,
            ),
        );
        tracker_catalog.set_source_fetch_concurrency_limit(
            option_i64(options, "bt-tracker-source-concurrency")
                .and_then(|value| usize::try_from(value).ok())
                .unwrap_or(MAX_TRACKER_SOURCE_CONCURRENCY),
        );
        self.public_tracker_catalog = tracker_catalog;
        self.set_public_tracker_source_timeout(source_timeout);
    }
}

#[cfg(feature = "bittorrent")]
fn build_lpd_manager(
    options: &HashMap<String, OptionValue>,
    outbound_policy: &OutboundNetworkPolicy,
) -> Arc<crate::engine::bittorrent::discovery::lpd::LpdManager> {
    use crate::engine::bittorrent::discovery::lpd::LpdManager;

    let lpd_port = option_i64(options, "lpd-listen-port")
        .and_then(|port| u16::try_from(port).ok())
        .unwrap_or(crate::constants::LPD_PORT);
    let lpd_interface = match options.get("bt-lpd-interface") {
        Some(OptionValue::Str(value)) if !value.trim().is_empty() => {
            match value.parse::<std::net::Ipv4Addr>() {
                Ok(address) => Some(address),
                Err(error) => {
                    tracing::warn!(%error, %value, "Ignoring invalid bt-lpd-interface");
                    None
                }
            }
        }
        _ => None,
    }
    .or_else(|| {
        outbound_policy
            .addresses()
            .into_iter()
            .find_map(|address| match address {
                std::net::IpAddr::V4(address) => Some(address),
                std::net::IpAddr::V6(_) => None,
            })
    });
    match LpdManager::with_interval_and_interface_and_port(
        crate::constants::LPD_DEFAULT_ANNOUNCE_INTERVAL_SECS,
        lpd_interface,
        lpd_port,
    ) {
        Ok(manager) => Arc::new(manager),
        Err(error) => {
            tracing::warn!(%error, "Using default LPD manager because configured LPD setup failed");
            Arc::new(LpdManager::new())
        }
    }
}

fn non_zero_limit(value: i64) -> Option<u64> {
    (value > 0).then_some(value as u64)
}

fn option_i64(options: &HashMap<String, OptionValue>, name: &str) -> Option<i64> {
    match options.get(name)? {
        OptionValue::Int(value) => Some(*value),
        OptionValue::Usize(value) => i64::try_from(*value).ok(),
        _ => None,
    }
}

#[cfg(feature = "bittorrent")]
fn option_usize(options: &HashMap<String, OptionValue>, name: &str) -> Option<usize> {
    match options.get(name)? {
        OptionValue::Int(value) => usize::try_from(*value).ok(),
        OptionValue::Usize(value) => Some(*value),
        _ => None,
    }
}

#[cfg(all(test, feature = "bittorrent"))]
mod tests {
    use super::*;

    #[tokio::test]
    async fn process_options_initialize_shared_bt_and_dht_configuration() {
        let options = HashMap::from([
            ("bt-max-open-files".to_string(), OptionValue::Int(17)),
            (
                "bt-tracker-source-concurrency".to_string(),
                OptionValue::Int(2),
            ),
            ("dht-message-timeout".to_string(), OptionValue::Int(23)),
            (
                "dht-listen-port".to_string(),
                OptionValue::Str("49001-49002".to_string()),
            ),
            (
                "dht-file-path".to_string(),
                OptionValue::Str("process-dht.dat".to_string()),
            ),
            (
                "dht-max-peer-info-hashes".to_string(),
                OptionValue::Int(1200),
            ),
            (
                "dht-max-peers-per-info-hash".to_string(),
                OptionValue::Int(37),
            ),
            ("dht-max-items".to_string(), OptionValue::Int(512)),
        ]);
        let engine =
            DownloadEngine::from_process_options(&options, Arc::new(RequestGroupMan::new())).await;

        assert_eq!(engine.open_file_budget.max_open_files(), 17);
        assert_eq!(
            engine
                .public_tracker_catalog()
                .source_fetch_concurrency_limit(),
            2
        );
        let dht_options = engine
            .bt_registry
            .read()
            .expect("BT registry should be readable")
            .global_dht_options();
        assert_eq!(dht_options.dht_message_timeout, 23);
        assert_eq!(dht_options.dht_listen_port.as_deref(), Some("49001-49002"));
        assert_eq!(
            dht_options.dht_file_path.as_deref(),
            Some("process-dht.dat")
        );
        assert_eq!(dht_options.dht_max_peer_info_hashes, 1200);
        assert_eq!(dht_options.dht_max_peers_per_info_hash, 37);
        assert_eq!(dht_options.dht_max_items, 512);
    }

    #[tokio::test]
    async fn process_options_apply_utp_connection_limit_before_transport_creation() {
        let options = HashMap::from([
            ("bt-utp-max-connections".to_string(), OptionValue::Int(1)),
            ("bt-utp-connect-timeout".to_string(), OptionValue::Int(14)),
            ("bt-utp-idle-timeout".to_string(), OptionValue::Int(46)),
            (
                "bt-utp-keepalive-interval".to_string(),
                OptionValue::Int(19),
            ),
        ]);
        let engine =
            DownloadEngine::from_process_options(&options, Arc::new(RequestGroupMan::new())).await;
        let reservation = tokio::net::UdpSocket::bind("127.0.0.1:0")
            .await
            .expect("reserve a local endpoint");
        let address = reservation.local_addr().unwrap();
        drop(reservation);

        let transport = engine
            .bt_listener
            .register_utp_transport(address)
            .await
            .expect("transport created after process options should use configured limits");
        let remote = tokio::net::UdpSocket::bind("127.0.0.1:0")
            .await
            .expect("bind a local remote endpoint");
        let endpoint = remote.local_addr().unwrap();

        let _first = transport
            .connect(endpoint)
            .await
            .expect("the configured single connection should be admitted");
        assert!(transport.connect(endpoint).await.is_err());

        engine.bt_listener.shutdown().await;
    }

    #[tokio::test]
    async fn process_options_apply_incoming_handshake_timeout_to_live_listener() {
        use crate::engine::bittorrent::peer::listener::BtPeerRouteConfig;
        use crate::engine::bittorrent::peer::storage::DefaultPeerStorage;
        use std::net::{IpAddr, Ipv4Addr};
        use std::sync::Mutex;
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let options = HashMap::from([(
            "bt-incoming-peer-handshake-timeout".to_string(),
            OptionValue::Int(1),
        )]);
        let engine =
            DownloadEngine::from_process_options(&options, Arc::new(RequestGroupMan::new())).await;
        let storage = Arc::new(Mutex::new(DefaultPeerStorage::new()));
        let (port, _incoming, _route) = engine
            .bt_listener
            .register(BtPeerRouteConfig {
                bind_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
                ports: vec![0],
                info_hash: [101; 20],
                info_hash_v2: None,
                local_peer_id: [1; 20],
                caretaker_id: 101,
                max_peers: 1,
                peer_storage: storage,
                crypto_policy: Default::default(),
                dht_enabled: false,
            })
            .await
            .unwrap();

        let mut stream = tokio::net::TcpStream::connect((Ipv4Addr::LOCALHOST, port))
            .await
            .unwrap();
        stream.write_all(b"x").await.unwrap();
        let mut byte = [0; 1];
        let read = tokio::time::timeout(Duration::from_secs(2), stream.read(&mut byte)).await;
        engine.bt_listener.shutdown().await;
        assert_eq!(
            read.expect("startup timeout should close the incomplete handshake")
                .unwrap(),
            0
        );
    }
}

fn option_string<'a>(options: &'a HashMap<String, OptionValue>, name: &str) -> Option<&'a str> {
    match options.get(name)? {
        OptionValue::Str(value) => Some(value),
        _ => None,
    }
}

#[cfg(feature = "bittorrent")]
fn option_bool(options: &HashMap<String, OptionValue>, name: &str) -> Option<bool> {
    match options.get(name)? {
        OptionValue::Bool(value) => Some(*value),
        _ => None,
    }
}