Merge pull request #72 from jabedude/bep52-magnet-support
Add support for BTv2 magnet links
This commit is contained in:
commit
c8189de3d2
21 changed files with 236 additions and 157 deletions
|
|
@ -12,7 +12,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.expect("first argument should be a magnet link");
|
.expect("first argument should be a magnet link");
|
||||||
let magnet = Magnet::parse(&magnet).unwrap();
|
let magnet = Magnet::parse(&magnet).unwrap();
|
||||||
let info_hash = magnet.info_hash;
|
let info_hash = magnet.as_id20().context("Supplied magnet link didn't contain a BTv1 infohash")?;
|
||||||
|
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use std::{
|
||||||
|
|
||||||
use bencode::{ByteBuf, ByteString};
|
use bencode::{ByteBuf, ByteString};
|
||||||
use clone_to_owned::CloneToOwned;
|
use clone_to_owned::CloneToOwned;
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{IgnoredAny, Unexpected},
|
de::{IgnoredAny, Unexpected},
|
||||||
Deserialize, Deserializer, Serialize,
|
Deserialize, Deserializer, Serialize,
|
||||||
|
|
@ -229,7 +229,7 @@ impl<'de> Deserialize<'de> for CompactNodeInfo {
|
||||||
let ip = Ipv4Addr::new(chunk[20], chunk[21], chunk[22], chunk[23]);
|
let ip = Ipv4Addr::new(chunk[20], chunk[21], chunk[22], chunk[23]);
|
||||||
let port = ((chunk[24] as u16) << 8) + chunk[25] as u16;
|
let port = ((chunk[24] as u16) << 8) + chunk[25] as u16;
|
||||||
buf.push(Node {
|
buf.push(Node {
|
||||||
id: Id20(node_id),
|
id: Id20::new(node_id),
|
||||||
addr: SocketAddrV4::new(ip, port),
|
addr: SocketAddrV4::new(ip, port),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ use futures::{stream::FuturesUnordered, Stream, StreamExt, TryFutureExt};
|
||||||
|
|
||||||
use leaky_bucket::RateLimiter;
|
use leaky_bucket::RateLimiter;
|
||||||
use librqbit_core::{
|
use librqbit_core::{
|
||||||
id20::Id20,
|
hash_id::Id20,
|
||||||
peer_id::generate_peer_id,
|
peer_id::generate_peer_id,
|
||||||
spawn_utils::{spawn, spawn_with_cancel},
|
spawn_utils::{spawn, spawn_with_cancel},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use std::time::Duration;
|
||||||
|
|
||||||
pub use crate::dht::DhtStats;
|
pub use crate::dht::DhtStats;
|
||||||
pub use crate::dht::{DhtConfig, DhtState, RequestPeersStream};
|
pub use crate::dht::{DhtConfig, DhtState, RequestPeersStream};
|
||||||
pub use librqbit_core::id20::Id20;
|
pub use librqbit_core::hash_id::Id20;
|
||||||
pub use persistence::{PersistentDht, PersistentDhtConfig};
|
pub use persistence::{PersistentDht, PersistentDhtConfig};
|
||||||
|
|
||||||
pub type Dht = Arc<DhtState>;
|
pub type Dht = Arc<DhtState>;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use std::{
|
||||||
|
|
||||||
use bencode::ByteString;
|
use bencode::ByteString;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use serde::{
|
use serde::{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{net::SocketAddr, time::Instant};
|
use std::{net::SocketAddr, time::Instant};
|
||||||
|
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
|
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
|
||||||
use tracing::{debug, trace};
|
use tracing::{debug, trace};
|
||||||
|
|
@ -132,7 +132,7 @@ impl<'a> Iterator for BucketTreeIterator<'a> {
|
||||||
pub fn generate_random_id(start: &Id20, bits: u8) -> Id20 {
|
pub fn generate_random_id(start: &Id20, bits: u8) -> Id20 {
|
||||||
let mut data = [0u8; 20];
|
let mut data = [0u8; 20];
|
||||||
rand::thread_rng().fill_bytes(&mut data);
|
rand::thread_rng().fill_bytes(&mut data);
|
||||||
let mut data = Id20(data);
|
let mut data = Id20::new(data);
|
||||||
let remaining_bits = 160 - bits;
|
let remaining_bits = 160 - bits;
|
||||||
for bit in 0..remaining_bits {
|
for bit in 0..remaining_bits {
|
||||||
data.set_bit(bit, start.get_bit(bit));
|
data.set_bit(bit, start.get_bit(bit));
|
||||||
|
|
@ -199,8 +199,8 @@ impl BucketTree {
|
||||||
BucketTree {
|
BucketTree {
|
||||||
data: vec![BucketTreeNode {
|
data: vec![BucketTreeNode {
|
||||||
bits: 160,
|
bits: 160,
|
||||||
start: Id20([0u8; 20]),
|
start: Id20::new([0u8; 20]),
|
||||||
end_inclusive: Id20([0xff; 20]),
|
end_inclusive: Id20::new([0xff; 20]),
|
||||||
data: BucketTreeNodeData::Leaf(Default::default()),
|
data: BucketTreeNodeData::Leaf(Default::default()),
|
||||||
}],
|
}],
|
||||||
size: 0,
|
size: 0,
|
||||||
|
|
@ -583,7 +583,7 @@ mod tests {
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use crate::routing_table::compute_split_start_end;
|
use crate::routing_table::compute_split_start_end;
|
||||||
|
|
@ -592,8 +592,8 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compute_split_start_end_root() {
|
fn compute_split_start_end_root() {
|
||||||
let start = Id20([0u8; 20]);
|
let start = Id20::new([0u8; 20]);
|
||||||
let end = Id20([0xff; 20]);
|
let end = Id20::new([0xff; 20]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compute_split_start_end(start, end, 160),
|
compute_split_start_end(start, end, 160),
|
||||||
(
|
(
|
||||||
|
|
@ -612,7 +612,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn compute_split_start_end_second_split() {
|
fn compute_split_start_end_second_split() {
|
||||||
let start = Id20::from_str("8000000000000000000000000000000000000000").unwrap();
|
let start = Id20::from_str("8000000000000000000000000000000000000000").unwrap();
|
||||||
let end = Id20([0xff; 20]);
|
let end = Id20::new([0xff; 20]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compute_split_start_end(start, end, 159),
|
compute_split_start_end(start, end, 159),
|
||||||
(
|
(
|
||||||
|
|
@ -631,7 +631,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn compute_split_start_end_3() {
|
fn compute_split_start_end_3() {
|
||||||
let start = Id20::from_str("8000000000000000000000000000000000000000").unwrap();
|
let start = Id20::from_str("8000000000000000000000000000000000000000").unwrap();
|
||||||
let end = Id20([0xff; 20]);
|
let end = Id20::new([0xff; 20]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compute_split_start_end(start, end, 159),
|
compute_split_start_end(start, end, 159),
|
||||||
(
|
(
|
||||||
|
|
@ -650,7 +650,7 @@ mod tests {
|
||||||
fn random_id_20() -> Id20 {
|
fn random_id_20() -> Id20 {
|
||||||
let mut id20 = [0u8; 20];
|
let mut id20 = [0u8; 20];
|
||||||
rand::thread_rng().fill(&mut id20);
|
rand::thread_rng().fill(&mut id20);
|
||||||
Id20(id20)
|
Id20::new(id20)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_socket_addr() -> SocketAddr {
|
fn generate_socket_addr() -> SocketAddr {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
use serde::Serializer;
|
use serde::Serializer;
|
||||||
|
|
||||||
pub fn serialize_id20<S>(id: &Id20, ser: S) -> Result<S::Ok, S::Error>
|
pub fn serialize_id20<S>(id: &Id20, ser: S) -> Result<S::Ok, S::Error>
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use tracing::debug;
|
||||||
use crate::{
|
use crate::{
|
||||||
peer_connection::PeerConnectionOptions, peer_info_reader, spawn_utils::BlockingSpawner,
|
peer_connection::PeerConnectionOptions, peer_info_reader, spawn_utils::BlockingSpawner,
|
||||||
};
|
};
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ReadMetainfoResult<Rx> {
|
pub enum ReadMetainfoResult<Rx> {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use std::{
|
||||||
use anyhow::{bail, Context};
|
use anyhow::{bail, Context};
|
||||||
use buffers::{ByteBuf, ByteString};
|
use buffers::{ByteBuf, ByteString};
|
||||||
use clone_to_owned::CloneToOwned;
|
use clone_to_owned::CloneToOwned;
|
||||||
use librqbit_core::{id20::Id20, lengths::ChunkInfo, peer_id::try_decode_peer_id};
|
use librqbit_core::{hash_id::Id20, lengths::ChunkInfo, peer_id::try_decode_peer_id};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use peer_binary_protocol::{
|
use peer_binary_protocol::{
|
||||||
extended::{handshake::ExtendedHandshake, ExtendedMessage},
|
extended::{handshake::ExtendedHandshake, ExtendedMessage},
|
||||||
|
|
@ -120,7 +120,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
|
||||||
|
|
||||||
trace!(
|
trace!(
|
||||||
"incoming connection: id={:?}",
|
"incoming connection: id={:?}",
|
||||||
try_decode_peer_id(Id20(handshake.peer_id))
|
try_decode_peer_id(Id20::new(handshake.peer_id))
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut write_buf = Vec::<u8>::with_capacity(PIECE_MESSAGE_DEFAULT_LEN);
|
let mut write_buf = Vec::<u8>::with_capacity(PIECE_MESSAGE_DEFAULT_LEN);
|
||||||
|
|
@ -181,7 +181,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
|
||||||
.await
|
.await
|
||||||
.context("error reading handshake")?;
|
.context("error reading handshake")?;
|
||||||
let h_supports_extended = h.supports_extended();
|
let h_supports_extended = h.supports_extended();
|
||||||
trace!("connected: id={:?}", try_decode_peer_id(Id20(h.peer_id)));
|
trace!("connected: id={:?}", try_decode_peer_id(Id20::new(h.peer_id)));
|
||||||
if h.info_hash != self.info_hash.0 {
|
if h.info_hash != self.info_hash.0 {
|
||||||
anyhow::bail!("info hash does not match");
|
anyhow::bail!("info hash does not match");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use bencode::from_bytes;
|
||||||
use buffers::{ByteBuf, ByteString};
|
use buffers::{ByteBuf, ByteString};
|
||||||
use librqbit_core::{
|
use librqbit_core::{
|
||||||
constants::CHUNK_SIZE,
|
constants::CHUNK_SIZE,
|
||||||
id20::Id20,
|
hash_id::Id20,
|
||||||
lengths::{ceil_div_u64, last_element_size_u64, ChunkInfo},
|
lengths::{ceil_div_u64, last_element_size_u64, ChunkInfo},
|
||||||
torrent_metainfo::TorrentMetaV1Info,
|
torrent_metainfo::TorrentMetaV1Info,
|
||||||
};
|
};
|
||||||
|
|
@ -226,7 +226,7 @@ impl PeerConnectionHandler for Handler {
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{net::SocketAddr, str::FromStr, sync::Once};
|
use std::{net::SocketAddr, str::FromStr, sync::Once};
|
||||||
|
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
use librqbit_core::peer_id::generate_peer_id;
|
use librqbit_core::peer_id::generate_peer_id;
|
||||||
|
|
||||||
use crate::spawn_utils::BlockingSpawner;
|
use crate::spawn_utils::BlockingSpawner;
|
||||||
|
|
|
||||||
|
|
@ -548,7 +548,7 @@ impl Session {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!("didn't find a matching torrent for {:?}", Id20(h.info_hash))
|
bail!("didn't find a matching torrent for {:?}", Id20::new(h.info_hash))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn task_tcp_listener(self: Arc<Self>, l: TcpListener) -> anyhow::Result<()> {
|
async fn task_tcp_listener(self: Arc<Self>, l: TcpListener) -> anyhow::Result<()> {
|
||||||
|
|
@ -753,10 +753,8 @@ impl Session {
|
||||||
|
|
||||||
let (info_hash, info, dht_rx, trackers, initial_peers) = match add {
|
let (info_hash, info, dht_rx, trackers, initial_peers) = match add {
|
||||||
AddTorrent::Url(magnet) if magnet.starts_with("magnet:") => {
|
AddTorrent::Url(magnet) if magnet.starts_with("magnet:") => {
|
||||||
let Magnet {
|
let magnet = Magnet::parse(&magnet).context("provided path is not a valid magnet URL")?;
|
||||||
info_hash,
|
let info_hash = magnet.as_id20().context("magnet link didn't contain a BTv1 infohash")?;
|
||||||
trackers,
|
|
||||||
} = Magnet::parse(&magnet).context("provided path is not a valid magnet URL")?;
|
|
||||||
|
|
||||||
let dht_rx = self
|
let dht_rx = self
|
||||||
.dht
|
.dht
|
||||||
|
|
@ -764,7 +762,7 @@ impl Session {
|
||||||
.context("magnet links without DHT are not supported")?
|
.context("magnet links without DHT are not supported")?
|
||||||
.get_peers(info_hash, announce_port)?;
|
.get_peers(info_hash, announce_port)?;
|
||||||
|
|
||||||
let trackers = trackers
|
let trackers = magnet.trackers
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|url| match reqwest::Url::parse(&url) {
|
.filter_map(|url| match reqwest::Url::parse(&url) {
|
||||||
Ok(url) => Some(url),
|
Ok(url) => Some(url),
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ use clone_to_owned::CloneToOwned;
|
||||||
use futures::{stream::FuturesUnordered, StreamExt};
|
use futures::{stream::FuturesUnordered, StreamExt};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use librqbit_core::{
|
use librqbit_core::{
|
||||||
id20::Id20,
|
hash_id::Id20,
|
||||||
lengths::{ChunkInfo, Lengths, ValidPieceIndex},
|
lengths::{ChunkInfo, Lengths, ValidPieceIndex},
|
||||||
spawn_utils::spawn_with_cancel,
|
spawn_utils::spawn_with_cancel,
|
||||||
speed_estimator::SpeedEstimator,
|
speed_estimator::SpeedEstimator,
|
||||||
|
|
@ -383,7 +383,7 @@ impl TorrentStateLive {
|
||||||
let peer = occ.get_mut();
|
let peer = occ.get_mut();
|
||||||
peer.state
|
peer.state
|
||||||
.incoming_connection(
|
.incoming_connection(
|
||||||
Id20(checked_peer.handshake.peer_id),
|
Id20::new(checked_peer.handshake.peer_id),
|
||||||
tx.clone(),
|
tx.clone(),
|
||||||
&self.peers.stats,
|
&self.peers.stats,
|
||||||
)
|
)
|
||||||
|
|
@ -393,7 +393,7 @@ impl TorrentStateLive {
|
||||||
Entry::Vacant(vac) => {
|
Entry::Vacant(vac) => {
|
||||||
atomic_inc(&self.peers.stats.seen);
|
atomic_inc(&self.peers.stats.seen);
|
||||||
let peer = Peer::new_live_for_incoming_connection(
|
let peer = Peer::new_live_for_incoming_connection(
|
||||||
Id20(checked_peer.handshake.peer_id),
|
Id20::new(checked_peer.handshake.peer_id),
|
||||||
tx.clone(),
|
tx.clone(),
|
||||||
&self.peers.stats,
|
&self.peers.stats,
|
||||||
);
|
);
|
||||||
|
|
@ -597,7 +597,7 @@ impl TorrentStateLive {
|
||||||
fn set_peer_live<B>(&self, handle: PeerHandle, h: Handshake<B>) {
|
fn set_peer_live<B>(&self, handle: PeerHandle, h: Handshake<B>) {
|
||||||
self.peers.with_peer_mut(handle, "set_peer_live", |p| {
|
self.peers.with_peer_mut(handle, "set_peer_live", |p| {
|
||||||
p.state
|
p.state
|
||||||
.connecting_to_live(Id20(h.peer_id), &self.peers.stats);
|
.connecting_to_live(Id20::new(h.peer_id), &self.peers.stats);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ pub mod stats;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
use librqbit_core::lengths::{ChunkInfo, ValidPieceIndex};
|
use librqbit_core::lengths::{ChunkInfo, ValidPieceIndex};
|
||||||
|
|
||||||
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use anyhow::bail;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use buffers::ByteString;
|
use buffers::ByteString;
|
||||||
use dht::RequestPeersStream;
|
use dht::RequestPeersStream;
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
use librqbit_core::lengths::Lengths;
|
use librqbit_core::lengths::Lengths;
|
||||||
use librqbit_core::peer_id::generate_peer_id;
|
use librqbit_core::peer_id::generate_peer_id;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use std::{
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use librqbit_core::id20::Id20;
|
use librqbit_core::hash_id::Id20;
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum TrackerRequestEvent {
|
pub enum TrackerRequestEvent {
|
||||||
|
|
@ -207,10 +207,10 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize() {
|
fn test_serialize() {
|
||||||
let info_hash = Id20([
|
let info_hash = Id20::new([
|
||||||
1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||||
]);
|
]);
|
||||||
let peer_id = Id20([
|
let peer_id = Id20::new([
|
||||||
1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||||
]);
|
]);
|
||||||
let request = TrackerRequest {
|
let request = TrackerRequest {
|
||||||
|
|
|
||||||
|
|
@ -2,94 +2,20 @@ use std::{cmp::Ordering, str::FromStr};
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize};
|
use serde::{Deserialize, Deserializer, Serialize};
|
||||||
|
|
||||||
/// A 20-byte hash used throughout librqbit, for torrent info hashes, peer ids etc.
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
|
pub struct Id<const N: usize>(pub [u8; N]);
|
||||||
pub struct Id20(pub [u8; 20]);
|
|
||||||
|
|
||||||
impl FromStr for Id20 {
|
impl<const N: usize> Id<N> {
|
||||||
type Err = anyhow::Error;
|
pub fn new(from: [u8; N]) -> Id<N> {
|
||||||
|
Id(from)
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
let mut out = [0u8; 20];
|
|
||||||
if s.len() != 40 {
|
|
||||||
anyhow::bail!("expected a hex string of length 40")
|
|
||||||
};
|
|
||||||
hex::decode_to_slice(s, &mut out)?;
|
|
||||||
Ok(Id20(out))
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for Id20 {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
for byte in self.0 {
|
|
||||||
write!(f, "{byte:02x?}")?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for Id20 {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: serde::Serializer,
|
|
||||||
{
|
|
||||||
serializer.serialize_bytes(&self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for Id20 {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
struct Visitor;
|
|
||||||
impl<'de> serde::de::Visitor<'de> for Visitor {
|
|
||||||
type Value = Id20;
|
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
write!(formatter, "a 20 byte slice or a 40 byte string")
|
|
||||||
}
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: serde::de::Error,
|
|
||||||
{
|
|
||||||
if v.len() != 40 {
|
|
||||||
return Err(E::invalid_length(40, &self));
|
|
||||||
}
|
|
||||||
let mut out = [0u8; 20];
|
|
||||||
match hex::decode_to_slice(v, &mut out) {
|
|
||||||
Ok(_) => Ok(Id20(out)),
|
|
||||||
Err(e) => Err(E::custom(e)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: serde::de::Error,
|
|
||||||
{
|
|
||||||
self.visit_bytes(v)
|
|
||||||
}
|
|
||||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: serde::de::Error,
|
|
||||||
{
|
|
||||||
if v.len() != 20 {
|
|
||||||
return Err(E::invalid_length(20, &self));
|
|
||||||
}
|
|
||||||
let mut buf = [0u8; 20];
|
|
||||||
buf.copy_from_slice(v);
|
|
||||||
Ok(Id20(buf))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
deserializer.deserialize_any(Visitor {})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Id20 {
|
|
||||||
pub fn as_string(&self) -> String {
|
pub fn as_string(&self) -> String {
|
||||||
hex::encode(self.0)
|
hex::encode(self.0)
|
||||||
}
|
}
|
||||||
pub fn distance(&self, other: &Id20) -> Id20 {
|
|
||||||
let mut xor = [0u8; 20];
|
pub fn distance(&self, other: &Id<N>) -> Id<N> {
|
||||||
|
let mut xor = [0u8; N];
|
||||||
for (idx, (s, o)) in self
|
for (idx, (s, o)) in self
|
||||||
.0
|
.0
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -99,7 +25,7 @@ impl Id20 {
|
||||||
{
|
{
|
||||||
xor[idx] = s ^ o;
|
xor[idx] = s ^ o;
|
||||||
}
|
}
|
||||||
Id20(xor)
|
Id(xor)
|
||||||
}
|
}
|
||||||
pub fn get_bit(&self, bit: u8) -> bool {
|
pub fn get_bit(&self, bit: u8) -> bool {
|
||||||
let n = self.0[(bit / 8) as usize];
|
let n = self.0[(bit / 8) as usize];
|
||||||
|
|
@ -123,8 +49,104 @@ impl Id20 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ord for Id20 {
|
impl<const N: usize> Default for Id<N> {
|
||||||
fn cmp(&self, other: &Id20) -> Ordering {
|
fn default() -> Self {
|
||||||
|
Id([0; N])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> std::fmt::Debug for Id<N> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
for byte in self.0 {
|
||||||
|
write!(f, "{:02x?}", byte)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> FromStr for Id<N> {
|
||||||
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
let mut out = [0u8; N];
|
||||||
|
if s.len() != N*2 {
|
||||||
|
anyhow::bail!("expected a hex string of length {}", N*2)
|
||||||
|
};
|
||||||
|
hex::decode_to_slice(s, &mut out)?;
|
||||||
|
Ok(Id(out))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Serialize for Id<N> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_bytes(&self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, const N: usize> Deserialize<'de> for Id<N> {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
struct IdVisitor<const N: usize>;
|
||||||
|
|
||||||
|
impl<'de, const N: usize> serde::de::Visitor<'de> for IdVisitor<N> {
|
||||||
|
type Value = Id<N>;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
formatter.write_str("a byte array of length ")
|
||||||
|
.and_then(|_| formatter.write_fmt(format_args!("{}", N)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
if v.len() != N * 2 {
|
||||||
|
return Err(E::invalid_length(40, &self));
|
||||||
|
}
|
||||||
|
let mut out = [0u8; N];
|
||||||
|
match hex::decode_to_slice(v, &mut out) {
|
||||||
|
Ok(_) => Ok(Id(out)),
|
||||||
|
Err(e) => Err(E::custom(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
self.visit_bytes(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
if v.len() != N {
|
||||||
|
return Err(E::invalid_length(N, &self));
|
||||||
|
}
|
||||||
|
let mut buf = [0u8; N];
|
||||||
|
buf.copy_from_slice(v);
|
||||||
|
Ok(Id(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_any(IdVisitor{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> PartialOrd<Id<N>> for Id<N> {
|
||||||
|
fn partial_cmp(&self, other: &Id<N>) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Ord for Id<N> {
|
||||||
|
fn cmp(&self, other: &Id<N>) -> Ordering {
|
||||||
for (s, o) in self.0.iter().copied().zip(other.0.iter().copied()) {
|
for (s, o) in self.0.iter().copied().zip(other.0.iter().copied()) {
|
||||||
match s.cmp(&o) {
|
match s.cmp(&o) {
|
||||||
Ordering::Less => return Ordering::Less,
|
Ordering::Less => return Ordering::Less,
|
||||||
|
|
@ -136,23 +158,30 @@ impl Ord for Id20 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd<Id20> for Id20 {
|
/// A 20-byte hash used throughout librqbit, for torrent info hashes, peer ids etc.
|
||||||
fn partial_cmp(&self, other: &Id20) -> Option<Ordering> {
|
pub type Id20 = Id<20>;
|
||||||
Some(self.cmp(other))
|
/// A 32-byte hash used in Bittorrent V2, for torrent info hashes, piece hashing, etc.
|
||||||
}
|
pub type Id32 = Id<32>;
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::Id20;
|
use std::str::FromStr;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_set_bit_range() {
|
fn test_set_bit_range() {
|
||||||
let mut id = Id20([0u8; 20]);
|
let mut id = Id20::default();
|
||||||
id.set_bits_range(9..17, true);
|
id.set_bits_range(9..17, true);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
id,
|
id,
|
||||||
Id20([0, 127, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
Id20::new([0, 127, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
#[test]
|
||||||
|
fn test_id32_from_str() {
|
||||||
|
let str = "06f04cc728bef957a658876ef807f0514e4d715392969998efef584d2c3e435e";
|
||||||
|
let _ih = Id32::from_str(str).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
pub mod constants;
|
pub mod constants;
|
||||||
pub mod directories;
|
pub mod directories;
|
||||||
pub mod id20;
|
pub mod hash_id;
|
||||||
pub mod lengths;
|
pub mod lengths;
|
||||||
pub mod magnet;
|
pub mod magnet;
|
||||||
pub mod peer_id;
|
pub mod peer_id;
|
||||||
|
|
|
||||||
|
|
@ -2,41 +2,61 @@ use std::str::FromStr;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
|
|
||||||
use crate::id20::Id20;
|
use crate::hash_id::{Id20, Id32};
|
||||||
|
|
||||||
|
|
||||||
/// A parsed magnet link.
|
/// A parsed magnet link.
|
||||||
pub struct Magnet {
|
pub struct Magnet {
|
||||||
pub info_hash: Id20,
|
id20: Option<Id20>,
|
||||||
|
id32: Option<Id32>,
|
||||||
pub trackers: Vec<String>,
|
pub trackers: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Magnet {
|
impl Magnet {
|
||||||
|
pub fn as_id20(&self) -> Option<Id20> {
|
||||||
|
self.id20
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_id32(&self) -> Option<Id32> {
|
||||||
|
self.id32
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse a magnet link.
|
/// Parse a magnet link.
|
||||||
pub fn parse(url: &str) -> anyhow::Result<Magnet> {
|
pub fn parse(url: &str) -> anyhow::Result<Magnet> {
|
||||||
let url = url::Url::parse(url).context("magnet link must be a valid URL")?;
|
let url = url::Url::parse(url).context("magnet link must be a valid URL")?;
|
||||||
if url.scheme() != "magnet" {
|
if url.scheme() != "magnet" {
|
||||||
anyhow::bail!("expected scheme magnet");
|
anyhow::bail!("expected scheme magnet");
|
||||||
}
|
}
|
||||||
let mut info_hash: Option<Id20> = None;
|
let mut info_hash_found = false;
|
||||||
|
let mut id20: Option<Id20> = None;
|
||||||
|
let mut id32: Option<Id32> = None;
|
||||||
let mut trackers = Vec::<String>::new();
|
let mut trackers = Vec::<String>::new();
|
||||||
for (key, value) in url.query_pairs() {
|
for (key, value) in url.query_pairs() {
|
||||||
match key.as_ref() {
|
match key.as_ref() {
|
||||||
"xt" => match value.as_ref().strip_prefix("urn:btih:") {
|
"xt" => {
|
||||||
Some(infohash) => {
|
if let Some(ih) = value.as_ref().strip_prefix("urn:btih:") {
|
||||||
info_hash.replace(Id20::from_str(infohash)?);
|
let i = Id20::from_str(ih)?;
|
||||||
|
id20.replace(i);
|
||||||
|
info_hash_found = true;
|
||||||
|
} else if let Some(ih) = value.as_ref().strip_prefix("urn:btmh:1220") {
|
||||||
|
let i = Id32::from_str(ih)?;
|
||||||
|
id32.replace(i);
|
||||||
|
info_hash_found = true;
|
||||||
|
} else {
|
||||||
|
anyhow::bail!("expected xt to start with btih or btmh");
|
||||||
}
|
}
|
||||||
None => anyhow::bail!("expected xt to start with urn:btih:"),
|
|
||||||
},
|
},
|
||||||
"tr" => trackers.push(value.into()),
|
"tr" => trackers.push(value.into()),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match info_hash {
|
match info_hash_found {
|
||||||
Some(info_hash) => Ok(Magnet {
|
true => Ok(Magnet {
|
||||||
info_hash,
|
id20,
|
||||||
|
id32,
|
||||||
trackers,
|
trackers,
|
||||||
}),
|
}),
|
||||||
None => {
|
false => {
|
||||||
anyhow::bail!("did not find infohash")
|
anyhow::bail!("did not find infohash")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -45,15 +65,35 @@ impl Magnet {
|
||||||
|
|
||||||
impl std::fmt::Display for Magnet {
|
impl std::fmt::Display for Magnet {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(
|
if let (Some(id20), Some(id32)) = (self.id20, self.id32) {
|
||||||
f,
|
write!(
|
||||||
"magnet:?xt=urn:btih:{}&tr={}",
|
f,
|
||||||
self.info_hash.as_string(),
|
"magnet:?xt=urn:btih:{}?xt=urn:btmh:1220{}&tr={}",
|
||||||
self.trackers.join("&tr=")
|
id20.as_string(),
|
||||||
)
|
id32.as_string(),
|
||||||
|
self.trackers.join("&tr=")
|
||||||
|
)
|
||||||
|
} else if let Some(id20) = self.id20 {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"magnet:?xt=urn:btih:{}&tr={}",
|
||||||
|
id20.as_string(),
|
||||||
|
self.trackers.join("&tr=")
|
||||||
|
)
|
||||||
|
} else if let Some(id32) = self.id32 {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"magnet:?xt=urn:btmh:1220{}&tr={}",
|
||||||
|
id32.as_string(),
|
||||||
|
self.trackers.join("&tr=")
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
panic!("no infohash")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -61,4 +101,16 @@ mod tests {
|
||||||
let magnet = "magnet:?xt=urn:btih:a621779b5e3d486e127c3efbca9b6f8d135f52e5&dn=rutor.info_%D0%92%D0%BE%D0%B9%D0%BD%D0%B0+%D0%B1%D1%83%D0%B4%D1%83%D1%89%D0%B5%D0%B3%D0%BE+%2F+The+Tomorrow+War+%282021%29+WEB-DLRip+%D0%BE%D1%82+MegaPeer+%7C+P+%7C+NewComers&tr=udp://opentor.org:2710&tr=udp://opentor.org:2710&tr=http://retracker.local/announce";
|
let magnet = "magnet:?xt=urn:btih:a621779b5e3d486e127c3efbca9b6f8d135f52e5&dn=rutor.info_%D0%92%D0%BE%D0%B9%D0%BD%D0%B0+%D0%B1%D1%83%D0%B4%D1%83%D1%89%D0%B5%D0%B3%D0%BE+%2F+The+Tomorrow+War+%282021%29+WEB-DLRip+%D0%BE%D1%82+MegaPeer+%7C+P+%7C+NewComers&tr=udp://opentor.org:2710&tr=udp://opentor.org:2710&tr=http://retracker.local/announce";
|
||||||
dbg!(url::Url::parse(magnet).unwrap());
|
dbg!(url::Url::parse(magnet).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_magnet_v2() {
|
||||||
|
use super::Magnet;
|
||||||
|
use crate::magnet::Id32;
|
||||||
|
use std::str::FromStr;
|
||||||
|
let magnet = "magnet:?xt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e&dn=bittorrent-v2-test
|
||||||
|
";
|
||||||
|
let info_hash = Id32::from_str("caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e").unwrap();
|
||||||
|
let m = Magnet::parse(&magnet).unwrap();
|
||||||
|
assert!(m.as_id32() == Some(info_hash));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::id20::Id20;
|
use crate::hash_id::Id20;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum AzureusStyleKind {
|
pub enum AzureusStyleKind {
|
||||||
|
|
@ -55,5 +55,5 @@ pub fn generate_peer_id() -> Id20 {
|
||||||
|
|
||||||
peer_id[..8].copy_from_slice(b"-rQ0001-");
|
peer_id[..8].copy_from_slice(b"-rQ0001-");
|
||||||
|
|
||||||
Id20(peer_id)
|
Id20::new(peer_id)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use clone_to_owned::CloneToOwned;
|
||||||
use itertools::Either;
|
use itertools::Either;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::id20::Id20;
|
use crate::hash_id::Id20;
|
||||||
|
|
||||||
pub type TorrentMetaV1Borrowed<'a> = TorrentMetaV1<ByteBuf<'a>>;
|
pub type TorrentMetaV1Borrowed<'a> = TorrentMetaV1<ByteBuf<'a>>;
|
||||||
pub type TorrentMetaV1Owned = TorrentMetaV1<ByteString>;
|
pub type TorrentMetaV1Owned = TorrentMetaV1<ByteString>;
|
||||||
|
|
@ -19,7 +19,7 @@ pub fn torrent_from_bytes<'de, ByteBuf: Deserialize<'de>>(
|
||||||
let mut de = BencodeDeserializer::new_from_buf(buf);
|
let mut de = BencodeDeserializer::new_from_buf(buf);
|
||||||
de.is_torrent_info = true;
|
de.is_torrent_info = true;
|
||||||
let mut t = TorrentMetaV1::deserialize(&mut de)?;
|
let mut t = TorrentMetaV1::deserialize(&mut de)?;
|
||||||
t.info_hash = Id20(
|
t.info_hash = Id20::new(
|
||||||
de.torrent_info_digest
|
de.torrent_info_digest
|
||||||
.ok_or_else(|| anyhow::anyhow!("programming error"))?,
|
.ok_or_else(|| anyhow::anyhow!("programming error"))?,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use bincode::Options;
|
||||||
use buffers::{ByteBuf, ByteString};
|
use buffers::{ByteBuf, ByteString};
|
||||||
use byteorder::{ByteOrder, BE};
|
use byteorder::{ByteOrder, BE};
|
||||||
use clone_to_owned::CloneToOwned;
|
use clone_to_owned::CloneToOwned;
|
||||||
use librqbit_core::{constants::CHUNK_SIZE, id20::Id20, lengths::ChunkInfo};
|
use librqbit_core::{constants::CHUNK_SIZE, hash_id::Id20, lengths::ChunkInfo};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use self::extended::ExtendedMessage;
|
use self::extended::ExtendedMessage;
|
||||||
|
|
@ -596,10 +596,10 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn test_handshake_serialize() {
|
fn test_handshake_serialize() {
|
||||||
let info_hash = Id20([
|
let info_hash = Id20::new([
|
||||||
1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||||
]);
|
]);
|
||||||
let peer_id = Id20([
|
let peer_id = Id20::new([
|
||||||
1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||||
]);
|
]);
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue