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
mod discovery;
mod metadata;
mod metadata_source;

use async_trait::async_trait;
use std::sync::Arc;
use std::time::Duration;
use tracing::{info, warn};

use crate::engine::bittorrent::dht::engine_set::DhtEngineSet;
use crate::engine::command::{Command, CommandStatus};
use crate::engine::download_event_hooks::{DownloadEventHooks, MetadataResolvedEvent};
use crate::error::{Aria2Error, FatalError, Result};
use crate::rate_limiter::RateLimiter;
use crate::request::request_group::{DownloadOptions, GroupId, RequestGroup};
use crate::util::rwlock_ext::RwLockRecover;

const MAX_MAGNET_TRACKERS_TO_TRY: usize = 10;
const MAX_MAGNET_TRACKER_PEERS: usize = 50;
const MAX_MAGNET_METADATA_PEERS_PER_SOURCE: usize = 20;

pub struct MagnetDownloadCommand {
    group: Arc<std::sync::RwLock<RequestGroup>>,
    magnet_link: Arc<aria2_protocol::bittorrent::magnet::MagnetLink>,
    output_path: std::path::PathBuf,
    started: bool,
    completed_bytes: u64,
    metadata_complete: bool,
    local_peer_id: [u8; 20],
    #[cfg(feature = "bittorrent")]
    initial_peer_swarm: Option<crate::engine::bittorrent::peer::message_handler::PeerSwarm>,
    dht_engines: DhtEngineSet,
    /// Process-wide rate limiter from `DownloadEngine::global_limiter`.
    /// Carried through to the internally-created `BtDownloadCommand`.
    global_limiter: Option<RateLimiter>,
    outbound_network_policy: Arc<crate::network::OutboundNetworkPolicy>,
    dns_cache: Arc<tokio::sync::Mutex<crate::dns::dns_cache::DnsCache>>,
    #[cfg(feature = "bittorrent")]
    public_tracker_catalog:
        Option<Arc<aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList>>,
    #[cfg(feature = "bittorrent")]
    bt_listener: Option<Arc<crate::engine::bittorrent::peer::listener::BtPeerListenerManager>>,
    #[cfg(feature = "bittorrent")]
    bt_registry: Option<Arc<std::sync::RwLock<crate::engine::bittorrent::registry::BtRegistry>>>,
    #[cfg(feature = "bittorrent")]
    lpd_manager: Option<Arc<crate::engine::bittorrent::discovery::lpd::LpdManager>>,
    #[cfg(feature = "bittorrent")]
    open_file_budget: Option<Arc<crate::filesystem::open_file_budget::OpenFileBudget>>,
}

impl MagnetDownloadCommand {
    async fn execute_handed_off_bt_command(
        command: &mut crate::engine::bittorrent::download::command::BtDownloadCommand,
    ) -> Result<()> {
        let result = command.execute().await;
        command.shutdown().await;
        result
    }

