mod types;
pub use types::{BtPeerConnectionOptions, BtPeerCryptoPolicy, PeerConnectionResult};
use crate::engine::bittorrent::peer::connection::BtPeerConn;
use crate::engine::bittorrent::peer::utp_transport::UtpTransportHandle;
use crate::error::Result;
use crate::network::OutboundNetworkPolicy;
use futures::stream::{self, StreamExt};
use tracing::{debug, info};
pub(crate) const PEER_CONNECT_SETTLE_TIME: std::time::Duration = std::time::Duration::from_secs(1);
pub struct BtPeerInteraction;
impl BtPeerInteraction {
#[allow(clippy::too_many_arguments)]
pub(crate) async fn connect_to_peers(
peer_addrs: &[aria2_protocol::bittorrent::peer::connection::PeerAddr],
info_hash_raw: &[u8; 20],
num_pieces: u32,
piece_length: u32,
total_length: u64,
connection_options: &BtPeerConnectionOptions,
utp_transport: Option<UtpTransportHandle>,
policy: &OutboundNetworkPolicy,
) -> Result<PeerConnectionResult> {
info!("[BT] Connecting to {} peers...", peer_addrs.len());
let mut results = stream::iter(peer_addrs.iter().cloned())
.map(|addr| {
let utp_transport = utp_transport.clone();
async move {
debug!("[BT] Connecting to peer {}:{}", addr.ip, addr.port);
let result = Self::connect_peer_ready(
&addr,
info_hash_raw,
connection_options,
num_pieces,
piece_length,
total_length,
utp_transport.clone(),
policy,
)
.await;
(addr, result)
}
})
.buffer_unordered(peer_addrs.len().max(1));
let mut active_connections = Vec::with_capacity(peer_addrs.len());
let mut failed_count = 0usize;
let mut settle_deadline = None;
loop {
let next = if let Some(deadline) = settle_deadline {
tokio::select! {
biased;
result = results.next() => result,
_ = tokio::time::sleep_until(deadline) => break,
}
} else {
results.next().await
};
let Some((addr, result)) = next else {
break;
};
match result {
Ok(conn) => {
active_connections.push(conn);
settle_deadline.get_or_insert_with(|| {
tokio::time::Instant::now() + PEER_CONNECT_SETTLE_TIME
});
}
Err(e) => {
debug!(
"[BT] Failed to connect peer {}:{}: {}",
addr.ip, addr.port, e
);
failed_count += 1;
}
}
}
info!("[BT] Active connections: {}", active_connections.len());
Ok(PeerConnectionResult {
connections: active_connections,
failed_count,
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn connect_peer_ready(
addr: &aria2_protocol::bittorrent::peer::connection::PeerAddr,
info_hash_raw: &[u8; 20],
connection_options: &BtPeerConnectionOptions,
num_pieces: u32,
piece_length: u32,
total_length: u64,
utp_transport: Option<UtpTransportHandle>,
policy: &OutboundNetworkPolicy,
) -> Result<BtPeerConn> {
let mut conn = Self::connect_single_peer(
addr,
info_hash_raw,
connection_options,
utp_transport,
policy,
)
.await?;
conn.set_timeouts(
connection_options.keep_alive_interval,
connection_options.peer_timeout,
);
conn.sync_peer_identity();
conn.allocate_session_resource(piece_length, num_pieces, total_length);
info!(
"[BT] Connected to peer {}:{} (encrypted={}, piece_length={}, total_length={})",
addr.ip,
addr.port,
conn.is_encrypted(),
piece_length,
total_length
);
conn.prepare_actor_startup(
connection_options.peer_agent.clone(),
connection_options.listen_port,
info_hash_raw,
num_pieces,
);
Ok(conn)
}
async fn connect_single_peer(
addr: &aria2_protocol::bittorrent::peer::connection::PeerAddr,
info_hash_raw: &[u8; 20],
connection_options: &BtPeerConnectionOptions,
utp_transport: Option<UtpTransportHandle>,
policy: &OutboundNetworkPolicy,
) -> Result<BtPeerConn> {
if connection_options.enable_utp
&& !connection_options.crypto.require_mse
&& let Some(utp_transport) = utp_transport.as_ref()
{
match addr.to_socket_addr() {
Ok(endpoint) => {
let utp_result = BtPeerConn::connect_utp_with_transport(
endpoint,
info_hash_raw,
connection_options.hybrid_info_hash_v2.as_ref(),
&connection_options.local_peer_id,
connection_options.connection_timeout,
connection_options.dht_enabled,
utp_transport,
)
.await;
match utp_result {
Ok(conn) => {
debug!("[BT] Connected to peer {}:{} over uTP", addr.ip, addr.port);
return Ok(conn);
}
Err(error) => {
debug!(
"[BT] uTP connection to {}:{} failed, trying TCP: {}",
addr.ip, addr.port, error
);
}
}
}
Err(error) => {
debug!(
"[BT] Skipping uTP for peer {}:{} because it is not a numeric IP address: {}",
addr.ip, addr.port, error
);
}
}
}
if connection_options.crypto.require_mse {
BtPeerConn::connect_mse_with_policy(
addr,
info_hash_raw,
connection_options.hybrid_info_hash_v2.as_ref(),
aria2_protocol::bittorrent::peer::mse::MseConnectionOptions {
force_encryption: connection_options.crypto.force_encryption,
prefer_encryption: connection_options.crypto.prefer_encryption,
local_peer_id: connection_options.local_peer_id,
timeout: connection_options.connection_timeout,
dht_enabled: connection_options.dht_enabled,
},
policy,
)
.await
} else {
let mse_result = BtPeerConn::connect_mse_with_policy(
addr,
info_hash_raw,
connection_options.hybrid_info_hash_v2.as_ref(),
aria2_protocol::bittorrent::peer::mse::MseConnectionOptions {
force_encryption: connection_options.crypto.force_encryption,
prefer_encryption: connection_options.crypto.prefer_encryption,
local_peer_id: connection_options.local_peer_id,
timeout: connection_options.connection_timeout,
dht_enabled: connection_options.dht_enabled,
},
policy,
)
.await;
match mse_result {
Ok(conn) => Ok(conn),
Err(_) => {
debug!("[BT] MSE failed, trying plain connection");
BtPeerConn::connect_plain_with_policy(
addr,
info_hash_raw,
connection_options.hybrid_info_hash_v2.as_ref(),
&connection_options.local_peer_id,
connection_options.connection_timeout,
connection_options.dht_enabled,
policy,
)
.await
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::{BtPeerConnectionOptions, BtPeerCryptoPolicy, BtPeerInteraction};
use crate::network::OutboundNetworkPolicy;
use aria2_protocol::bittorrent::peer::connection::PeerAddr;
use std::time::Duration;
#[tokio::test]
async fn a_slow_peer_does_not_hold_a_ready_peer_batch_open() {
let info_hash = [7; 20];
let remote_peer_id = [8; 20];
let good_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let good_addr = good_listener.local_addr().unwrap();
let good_server = tokio::spawn(async move {
loop {
let (stream, _) = good_listener.accept().await.unwrap();
if let Ok(incoming) =
aria2_protocol::bittorrent::peer::incoming::receive(stream, &[info_hash]).await
&& let Ok(connection) = incoming.complete(remote_peer_id, None, false).await
{
return connection;
}
}
});
let slow_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let slow_addr = slow_listener.local_addr().unwrap();
let slow_server = tokio::spawn(async move {
while let Ok((stream, _)) = slow_listener.accept().await {
tokio::spawn(async move {
let _stream = stream;
tokio::time::sleep(Duration::from_secs(5)).await;
});
}
});
let options = BtPeerConnectionOptions {
crypto: BtPeerCryptoPolicy::default(),
connection_timeout: Duration::from_secs(2),
keep_alive_interval: Duration::from_secs(120),
peer_timeout: Duration::from_secs(60),
local_peer_id: [9; 20],
peer_agent: "aria2-rust-test".to_string(),
enable_utp: false,
utp_listen_port: None,
dht_enabled: false,
listen_port: None,
hybrid_info_hash_v2: None,
};
let peers = [
PeerAddr::new(&good_addr.ip().to_string(), good_addr.port()),
PeerAddr::new(&slow_addr.ip().to_string(), slow_addr.port()),
];
let result = tokio::time::timeout(
Duration::from_millis(1500),
BtPeerInteraction::connect_to_peers(
&peers,
&info_hash,
1,
16 * 1024,
3,
&options,
None,
&OutboundNetworkPolicy::direct(),
),
)
.await
.expect("the ready peer should be returned before the slow peer times out")
.unwrap();
assert_eq!(result.connections.len(), 1);
drop(result);
good_server.abort();
slow_server.abort();
}
#[tokio::test]
async fn later_peer_success_does_not_extend_the_settle_deadline() {
let info_hash = [7; 20];
let remote_peer_id = [8; 20];
let first_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let first_addr = first_listener.local_addr().unwrap();
let first_server = tokio::spawn(async move {
loop {
let (stream, _) = first_listener.accept().await.unwrap();
if let Ok(incoming) =
aria2_protocol::bittorrent::peer::incoming::receive(stream, &[info_hash]).await
&& let Ok(connection) = incoming.complete(remote_peer_id, None, false).await
{
return connection;
}
}
});
let delayed_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let delayed_addr = delayed_listener.local_addr().unwrap();
let delayed_server = tokio::spawn(async move {
loop {
let (stream, _) = delayed_listener.accept().await.unwrap();
if let Ok(incoming) =
aria2_protocol::bittorrent::peer::incoming::receive(stream, &[info_hash]).await
&& let Ok(connection) = incoming.complete([10; 20], None, false).await
{
return connection;
}
tokio::time::sleep(Duration::from_millis(400)).await;
}
});
let pending_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let pending_addr = pending_listener.local_addr().unwrap();
let pending_server = tokio::spawn(async move {
while let Ok((stream, _)) = pending_listener.accept().await {
tokio::spawn(async move {
let _stream = stream;
tokio::time::sleep(Duration::from_secs(5)).await;
});
}
});
let options = BtPeerConnectionOptions {
crypto: BtPeerCryptoPolicy::default(),
connection_timeout: Duration::from_secs(2),
keep_alive_interval: Duration::from_secs(120),
peer_timeout: Duration::from_secs(60),
local_peer_id: [9; 20],
peer_agent: "aria2-rust-test".to_string(),
enable_utp: false,
utp_listen_port: None,
dht_enabled: false,
listen_port: None,
hybrid_info_hash_v2: None,
};
let peers = [
PeerAddr::new(&first_addr.ip().to_string(), first_addr.port()),
PeerAddr::new(&delayed_addr.ip().to_string(), delayed_addr.port()),
PeerAddr::new(&pending_addr.ip().to_string(), pending_addr.port()),
];
let result = tokio::time::timeout(
Duration::from_millis(1250),
BtPeerInteraction::connect_to_peers(
&peers,
&info_hash,
1,
16 * 1024,
3,
&options,
None,
&OutboundNetworkPolicy::direct(),
),
)
.await
.expect("a later success must not renew the first peer's settle deadline")
.unwrap();
assert_eq!(result.connections.len(), 2);
drop(result);
first_server.abort();
delayed_server.abort();
pending_server.abort();
}
#[tokio::test]
async fn ipv6_peer_with_utp_enabled_falls_back_to_tcp() {
let listener = tokio::net::TcpListener::bind("[::1]:0").await.unwrap();
let endpoint = listener.local_addr().unwrap();
let info_hash = [7; 20];
let remote_peer_id = [8; 20];
let server = tokio::spawn(async move {
loop {
let (stream, _) = listener.accept().await.unwrap();
if let Ok(incoming) =
aria2_protocol::bittorrent::peer::incoming::receive(stream, &[info_hash]).await
&& let Ok(connection) = incoming.complete(remote_peer_id, None, false).await
{
return connection;
}
}
});
let options = BtPeerConnectionOptions {
crypto: BtPeerCryptoPolicy::default(),
connection_timeout: Duration::from_millis(150),
keep_alive_interval: Duration::from_secs(120),
peer_timeout: Duration::from_secs(60),
local_peer_id: [9; 20],
peer_agent: "aria2-rust-test".to_string(),
enable_utp: true,
utp_listen_port: None,
dht_enabled: false,
listen_port: None,
hybrid_info_hash_v2: None,
};
let result = BtPeerInteraction::connect_single_peer(
&PeerAddr::new(&endpoint.ip().to_string(), endpoint.port()),
&info_hash,
&options,
None,
&OutboundNetworkPolicy::direct(),
)
.await;
if let Err(error) = result {
server.abort();
panic!("IPv6 peer connection should fall back from uTP to TCP: {error}");
}
server.abort();
}
}