use librqbit_core::hash_id::Id to parse info-hash from string

This commit is contained in:
yggverse 2025-08-07 14:33:15 +03:00
parent 3e94c483d6
commit a3e3cb0897
4 changed files with 12 additions and 12 deletions

View file

@ -20,7 +20,7 @@ pub fn magnet(info_hash: &str, trackers: Option<&Vec<url::Url>>) -> String {
let mut b = if info_hash.len() == 40 { let mut b = if info_hash.len() == 40 {
format!("magnet:?xt=urn:btih:{info_hash}") format!("magnet:?xt=urn:btih:{info_hash}")
} else { } else {
todo!("info-hash v2 yet not supported") todo!("info-hash v2 yet not supported") // librqbit_core::hash_id::Id
}; };
if let Some(t) = trackers { if let Some(t) = trackers {
for tracker in t { for tracker in t {

View file

@ -73,7 +73,7 @@ fn index(
.map(|t| t.format(&meta.format_time).to_string()), .map(|t| t.format(&meta.format_time).to_string()),
indexed: torrent.time.format(&meta.format_time).to_string(), indexed: torrent.time.format(&meta.format_time).to_string(),
magnet: format::magnet(&torrent.info_hash, meta.trackers.as_ref()), magnet: format::magnet(&torrent.info_hash, meta.trackers.as_ref()),
scrape: scraper.scrape(torrent.info_hash.as_bytes()), scrape: scraper.scrape(&torrent.info_hash),
size: format::bytes(torrent.size), size: format::bytes(torrent.size),
files: torrent.files.as_ref().map_or("1 file".into(), |f| { files: torrent.files.as_ref().map_or("1 file".into(), |f| {
let l = f.len(); let l = f.len();

View file

@ -1,7 +1,7 @@
mod udp; mod udp;
use rocket::serde::Serialize; use rocket::serde::Serialize;
use std::net::SocketAddr; use std::{net::SocketAddr, str::FromStr};
use udp::Udp; use udp::Udp;
#[derive(Serialize, Default)] #[derive(Serialize, Default)]
@ -24,14 +24,13 @@ impl Scraper {
} }
} }
pub fn scrape(&self, info_hash: &[u8]) -> Option<Scrape> { pub fn scrape(&self, info_hash: &str) -> Option<Scrape> {
self.udp.as_ref()?; self.udp.as_ref()?;
if info_hash.len() != 40 {
todo!("info-hash v2 yet not supported")
}
let mut t = Scrape::default(); let mut t = Scrape::default();
if let Some(ref u) = self.udp { if let Some(ref u) = self.udp {
let r = u.scrape(info_hash).ok()?; // @TODO handle let r = u
.scrape(librqbit_core::Id20::from_str(info_hash).ok()?)
.ok()?; // @TODO handle
t.leechers += r.leechers; t.leechers += r.leechers;
t.peers += r.peers; t.peers += r.peers;
t.seeders += r.seeders; t.seeders += r.seeders;

View file

@ -1,4 +1,5 @@
use super::Scrape; use super::Scrape;
use librqbit_core::hash_id::Id20;
use rand::Rng; use rand::Rng;
use std::{ use std::{
io::Error, io::Error,
@ -36,7 +37,7 @@ impl Udp {
) )
} }
pub fn scrape(&self, info_hash: &[u8]) -> Result<Scrape, Error> { pub fn scrape(&self, info_hash: Id20) -> Result<Scrape, Error> {
let mut t = Scrape::default(); let mut t = Scrape::default();
for route in &self.0 { for route in &self.0 {
for remote in &route.remote { for remote in &route.remote {
@ -50,7 +51,7 @@ impl Udp {
&scrape_request( &scrape_request(
u64::from_be_bytes(b[8..16].try_into().unwrap()), u64::from_be_bytes(b[8..16].try_into().unwrap()),
rand::rng().random::<u32>(), rand::rng().random::<u32>(),
&[info_hash.to_vec()], &[info_hash],
), ),
remote, remote,
)?; )?;
@ -76,7 +77,7 @@ fn connection_request() -> Vec<u8> {
b b
} }
fn scrape_request(connection_id: u64, transaction_id: u32, info_hashes: &[Vec<u8>]) -> Vec<u8> { fn scrape_request(connection_id: u64, transaction_id: u32, info_hashes: &[Id20]) -> Vec<u8> {
let mut b = Vec::new(); let mut b = Vec::new();
b.extend_from_slice(&connection_id.to_be_bytes()); b.extend_from_slice(&connection_id.to_be_bytes());
b.extend_from_slice(&2u32.to_be_bytes()); b.extend_from_slice(&2u32.to_be_bytes());
@ -87,7 +88,7 @@ fn scrape_request(connection_id: u64, transaction_id: u32, info_hashes: &[Vec<u8
todo!() todo!()
} }
for hash in info_hashes { for hash in info_hashes {
b.extend_from_slice(hash); b.extend_from_slice(&hash.0);
} }
b b
} }