Merge pull request #72 from jabedude/bep52-magnet-support

Add support for BTv2 magnet links
This commit is contained in:
Igor Katson 2024-01-08 11:27:27 +00:00 committed by GitHub
commit c8189de3d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 236 additions and 157 deletions

View file

@ -9,7 +9,7 @@ use tracing::debug;
use crate::{
peer_connection::PeerConnectionOptions, peer_info_reader, spawn_utils::BlockingSpawner,
};
use librqbit_core::id20::Id20;
use librqbit_core::hash_id::Id20;
#[derive(Debug)]
pub enum ReadMetainfoResult<Rx> {

View file

@ -6,7 +6,7 @@ use std::{
use anyhow::{bail, Context};
use buffers::{ByteBuf, ByteString};
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 peer_binary_protocol::{
extended::{handshake::ExtendedHandshake, ExtendedMessage},
@ -120,7 +120,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
trace!(
"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);
@ -181,7 +181,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
.await
.context("error reading handshake")?;
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 {
anyhow::bail!("info hash does not match");
}

View file

@ -4,7 +4,7 @@ use bencode::from_bytes;
use buffers::{ByteBuf, ByteString};
use librqbit_core::{
constants::CHUNK_SIZE,
id20::Id20,
hash_id::Id20,
lengths::{ceil_div_u64, last_element_size_u64, ChunkInfo},
torrent_metainfo::TorrentMetaV1Info,
};
@ -226,7 +226,7 @@ impl PeerConnectionHandler for Handler {
mod tests {
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 crate::spawn_utils::BlockingSpawner;

View file

@ -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<()> {
@ -753,10 +753,8 @@ impl Session {
let (info_hash, info, dht_rx, trackers, initial_peers) = match add {
AddTorrent::Url(magnet) if magnet.starts_with("magnet:") => {
let Magnet {
info_hash,
trackers,
} = Magnet::parse(&magnet).context("provided path is not a valid magnet URL")?;
let magnet = Magnet::parse(&magnet).context("provided path is not a valid magnet URL")?;
let info_hash = magnet.as_id20().context("magnet link didn't contain a BTv1 infohash")?;
let dht_rx = self
.dht
@ -764,7 +762,7 @@ impl Session {
.context("magnet links without DHT are not supported")?
.get_peers(info_hash, announce_port)?;
let trackers = trackers
let trackers = magnet.trackers
.into_iter()
.filter_map(|url| match reqwest::Url::parse(&url) {
Ok(url) => Some(url),

View file

@ -63,7 +63,7 @@ use clone_to_owned::CloneToOwned;
use futures::{stream::FuturesUnordered, StreamExt};
use itertools::Itertools;
use librqbit_core::{
id20::Id20,
hash_id::Id20,
lengths::{ChunkInfo, Lengths, ValidPieceIndex},
spawn_utils::spawn_with_cancel,
speed_estimator::SpeedEstimator,
@ -383,7 +383,7 @@ impl TorrentStateLive {
let peer = occ.get_mut();
peer.state
.incoming_connection(
Id20(checked_peer.handshake.peer_id),
Id20::new(checked_peer.handshake.peer_id),
tx.clone(),
&self.peers.stats,
)
@ -393,7 +393,7 @@ impl TorrentStateLive {
Entry::Vacant(vac) => {
atomic_inc(&self.peers.stats.seen);
let peer = Peer::new_live_for_incoming_connection(
Id20(checked_peer.handshake.peer_id),
Id20::new(checked_peer.handshake.peer_id),
tx.clone(),
&self.peers.stats,
);
@ -597,7 +597,7 @@ impl TorrentStateLive {
fn set_peer_live<B>(&self, handle: PeerHandle, h: Handshake<B>) {
self.peers.with_peer_mut(handle, "set_peer_live", |p| {
p.state
.connecting_to_live(Id20(h.peer_id), &self.peers.stats);
.connecting_to_live(Id20::new(h.peer_id), &self.peers.stats);
});
}

View file

@ -2,7 +2,7 @@ pub mod stats;
use std::collections::HashSet;
use librqbit_core::id20::Id20;
use librqbit_core::hash_id::Id20;
use librqbit_core::lengths::{ChunkInfo, ValidPieceIndex};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};

View file

@ -16,7 +16,7 @@ use anyhow::bail;
use anyhow::Context;
use buffers::ByteString;
use dht::RequestPeersStream;
use librqbit_core::id20::Id20;
use librqbit_core::hash_id::Id20;
use librqbit_core::lengths::Lengths;
use librqbit_core::peer_id::generate_peer_id;

View file

@ -8,7 +8,7 @@ use std::{
str::FromStr,
};
use librqbit_core::id20::Id20;
use librqbit_core::hash_id::Id20;
#[derive(Clone, Copy)]
pub enum TrackerRequestEvent {
@ -207,10 +207,10 @@ mod tests {
use super::*;
#[test]
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,
]);
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,
]);
let request = TrackerRequest {