    pub fn new(
        gid: GroupId,
        magnet_uri: &str,
        options: &DownloadOptions,
        output_dir: Option<&str>,
    ) -> Result<Self> {
        let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
            gid,
            vec![magnet_uri.to_string()],
            options.clone(),
        )));
        Self::new_with_group(group, output_dir)
    }

    /// Set the process-wide rate limiter (from `DownloadEngine::global_limiter`).
    ///
    /// When set, the internally-created `BtDownloadCommand` is given this
    /// limiter so the resolved torrent's piece writes share the global ceiling.
    pub fn set_global_limiter(&mut self, limiter: RateLimiter) {
        self.global_limiter = Some(limiter);
    }

    pub fn set_outbound_network_policy(
        &mut self,
        policy: Arc<crate::network::OutboundNetworkPolicy>,
    ) {
        self.outbound_network_policy = policy;
    }

    pub(crate) fn set_dns_cache(
        &mut self,
        dns_cache: Arc<tokio::sync::Mutex<crate::dns::dns_cache::DnsCache>>,
    ) {
        self.dns_cache = dns_cache;
    }

    /// Create a magnet download command that reuses an externally-managed
    /// `RequestGroup` (e.g. from the engine's promotion flow).
    ///
    /// The first URI in the group is treated as the magnet link. Output
    /// directory falls back to the group's `DownloadOptions` when not
    /// explicitly overridden. The group's existing GID and progress counters
    /// are reused.
    pub fn new_with_group(
        group: Arc<std::sync::RwLock<RequestGroup>>,
        output_dir: Option<&str>,
    ) -> Result<Self> {
        let (magnet_uri, options) = {
            let g = group.recover();
            let uri = g.uris().first().cloned().ok_or_else(|| {
                Aria2Error::Fatal(FatalError::Config(
                    "RequestGroup has no URIs for magnet download".into(),
                ))
            })?;
            let opts = g.options_arc();
            (uri, opts)
        };

        let magnet_link = aria2_protocol::bittorrent::magnet::MagnetLink::parse(&magnet_uri)
            .map_err(|e| Aria2Error::MagnetParse(format!("Invalid magnet link: {}", e)))?;

        let dir = output_dir
            .map(|d| d.to_string())
            .or_else(|| options.dir.clone())
            .unwrap_or_else(|| ".".to_string());

        let filename = options
            .out
            .as_deref()
            .map(validate_magnet_output_name)
            .transpose()?
            .or_else(|| {
                magnet_link
                    .display_name
                    .as_deref()
                    .and_then(sanitize_magnet_display_name)
            })
            .unwrap_or_else(|| "magnet_download".to_string());
        let path = std::path::PathBuf::from(&dir).join(&filename);

        {
            let group = group.recover();
            group.set_output_name(filename.clone());
            group.set_resolved_output_path(path.to_string_lossy().into_owned());
        }

        info!(
            "MagnetDownloadCommand created (shared group): {} -> {} (hash={})",
            filename,
            path.display(),
            magnet_link.info_hash_hex()
        );

        Ok(Self {
            group,
            magnet_link: Arc::new(magnet_link),
            output_path: path,
            started: false,
            completed_bytes: 0,
            metadata_complete: false,
            local_peer_id: aria2_protocol::bittorrent::peer::id::generate_peer_id_with_prefix(
                &options.peer_id_prefix,
            ),
            #[cfg(feature = "bittorrent")]
            initial_peer_swarm: None,
            dht_engines: DhtEngineSet::default(),
            global_limiter: None,
            outbound_network_policy: Arc::new(crate::network::OutboundNetworkPolicy::direct()),
            dns_cache: Arc::new(tokio::sync::Mutex::new(
                crate::dns::dns_cache::DnsCache::new(),
            )),
            #[cfg(feature = "bittorrent")]
            public_tracker_catalog: None,
            #[cfg(feature = "bittorrent")]
            bt_listener: None,
            #[cfg(feature = "bittorrent")]
            bt_registry: None,
            #[cfg(feature = "bittorrent")]
            lpd_manager: None,
            #[cfg(feature = "bittorrent")]
            open_file_budget: None,
        })
    }

    #[cfg(feature = "bittorrent")]
    pub fn set_public_tracker_catalog(
        &mut self,
        catalog: Arc<aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList>,
    ) {
        self.public_tracker_catalog = Some(catalog);
    }

    #[cfg(feature = "bittorrent")]
    pub fn set_bt_listener(
        &mut self,
        listener: Arc<crate::engine::bittorrent::peer::listener::BtPeerListenerManager>,
    ) {
        self.bt_listener = Some(listener);
    }

    #[cfg(feature = "bittorrent")]
    pub fn set_bt_registry(
        &mut self,
        registry: Arc<std::sync::RwLock<crate::engine::bittorrent::registry::BtRegistry>>,
    ) {
        self.bt_registry = Some(registry);
    }

    #[cfg(feature = "bittorrent")]
    pub fn set_lpd_manager(
        &mut self,
        manager: Arc<crate::engine::bittorrent::discovery::lpd::LpdManager>,
    ) {
        self.lpd_manager = Some(manager);
    }

    #[cfg(feature = "bittorrent")]
    pub(crate) fn set_open_file_budget(
        &mut self,
        budget: Arc<crate::filesystem::open_file_budget::OpenFileBudget>,
    ) {
        self.open_file_budget = Some(budget);
    }

    pub fn group(&self) -> std::sync::RwLockReadGuard<'_, RequestGroup> {
        self.group.recover()
    }

    /// BEP 0027 (Private Torrent) enforcement after metadata exchange.
    ///
    /// For magnet links, a DHT engine may be borrowed or started BEFORE metadata arrives
    /// because DHT-based peer discovery is required to find peers that can
    /// serve the metadata via BEP 0010 (Extension for Peers to Send Metadata
    /// File). Once the metadata has been fetched, if the torrent's `private`
    /// flag is set, this torrent must stop using DHT to comply with BEP 0027,
    /// which forbids DHT, PEX, and LPD for private torrents. A process-global
    /// engine borrowed by this command remains alive for other torrents.
    ///
    /// This method parses the fetched `torrent_bytes`, checks `is_private()`,
    /// and if true, releases the magnet's DHT handle. An exclusively owned
    /// temporary engine is shut down; a shared process engine is left running.
    /// The downstream `BtDownloadCommand` then enforces BEP 0027 and will not
    /// use or start DHT for this torrent.
    ///
    /// Extracted as a standalone async method so the policy logic can be
    /// unit tested without mocking the BEP 0010 metadata exchange network I/O.
    async fn enforce_bep0027_after_metadata(&mut self, torrent_bytes: &[u8]) -> Result<()> {
        let is_private =
            aria2_protocol::bittorrent::torrent::parser::TorrentMeta::parse(torrent_bytes)
                .map_err(|e| {
                    Aria2Error::BittorrentParse(format!("Fetched metadata parse failed: {}", e))
                })?
                .is_private();

        if is_private {
            info!("Private torrent detected after metadata exchange: shutting down DHT (BEP 0027)");
            self.shutdown_dht_engine().await;
            // shutdown_dht_engine empties this family-scoped set, so the
            // downstream BtDownloadCommand cannot accidentally reuse it.
        }

        Ok(())
    }
}

