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

@ -2,94 +2,20 @@ use std::{cmp::Ordering, str::FromStr};
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, Default)]
pub struct Id20(pub [u8; 20]);
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Id<const N: usize>(pub [u8; N]);
impl FromStr for Id20 {
type Err = anyhow::Error;
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<const N: usize> Id<N> {
pub fn new(from: [u8; N]) -> Id<N> {
Id(from)
}
}
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 {
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
.0
.iter()
@ -99,7 +25,7 @@ impl Id20 {
{
xor[idx] = s ^ o;
}
Id20(xor)
Id(xor)
}
pub fn get_bit(&self, bit: u8) -> bool {
let n = self.0[(bit / 8) as usize];
@ -123,8 +49,104 @@ impl Id20 {
}
}
impl Ord for Id20 {
fn cmp(&self, other: &Id20) -> Ordering {
impl<const N: usize> Default for Id<N> {
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()) {
match s.cmp(&o) {
Ordering::Less => return Ordering::Less,
@ -136,23 +158,30 @@ impl Ord for Id20 {
}
}
impl PartialOrd<Id20> for Id20 {
fn partial_cmp(&self, other: &Id20) -> Option<Ordering> {
Some(self.cmp(other))
}
}
/// A 20-byte hash used throughout librqbit, for torrent info hashes, peer ids etc.
pub type Id20 = Id<20>;
/// A 32-byte hash used in Bittorrent V2, for torrent info hashes, piece hashing, etc.
pub type Id32 = Id<32>;
#[cfg(test)]
mod tests {
use super::Id20;
use std::str::FromStr;
use super::*;
#[test]
fn test_set_bit_range() {
let mut id = Id20([0u8; 20]);
let mut id = Id20::default();
id.set_bits_range(9..17, true);
assert_eq!(
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();
}
}

View file

@ -1,6 +1,6 @@
pub mod constants;
pub mod directories;
pub mod id20;
pub mod hash_id;
pub mod lengths;
pub mod magnet;
pub mod peer_id;

View file

@ -2,41 +2,61 @@ use std::str::FromStr;
use anyhow::Context;
use crate::id20::Id20;
use crate::hash_id::{Id20, Id32};
/// A parsed magnet link.
pub struct Magnet {
pub info_hash: Id20,
id20: Option<Id20>,
id32: Option<Id32>,
pub trackers: Vec<String>,
}
impl Magnet {
pub fn as_id20(&self) -> Option<Id20> {
self.id20
}
pub fn as_id32(&self) -> Option<Id32> {
self.id32
}
/// Parse a magnet link.
pub fn parse(url: &str) -> anyhow::Result<Magnet> {
let url = url::Url::parse(url).context("magnet link must be a valid URL")?;
if url.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();
for (key, value) in url.query_pairs() {
match key.as_ref() {
"xt" => match value.as_ref().strip_prefix("urn:btih:") {
Some(infohash) => {
info_hash.replace(Id20::from_str(infohash)?);
"xt" => {
if let Some(ih) = value.as_ref().strip_prefix("urn:btih:") {
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()),
_ => {}
}
}
match info_hash {
Some(info_hash) => Ok(Magnet {
info_hash,
match info_hash_found {
true => Ok(Magnet {
id20,
id32,
trackers,
}),
None => {
false => {
anyhow::bail!("did not find infohash")
}
}
@ -45,15 +65,35 @@ impl Magnet {
impl std::fmt::Display for Magnet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"magnet:?xt=urn:btih:{}&tr={}",
self.info_hash.as_string(),
self.trackers.join("&tr=")
)
if let (Some(id20), Some(id32)) = (self.id20, self.id32) {
write!(
f,
"magnet:?xt=urn:btih:{}?xt=urn:btmh:1220{}&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)]
mod tests {
#[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";
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));
}
}

View file

@ -1,4 +1,4 @@
use crate::id20::Id20;
use crate::hash_id::Id20;
#[derive(Debug)]
pub enum AzureusStyleKind {
@ -55,5 +55,5 @@ pub fn generate_peer_id() -> Id20 {
peer_id[..8].copy_from_slice(b"-rQ0001-");
Id20(peer_id)
Id20::new(peer_id)
}

View file

@ -7,7 +7,7 @@ use clone_to_owned::CloneToOwned;
use itertools::Either;
use serde::{Deserialize, Serialize};
use crate::id20::Id20;
use crate::hash_id::Id20;
pub type TorrentMetaV1Borrowed<'a> = TorrentMetaV1<ByteBuf<'a>>;
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);
de.is_torrent_info = true;
let mut t = TorrentMetaV1::deserialize(&mut de)?;
t.info_hash = Id20(
t.info_hash = Id20::new(
de.torrent_info_digest
.ok_or_else(|| anyhow::anyhow!("programming error"))?,
);