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:?}");
}
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)]
struct Tmp<'a> {
announce: &'a str,
#[serde(rename = "announce-list")]
announce_list: &'a [&'a [String]],
announce_list: &'a [&'a [url::Url]],
info: bencode::raw_value::RawValue<&'a [u8]>,
}
@ -472,7 +472,7 @@ pub(crate) struct CheckedIncomingConnection {
struct InternalAddResult {
info_hash: Id20,
metadata: Option<TorrentMetadata>,
trackers: Vec<String>,
trackers: Vec<url::Url>,
name: Option<String>,
}
@ -909,7 +909,11 @@ impl Session {
InternalAddResult {
info_hash,
trackers: magnet.trackers,
trackers: magnet
.trackers
.into_iter()
.filter_map(|t| url::Url::parse(&t).ok())
.collect(),
metadata: None,
name: magnet.name,
}
@ -955,7 +959,10 @@ impl Session {
torrent.torrent_bytes,
torrent.info_bytes,
)?),
trackers,
trackers: trackers
.iter()
.filter_map(|t| url::Url::parse(t).ok())
.collect(),
name: None,
}
}
@ -1312,7 +1319,7 @@ impl Session {
fn make_peer_rx(
self: &Arc<Self>,
info_hash: Id20,
mut trackers: Vec<String>,
mut trackers: Vec<url::Url>,
announce: bool,
force_tracker_interval: Option<Duration>,
initial_peers: Vec<SocketAddr>,
@ -1329,7 +1336,7 @@ impl Session {
if is_private && trackers.len() > 1 {
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 {
@ -1339,7 +1346,7 @@ impl Session {
let tracker_rx = TrackerComms::start(
info_hash,
self.peer_id,
trackers,
trackers.into_iter().collect(),
Box::new(tracker_rx_stats),
force_tracker_interval,
announce_port,
@ -1396,7 +1403,7 @@ impl Session {
self: &Arc<Self>,
info_hash: Id20,
peer_rx: PeerStream,
trackers: &[String],
trackers: &[url::Url],
peer_opts: Option<PeerConnectionOptions>,
) -> anyhow::Result<ResolveMagnetResult> {
match read_metainfo_from_peer_receiver(
@ -1528,9 +1535,10 @@ mod tests {
#[test]
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()
.filter_map(|t| std::str::from_utf8(t.as_ref()).ok().map(|t| t.to_owned()))
.filter_map(|t| t.parse().ok())
.collect_vec()
}

View file

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

View file

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

View file

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