mod choke_api;
mod constructor;
mod integration_api;
use std::sync::Arc;
use std::time::{Duration, Instant};
use crate::engine::bittorrent::dht::engine_set::DhtEngineSet;
use crate::engine::bittorrent::discovery::lpd::LpdManager;
use crate::engine::bittorrent::peer::choking_algorithm::ChokingAlgorithm;
use crate::engine::bittorrent::torrent::file_layout::MultiFileLayout;
use crate::rate_limiter::RateLimiter;
use crate::request::request_group::{AtomicProgress, RequestGroup};
use crate::util::rwlock_ext::RwLockRecover;
pub use constructor::prepare_group_metadata;
pub(crate) use constructor::{
apply_file_mappings, apply_select_file_filter, build_download_context_from_meta,
};
pub(crate) const MAX_PUBLIC_TRACKERS_TO_TRY: usize = 10;
#[derive(Debug)]
pub(crate) struct BtRuntimeState {
connections: std::sync::atomic::AtomicUsize,
max_peers: Arc<std::sync::atomic::AtomicUsize>,
}
impl BtRuntimeState {
pub(crate) fn new(max_peers: Arc<std::sync::atomic::AtomicUsize>) -> Self {
Self {
connections: std::sync::atomic::AtomicUsize::new(0),
max_peers,
}
}
pub(crate) fn set_connections(&self, connections: usize) {
self.connections
.store(connections, std::sync::atomic::Ordering::Release);
}
pub(crate) fn connections(&self) -> usize {
self.connections.load(std::sync::atomic::Ordering::Acquire)
}
pub(crate) fn max_peers(&self) -> usize {
self.max_peers.load(std::sync::atomic::Ordering::Acquire)
}
pub(crate) fn max_peers_state(&self) -> Arc<std::sync::atomic::AtomicUsize> {
Arc::clone(&self.max_peers)
}
pub(crate) fn min_peers(&self) -> usize {
let max_peers = self.max_peers.load(std::sync::atomic::Ordering::Acquire);
if max_peers == 0 {
0
} else {
(max_peers * 4 / 5).max(1)
}
}
pub(crate) fn less_than_min_peers(&self) -> bool {
self.connections() < self.min_peers()
}
pub(crate) fn less_than_max_peers(&self) -> bool {
self.max_peers() == 0 || self.connections() < self.max_peers()
}
}
impl Drop for BtDownloadCommand {
fn drop(&mut self) {
self.bt_peer_route.take();
if let Some(registry) = self.bt_registry.as_ref()
&& let Ok(mut registry) = registry.write()
{
for engine in self.dht_engines.iter() {
registry.clear_dht_engine_for_gid_if(self.group.recover().gid().value(), engine);
}
let gid = self.group.recover().gid().value();
let owns_registration = self.tracker_runtime.as_ref().is_some_and(|runtime| {
registry
.get(gid)
.and_then(|object| object.tracker_runtime.as_ref())
.is_some_and(|registered| Arc::ptr_eq(registered, runtime))
});
if owns_registration {
registry.remove(gid);
}
}
let mut storage = self
.peer_storage
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let peers: Vec<_> = storage.used_peers().iter().cloned().collect();
for peer in peers {
storage.return_peer(&peer);
}
}
}
pub struct BtDownloadCommand {
pub(crate) local_peer_id: [u8; 20],
pub(crate) group: Arc<std::sync::RwLock<RequestGroup>>,
pub(crate) progress: Arc<AtomicProgress>,
pub(crate) output_path: std::path::PathBuf,
pub(crate) started: bool,
pub(crate) started_at: Option<Instant>,
pub(crate) completed_bytes: u64,
pub(crate) torrent_data: Vec<u8>,
pub(crate) local_metadata: Arc<[u8]>,
pub(crate) seed_enabled: bool,
pub(crate) seed_time: Option<std::time::Duration>,
pub(crate) seed_ratio: Option<f64>,
pub(crate) total_uploaded: u64,
pub(crate) tracker_actor:
Option<crate::engine::bittorrent::download::execute::BtTrackerAnnouncerActor>,
pub(crate) listen_port: u16,
pub(crate) bt_runtime: std::sync::Arc<BtRuntimeState>,
pub(crate) peer_coordinator: crate::engine::bittorrent::peer::coordinator::BtPeerCoordinator,
pub(crate) initial_peer_swarm:
Option<crate::engine::bittorrent::peer::message_handler::PeerSwarm>,
pub(crate) dht_engines: DhtEngineSet,
pub(crate) public_trackers:
Option<std::sync::Arc<aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList>>,
pub(crate) choking_algo: Option<ChokingAlgorithm>,
pub(crate) multi_file_layout: Option<MultiFileLayout>,
pub(crate) file_allocation: String,
pub(crate) secure_falloc: bool,
pub(crate) check_integrity: bool,
pub(crate) hash_check_only: bool,
pub(crate) bt_enable_hook_after_hash_check: bool,
pub(crate) bt_hash_check_seed: bool,
pub(crate) bt_seed_unverified: bool,
pub(crate) hash_check_completed: bool,
pub(crate) bt_complete_event_emitted: bool,
pub(crate) progress_manager:
Option<crate::engine::bittorrent::persistence::progress_info_file::BtProgressManager>,
pub(crate) progress_save_interval: Duration,
pub(crate) lpd_manager: Option<Arc<LpdManager>>,
pub(crate) lpd_registered_info_hash: Option<[u8; 20]>,
pub(crate) hook_manager: Option<Arc<crate::engine::hook_manager::HookManager>>,
pub(crate) pex_known_peers: Vec<aria2_protocol::bittorrent::peer::connection::PeerAddr>,
pub(crate) pex_last_send_time: Option<Instant>,
pub(crate) pex_send_interval: Duration,
pub(crate) dht_periodic_lookup: super::execute::DhtPeriodicLookup,
#[allow(dead_code)]
pub(crate) download_path_lock: Option<crate::filesystem::file_lock::DownloadPathLock>,
pub(crate) output_path_reservation:
Option<crate::engine::active_output_registry::OutputPathReservation>,
pub(crate) is_private: bool,
pub(crate) bt_registry: Option<Arc<std::sync::RwLock<super::super::registry::BtRegistry>>>,
pub(crate) tracker_runtime:
Option<crate::engine::bittorrent::tracker::communication::SharedTrackerRuntime>,
pub(crate) global_limiter: Option<RateLimiter>,
pub(crate) torrent_upload_limiter: RateLimiter,
pub(crate) outbound_network_policy: Arc<crate::network::OutboundNetworkPolicy>,
pub(crate) peer_rejection: crate::engine::bittorrent::peer::storage::SharedPeerRejection,
pub(crate) peer_storage: std::sync::Arc<
std::sync::Mutex<crate::engine::bittorrent::peer::storage::DefaultPeerStorage>,
>,
pub(crate) incoming_peers:
Option<crate::engine::bittorrent::peer::listener::IncomingPeerReceiver>,
pub(crate) utp_socket:
Option<Arc<tokio::sync::Mutex<aria2_protocol::bittorrent::utp::UtpSocket>>>,
pub(crate) bt_listener:
Option<Arc<crate::engine::bittorrent::peer::listener::BtPeerListenerManager>>,
pub(crate) bt_peer_route: Option<crate::engine::bittorrent::peer::listener::BtPeerRouteHandle>,
pub(crate) checkpoint: Option<crate::engine::bittorrent::persistence::checkpoint::BtCheckpoint>,
pub(crate) checkpoint_bytes_since_save: u64,
pub(crate) checkpoint_last_save: Instant,
}
impl BtDownloadCommand {
pub fn group(&self) -> std::sync::RwLockReadGuard<'_, RequestGroup> {
use crate::util::rwlock_ext::RwLockRecover;
self.group.recover()
}
pub fn group_handle(&self) -> Arc<std::sync::RwLock<RequestGroup>> {
Arc::clone(&self.group)
}
pub async fn shutdown(&mut self) {
if let Some(mut swarm) = self.initial_peer_swarm.take() {
swarm.shutdown_all().await;
}
let engines = std::mem::take(&mut self.dht_engines).into_vec();
if let Some(registry) = self.bt_registry.as_ref()
&& let Ok(mut registry) = registry.write()
{
for engine in &engines {
registry.clear_dht_engine_for_gid_if(self.group.recover().gid().value(), engine);
}
}
for engine in engines {
let is_global = self
.bt_registry
.as_ref()
.and_then(|registry| registry.read().ok())
.is_some_and(|registry| registry.is_global_dht_engine(&engine));
if !is_global {
engine.shutdown_async().await;
}
}
if let (Some(manager), Some(info_hash)) =
(&self.lpd_manager, self.lpd_registered_info_hash.take())
{
let info_hash_hex = hex::encode(info_hash);
manager.unregister_torrent(&info_hash_hex).await;
}
if let Some(actor) = self.tracker_actor.take() {
actor.stop().await;
}
self.bt_peer_route.take();
}
pub fn set_global_limiter(&mut self, limiter: RateLimiter) {
self.global_limiter = Some(limiter);
}
pub fn is_multi_file(&self) -> bool {
self.multi_file_layout
.as_ref()
.is_some_and(|l| l.is_multi_file())
}
}
#[cfg(test)]
mod tests {
use super::{BtDownloadCommand, BtRuntimeState};
use std::sync::Arc;
#[tokio::test]
async fn shutdown_persists_owned_dht_engine_before_drop() {
let torrent = crate::engine::bittorrent::download::command_tests::build_test_torrent();
let options = crate::request::request_group::DownloadOptions::default();
let mut command = BtDownloadCommand::new(
crate::request::request_group::GroupId::new(9),
&torrent,
&options,
None,
)
.expect("test torrent should construct");
let temp_dir = tempfile::tempdir().expect("temporary directory should be created");
let dht_path = temp_dir.path().join("dht.dat");
let dht = aria2_protocol::bittorrent::dht::engine::DhtEngine::start(
aria2_protocol::bittorrent::dht::engine::DhtEngineConfig {
self_id: [0xA5; 20],
dht_file_path: Some(dht_path.clone()),
..aria2_protocol::bittorrent::dht::engine::DhtEngineConfig::local()
},
)
.await
.expect("local DHT engine should start");
command.dht_engines.insert(dht);
command.shutdown().await;
let persisted =
aria2_protocol::bittorrent::dht::persistence::DhtPersistence::load_from_file_sync(
&dht_path,
)
.expect("command shutdown should persist dht.dat");
assert_eq!(persisted.self_id, [0xA5; 20]);
}
#[tokio::test]
async fn task_shutdown_keeps_the_shared_dht_engine_alive() {
let torrent = crate::engine::bittorrent::download::command_tests::build_test_torrent();
let options = crate::request::request_group::DownloadOptions::default();
let mut command = BtDownloadCommand::new(
crate::request::request_group::GroupId::new(778),
&torrent,
&options,
None,
)
.expect("test torrent should construct");
let registry = std::sync::Arc::new(std::sync::RwLock::new(
crate::engine::bittorrent::registry::BtRegistry::new(),
));
command.set_bt_registry(std::sync::Arc::clone(®istry));
let dht = aria2_protocol::bittorrent::dht::engine::DhtEngine::start(
aria2_protocol::bittorrent::dht::engine::DhtEngineConfig::local(),
)
.await
.expect("local DHT engine should start");
command.dht_engines.insert(std::sync::Arc::clone(&dht));
registry
.write()
.expect("BT registry should be writable")
.set_global_dht_engine(std::sync::Arc::clone(&dht));
command.shutdown().await;
assert!(!matches!(
dht.stats().await.state,
aria2_protocol::bittorrent::dht::engine::DhtEngineState::Stopped
));
dht.shutdown_async().await;
}
#[test]
fn runtime_state_uses_the_same_min_peer_boundary_as_tracker_demand() {
let max_peers = Arc::new(std::sync::atomic::AtomicUsize::new(55));
let runtime = BtRuntimeState::new(Arc::clone(&max_peers));
assert_eq!(runtime.min_peers(), 44);
assert!(runtime.less_than_min_peers());
runtime.set_connections(44);
assert!(!runtime.less_than_min_peers());
max_peers.store(0, std::sync::atomic::Ordering::Release);
assert_eq!(runtime.min_peers(), 0);
assert!(!runtime.less_than_min_peers());
assert!(runtime.less_than_max_peers());
}
#[test]
fn runtime_state_accepts_runtime_max_peer_changes() {
let max_peers = Arc::new(std::sync::atomic::AtomicUsize::new(10));
let runtime = BtRuntimeState::new(Arc::clone(&max_peers));
runtime.set_connections(7);
assert!(runtime.less_than_min_peers());
max_peers.store(8, std::sync::atomic::Ordering::Release);
assert!(!runtime.less_than_min_peers());
assert_eq!(runtime.max_peers(), 8);
}
#[test]
fn explicit_zero_seed_time_overrides_the_default_seed_ratio() {
let torrent = crate::engine::bittorrent::download::command_tests::build_test_torrent();
let options = crate::request::request_group::DownloadOptions {
seed_time: Some(0.0),
..Default::default()
};
let command = BtDownloadCommand::new(
crate::request::request_group::GroupId::new(10),
&torrent,
&options,
None,
)
.expect("test torrent should construct");
assert!(!command.seed_enabled);
}
#[test]
fn positive_seed_options_enable_seeding() {
let torrent = crate::engine::bittorrent::download::command_tests::build_test_torrent();
let options = crate::request::request_group::DownloadOptions {
seed_time: Some(1.0),
..Default::default()
};
let command = BtDownloadCommand::new(
crate::request::request_group::GroupId::new(11),
&torrent,
&options,
None,
)
.expect("test torrent should construct");
assert!(command.seed_enabled);
assert_eq!(
command.seed_time,
Some(std::time::Duration::from_secs(60)),
"seed-time is expressed in fractional minutes"
);
}
#[test]
fn command_uses_configured_peer_id_prefix() {
let torrent = crate::engine::bittorrent::download::command_tests::build_test_torrent();
let options = crate::request::request_group::DownloadOptions {
peer_id_prefix: "TEST-PREFIX-".to_string(),
..Default::default()
};
let command = BtDownloadCommand::new(
crate::request::request_group::GroupId::new(12),
&torrent,
&options,
None,
)
.expect("test torrent should construct");
assert!(command.local_peer_id.starts_with(b"TEST-PREFIX-"));
}
#[test]
fn command_loads_configured_peer_blocklist_into_peer_storage() {
let torrent = crate::engine::bittorrent::download::command_tests::build_test_torrent();
let path = std::env::temp_dir().join(format!(
"aria2-rust-command-blocklist-{}.txt",
std::process::id()
));
std::fs::write(&path, "10.0.0.0/8\n").expect("blocklist fixture should be writable");
let options = crate::request::request_group::DownloadOptions {
bt_peer_blocklist: Some(path.to_string_lossy().into_owned()),
..Default::default()
};
let command = BtDownloadCommand::new(
crate::request::request_group::GroupId::new(13),
&torrent,
&options,
None,
)
.expect("test torrent should construct");
let blocked =
crate::engine::bittorrent::peer::storage::PeerEntry::new("10.0.0.1".into(), 6881);
let allowed =
crate::engine::bittorrent::peer::storage::PeerEntry::new("192.0.2.1".into(), 6881);
let mut storage = command.peer_storage.lock().unwrap();
assert!(!storage.add_peer(blocked));
assert!(storage.add_peer(allowed));
drop(storage);
let _ = std::fs::remove_file(path);
}
}