use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::{info, warn};
use crate::config::{parse_index_out, parse_integer_segments};
use crate::constants;
use crate::engine::choking_algorithm::{ChokingAlgorithm, ChokingConfig};
use crate::engine::http_tracker_client::TrackerState;
use crate::engine::multi_file_layout::MultiFileLayout;
use crate::error::{Aria2Error, FatalError, Result};
use crate::filesystem::file_lock::DownloadPathLock;
use crate::request::request_group::{BtFileMapping, DownloadOptions, GroupId, RequestGroup};
use crate::util::rwlock_ext::RwLockRecover;
use super::BtDownloadCommand;
pub(crate) fn build_download_context_from_meta(
meta: &aria2_protocol::bittorrent::torrent::parser::TorrentMeta,
path: String,
) -> crate::error::Result<crate::download::DownloadContext> {
use crate::download::DownloadContext;
use crate::download::download_context::{BtFileMode, ContextAttributeType, TorrentAttribute};
use crate::download::file_entry::FileEntry;
let mut ctx = if meta.is_single_file() {
DownloadContext::new(meta.info.piece_length, meta.total_size(), path)
} else {
let base_dir = std::path::Path::new(&path)
.parent()
.unwrap_or_else(|| std::path::Path::new("."));
let v1_files = meta.info.files.as_deref();
let v2_files = meta.info.v2_files.as_deref();
let file_count = v1_files
.map(|files| {
files
.iter()
.filter(|file| {
!file
.path
.first()
.is_some_and(|component| component == ".pad")
})
.count()
})
.or_else(|| v2_files.map(|files| files.len()))
.unwrap_or(0);
let mut entries = Vec::with_capacity(file_count);
let mut offset = 0u64;
let mut add_entry = |length: u64, path: &Vec<String>, offset: &mut u64| {
let original_name = path.join("/");
let file_path = base_dir.join(std::path::Path::new(&original_name));
let mut entry = FileEntry::new(
file_path.to_string_lossy().into_owned(),
length,
*offset,
Vec::new(),
);
entry.set_original_name(original_name.clone());
entry.set_suffix_path(original_name);
entries.push(entry);
*offset = offset.saturating_add(length);
};
if let Some(files) = v1_files {
for file in files {
if file
.path
.first()
.is_some_and(|component| component == ".pad")
{
offset = offset.saturating_add(file.length);
continue;
}
add_entry(file.length, &file.path, &mut offset);
}
} else if let Some(files) = v2_files {
for file in files {
add_entry(file.length, &file.path, &mut offset);
}
}
let mut context = DownloadContext::new_default();
context.set_piece_length(meta.info.piece_length);
context.set_file_entries(entries);
context
};
if meta.is_single_file()
&& let Some(entry) = ctx.get_file_entries_mut().first_mut()
{
entry.set_original_name(meta.info.name.clone());
entry.set_suffix_path(meta.info.name.clone());
}
if meta.info.meta_version != Some(2) {
let piece_hashes_hex: Vec<String> = meta.info.pieces.iter().map(hex::encode).collect();
ctx.set_piece_hashes("sha-1".to_string(), piece_hashes_hex);
}
let torrent_attr = TorrentAttribute {
name: meta.info.name.clone(),
mode: if meta.is_single_file() {
BtFileMode::Single
} else {
BtFileMode::Multi
},
announce_list: meta.announce_list.clone(),
nodes: Vec::new(),
info_hash: meta.info_hash.as_hex(),
metadata: Vec::new(),
metadata_size: 0,
private_torrent: meta.is_private(),
creation_date: meta.creation_date.unwrap_or(0),
comment: meta.comment.clone().unwrap_or_default(),
created_by: meta.created_by.clone().unwrap_or_default(),
url_list: meta.web_seeds.clone(),
};
ctx.set_attribute(ContextAttributeType::BitTorrent, Box::new(torrent_attr));
Ok(ctx)
}
pub(crate) fn apply_file_mappings(
context: &mut crate::download::DownloadContext,
mappings: &[BtFileMapping],
) -> Result<()> {
if mappings.is_empty() {
return Ok(());
}
let entries = context.get_file_entries_mut();
if entries.len() == 1 && mappings.len() == 1 && mappings[0].original_name.is_empty() {
apply_file_mapping(&mut entries[0], &mappings[0]);
return Ok(());
}
for entry in entries.iter_mut() {
entry.set_requested(false);
}
for mapping in mappings {
let entry = entries
.iter_mut()
.find(|entry| entry.original_name() == mapping.original_name)
.ok_or_else(|| {
Aria2Error::Fatal(FatalError::Config(format!(
"No entry '{}' in torrent metadata",
mapping.original_name
)))
})?;
apply_file_mapping(entry, mapping);
}
Ok(())
}
fn apply_file_mapping(entry: &mut crate::download::file_entry::FileEntry, mapping: &BtFileMapping) {
entry.set_requested(true);
entry.set_path(mapping.path.clone());
entry.set_uris(&mapping.uris);
entry.set_max_connection_per_server(mapping.max_connection_per_server);
entry.set_unique_protocol(mapping.unique_protocol);
}
pub(crate) fn apply_select_file_filter(
context: &mut crate::download::DownloadContext,
select_file: Option<&str>,
) -> Result<()> {
let Some(select_file) = select_file else {
return Ok(());
};
if select_file.trim().is_empty() {
context.set_file_filter(Vec::new());
return Ok(());
}
let max_index = i64::try_from(usize::MAX).unwrap_or(i64::MAX);
let ranges = parse_integer_segments(select_file, 1, max_index)
.map_err(|error| Aria2Error::Fatal(FatalError::Config(error)))?
.into_iter()
.map(|range| -> Result<_> {
let start = usize::try_from(*range.start()).map_err(|_| {
Aria2Error::Fatal(FatalError::Config(
"select-file index does not fit the current platform".to_string(),
))
})?;
let end = usize::try_from(*range.end()).map_err(|_| {
Aria2Error::Fatal(FatalError::Config(
"select-file index does not fit the current platform".to_string(),
))
})?;
Ok(start..=end)
})
.collect::<Result<Vec<_>>>()?;
context.set_file_filter_ranges(&ranges);
Ok(())
}
fn apply_index_out_paths(
context: &mut crate::download::DownloadContext,
index_out: Option<&str>,
dir: &str,
) -> Result<()> {
let Some(index_out) = index_out else {
return Ok(());
};
for (index, suffix_path) in
parse_index_out(index_out).map_err(|error| Aria2Error::Fatal(FatalError::Config(error)))?
{
let path = std::path::Path::new(dir).join(suffix_path);
context
.set_file_path_with_index(index, path.to_string_lossy().into_owned())
.map_err(|error| Aria2Error::Fatal(FatalError::Config(error)))?;
}
Ok(())
}
impl BtDownloadCommand {
pub fn new_with_group(
group: std::sync::Arc<std::sync::RwLock<RequestGroup>>,
torrent_bytes: &[u8],
options: &DownloadOptions,
output_dir: Option<&str>,
) -> Result<Self> {
Self::new_with_group_and_mappings(group, torrent_bytes, options, output_dir, &[])
}
pub(crate) fn new_with_group_and_mappings(
group: std::sync::Arc<std::sync::RwLock<RequestGroup>>,
torrent_bytes: &[u8],
options: &DownloadOptions,
output_dir: Option<&str>,
file_mappings: &[BtFileMapping],
) -> Result<Self> {
let gid = group.recover().gid();
let mut command = Self::new(gid, torrent_bytes, options, output_dir)?;
let current_info_hash = command.group.recover().get_bt_info_hash_hex();
let existing_context = group.recover().get_download_context();
let parsed_context = if existing_context.as_ref().is_some_and(|context| {
context.get_bt_info_hash_hex().is_some_and(|info_hash| {
current_info_hash
.as_deref()
.is_some_and(|current| info_hash.eq_ignore_ascii_case(current))
})
}) {
existing_context
} else if file_mappings.is_empty() {
command.group.recover().get_download_context()
} else {
let meta =
aria2_protocol::bittorrent::torrent::parser::TorrentMeta::parse(torrent_bytes)
.map_err(|error| {
Aria2Error::Fatal(FatalError::Config(format!(
"Torrent parse failed: {error}"
)))
})?;
let dir = output_dir
.map(str::to_owned)
.or_else(|| options.dir.clone())
.unwrap_or_else(|| ".".to_string());
let context_path = if meta.is_single_file() {
command.output_path.to_string_lossy().into_owned()
} else {
std::path::Path::new(&dir)
.join(&meta.info.name)
.to_string_lossy()
.into_owned()
};
let mut context = build_download_context_from_meta(&meta, context_path)?;
apply_index_out_paths(&mut context, options.index_out.as_deref(), &dir)?;
apply_file_mappings(&mut context, file_mappings)?;
apply_select_file_filter(&mut context, options.select_file.as_deref())?;
Some(std::sync::Arc::new(context))
};
let (piece_count, piece_length, info_hash) = {
let temporary = command.group.recover();
(
temporary.get_bt_num_pieces(),
temporary.get_bt_piece_length(),
temporary.get_bt_info_hash_hex(),
)
};
{
let external = group.recover();
if let Some(context) = parsed_context {
external.set_download_context(context);
}
if let Some(info_hash) = info_hash {
external.set_bt_metadata(piece_count, piece_length, info_hash);
}
}
command.group = group;
command.progress = command.group.recover().progress.clone();
command.total_uploaded = command.group.recover().get_uploaded_length();
command.apply_context_paths()?;
Ok(command)
}
fn apply_context_paths(&mut self) -> Result<()> {
let paths = self
.group
.recover()
.get_download_context()
.map(|context| {
context
.get_file_entries()
.iter()
.map(|entry| std::path::PathBuf::from(entry.path()))
.collect::<Vec<_>>()
})
.unwrap_or_default();
if let Some(layout) = self.multi_file_layout.as_mut() {
if paths.len() == layout.num_files() {
for (index, path) in paths.into_iter().enumerate() {
layout
.set_file_absolute_path(index, path)
.map_err(|error| Aria2Error::Fatal(FatalError::Config(error)))?;
}
}
} else if let Some(path) = paths.into_iter().next()
&& !path.as_os_str().is_empty()
{
self.output_path = path;
}
Ok(())
}
pub fn new(
gid: GroupId,
torrent_bytes: &[u8],
options: &DownloadOptions,
output_dir: Option<&str>,
) -> Result<Self> {
let meta = aria2_protocol::bittorrent::torrent::parser::TorrentMeta::parse(torrent_bytes)
.map_err(|e| {
Aria2Error::Fatal(FatalError::Config(format!("Torrent parse failed: {}", e)))
})?;
let is_private = meta.is_private();
if is_private {
info!(
"[BT] Private torrent detected (BEP 0027): DHT/PEX/LPD and public trackers will be disabled"
);
}
let dir = output_dir
.map(|d| d.to_string())
.or_else(|| options.dir.clone())
.unwrap_or_else(|| ".".to_string());
let filename = meta.info.name.clone();
let path = std::path::PathBuf::from(&dir).join(&filename);
let group = RequestGroup::new(
gid,
vec![format!("bt://{}", meta.info_hash.as_hex())],
options.clone(),
);
group.set_bt_metadata(
meta.num_pieces() as u32,
meta.info.piece_length,
meta.info_hash.as_hex(),
);
let mut ctx = build_download_context_from_meta(&meta, path.to_string_lossy().to_string())?;
apply_index_out_paths(&mut ctx, options.index_out.as_deref(), &dir)?;
apply_select_file_filter(&mut ctx, options.select_file.as_deref())?;
group.set_download_context(std::sync::Arc::new(ctx));
let seed_time = options.seed_time.and_then(|t| {
if t <= 0.0 || !t.is_finite() {
None
} else {
Some(std::time::Duration::from_secs((t * 60.0).floor() as u64))
}
});
let seed_ratio = options.seed_ratio.filter(|&r| r > 0.0);
let choking_algo = if options.bt_max_upload_slots.is_some()
|| options.bt_optimistic_unchoke_interval.is_some()
|| options.bt_snubbed_timeout.is_some()
{
let config = ChokingConfig {
max_upload_slots: options
.bt_max_upload_slots
.unwrap_or(constants::BT_DEFAULT_MAX_UPLOAD_SLOTS as u32)
as usize,
optimistic_unchoke_interval_secs: options
.bt_optimistic_unchoke_interval
.unwrap_or(constants::BT_OPTIMISTIC_UNCHOKE_INTERVAL_SECS),
snubbed_timeout_secs: options
.bt_snubbed_timeout
.unwrap_or(constants::BT_SNUBBED_TIMEOUT_SECS),
choke_rotation_interval_secs: constants::BT_CHOKE_ROTATION_INTERVAL_SECS,
};
Some(ChokingAlgorithm::new(config))
} else {
None
};
let multi_file_layout = if !meta.is_single_file() {
let layout_base_dir = std::path::PathBuf::from(&dir);
match MultiFileLayout::from_info_dict(&meta.info, &layout_base_dir) {
Ok(layout) => Some(layout),
Err(e) => {
return Err(Aria2Error::Fatal(FatalError::Config(format!(
"MultiFileLayout creation failed: {}",
e
))));
}
}
} else {
None
};
let effective_output_path = if multi_file_layout.is_some() {
std::path::PathBuf::from(&dir)
} else {
path.clone()
};
info!(
"BtDownloadCommand created: {} -> {} ({} bytes, {} pieces) seed={:?} ratio={:?} multi_file={}",
meta.info.name,
effective_output_path.display(),
meta.total_size(),
meta.num_pieces(),
seed_time,
seed_ratio,
multi_file_layout.is_some()
);
let download_path_lock =
match DownloadPathLock::acquire_for_download(std::path::Path::new(&dir)) {
Ok(lock) => Some(lock),
Err(e) => {
warn!(
"Failed to acquire download path lock: {}. Proceeding without lock.",
e
);
None
}
};
let progress = group.progress.clone();
let peer_storage = {
let mut storage = crate::engine::bt_peer_storage::DefaultPeerStorage::new();
if let Some(path) = options.bt_peer_blocklist.as_deref() {
let mut blocklist = crate::engine::bt_peer_blocklist::BtPeerBlocklist::new();
blocklist
.load_from_file(std::path::Path::new(path))
.map_err(|error| Aria2Error::Fatal(FatalError::Config(error)))?;
storage.set_peer_blocklist(Arc::new(blocklist));
}
Arc::new(std::sync::Mutex::new(storage))
};
let utp_socket = if options.enable_utp {
let socket = match options.utp_listen_port {
Some(port) => aria2_protocol::bittorrent::utp::UtpSocket::bind_port(port),
None => aria2_protocol::bittorrent::utp::UtpSocket::bind_any(),
}
.map_err(|error| {
Aria2Error::Fatal(FatalError::Config(format!(
"Failed to bind uTP socket: {error}"
)))
})?;
Some(Arc::new(tokio::sync::Mutex::new(socket)))
} else {
None
};
let mut command = Self {
local_peer_id: aria2_protocol::bittorrent::peer::id::generate_peer_id_with_prefix(
&options.peer_id_prefix,
),
group: Arc::new(std::sync::RwLock::new(group)),
progress,
output_path: effective_output_path,
started: false,
started_at: None,
completed_bytes: 0,
torrent_data: torrent_bytes.to_vec(),
seed_enabled: options.seed_time != Some(0.0)
&& (options.seed_time.unwrap_or(0.0) > 0.0
|| options.seed_ratio.unwrap_or(0.0) > 0.0),
seed_time,
seed_ratio,
total_uploaded: 0,
udp_client: None,
tracker_announcer: None,
listen_port: 0,
bt_runtime: std::sync::Arc::new(super::BtRuntimeState::new(options.bt_max_peers)),
peer_coordinator: crate::engine::bt_peer_coordinator::BtPeerCoordinator::new(
options.bt_max_peers,
10,
),
dht_engine: None,
public_trackers: None,
choking_algo,
multi_file_layout,
file_allocation: options
.file_allocation
.clone()
.unwrap_or_else(|| crate::constants::DEFAULT_FILE_ALLOCATION.to_string()),
secure_falloc: options.secure_falloc,
check_integrity: options.check_integrity,
hash_check_only: options.hash_check_only,
bt_enable_hook_after_hash_check: options.bt_enable_hook_after_hash_check,
bt_hash_check_seed: options.bt_hash_check_seed,
bt_seed_unverified: options.bt_seed_unverified,
hash_check_completed: false,
bt_complete_event_emitted: false,
progress_manager: None,
progress_save_interval: Duration::from_secs(60),
lpd_manager: None,
lpd_registered_info_hash: None,
hook_manager: None,
pex_known_peers: Vec::new(),
pex_last_send_time: None,
pex_send_interval: Duration::from_secs(60),
allowed_fast_sent_peers: HashMap::new(),
suggest_sent_counts: HashMap::new(),
endgame_state: super::super::bt_download_execute::EndgameState::new(),
tracker_state: TrackerState::new(),
web_seed_manager: None,
dht_periodic_lookup: super::super::bt_download_execute::execute::DhtPeriodicLookup::new(
),
download_path_lock,
seed_manager: None,
is_private,
bt_registry: None,
global_limiter: None,
peer_rejection: crate::engine::bt_peer_storage::PeerRejectionState::shared(),
peer_storage,
incoming_peers: None,
utp_socket,
bt_listener: Some(std::sync::Arc::new(
crate::engine::bt_peer_listener::BtPeerListenerManager::new(),
)),
bt_peer_route: None,
checkpoint: None,
checkpoint_bytes_since_save: 0,
checkpoint_last_save: Instant::now(),
};
command.apply_context_paths()?;
Ok(command)
}
}