use std::sync::Arc;
use tracing::{debug, warn};
use super::command::Command;
use super::engine_command::TaskResult;
use super::protocol_adapter::{ProtocolAdapterRegistry, ProtocolCommandRequest, ProtocolServices};
use crate::error::Aria2Error;
use crate::network::ConnectionContext;
use crate::request::request_group::{DownloadOptions, GroupId, RequestGroup};
use crate::util::rwlock_ext::RwLockRecover;
use tokio_util::sync::CancellationToken;
pub(crate) struct CommandDependencies {
pub(crate) services: ProtocolServices,
pub(crate) protocol_adapters: Arc<ProtocolAdapterRegistry>,
}
pub(crate) fn spawn_download_task(
group: Arc<std::sync::RwLock<RequestGroup>>,
dependencies: CommandDependencies,
generation: u64,
completion_tx: tokio::sync::mpsc::UnboundedSender<(GroupId, u64, TaskResult)>,
) -> Option<(tokio::task::JoinHandle<()>, CancellationToken)> {
let gid = group.recover().gid();
let uris = group.recover().uris().to_vec();
let options = group.recover().options_arc();
group.recover().inc_commands();
let first_uri = match uris.first() {
Some(u) => u.clone(),
None => {
warn!(gid = gid.value(), "No URIs in group, cannot spawn task");
group.recover().dec_commands();
return None;
}
};
let shutdown = CancellationToken::new();
let task_shutdown = shutdown.clone();
let completion_tx = completion_tx.clone();
let handle = tokio::spawn(async move {
let (result, connection_context) = tokio::select! {
command_result = create_command_for_group(
Arc::clone(&group),
first_uri.to_string(),
options,
dependencies,
) => {
match command_result {
Ok(mut cmd) => {
let result = cmd.execute().await;
if result.is_err() {
cmd.shutdown().await;
}
(result, cmd.connection_context())
}
Err(error) => (Err(error), None),
}
}
_ = task_shutdown.cancelled() => {
(Err(Aria2Error::DownloadFailed("download shutdown requested".into())), None)
}
};
let task_result = match result {
Ok(()) => {
debug!(gid = gid.value(), "Download task completed successfully");
TaskResult::Success
}
Err(Aria2Error::Recoverable(recoverable)) => {
warn!(
gid = gid.value(),
"Download task failed with recoverable error"
);
failed_task_result(Aria2Error::Recoverable(recoverable), connection_context)
}
Err(e) => {
warn!(gid = gid.value(), error = %e, "Download task failed");
failed_task_result(e, connection_context)
}
};
if completion_tx.send((gid, generation, task_result)).is_err() {
debug!(
gid = gid.value(),
"Completion channel closed, engine likely shut down"
);
}
});
Some((handle, shutdown))
}
fn failed_task_result(
error: Aria2Error,
connection_context: Option<ConnectionContext>,
) -> TaskResult {
match connection_context {
Some(connection_context) => TaskResult::FailedWithContext {
error,
connection_context,
},
None => TaskResult::Failed(error),
}
}
async fn create_command_for_group(
group: Arc<std::sync::RwLock<RequestGroup>>,
first_uri: String,
options: Arc<DownloadOptions>,
dependencies: CommandDependencies,
) -> crate::error::Result<Box<dyn Command>> {
create_command_for_uri(&first_uri, group, &options, dependencies).await
}
async fn create_command_for_uri(
uri: &str,
group: Arc<std::sync::RwLock<RequestGroup>>,
options: &DownloadOptions,
dependencies: CommandDependencies,
) -> crate::error::Result<Box<dyn Command>> {
dependencies
.protocol_adapters
.create(
ProtocolCommandRequest {
group,
first_uri: uri.to_string(),
options: Arc::new(options.clone()),
},
&dependencies.services,
)
.await
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dns::dns_cache::DnsCache;
use crate::network::OutboundNetworkPolicy;
use crate::request::request_group::{DownloadOptions, GroupId, RequestGroup};
fn dependencies(dns_cache: Arc<tokio::sync::Mutex<DnsCache>>) -> CommandDependencies {
CommandDependencies {
services: ProtocolServices {
dns_cache,
outbound_network_policy: Arc::new(OutboundNetworkPolicy::direct()),
global_limiter: None,
},
protocol_adapters: Arc::new(ProtocolAdapterRegistry::builtins(
Arc::new(crate::ftp::connection_pool::FtpConnectionPool::new(4)),
#[cfg(feature = "bittorrent")]
crate::engine::bittorrent::command_adapter::BtCommandServices {
public_tracker_catalog: Arc::new(
aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList::new(),
),
bt_registry: Arc::new(std::sync::RwLock::new(
crate::engine::bittorrent::registry::BtRegistry::new(),
)),
bt_listener: Arc::new(
crate::engine::bittorrent::peer::listener::BtPeerListenerManager::new(),
),
lpd_manager: Arc::new(
crate::engine::bittorrent::discovery::lpd::LpdManager::new(),
),
open_file_budget: crate::filesystem::open_file_budget::OpenFileBudget::new(100),
},
)),
}
}
#[tokio::test]
async fn async_dns_false_uses_the_protocol_default_resolver_path() {
for scheme in ["http", "ftp"] {
let cache = Arc::new(tokio::sync::Mutex::new(DnsCache::new()));
let options = DownloadOptions {
async_dns: false,
..DownloadOptions::default()
};
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(70),
vec![format!("{scheme}://localhost/file.bin")],
options.clone(),
)));
let _command = create_command_for_uri(
&format!("{scheme}://localhost/file.bin"),
group,
&options,
dependencies(Arc::clone(&cache)),
)
.await
.expect("command construction should not require the shared DNS cache");
assert_eq!(
cache.lock().await.len(),
0,
"async-dns=false must not pre-resolve {scheme} through the shared cache"
);
}
let cache = Arc::new(tokio::sync::Mutex::new(DnsCache::new()));
let options = DownloadOptions {
async_dns: false,
http_proxy: Some("http://localhost:8080".to_string()),
..DownloadOptions::default()
};
let uri = "http://origin.invalid/file.bin";
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(72),
vec![uri.to_string()],
options.clone(),
)));
let _command =
create_command_for_uri(uri, group, &options, dependencies(Arc::clone(&cache)))
.await
.expect("proxy command construction should use the default resolver path");
assert_eq!(
cache.lock().await.len(),
0,
"async-dns=false must not query the shared resolver for a proxy or origin"
);
let cache = Arc::new(tokio::sync::Mutex::new(DnsCache::new()));
let options = DownloadOptions {
async_dns: false,
http_proxy: Some("http://localhost:8080".to_string()),
..DownloadOptions::default()
};
let uri = "http://origin.invalid/file.bin";
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(74),
vec![uri.to_string()],
options.clone(),
)));
let mut deps = dependencies(Arc::clone(&cache));
deps.services.outbound_network_policy =
Arc::new(OutboundNetworkPolicy::single("127.0.0.1".parse().unwrap()));
let _command = create_command_for_uri(uri, group, &options, deps)
.await
.expect("interface-bound proxy should use system DNS for source selection");
assert_eq!(
cache.lock().await.len(),
0,
"async-dns=false must not use the shared resolver when choosing a proxy source"
);
let socket = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind an unused explicit DNS endpoint");
let dns_server = socket.local_addr().expect("read DNS endpoint");
drop(socket);
let cache = Arc::new(tokio::sync::Mutex::new(DnsCache::with_dns_servers(
std::time::Duration::from_millis(300),
vec![dns_server],
)));
let options = DownloadOptions {
async_dns: false,
..DownloadOptions::default()
};
let uri = "http://localhost:8080/file.bin";
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(77),
vec![uri.to_string()],
options.clone(),
)));
let mut deps = dependencies(Arc::clone(&cache));
deps.services.outbound_network_policy =
Arc::new(OutboundNetworkPolicy::single("127.0.0.1".parse().unwrap()));
let _command = create_command_for_uri(uri, group, &options, deps)
.await
.expect("direct interface binding should use system DNS for address-family selection");
assert_eq!(
cache.lock().await.len(),
0,
"async-dns=false direct binding must not query explicit async DNS servers"
);
}
#[cfg(not(feature = "sftp"))]
#[tokio::test]
async fn sftp_uri_is_rejected_instead_of_falling_through_to_http() {
let uri = "sftp://user@example.test/file.bin";
let options = DownloadOptions::default();
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(79),
vec![uri.to_string()],
options.clone(),
)));
let result = create_command_for_uri(
uri,
group,
&options,
dependencies(Arc::new(tokio::sync::Mutex::new(DnsCache::new()))),
)
.await;
assert!(matches!(
result,
Err(crate::error::Aria2Error::Fatal(
crate::error::FatalError::UnsupportedProtocol { protocol }
)) if protocol == "sftp"
));
}
#[tokio::test]
async fn async_dns_true_populates_the_shared_cache_for_protocol_commands() {
for scheme in ["http", "ftp"] {
let cache = Arc::new(tokio::sync::Mutex::new(DnsCache::new()));
let options = DownloadOptions::default();
let uri = format!("{scheme}://localhost/file.bin");
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(71),
vec![uri.clone()],
options.clone(),
)));
let _command =
create_command_for_uri(&uri, group, &options, dependencies(Arc::clone(&cache)))
.await
.expect("command construction should resolve localhost");
assert_eq!(
cache.lock().await.len(),
1,
"async-dns=true must use the shared cache for {scheme}"
);
}
let cache = Arc::new(tokio::sync::Mutex::new(DnsCache::new()));
let options = DownloadOptions {
http_proxy: Some("http://localhost:8080".to_string()),
..DownloadOptions::default()
};
let uri = "http://origin.invalid/file.bin";
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(73),
vec![uri.to_string()],
options.clone(),
)));
let _command =
create_command_for_uri(uri, group, &options, dependencies(Arc::clone(&cache)))
.await
.expect("proxied command should resolve the first-hop proxy");
let cache = cache.lock().await;
let proxy_addresses = cache
.resolve_no_network("localhost", 8080)
.expect("proxy endpoint must be in the DNS cache");
assert!(
!proxy_addresses.is_empty() && proxy_addresses.iter().all(|addr| addr.port() == 8080),
"async-dns=true must resolve the proxy endpoint"
);
assert!(
cache.resolve_no_network("origin.invalid", 80).is_err(),
"a proxy must resolve the first hop rather than the origin"
);
}
#[tokio::test]
async fn explicit_dns_failure_aborts_http_command_setup() {
let socket = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind an unused explicit DNS endpoint");
let dns_server = socket.local_addr().expect("read DNS endpoint");
drop(socket);
let cache = Arc::new(tokio::sync::Mutex::new(DnsCache::with_dns_servers(
std::time::Duration::from_millis(300),
vec![dns_server],
)));
let options = DownloadOptions::default();
let uri = "http://explicit-only.invalid/file.bin";
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(75),
vec![uri.to_string()],
options.clone(),
)));
let result = create_command_for_uri(uri, group, &options, dependencies(cache)).await;
let error = match result {
Ok(_) => {
panic!("an explicit resolver failure must stop before reqwest system DNS fallback")
}
Err(error) => error,
};
assert!(
error
.to_string()
.contains("using explicitly configured servers"),
"unexpected command-construction error: {error}"
);
}
#[tokio::test]
async fn numeric_http_origin_bypasses_explicit_dns_servers() {
let socket = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind an unused explicit DNS endpoint");
let dns_server = socket.local_addr().expect("read DNS endpoint");
drop(socket);
let cache = Arc::new(tokio::sync::Mutex::new(DnsCache::with_dns_servers(
std::time::Duration::from_millis(300),
vec![dns_server],
)));
let options = DownloadOptions::default();
let uri = "http://127.0.0.1:8080/file.bin";
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(76),
vec![uri.to_string()],
options.clone(),
)));
let _command =
create_command_for_uri(uri, group, &options, dependencies(Arc::clone(&cache)))
.await
.expect("an IP literal must not require DNS");
assert_eq!(
cache.lock().await.len(),
0,
"an IP literal must not query explicitly configured DNS servers"
);
}
}