Change tracker type to be url::Url in most places

This commit is contained in:
Igor Katson 2025-02-27 12:35:37 +00:00
parent 3eb1558451
commit d709557372
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
4 changed files with 27 additions and 24 deletions

View file

@ -443,12 +443,12 @@ async fn create_tcp_listener(
bail!("no free TCP ports in range {port_range:?}"); bail!("no free TCP ports in range {port_range:?}");
} }
fn torrent_file_from_info_bytes(info_bytes: &[u8], trackers: &[String]) -> anyhow::Result<Bytes> { fn torrent_file_from_info_bytes(info_bytes: &[u8], trackers: &[url::Url]) -> anyhow::Result<Bytes> {
#[derive(Serialize)] #[derive(Serialize)]
struct Tmp<'a> { struct Tmp<'a> {
announce: &'a str, announce: &'a str,
#[serde(rename = "announce-list")] #[serde(rename = "announce-list")]
announce_list: &'a [&'a [String]], announce_list: &'a [&'a [url::Url]],
info: bencode::raw_value::RawValue<&'a [u8]>, info: bencode::raw_value::RawValue<&'a [u8]>,
} }
@ -472,7 +472,7 @@ pub(crate) struct CheckedIncomingConnection {
struct InternalAddResult { struct InternalAddResult {
info_hash: Id20, info_hash: Id20,
metadata: Option<TorrentMetadata>, metadata: Option<TorrentMetadata>,
trackers: Vec<String>, trackers: Vec<url::Url>,
name: Option<String>, name: Option<String>,
} }
@ -909,7 +909,11 @@ impl Session {
InternalAddResult { InternalAddResult {
info_hash, info_hash,
trackers: magnet.trackers, trackers: magnet
.trackers
.into_iter()
.filter_map(|t| url::Url::parse(&t).ok())
.collect(),
metadata: None, metadata: None,
name: magnet.name, name: magnet.name,
} }
@ -955,7 +959,10 @@ impl Session {
torrent.torrent_bytes, torrent.torrent_bytes,
torrent.info_bytes, torrent.info_bytes,
)?), )?),
trackers, trackers: trackers
.iter()
.filter_map(|t| url::Url::parse(t).ok())
.collect(),
name: None, name: None,
} }
} }
@ -1312,7 +1319,7 @@ impl Session {
fn make_peer_rx( fn make_peer_rx(
self: &Arc<Self>, self: &Arc<Self>,
info_hash: Id20, info_hash: Id20,
mut trackers: Vec<String>, mut trackers: Vec<url::Url>,
announce: bool, announce: bool,
force_tracker_interval: Option<Duration>, force_tracker_interval: Option<Duration>,
initial_peers: Vec<SocketAddr>, initial_peers: Vec<SocketAddr>,
@ -1329,7 +1336,7 @@ impl Session {
if is_private && trackers.len() > 1 { if is_private && trackers.len() > 1 {
warn!("private trackers are not fully implemented, so using only the first tracker"); warn!("private trackers are not fully implemented, so using only the first tracker");
trackers.resize_with(1, Default::default); trackers.truncate(1);
} }
let tracker_rx_stats = PeerRxTorrentInfo { let tracker_rx_stats = PeerRxTorrentInfo {
@ -1339,7 +1346,7 @@ impl Session {
let tracker_rx = TrackerComms::start( let tracker_rx = TrackerComms::start(
info_hash, info_hash,
self.peer_id, self.peer_id,
trackers, trackers.into_iter().collect(),
Box::new(tracker_rx_stats), Box::new(tracker_rx_stats),
force_tracker_interval, force_tracker_interval,
announce_port, announce_port,
@ -1396,7 +1403,7 @@ impl Session {
self: &Arc<Self>, self: &Arc<Self>,
info_hash: Id20, info_hash: Id20,
peer_rx: PeerStream, peer_rx: PeerStream,
trackers: &[String], trackers: &[url::Url],
peer_opts: Option<PeerConnectionOptions>, peer_opts: Option<PeerConnectionOptions>,
) -> anyhow::Result<ResolveMagnetResult> { ) -> anyhow::Result<ResolveMagnetResult> {
match read_metainfo_from_peer_receiver( match read_metainfo_from_peer_receiver(
@ -1528,9 +1535,10 @@ mod tests {
#[test] #[test]
fn test_torrent_file_from_info_and_bytes() { fn test_torrent_file_from_info_and_bytes() {
fn get_trackers(info: &TorrentMetaV1<ByteBuf>) -> Vec<String> { fn get_trackers(info: &TorrentMetaV1<ByteBuf>) -> Vec<url::Url> {
info.iter_announce() info.iter_announce()
.filter_map(|t| std::str::from_utf8(t.as_ref()).ok().map(|t| t.to_owned())) .filter_map(|t| std::str::from_utf8(t.as_ref()).ok().map(|t| t.to_owned()))
.filter_map(|t| t.parse().ok())
.collect_vec() .collect_vec()
} }

View file

@ -114,7 +114,7 @@ impl SessionPersistenceStore for PostgresSessionStorage {
.shared() .shared()
.trackers .trackers
.iter() .iter()
.cloned() .map(|t| t.to_string())
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )
.bind( .bind(

View file

@ -188,7 +188,7 @@ pub struct ManagedTorrentShared {
pub id: TorrentId, pub id: TorrentId,
pub info_hash: Id20, pub info_hash: Id20,
pub(crate) spawner: BlockingSpawner, pub(crate) spawner: BlockingSpawner,
pub trackers: HashSet<String>, pub trackers: HashSet<url::Url>,
pub peer_id: Id20, pub peer_id: Id20,
pub span: tracing::Span, pub span: tracing::Span,
pub(crate) options: ManagedTorrentOptions, pub(crate) options: ManagedTorrentOptions,

View file

@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -91,7 +92,7 @@ impl TrackerComms {
pub fn start( pub fn start(
info_hash: Id20, info_hash: Id20,
peer_id: Id20, peer_id: Id20,
trackers: Vec<String>, trackers: HashSet<Url>,
stats: Box<dyn TorrentStatsProvider>, stats: Box<dyn TorrentStatsProvider>,
force_interval: Option<Duration>, force_interval: Option<Duration>,
tcp_listen_port: Option<u16>, tcp_listen_port: Option<u16>,
@ -99,19 +100,13 @@ impl TrackerComms {
) -> Option<BoxStream<'static, SocketAddr>> { ) -> Option<BoxStream<'static, SocketAddr>> {
let trackers = trackers let trackers = trackers
.into_iter() .into_iter()
.filter_map(|t| match Url::parse(&t) { .filter_map(|t| match t.scheme() {
Ok(parsed) => match parsed.scheme() { "http" | "https" => Some(SupportedTracker::Http(t)),
"http" | "https" => Some(SupportedTracker::Http(parsed)), "udp" => Some(SupportedTracker::Udp(t)),
"udp" => Some(SupportedTracker::Udp(parsed)),
_ => { _ => {
debug!("unsuppoted tracker URL: {}", t); debug!("unsuppoted tracker URL: {}", t);
None None
} }
},
Err(e) => {
debug!("error parsing tracker URL {}: {}", t, e);
None
}
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if trackers.is_empty() { if trackers.is_empty() {