diff --git a/packages/primitives/src/info_hash.rs b/packages/primitives/src/info_hash.rs deleted file mode 100644 index 61b40a746..000000000 --- a/packages/primitives/src/info_hash.rs +++ /dev/null @@ -1,220 +0,0 @@ -use std::hash::{DefaultHasher, Hash, Hasher}; -use std::ops::{Deref, DerefMut}; -use std::panic::Location; - -use thiserror::Error; -use zerocopy::FromBytes; - -/// `BitTorrent` Info Hash v1 -#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] -pub struct InfoHash { - data: aquatic_udp_protocol::InfoHash, -} - -pub const INFO_HASH_BYTES_LEN: usize = 20; - -impl InfoHash { - /// Create a new `InfoHash` from a byte slice. - /// - /// # Panics - /// - /// Will panic if byte slice does not contains the exact amount of bytes need for the `InfoHash`. - #[must_use] - pub fn from_bytes(bytes: &[u8]) -> Self { - let data = aquatic_udp_protocol::InfoHash::read_from(bytes).expect("it should have the exact amount of bytes"); - - Self { data } - } - - /// Returns the `InfoHash` internal byte array. - #[must_use] - pub fn bytes(&self) -> [u8; 20] { - self.0 - } - - /// Returns the `InfoHash` as a hex string. - #[must_use] - pub fn to_hex_string(&self) -> String { - self.to_string() - } -} - -impl Default for InfoHash { - fn default() -> Self { - Self { - data: aquatic_udp_protocol::InfoHash(Default::default()), - } - } -} - -impl From for InfoHash { - fn from(data: aquatic_udp_protocol::InfoHash) -> Self { - Self { data } - } -} - -impl Deref for InfoHash { - type Target = aquatic_udp_protocol::InfoHash; - - fn deref(&self) -> &Self::Target { - &self.data - } -} - -impl DerefMut for InfoHash { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.data - } -} - -impl Ord for InfoHash { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.0.cmp(&other.0) - } -} - -impl PartialOrd for InfoHash { - fn partial_cmp(&self, other: &InfoHash) -> Option { - Some(self.cmp(other)) - } -} - -impl std::fmt::Display for InfoHash { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut chars = [0u8; 40]; - binascii::bin2hex(&self.0, &mut chars).expect("failed to hexlify"); - write!(f, "{}", std::str::from_utf8(&chars).unwrap()) - } -} - -impl std::str::FromStr for InfoHash { - type Err = binascii::ConvertError; - - fn from_str(s: &str) -> Result { - let mut i = Self::default(); - if s.len() != 40 { - return Err(binascii::ConvertError::InvalidInputLength); - } - binascii::hex2bin(s.as_bytes(), &mut i.0)?; - Ok(i) - } -} - -impl std::convert::From<&[u8]> for InfoHash { - fn from(data: &[u8]) -> InfoHash { - assert_eq!(data.len(), 20); - let mut ret = Self::default(); - ret.0.clone_from_slice(data); - ret - } -} - -/// for testing -impl std::convert::From<&DefaultHasher> for InfoHash { - fn from(data: &DefaultHasher) -> InfoHash { - let n = data.finish().to_le_bytes(); - let bytes = [ - n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[0], n[1], n[2], - n[3], - ]; - let data = aquatic_udp_protocol::InfoHash(bytes); - Self { data } - } -} - -impl std::convert::From<&i32> for InfoHash { - fn from(n: &i32) -> InfoHash { - let n = n.to_le_bytes(); - let bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, n[0], n[1], n[2], n[3]]; - let data = aquatic_udp_protocol::InfoHash(bytes); - Self { data } - } -} - -impl std::convert::From<[u8; 20]> for InfoHash { - fn from(bytes: [u8; 20]) -> Self { - let data = aquatic_udp_protocol::InfoHash(bytes); - Self { data } - } -} - -/// Errors that can occur when converting from a `Vec` to an `InfoHash`. -#[derive(Error, Debug)] -pub enum ConversionError { - /// Not enough bytes for infohash. An infohash is 20 bytes. - #[error("not enough bytes for infohash: {message} {location}")] - NotEnoughBytes { - location: &'static Location<'static>, - message: String, - }, - /// Too many bytes for infohash. An infohash is 20 bytes. - #[error("too many bytes for infohash: {message} {location}")] - TooManyBytes { - location: &'static Location<'static>, - message: String, - }, -} - -impl TryFrom> for InfoHash { - type Error = ConversionError; - - fn try_from(bytes: Vec) -> Result { - if bytes.len() < INFO_HASH_BYTES_LEN { - return Err(ConversionError::NotEnoughBytes { - location: Location::caller(), - message: format! {"got {} bytes, expected {}", bytes.len(), INFO_HASH_BYTES_LEN}, - }); - } - if bytes.len() > INFO_HASH_BYTES_LEN { - return Err(ConversionError::TooManyBytes { - location: Location::caller(), - message: format! {"got {} bytes, expected {}", bytes.len(), INFO_HASH_BYTES_LEN}, - }); - } - Ok(Self::from_bytes(&bytes)) - } -} - -impl serde::ser::Serialize for InfoHash { - fn serialize(&self, serializer: S) -> Result { - let mut buffer = [0u8; 40]; - let bytes_out = binascii::bin2hex(&self.0, &mut buffer).ok().unwrap(); - let str_out = std::str::from_utf8(bytes_out).unwrap(); - serializer.serialize_str(str_out) - } -} - -impl<'de> serde::de::Deserialize<'de> for InfoHash { - fn deserialize>(des: D) -> Result { - des.deserialize_str(InfoHashVisitor) - } -} - -struct InfoHashVisitor; - -impl<'v> serde::de::Visitor<'v> for InfoHashVisitor { - type Value = InfoHash; - - fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(formatter, "a 40 character long hash") - } - - fn visit_str(self, v: &str) -> Result { - if v.len() != 40 { - return Err(serde::de::Error::invalid_value( - serde::de::Unexpected::Str(v), - &"a 40 character long string", - )); - } - - let mut res = InfoHash::default(); - - if binascii::hex2bin(v.as_bytes(), &mut res.0).is_err() { - return Err(serde::de::Error::invalid_value( - serde::de::Unexpected::Str(v), - &"a hexadecimal string", - )); - }; - Ok(res) - } -} diff --git a/packages/primitives/src/lib.rs b/packages/primitives/src/lib.rs index 08fc58976..667bf7015 100644 --- a/packages/primitives/src/lib.rs +++ b/packages/primitives/src/lib.rs @@ -4,17 +4,15 @@ //! which is a `BitTorrent` tracker server. These structures are used not only //! by the tracker server crate, but also by other crates in the Torrust //! ecosystem. -use std::collections::BTreeMap; -use std::time::Duration; - -use info_hash::InfoHash; - -pub mod info_hash; pub mod pagination; pub mod peer; pub mod swarm_metadata; pub mod torrent_metrics; +use bittorrent_primitives::info_hash::InfoHash; +use std::collections::BTreeMap; +use std::time::Duration; + /// Duration since the Unix Epoch. pub type DurationSinceUnixEpoch = Duration; diff --git a/packages/torrent-repository/benches/helpers/asyn.rs b/packages/torrent-repository/benches/helpers/asyn.rs index 08862abc8..dec3984c6 100644 --- a/packages/torrent-repository/benches/helpers/asyn.rs +++ b/packages/torrent-repository/benches/helpers/asyn.rs @@ -1,8 +1,8 @@ use std::sync::Arc; use std::time::{Duration, Instant}; +use bittorrent_primitives::info_hash::InfoHash; use futures::stream::FuturesUnordered; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_torrent_repository::repository::RepositoryAsync; use super::utils::{generate_unique_info_hashes, DEFAULT_PEER}; diff --git a/packages/torrent-repository/benches/helpers/sync.rs b/packages/torrent-repository/benches/helpers/sync.rs index 77055911d..048e709bc 100644 --- a/packages/torrent-repository/benches/helpers/sync.rs +++ b/packages/torrent-repository/benches/helpers/sync.rs @@ -1,8 +1,8 @@ use std::sync::Arc; use std::time::{Duration, Instant}; +use bittorrent_primitives::info_hash::InfoHash; use futures::stream::FuturesUnordered; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_torrent_repository::repository::Repository; use super::utils::{generate_unique_info_hashes, DEFAULT_PEER}; diff --git a/packages/torrent-repository/benches/helpers/utils.rs b/packages/torrent-repository/benches/helpers/utils.rs index e21ac7332..51b09ec0f 100644 --- a/packages/torrent-repository/benches/helpers/utils.rs +++ b/packages/torrent-repository/benches/helpers/utils.rs @@ -2,7 +2,7 @@ use std::collections::HashSet; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer::Peer; use torrust_tracker_primitives::DurationSinceUnixEpoch; use zerocopy::I64; diff --git a/packages/torrent-repository/src/repository/dash_map_mutex_std.rs b/packages/torrent-repository/src/repository/dash_map_mutex_std.rs index 4354c12ec..54a83aeb4 100644 --- a/packages/torrent-repository/src/repository/dash_map_mutex_std.rs +++ b/packages/torrent-repository/src/repository/dash_map_mutex_std.rs @@ -1,8 +1,8 @@ use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use dashmap::DashMap; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; diff --git a/packages/torrent-repository/src/repository/mod.rs b/packages/torrent-repository/src/repository/mod.rs index f198288f8..14f03ed9d 100644 --- a/packages/torrent-repository/src/repository/mod.rs +++ b/packages/torrent-repository/src/repository/mod.rs @@ -1,5 +1,5 @@ +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; diff --git a/packages/torrent-repository/src/repository/rw_lock_std.rs b/packages/torrent-repository/src/repository/rw_lock_std.rs index 5439fdd79..409a16498 100644 --- a/packages/torrent-repository/src/repository/rw_lock_std.rs +++ b/packages/torrent-repository/src/repository/rw_lock_std.rs @@ -1,5 +1,5 @@ +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; @@ -21,7 +21,7 @@ impl RwLockStd { /// Panics if unable to get a lock. pub fn write( &self, - ) -> std::sync::RwLockWriteGuard<'_, std::collections::BTreeMap> { + ) -> std::sync::RwLockWriteGuard<'_, std::collections::BTreeMap> { self.torrents.write().expect("it should get lock") } } diff --git a/packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs b/packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs index 7d58b0b10..8814f09ed 100644 --- a/packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs +++ b/packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs @@ -1,7 +1,7 @@ use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; diff --git a/packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs b/packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs index 90451ca9f..46f4a9567 100644 --- a/packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs +++ b/packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs @@ -2,10 +2,10 @@ use std::iter::zip; use std::pin::Pin; use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use futures::future::join_all; use futures::{Future, FutureExt}; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; diff --git a/packages/torrent-repository/src/repository/rw_lock_tokio.rs b/packages/torrent-repository/src/repository/rw_lock_tokio.rs index baaa01232..ce6646e92 100644 --- a/packages/torrent-repository/src/repository/rw_lock_tokio.rs +++ b/packages/torrent-repository/src/repository/rw_lock_tokio.rs @@ -1,5 +1,5 @@ +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; @@ -19,10 +19,7 @@ impl RwLockTokio { pub fn write( &self, ) -> impl std::future::Future< - Output = tokio::sync::RwLockWriteGuard< - '_, - std::collections::BTreeMap, - >, + Output = tokio::sync::RwLockWriteGuard<'_, std::collections::BTreeMap>, > { self.torrents.write() } diff --git a/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs b/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs index 1887f70c7..7efb093e9 100644 --- a/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs +++ b/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs @@ -1,7 +1,7 @@ use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; diff --git a/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs b/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs index 6c9c08a73..e08a6af59 100644 --- a/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs +++ b/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs @@ -1,7 +1,7 @@ use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; diff --git a/packages/torrent-repository/src/repository/skip_map_mutex_std.rs b/packages/torrent-repository/src/repository/skip_map_mutex_std.rs index dd0d9c1b1..47fe9620a 100644 --- a/packages/torrent-repository/src/repository/skip_map_mutex_std.rs +++ b/packages/torrent-repository/src/repository/skip_map_mutex_std.rs @@ -1,8 +1,8 @@ use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use crossbeam_skiplist::SkipMap; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; diff --git a/packages/torrent-repository/tests/common/repo.rs b/packages/torrent-repository/tests/common/repo.rs index f317d0d17..ebd829f3c 100644 --- a/packages/torrent-repository/tests/common/repo.rs +++ b/packages/torrent-repository/tests/common/repo.rs @@ -1,5 +1,5 @@ +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; diff --git a/packages/torrent-repository/tests/repository/mod.rs b/packages/torrent-repository/tests/repository/mod.rs index 05d538582..c5cf2059c 100644 --- a/packages/torrent-repository/tests/repository/mod.rs +++ b/packages/torrent-repository/tests/repository/mod.rs @@ -2,9 +2,9 @@ use std::collections::{BTreeMap, HashSet}; use std::hash::{DefaultHasher, Hash, Hasher}; use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes}; +use bittorrent_primitives::info_hash::InfoHash; use rstest::{fixture, rstest}; use torrust_tracker_configuration::TrackerPolicy; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::PersistentTorrents; diff --git a/src/console/clients/checker/checks/http.rs b/src/console/clients/checker/checks/http.rs index 0904f4e6e..b64297bed 100644 --- a/src/console/clients/checker/checks/http.rs +++ b/src/console/clients/checker/checks/http.rs @@ -1,8 +1,8 @@ use std::str::FromStr as _; use std::time::Duration; +use bittorrent_primitives::info_hash::InfoHash; use serde::Serialize; -use torrust_tracker_primitives::info_hash::InfoHash; use url::Url; use crate::console::clients::http::Error; diff --git a/src/console/clients/http/app.rs b/src/console/clients/http/app.rs index a54db5f8b..6730c027d 100644 --- a/src/console/clients/http/app.rs +++ b/src/console/clients/http/app.rs @@ -17,10 +17,10 @@ use std::str::FromStr; use std::time::Duration; use anyhow::Context; +use bittorrent_primitives::info_hash::InfoHash; use clap::{Parser, Subcommand}; use reqwest::Url; use torrust_tracker_configuration::DEFAULT_TIMEOUT; -use torrust_tracker_primitives::info_hash::InfoHash; use crate::shared::bit_torrent::tracker::http::client::requests::announce::QueryBuilder; use crate::shared::bit_torrent::tracker::http::client::responses::announce::Announce; diff --git a/src/console/clients/udp/app.rs b/src/console/clients/udp/app.rs index c2ba647b8..a2736c365 100644 --- a/src/console/clients/udp/app.rs +++ b/src/console/clients/udp/app.rs @@ -61,9 +61,9 @@ use std::str::FromStr; use anyhow::Context; use aquatic_udp_protocol::{Response, TransactionId}; +use bittorrent_primitives::info_hash::InfoHash as TorrustInfoHash; use clap::{Parser, Subcommand}; use torrust_tracker_configuration::DEFAULT_TIMEOUT; -use torrust_tracker_primitives::info_hash::InfoHash as TorrustInfoHash; use tracing::level_filters::LevelFilter; use url::Url; diff --git a/src/console/clients/udp/checker.rs b/src/console/clients/udp/checker.rs index 437af33e0..14e94c132 100644 --- a/src/console/clients/udp/checker.rs +++ b/src/console/clients/udp/checker.rs @@ -7,7 +7,7 @@ use aquatic_udp_protocol::{ AnnounceActionPlaceholder, AnnounceEvent, AnnounceRequest, ConnectRequest, ConnectionId, NumberOfBytes, NumberOfPeers, PeerId, PeerKey, Port, Response, ScrapeRequest, TransactionId, }; -use torrust_tracker_primitives::info_hash::InfoHash as TorrustInfoHash; +use bittorrent_primitives::info_hash::InfoHash as TorrustInfoHash; use super::Error; use crate::shared::bit_torrent::tracker::udp::client::UdpTrackerClient; diff --git a/src/core/databases/mod.rs b/src/core/databases/mod.rs index f559eb80e..e29ce22e8 100644 --- a/src/core/databases/mod.rs +++ b/src/core/databases/mod.rs @@ -50,7 +50,7 @@ pub mod sqlite; use std::marker::PhantomData; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::PersistentTorrents; use self::error::Error; diff --git a/src/core/databases/mysql.rs b/src/core/databases/mysql.rs index 28a5f363b..1b849421b 100644 --- a/src/core/databases/mysql.rs +++ b/src/core/databases/mysql.rs @@ -2,11 +2,11 @@ use std::str::FromStr; use std::time::Duration; +use bittorrent_primitives::info_hash::InfoHash; use r2d2::Pool; use r2d2_mysql::mysql::prelude::Queryable; use r2d2_mysql::mysql::{params, Opts, OptsBuilder}; use r2d2_mysql::MySqlConnectionManager; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::PersistentTorrents; use super::driver::Driver; diff --git a/src/core/databases/sqlite.rs b/src/core/databases/sqlite.rs index 69470ee04..5bb23bb3e 100644 --- a/src/core/databases/sqlite.rs +++ b/src/core/databases/sqlite.rs @@ -2,11 +2,11 @@ use std::panic::Location; use std::str::FromStr; +use bittorrent_primitives::info_hash::InfoHash; use r2d2::Pool; use r2d2_sqlite::rusqlite::params; use r2d2_sqlite::rusqlite::types::Null; use r2d2_sqlite::SqliteConnectionManager; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::{DurationSinceUnixEpoch, PersistentTorrents}; use super::driver::Driver; diff --git a/src/core/error.rs b/src/core/error.rs index d89b030c4..ba87c84c8 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -8,8 +8,8 @@ //! use std::panic::Location; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_located_error::LocatedError; -use torrust_tracker_primitives::info_hash::InfoHash; use super::auth::ParseKeyError; use super::databases; diff --git a/src/core/mod.rs b/src/core/mod.rs index f12eb9a3d..ffae8b9ce 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -63,7 +63,7 @@ //! use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; //! use torrust_tracker_primitives::DurationSinceUnixEpoch; //! use torrust_tracker_primitives::peer; -//! use torrust_tracker_primitives::info_hash::InfoHash; +//! use bittorrent_primitives::info_hash::InfoHash; //! //! let info_hash = InfoHash::from_str("3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0").unwrap(); //! @@ -136,7 +136,7 @@ //! The returned struct is: //! //! ```rust,no_run -//! use torrust_tracker_primitives::info_hash::InfoHash; +//! use bittorrent_primitives::info_hash::InfoHash; //! use std::collections::HashMap; //! //! pub struct ScrapeData { @@ -165,7 +165,7 @@ //! There are two data structures for infohashes: byte arrays and hex strings: //! //! ```rust,no_run -//! use torrust_tracker_primitives::info_hash::InfoHash; +//! use bittorrent_primitives::info_hash::InfoHash; //! use std::str::FromStr; //! //! let info_hash: InfoHash = [255u8; 20].into(); @@ -456,6 +456,7 @@ use std::sync::Arc; use std::time::Duration; use auth::PeerKey; +use bittorrent_primitives::info_hash::InfoHash; use databases::driver::Driver; use derive_more::Constructor; use error::PeerKeyError; @@ -464,7 +465,6 @@ use torrust_tracker_clock::clock::Time; use torrust_tracker_configuration::v2_0_0::database; use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT}; use torrust_tracker_located_error::Located; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; @@ -1253,15 +1253,15 @@ mod tests { use std::sync::Arc; use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TORRENT_PEERS_LIMIT; - use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::DurationSinceUnixEpoch; use torrust_tracker_test_helpers::configuration; use crate::core::peer::Peer; use crate::core::services::tracker_factory; use crate::core::{TorrentsMetrics, Tracker}; - use crate::shared::bit_torrent::info_hash::fixture::gen_seeded_infohash; + use bittorrent_primitives::info_hash::fixture::gen_seeded_infohash; fn public_tracker() -> Tracker { tracker_factory(&configuration::ephemeral_public()) @@ -1716,7 +1716,7 @@ mod tests { use std::net::{IpAddr, Ipv4Addr}; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use crate::core::tests::the_tracker::{complete_peer, incomplete_peer, public_tracker}; use crate::core::{PeersWanted, ScrapeData, SwarmMetadata}; @@ -1880,7 +1880,7 @@ mod tests { mod handling_an_scrape_request { - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use crate::core::tests::the_tracker::{ diff --git a/src/core/services/torrent.rs b/src/core/services/torrent.rs index 3b014982d..e63d2efa2 100644 --- a/src/core/services/torrent.rs +++ b/src/core/services/torrent.rs @@ -6,7 +6,7 @@ //! - [`get_torrents`]: it returns data about some torrent in bulk excluding the peer list. use std::sync::Arc; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::peer; use torrust_tracker_torrent_repository::entry::EntrySync; @@ -125,8 +125,8 @@ mod tests { use std::str::FromStr; use std::sync::Arc; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::Configuration; - use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_test_helpers::configuration; use crate::core::services::torrent::tests::sample_peer; @@ -178,8 +178,8 @@ mod tests { use std::str::FromStr; use std::sync::Arc; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::Configuration; - use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_test_helpers::configuration; use crate::core::services::torrent::tests::sample_peer; diff --git a/src/servers/apis/v1/context/torrent/handlers.rs b/src/servers/apis/v1/context/torrent/handlers.rs index ebca504fd..0ba713f62 100644 --- a/src/servers/apis/v1/context/torrent/handlers.rs +++ b/src/servers/apis/v1/context/torrent/handlers.rs @@ -7,9 +7,9 @@ use std::sync::Arc; use axum::extract::{Path, State}; use axum::response::{IntoResponse, Response}; use axum_extra::extract::Query; +use bittorrent_primitives::info_hash::InfoHash; use serde::{de, Deserialize, Deserializer}; use thiserror::Error; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use super::responses::{torrent_info_response, torrent_list_response, torrent_not_known_response}; diff --git a/src/servers/apis/v1/context/torrent/resources/torrent.rs b/src/servers/apis/v1/context/torrent/resources/torrent.rs index 657382c0c..8fbb89418 100644 --- a/src/servers/apis/v1/context/torrent/resources/torrent.rs +++ b/src/servers/apis/v1/context/torrent/resources/torrent.rs @@ -98,7 +98,7 @@ mod tests { use std::str::FromStr; use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; use super::Torrent; diff --git a/src/servers/apis/v1/context/whitelist/handlers.rs b/src/servers/apis/v1/context/whitelist/handlers.rs index 32e434918..04085f8ab 100644 --- a/src/servers/apis/v1/context/whitelist/handlers.rs +++ b/src/servers/apis/v1/context/whitelist/handlers.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use axum::extract::{Path, State}; use axum::response::Response; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use super::responses::{ failed_to_reload_whitelist_response, failed_to_remove_torrent_from_whitelist_response, failed_to_whitelist_torrent_response, diff --git a/src/servers/http/percent_encoding.rs b/src/servers/http/percent_encoding.rs index c3243d597..323444cc7 100644 --- a/src/servers/http/percent_encoding.rs +++ b/src/servers/http/percent_encoding.rs @@ -16,7 +16,7 @@ //! - //! - use aquatic_udp_protocol::PeerId; -use torrust_tracker_primitives::info_hash::{self, InfoHash}; +use bittorrent_primitives::info_hash::{self, InfoHash}; use torrust_tracker_primitives::peer; /// Percent decodes a percent encoded infohash. Internally an @@ -28,7 +28,7 @@ use torrust_tracker_primitives::peer; /// ```rust /// use std::str::FromStr; /// use torrust_tracker::servers::http::percent_encoding::percent_decode_info_hash; -/// use torrust_tracker_primitives::info_hash::InfoHash; +/// use bittorrent_primitives::info_hash::InfoHash; /// use torrust_tracker_primitives::peer; /// /// let encoded_infohash = "%3B%24U%04%CF%5F%11%BB%DB%E1%20%1C%EAjk%F4Z%EE%1B%C0"; @@ -61,7 +61,7 @@ pub fn percent_decode_info_hash(raw_info_hash: &str) -> Result) -> Result) -> AnnounceEvent { mod tests { use aquatic_udp_protocol::PeerId; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_test_helpers::configuration; use crate::core::services::tracker_factory; diff --git a/src/servers/http/v1/handlers/scrape.rs b/src/servers/http/v1/handlers/scrape.rs index ca4c85207..10f945d70 100644 --- a/src/servers/http/v1/handlers/scrape.rs +++ b/src/servers/http/v1/handlers/scrape.rs @@ -110,7 +110,7 @@ mod tests { use std::net::IpAddr; use std::str::FromStr; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_test_helpers::configuration; use crate::core::services::tracker_factory; diff --git a/src/servers/http/v1/requests/announce.rs b/src/servers/http/v1/requests/announce.rs index 029bdbc01..a9a9f8a76 100644 --- a/src/servers/http/v1/requests/announce.rs +++ b/src/servers/http/v1/requests/announce.rs @@ -6,9 +6,9 @@ use std::panic::Location; use std::str::FromStr; use aquatic_udp_protocol::{NumberOfBytes, PeerId}; +use bittorrent_primitives::info_hash::{self, InfoHash}; use thiserror::Error; use torrust_tracker_located_error::{Located, LocatedError}; -use torrust_tracker_primitives::info_hash::{self, InfoHash}; use torrust_tracker_primitives::peer; use crate::servers::http::percent_encoding::{percent_decode_info_hash, percent_decode_peer_id}; @@ -32,7 +32,7 @@ const NUMWANT: &str = "numwant"; /// ```rust /// use aquatic_udp_protocol::{NumberOfBytes, PeerId}; /// use torrust_tracker::servers::http::v1::requests::announce::{Announce, Compact, Event}; -/// use torrust_tracker_primitives::info_hash::InfoHash; +/// use bittorrent_primitives::info_hash::InfoHash; /// /// let request = Announce { /// // Mandatory params @@ -379,7 +379,7 @@ mod tests { mod announce_request { use aquatic_udp_protocol::{NumberOfBytes, PeerId}; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use crate::servers::http::v1::query::Query; use crate::servers::http::v1::requests::announce::{ diff --git a/src/servers/http/v1/requests/scrape.rs b/src/servers/http/v1/requests/scrape.rs index c61d3be1f..0a47a4fb4 100644 --- a/src/servers/http/v1/requests/scrape.rs +++ b/src/servers/http/v1/requests/scrape.rs @@ -3,9 +3,9 @@ //! Data structures and logic for parsing the `scrape` request. use std::panic::Location; +use bittorrent_primitives::info_hash::{self, InfoHash}; use thiserror::Error; use torrust_tracker_located_error::{Located, LocatedError}; -use torrust_tracker_primitives::info_hash::{self, InfoHash}; use crate::servers::http::percent_encoding::percent_decode_info_hash; use crate::servers::http::v1::query::Query; @@ -84,7 +84,7 @@ mod tests { mod scrape_request { - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use crate::servers::http::v1::query::Query; use crate::servers::http::v1::requests::scrape::{Scrape, INFO_HASH}; diff --git a/src/servers/http/v1/responses/scrape.rs b/src/servers/http/v1/responses/scrape.rs index 9690d4392..0aef70cb1 100644 --- a/src/servers/http/v1/responses/scrape.rs +++ b/src/servers/http/v1/responses/scrape.rs @@ -13,7 +13,7 @@ use crate::core::ScrapeData; /// /// ```rust /// use torrust_tracker::servers::http::v1::responses::scrape::Bencoded; -/// use torrust_tracker_primitives::info_hash::InfoHash; +/// use bittorrent_primitives::info_hash::InfoHash; /// use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; /// use torrust_tracker::core::ScrapeData; /// @@ -92,7 +92,7 @@ impl IntoResponse for Bencoded { mod tests { mod scrape_response { - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use crate::core::ScrapeData; diff --git a/src/servers/http/v1/services/announce.rs b/src/servers/http/v1/services/announce.rs index 9c5dfdad2..51ec43d56 100644 --- a/src/servers/http/v1/services/announce.rs +++ b/src/servers/http/v1/services/announce.rs @@ -11,7 +11,7 @@ use std::net::IpAddr; use std::sync::Arc; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer; use crate::core::{statistics, AnnounceData, PeersWanted, Tracker}; @@ -54,7 +54,7 @@ mod tests { use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; use torrust_tracker_test_helpers::configuration; diff --git a/src/servers/http/v1/services/scrape.rs b/src/servers/http/v1/services/scrape.rs index 0d561c7bc..f040e0430 100644 --- a/src/servers/http/v1/services/scrape.rs +++ b/src/servers/http/v1/services/scrape.rs @@ -11,7 +11,7 @@ use std::net::IpAddr; use std::sync::Arc; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use crate::core::{statistics, ScrapeData, Tracker}; @@ -62,7 +62,7 @@ mod tests { use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; use torrust_tracker_test_helpers::configuration; diff --git a/src/servers/udp/handlers.rs b/src/servers/udp/handlers.rs index 69a427e0e..6af634c32 100644 --- a/src/servers/udp/handlers.rs +++ b/src/servers/udp/handlers.rs @@ -10,8 +10,8 @@ use aquatic_udp_protocol::{ ErrorResponse, Ipv4AddrBytes, Ipv6AddrBytes, NumberOfDownloads, NumberOfPeers, Port, Request, Response, ResponsePeer, ScrapeRequest, ScrapeResponse, TorrentScrapeStatistics, TransactionId, }; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_located_error::DynError; -use torrust_tracker_primitives::info_hash::InfoHash; use tracing::{instrument, Level}; use uuid::Uuid; use zerocopy::network_endian::I32; diff --git a/src/servers/udp/logging.rs b/src/servers/udp/logging.rs index 3891278d7..a61668e83 100644 --- a/src/servers/udp/logging.rs +++ b/src/servers/udp/logging.rs @@ -4,7 +4,7 @@ use std::net::SocketAddr; use std::time::Duration; use aquatic_udp_protocol::{Request, Response, TransactionId}; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use super::handlers::RequestId; use crate::servers::udp::UDP_TRACKER_LOG_TARGET; diff --git a/src/servers/udp/mod.rs b/src/servers/udp/mod.rs index 91b19a91d..d41bc8b3f 100644 --- a/src/servers/udp/mod.rs +++ b/src/servers/udp/mod.rs @@ -342,7 +342,7 @@ //! > packet. //! //! We are using a wrapper struct for the aquatic [`AnnounceRequest`](aquatic_udp_protocol::request::AnnounceRequest) -//! struct, because we have our internal [`InfoHash`](torrust_tracker_primitives::info_hash::InfoHash) +//! struct, because we have our internal [`InfoHash`](bittorrent_primitives::info_hash::InfoHash) //! struct. //! //! ```text diff --git a/src/shared/bit_torrent/info_hash.rs b/src/shared/bit_torrent/info_hash.rs deleted file mode 100644 index 506c37758..000000000 --- a/src/shared/bit_torrent/info_hash.rs +++ /dev/null @@ -1,288 +0,0 @@ -//! A `BitTorrent` `InfoHash`. It's a unique identifier for a `BitTorrent` torrent. -//! -//! "The 20-byte sha1 hash of the bencoded form of the info value -//! from the metainfo file." -//! -//! See [BEP 3. The `BitTorrent` Protocol Specification](https://www.bittorrent.org/beps/bep_0003.html) -//! for the official specification. -//! -//! This modules provides a type that can be used to represent infohashes. -//! -//! > **NOTICE**: It only supports Info Hash v1. -//! -//! Typically infohashes are represented as hex strings, but internally they are -//! a 20-byte array. -//! -//! # Calculating the info-hash of a torrent file -//! -//! A sample torrent: -//! -//! - Torrent file: `mandelbrot_2048x2048_infohash_v1.png.torrent` -//! - File: `mandelbrot_2048x2048.png` -//! - Info Hash v1: `5452869be36f9f3350ccee6b4544e7e76caaadab` -//! - Sha1 hash of the info dictionary: `5452869BE36F9F3350CCEE6B4544E7E76CAAADAB` -//! -//! A torrent file is a binary file encoded with [Bencode encoding](https://en.wikipedia.org/wiki/Bencode): -//! -//! ```text -//! 0000000: 6431 303a 6372 6561 7465 6420 6279 3138 d10:created by18 -//! 0000010: 3a71 4269 7474 6f72 7265 6e74 2076 342e :qBittorrent v4. -//! 0000020: 342e 3131 333a 6372 6561 7469 6f6e 2064 4.113:creation d -//! 0000030: 6174 6569 3136 3739 3637 3436 3238 6534 atei1679674628e4 -//! 0000040: 3a69 6e66 6f64 363a 6c65 6e67 7468 6931 :infod6:lengthi1 -//! 0000050: 3732 3230 3465 343a 6e61 6d65 3234 3a6d 72204e4:name24:m -//! 0000060: 616e 6465 6c62 726f 745f 3230 3438 7832 andelbrot_2048x2 -//! 0000070: 3034 382e 706e 6731 323a 7069 6563 6520 048.png12:piece -//! 0000080: 6c65 6e67 7468 6931 3633 3834 6536 3a70 lengthi16384e6:p -//! 0000090: 6965 6365 7332 3230 3a7d 9171 0d9d 4dba ieces220:}.q..M. -//! 00000a0: 889b 5420 54d5 2672 8d5a 863f e121 df77 ..T T.&r.Z.?.!.w -//! 00000b0: c7f7 bb6c 7796 2166 2538 c5d9 cdab 8b08 ...lw.!f%8...... -//! 00000c0: ef8c 249b b2f5 c4cd 2adf 0bc0 0cf0 addf ..$.....*....... -//! 00000d0: 7290 e5b6 414c 236c 479b 8e9f 46aa 0c0d r...AL#lG...F... -//! 00000e0: 8ed1 97ff ee68 8b5f 34a3 87d7 71c5 a6f9 .....h._4...q... -//! 00000f0: 8e2e a631 7cbd f0f9 e223 f9cc 80af 5400 ...1|....#....T. -//! 0000100: 04f9 8569 1c77 89c1 764e d6aa bf61 a6c2 ...i.w..vN...a.. -//! 0000110: 8099 abb6 5f60 2f40 a825 be32 a33d 9d07 ...._`/@.%.2.=.. -//! 0000120: 0c79 6898 d49d 6349 af20 5866 266f 986b .yh...cI. Xf&o.k -//! 0000130: 6d32 34cd 7d08 155e 1ad0 0009 57ab 303b m24.}..^....W.0; -//! 0000140: 2060 c1dc 1287 d6f3 e745 4f70 6709 3631 `.......EOpg.61 -//! 0000150: 55f2 20f6 6ca5 156f 2c89 9569 1653 817d U. .l..o,..i.S.} -//! 0000160: 31f1 b6bd 3742 cc11 0bb2 fc2b 49a5 85b6 1...7B.....+I... -//! 0000170: fc76 7444 9365 65 .vtD.ee -//! ``` -//! -//! You can generate that output with the command: -//! -//! ```text -//! xxd mandelbrot_2048x2048_infohash_v1.png.torrent -//! ``` -//! -//! And you can show only the bytes (hexadecimal): -//! -//! ```text -//! 6431303a6372656174656420627931383a71426974746f7272656e742076 -//! 342e342e3131333a6372656174696f6e2064617465693136373936373436 -//! 323865343a696e666f64363a6c656e6774686931373232303465343a6e61 -//! 6d6532343a6d616e64656c62726f745f3230343878323034382e706e6731 -//! 323a7069656365206c656e67746869313633383465363a70696563657332 -//! 32303a7d91710d9d4dba889b542054d526728d5a863fe121df77c7f7bb6c -//! 779621662538c5d9cdab8b08ef8c249bb2f5c4cd2adf0bc00cf0addf7290 -//! e5b6414c236c479b8e9f46aa0c0d8ed197ffee688b5f34a387d771c5a6f9 -//! 8e2ea6317cbdf0f9e223f9cc80af540004f985691c7789c1764ed6aabf61 -//! a6c28099abb65f602f40a825be32a33d9d070c796898d49d6349af205866 -//! 266f986b6d3234cd7d08155e1ad0000957ab303b2060c1dc1287d6f3e745 -//! 4f706709363155f220f66ca5156f2c8995691653817d31f1b6bd3742cc11 -//! 0bb2fc2b49a585b6fc767444936565 -//! ``` -//! -//! You can generate that output with the command: -//! -//! ```text -//! `xxd -ps mandelbrot_2048x2048_infohash_v1.png.torrent`. -//! ``` -//! -//! The same data can be represented in a JSON format: -//! -//! ```json -//! { -//! "created by": "qBittorrent v4.4.1", -//! "creation date": 1679674628, -//! "info": { -//! "length": 172204, -//! "name": "mandelbrot_2048x2048.png", -//! "piece length": 16384, -//! "pieces": "7D 91 71 0D 9D 4D BA 88 9B 54 20 54 D5 26 72 8D 5A 86 3F E1 21 DF 77 C7 F7 BB 6C 77 96 21 66 25 38 C5 D9 CD AB 8B 08 EF 8C 24 9B B2 F5 C4 CD 2A DF 0B C0 0C F0 AD DF 72 90 E5 B6 41 4C 23 6C 47 9B 8E 9F 46 AA 0C 0D 8E D1 97 FF EE 68 8B 5F 34 A3 87 D7 71 C5 A6 F9 8E 2E A6 31 7C BD F0 F9 E2 23 F9 CC 80 AF 54 00 04 F9 85 69 1C 77 89 C1 76 4E D6 AA BF 61 A6 C2 80 99 AB B6 5F 60 2F 40 A8 25 BE 32 A3 3D 9D 07 0C 79 68 98 D4 9D 63 49 AF 20 58 66 26 6F 98 6B 6D 32 34 CD 7D 08 15 5E 1A D0 00 09 57 AB 30 3B 20 60 C1 DC 12 87 D6 F3 E7 45 4F 70 67 09 36 31 55 F2 20 F6 6C A5 15 6F 2C 89 95 69 16 53 81 7D 31 F1 B6 BD 37 42 CC 11 0B B2 FC 2B 49 A5 85 B6 FC 76 74 44 93" -//! } -//! } -//! ``` -//! -//! The JSON object was generated with: -//! -//! As you can see, there is a `info` attribute: -//! -//! ```json -//! { -//! "length": 172204, -//! "name": "mandelbrot_2048x2048.png", -//! "piece length": 16384, -//! "pieces": "7D 91 71 0D 9D 4D BA 88 9B 54 20 54 D5 26 72 8D 5A 86 3F E1 21 DF 77 C7 F7 BB 6C 77 96 21 66 25 38 C5 D9 CD AB 8B 08 EF 8C 24 9B B2 F5 C4 CD 2A DF 0B C0 0C F0 AD DF 72 90 E5 B6 41 4C 23 6C 47 9B 8E 9F 46 AA 0C 0D 8E D1 97 FF EE 68 8B 5F 34 A3 87 D7 71 C5 A6 F9 8E 2E A6 31 7C BD F0 F9 E2 23 F9 CC 80 AF 54 00 04 F9 85 69 1C 77 89 C1 76 4E D6 AA BF 61 A6 C2 80 99 AB B6 5F 60 2F 40 A8 25 BE 32 A3 3D 9D 07 0C 79 68 98 D4 9D 63 49 AF 20 58 66 26 6F 98 6B 6D 32 34 CD 7D 08 15 5E 1A D0 00 09 57 AB 30 3B 20 60 C1 DC 12 87 D6 F3 E7 45 4F 70 67 09 36 31 55 F2 20 F6 6C A5 15 6F 2C 89 95 69 16 53 81 7D 31 F1 B6 BD 37 42 CC 11 0B B2 FC 2B 49 A5 85 B6 FC 76 74 44 93" -//! } -//! ``` -//! -//! The infohash is the [SHA1](https://en.wikipedia.org/wiki/SHA-1) hash -//! of the `info` attribute. That is, the SHA1 hash of: -//! -//! ```text -//! 64363a6c656e6774686931373232303465343a6e61 -//! d6532343a6d616e64656c62726f745f3230343878323034382e706e6731 -//! 23a7069656365206c656e67746869313633383465363a70696563657332 -//! 2303a7d91710d9d4dba889b542054d526728d5a863fe121df77c7f7bb6c -//! 79621662538c5d9cdab8b08ef8c249bb2f5c4cd2adf0bc00cf0addf7290 -//! 5b6414c236c479b8e9f46aa0c0d8ed197ffee688b5f34a387d771c5a6f9 -//! e2ea6317cbdf0f9e223f9cc80af540004f985691c7789c1764ed6aabf61 -//! 6c28099abb65f602f40a825be32a33d9d070c796898d49d6349af205866 -//! 66f986b6d3234cd7d08155e1ad0000957ab303b2060c1dc1287d6f3e745 -//! f706709363155f220f66ca5156f2c8995691653817d31f1b6bd3742cc11 -//! bb2fc2b49a585b6fc7674449365 -//! ``` -//! -//! You can hash that byte string with -//! -//! The result is a 20-char string: `5452869BE36F9F3350CCEE6B4544E7E76CAAADAB` - -use torrust_tracker_primitives::info_hash::InfoHash; - -pub mod fixture { - use std::hash::{DefaultHasher, Hash, Hasher}; - - use super::InfoHash; - - /// Generate as semi-stable pseudo-random infohash - /// - /// Note: If the [`DefaultHasher`] implementation changes - /// so will the resulting info-hashes. - /// - /// The results should not be relied upon between versions. - #[must_use] - pub fn gen_seeded_infohash(seed: &u64) -> InfoHash { - let mut buf_a: [[u8; 8]; 4] = Default::default(); - let mut buf_b = InfoHash::default(); - - let mut hasher = DefaultHasher::new(); - seed.hash(&mut hasher); - - for u in &mut buf_a { - seed.hash(&mut hasher); - *u = hasher.finish().to_le_bytes(); - } - - for (a, b) in buf_a.iter().flat_map(|a| a.iter()).zip(buf_b.0.iter_mut()) { - *b = *a; - } - - buf_b - } -} - -#[cfg(test)] -mod tests { - - use std::str::FromStr; - - use serde::{Deserialize, Serialize}; - use serde_json::json; - - use super::InfoHash; - - #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] - struct ContainingInfoHash { - pub info_hash: InfoHash, - } - - #[test] - fn an_info_hash_can_be_created_from_a_valid_40_utf8_char_string_representing_an_hexadecimal_value() { - let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); - assert!(info_hash.is_ok()); - } - - #[test] - fn an_info_hash_can_not_be_created_from_a_utf8_string_representing_a_not_valid_hexadecimal_value() { - let info_hash = InfoHash::from_str("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG"); - assert!(info_hash.is_err()); - } - - #[test] - fn an_info_hash_can_only_be_created_from_a_40_utf8_char_string() { - let info_hash = InfoHash::from_str(&"F".repeat(39)); - assert!(info_hash.is_err()); - - let info_hash = InfoHash::from_str(&"F".repeat(41)); - assert!(info_hash.is_err()); - } - - #[test] - fn an_info_hash_should_by_displayed_like_a_40_utf8_lowercased_char_hex_string() { - let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap(); - - let output = format!("{info_hash}"); - - assert_eq!(output, "ffffffffffffffffffffffffffffffffffffffff"); - } - - #[test] - fn an_info_hash_should_return_its_a_40_utf8_lowercased_char_hex_representations_as_string() { - let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap(); - - assert_eq!(info_hash.to_hex_string(), "ffffffffffffffffffffffffffffffffffffffff"); - } - - #[test] - fn an_info_hash_can_be_created_from_a_valid_20_byte_array_slice() { - let info_hash: InfoHash = [255u8; 20].as_slice().into(); - - assert_eq!( - info_hash, - InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap() - ); - } - - #[test] - fn an_info_hash_can_be_created_from_a_valid_20_byte_array() { - let info_hash: InfoHash = [255u8; 20].into(); - - assert_eq!( - info_hash, - InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap() - ); - } - - #[test] - fn an_info_hash_can_be_created_from_a_byte_vector() { - let info_hash: InfoHash = [255u8; 20].to_vec().try_into().unwrap(); - - assert_eq!( - info_hash, - InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap() - ); - } - - #[test] - fn it_should_fail_trying_to_create_an_info_hash_from_a_byte_vector_with_less_than_20_bytes() { - assert!(InfoHash::try_from([255u8; 19].to_vec()).is_err()); - } - - #[test] - fn it_should_fail_trying_to_create_an_info_hash_from_a_byte_vector_with_more_than_20_bytes() { - assert!(InfoHash::try_from([255u8; 21].to_vec()).is_err()); - } - - #[test] - fn an_info_hash_can_be_serialized() { - let s = ContainingInfoHash { - info_hash: InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap(), - }; - - let json_serialized_value = serde_json::to_string(&s).unwrap(); - - assert_eq!( - json_serialized_value, - r#"{"info_hash":"ffffffffffffffffffffffffffffffffffffffff"}"# - ); - } - - #[test] - fn an_info_hash_can_be_deserialized() { - let json = json!({ - "info_hash": "ffffffffffffffffffffffffffffffffffffffff", - }); - - let s: ContainingInfoHash = serde_json::from_value(json).unwrap(); - - assert_eq!( - s, - ContainingInfoHash { - info_hash: InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap() - } - ); - } -} diff --git a/src/shared/bit_torrent/mod.rs b/src/shared/bit_torrent/mod.rs index 8074661be..7d6b12f09 100644 --- a/src/shared/bit_torrent/mod.rs +++ b/src/shared/bit_torrent/mod.rs @@ -68,5 +68,4 @@ //! Percent Encoding spec | //!Bencode & bdecode in your browser | pub mod common; -pub mod info_hash; pub mod tracker; diff --git a/src/shared/bit_torrent/tracker/http/client/requests/announce.rs b/src/shared/bit_torrent/tracker/http/client/requests/announce.rs index 3c6b14222..f3ce327ea 100644 --- a/src/shared/bit_torrent/tracker/http/client/requests/announce.rs +++ b/src/shared/bit_torrent/tracker/http/client/requests/announce.rs @@ -3,8 +3,8 @@ use std::net::{IpAddr, Ipv4Addr}; use std::str::FromStr; use aquatic_udp_protocol::PeerId; +use bittorrent_primitives::info_hash::InfoHash; use serde_repr::Serialize_repr; -use torrust_tracker_primitives::info_hash::InfoHash; use crate::shared::bit_torrent::tracker::http::{percent_encode_byte_array, ByteArray20}; diff --git a/src/shared/bit_torrent/tracker/http/client/requests/scrape.rs b/src/shared/bit_torrent/tracker/http/client/requests/scrape.rs index 4d12fc2d2..58b9e0dc7 100644 --- a/src/shared/bit_torrent/tracker/http/client/requests/scrape.rs +++ b/src/shared/bit_torrent/tracker/http/client/requests/scrape.rs @@ -2,7 +2,7 @@ use std::error::Error; use std::fmt::{self}; use std::str::FromStr; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use crate::shared::bit_torrent::tracker::http::{percent_encode_byte_array, ByteArray20}; diff --git a/tests/servers/api/environment.rs b/tests/servers/api/environment.rs index 2f4606be7..bffe42603 100644 --- a/tests/servers/api/environment.rs +++ b/tests/servers/api/environment.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use futures::executor::block_on; use torrust_tracker::bootstrap::app::initialize_with_configuration; use torrust_tracker::bootstrap::jobs::make_rust_tls; @@ -8,7 +9,6 @@ use torrust_tracker::core::Tracker; use torrust_tracker::servers::apis::server::{ApiServer, Launcher, Running, Stopped}; use torrust_tracker::servers::registar::Registar; use torrust_tracker_configuration::{Configuration, HttpApi}; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer; use super::connection_info::ConnectionInfo; diff --git a/tests/servers/api/v1/contract/context/stats.rs b/tests/servers/api/v1/contract/context/stats.rs index a034a7778..2c8e8d6a5 100644 --- a/tests/servers/api/v1/contract/context/stats.rs +++ b/tests/servers/api/v1/contract/context/stats.rs @@ -1,7 +1,7 @@ use std::str::FromStr; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker::servers::apis::v1::context::stats::resources::Stats; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer::fixture::PeerBuilder; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; diff --git a/tests/servers/api/v1/contract/context/torrent.rs b/tests/servers/api/v1/contract/context/torrent.rs index f5e930be3..e500ac63c 100644 --- a/tests/servers/api/v1/contract/context/torrent.rs +++ b/tests/servers/api/v1/contract/context/torrent.rs @@ -1,8 +1,8 @@ use std::str::FromStr; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker::servers::apis::v1::context::torrent::resources::peer::Peer; use torrust_tracker::servers::apis::v1::context::torrent::resources::torrent::{self, Torrent}; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer::fixture::PeerBuilder; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; diff --git a/tests/servers/api/v1/contract/context/whitelist.rs b/tests/servers/api/v1/contract/context/whitelist.rs index b30a7dbf8..49ce3e865 100644 --- a/tests/servers/api/v1/contract/context/whitelist.rs +++ b/tests/servers/api/v1/contract/context/whitelist.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; diff --git a/tests/servers/http/environment.rs b/tests/servers/http/environment.rs index b6bb21c16..20b126c18 100644 --- a/tests/servers/http/environment.rs +++ b/tests/servers/http/environment.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use futures::executor::block_on; use torrust_tracker::bootstrap::app::initialize_with_configuration; use torrust_tracker::bootstrap::jobs::make_rust_tls; @@ -7,7 +8,6 @@ use torrust_tracker::core::Tracker; use torrust_tracker::servers::http::server::{HttpServer, Launcher, Running, Stopped}; use torrust_tracker::servers::registar::Registar; use torrust_tracker_configuration::{Configuration, HttpTracker}; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer; pub struct Environment { diff --git a/tests/servers/http/requests/announce.rs b/tests/servers/http/requests/announce.rs index fa20553d0..740c86d38 100644 --- a/tests/servers/http/requests/announce.rs +++ b/tests/servers/http/requests/announce.rs @@ -3,8 +3,8 @@ use std::net::{IpAddr, Ipv4Addr}; use std::str::FromStr; use aquatic_udp_protocol::PeerId; +use bittorrent_primitives::info_hash::InfoHash; use serde_repr::Serialize_repr; -use torrust_tracker_primitives::info_hash::InfoHash; use crate::servers::http::{percent_encode_byte_array, ByteArray20}; diff --git a/tests/servers/http/requests/scrape.rs b/tests/servers/http/requests/scrape.rs index f66605855..ecef541f1 100644 --- a/tests/servers/http/requests/scrape.rs +++ b/tests/servers/http/requests/scrape.rs @@ -1,7 +1,7 @@ use std::fmt; use std::str::FromStr; -use torrust_tracker_primitives::info_hash::InfoHash; +use bittorrent_primitives::info_hash::InfoHash; use crate::servers::http::{percent_encode_byte_array, ByteArray20}; diff --git a/tests/servers/http/v1/contract.rs b/tests/servers/http/v1/contract.rs index 405a35dc5..554849aee 100644 --- a/tests/servers/http/v1/contract.rs +++ b/tests/servers/http/v1/contract.rs @@ -103,10 +103,10 @@ mod for_all_config_modes { use std::str::FromStr; use aquatic_udp_protocol::PeerId; + use bittorrent_primitives::info_hash::InfoHash; use local_ip_address::local_ip; use reqwest::{Response, StatusCode}; use tokio::net::TcpListener; - use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer::fixture::PeerBuilder; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; @@ -1042,8 +1042,8 @@ mod for_all_config_modes { use std::str::FromStr; use aquatic_udp_protocol::PeerId; + use bittorrent_primitives::info_hash::InfoHash; use tokio::net::TcpListener; - use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer::fixture::PeerBuilder; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; @@ -1300,7 +1300,7 @@ mod configured_as_whitelisted { mod and_receiving_an_announce_request { use std::str::FromStr; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; @@ -1358,7 +1358,7 @@ mod configured_as_whitelisted { use std::str::FromStr; use aquatic_udp_protocol::PeerId; - use torrust_tracker_primitives::info_hash::InfoHash; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer::fixture::PeerBuilder; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; @@ -1457,8 +1457,8 @@ mod configured_as_private { use std::str::FromStr; use std::time::Duration; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker::core::auth::Key; - use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; @@ -1552,8 +1552,8 @@ mod configured_as_private { use std::time::Duration; use aquatic_udp_protocol::PeerId; + use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker::core::auth::Key; - use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer::fixture::PeerBuilder; use torrust_tracker_test_helpers::configuration; use tracing::level_filters::LevelFilter; diff --git a/tests/servers/udp/environment.rs b/tests/servers/udp/environment.rs index b7ac2336c..83dc076ce 100644 --- a/tests/servers/udp/environment.rs +++ b/tests/servers/udp/environment.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use std::sync::Arc; +use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker::bootstrap::app::initialize_with_configuration; use torrust_tracker::core::Tracker; use torrust_tracker::servers::registar::Registar; @@ -8,7 +9,6 @@ use torrust_tracker::servers::udp::server::spawner::Spawner; use torrust_tracker::servers::udp::server::states::{Running, Stopped}; use torrust_tracker::servers::udp::server::Server; use torrust_tracker_configuration::{Configuration, UdpTracker, DEFAULT_TIMEOUT}; -use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer; pub struct Environment