fn validate_magnet_output_name(name: &str) -> Result<String> {
    let path = std::path::Path::new(name);
    let is_single_component = path.components().count() == 1;
    if name.is_empty() || !is_single_component || name == "." || name == ".." {
        return Err(Aria2Error::Fatal(FatalError::Config(format!(
            "magnet output name must be a file name, got '{name}'"
        ))));
    }
    Ok(name.to_owned())
}

fn sanitize_magnet_display_name(name: &str) -> Option<String> {
    validate_magnet_output_name(name).ok()
}

#[async_trait]
impl Command for MagnetDownloadCommand {
    async fn shutdown(&mut self) {
        #[cfg(feature = "bittorrent")]
        if let Some(mut swarm) = self.initial_peer_swarm.take() {
            swarm.shutdown_all().await;
        }
        self.shutdown_dht_engine().await;
    }

    async fn execute(&mut self) -> Result<()> {
        if !self.started {
            self.group.recover_mut().start()?;
            self.started = true;
        }

        let ml = Arc::clone(&self.magnet_link);

        info!(
            "Magnet download: hash={}, name={:?}",
            ml.info_hash_hex(),
            ml.display_name
        );

        if let Some(parent) = self.output_path.parent()
            && !parent.exists()
        {
            std::fs::create_dir_all(parent).map_err(|e| {
                Aria2Error::Fatal(FatalError::Config(format!("mkdir failed: {}", e)))
            })?;
        }

        let (load_saved_metadata, save_metadata, metadata_only) = {
            let group = self.group.recover();
            (
                group.options().bt_load_saved_metadata,
                group.options().bt_save_metadata,
                group.options().bt_metadata_only,
            )
        };

        let torrent_bytes = if load_saved_metadata
            && let Some(metadata) = self.load_saved_metadata_for_magnet(&ml)
        {
            metadata
        } else {
            self.fetch_magnet_metadata(&ml).await?
        };

        let torrent_bytes =
            aria2_protocol::bittorrent::torrent::metainfo::normalize_magnet_metadata(
                &torrent_bytes,
                &ml.trackers,
            )
            .map_err(|error| {
                Aria2Error::Fatal(FatalError::Config(format!(
                    "Fetched magnet metadata is invalid: {error}"
                )))
            })?;

        let torrent_bytes = aria2_protocol::bittorrent::torrent::metainfo::merge_magnet_web_seeds(
            &torrent_bytes,
            &ml.ws,
        )
        .map_err(|error| {
            Aria2Error::Fatal(FatalError::Config(format!(
                "Fetched metadata could not include magnet web seeds: {error}"
            )))
        })?;

        Self::metadata_matches_magnet(&ml, &torrent_bytes).map_err(|error| {
            Aria2Error::Fatal(FatalError::Config(format!(
                "Fetched metadata does not match magnet: {}",
                error
            )))
        })?;

        info!("Fetched torrent metadata: {} bytes", torrent_bytes.len());

        // BEP 0027 (Private Torrent): DHT was started before metadata exchange
        // for peer discovery. Now that the metadata is available, parse it and
        // shut down DHT if the torrent is private. The downstream
        // BtDownloadCommand (created from the same bytes) will also enforce
        // BEP 0027 by refusing to start its own DHT when is_private is set.
        self.enforce_bep0027_after_metadata(&torrent_bytes).await?;

        if save_metadata {
            let path = self.saved_metadata_path_for_magnet(&ml);
            match Self::save_metadata_file(&path, &torrent_bytes) {
                Ok(true) => info!(path = %path.display(), "Saved BitTorrent metadata"),
                Ok(false) => {
                    info!(path = %path.display(), "BitTorrent metadata file already exists; keeping it")
                }
                Err(error) => {
                    warn!(path = %path.display(), %error, "Failed to save BitTorrent metadata")
                }
            }
        }

        if metadata_only {
            #[cfg(feature = "bittorrent")]
            if let Some(mut swarm) = self.initial_peer_swarm.take() {
                swarm.shutdown_all().await;
            }
            self.shutdown_dht_engine().await;
            self.group.recover_mut().complete()?;
            self.metadata_complete = true;
            DownloadEventHooks::shared().notify_metadata_resolved(MetadataResolvedEvent::new(
                self.group.recover().gid(),
                Vec::new(),
            ));
            info!("Magnet metadata download complete; payload download skipped");
            return Ok(());
        }

        use crate::engine::bittorrent::download::command::BtDownloadCommand;
        let gid = self.group.recover().gid();
        let mut bt_cmd = BtDownloadCommand::new_with_group_and_mappings_with_policy(
            Arc::clone(&self.group),
            &torrent_bytes,
            self.group.recover().options(),
            self.output_path.parent().and_then(|p| p.to_str()),
            &[],
            &self.outbound_network_policy,
        )?;
        bt_cmd.set_dns_cache(Arc::clone(&self.dns_cache));
        bt_cmd.local_peer_id = self.local_peer_id;
        bt_cmd.initial_peer_swarm = self.initial_peer_swarm.take();
        self.group.recover_mut().set_bt_metadata_data(torrent_bytes);
        DownloadEventHooks::shared()
            .notify_metadata_resolved(MetadataResolvedEvent::new(gid, vec![gid]));
        if let Some(gl) = self.global_limiter.clone() {
            bt_cmd.set_global_limiter(gl);
        }
        #[cfg(feature = "bittorrent")]
        if let Some(budget) = self.open_file_budget.clone() {
            bt_cmd.set_open_file_budget(budget);
        }
        #[cfg(feature = "bittorrent")]
        if let Some(catalog) = self.public_tracker_catalog.clone() {
            bt_cmd.set_public_tracker_catalog(catalog);
        }
        if let Some(listener) = self.bt_listener.clone() {
            bt_cmd.set_bt_listener(listener);
        }
        if let Some(registry) = self.bt_registry.clone() {
            bt_cmd.set_bt_registry(registry);
        }
        if let Some(manager) = self.lpd_manager.clone() {
            bt_cmd.set_lpd_manager(manager);
        }

        self.handoff_dht_engine_to_bt(&mut bt_cmd).await;

        Self::execute_handed_off_bt_command(&mut bt_cmd).await?;

        self.shutdown_dht_engine().await;

        self.completed_bytes = self.group.recover().total_length();

        info!("Magnet download complete: {}", self.output_path.display());
        Ok(())
    }

    fn status(&self) -> CommandStatus {
        if self.metadata_complete
            || self.group.recover().status()
                == crate::request::request_group::DownloadStatus::Complete
        {
            CommandStatus::Completed
        } else if self.completed_bytes > 0 {
            CommandStatus::Running
        } else {
            CommandStatus::Pending
        }
    }

    fn gid(&self) -> GroupId {
        self.group.recover().gid()
    }

    fn request_group(
        &self,
    ) -> Option<std::sync::Arc<std::sync::RwLock<crate::request::request_group::RequestGroup>>>
    {
        Some(std::sync::Arc::clone(&self.group))
    }

    fn timeout(&self) -> Option<Duration> {
        self.group.recover().timeout()
    }
}

#[cfg(test)]
mod tests;