From c750cbbe20dde7b5f488beeaddd5ddaf2bb98854 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 26 Sep 2023 17:31:49 +0100 Subject: [PATCH] fix: [#226] user torrust-serde-bencode Using the crate `torrust-serde-bencode` instead og the upstream repo fixed the problem with some torrents deserialization. There was a problem with list (nodes) containing other list (tuples) with different value types. ```json { "info": { "length": 8, "name": "minimal.txt", "piece length": 16384, "pieces": "30 9A 84 51 4A F1 09 D0 8C 34 42 11 26 67 7F 84 B3 23 4D 78" }, "nodes": [ [ "188.163.121.224", 56711 ], [ "162.250.131.26", 13386 ] ] } ``` --- Cargo.lock | 21 ++++++++++----------- Cargo.toml | 2 +- src/bin/parse_torrent.rs | 4 ++-- src/models/torrent_file.rs | 7 ++----- src/utils/parse_torrent.rs | 11 ++++++----- tests/common/contexts/torrent/fixtures.rs | 4 ++-- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f981ccf1..155caf97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2212,16 +2212,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde_bencode" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "934d8bdbaa0126dafaea9a8833424a211d9661897717846c6bb782349ca1c30d" -dependencies = [ - "serde", - "serde_bytes", -] - [[package]] name = "serde_bytes" version = "0.11.12" @@ -2837,7 +2827,6 @@ dependencies = [ "reqwest", "sailfish", "serde", - "serde_bencode", "serde_bytes", "serde_derive", "serde_json", @@ -2849,12 +2838,22 @@ dependencies = [ "thiserror", "tokio", "toml 0.7.8", + "torrust-serde-bencode", "tower-http", "urlencoding", "uuid", "which", ] +[[package]] +name = "torrust-serde-bencode" +version = "0.2.3" +source = "git+https://github.com/torrust/torrust-serde-bencode.git?rev=fd7bdce7c605204e0aa3245d6e4d6d9b4d6a2da5#fd7bdce7c605204e0aa3245d6e4d6d9b4d6a2da5" +dependencies = [ + "serde", + "serde_bytes", +] + [[package]] name = "tower" version = "0.4.13" diff --git a/Cargo.toml b/Cargo.toml index c632a817..0a7d8352 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ derive_more = "0.99" serde = { version = "1.0", features = ["rc"] } serde_derive = "1" serde_json = "1.0" -serde_bencode = "0.2.3" +torrust-serde-bencode = { git = "https://github.com/torrust/torrust-serde-bencode.git", rev = "fd7bdce7c605204e0aa3245d6e4d6d9b4d6a2da5" } serde_bytes = "0.11" urlencoding = "2.1" argon2 = "0.5" diff --git a/src/bin/parse_torrent.rs b/src/bin/parse_torrent.rs index ccef09a9..2aa32d70 100644 --- a/src/bin/parse_torrent.rs +++ b/src/bin/parse_torrent.rs @@ -5,9 +5,9 @@ use std::env; use std::fs::File; use std::io::{self, Read}; -use serde_bencode::de::from_bytes; -use serde_bencode::value::Value as BValue; use torrust_index_backend::utils::parse_torrent; +use torrust_serde_bencode::de::from_bytes; +use torrust_serde_bencode::value::Value as BValue; fn main() -> io::Result<()> { let args: Vec = env::args().collect(); diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs index 98e57b92..a5462a78 100644 --- a/src/models/torrent_file.rs +++ b/src/models/torrent_file.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; -use serde_bencode::ser; use serde_bytes::ByteBuf; use sha1::{Digest, Sha1}; +use torrust_serde_bencode::ser; use super::info_hash::InfoHash; use crate::utils::hex::{from_bytes, into_bytes}; @@ -12,7 +12,7 @@ pub struct Torrent { #[serde(default)] pub announce: Option, #[serde(default)] - pub nodes: Option>, + pub nodes: Option>, #[serde(default)] pub encoding: Option, #[serde(default)] @@ -30,9 +30,6 @@ pub struct Torrent { pub created_by: Option, } -#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] -pub struct TorrentNode(String, i64); - #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub struct TorrentInfoDictionary { pub name: String, diff --git a/src/utils/parse_torrent.rs b/src/utils/parse_torrent.rs index 69e69011..c528f6e4 100644 --- a/src/utils/parse_torrent.rs +++ b/src/utils/parse_torrent.rs @@ -2,9 +2,9 @@ use std::error; use derive_more::{Display, Error}; use serde::{self, Deserialize, Serialize}; -use serde_bencode::value::Value; -use serde_bencode::{de, Error}; use sha1::{Digest, Sha1}; +use torrust_serde_bencode::value::Value; +use torrust_serde_bencode::{de, Error}; use crate::models::info_hash::InfoHash; use crate::models::torrent_file::Torrent; @@ -73,7 +73,7 @@ pub fn decode_torrent(bytes: &[u8]) -> Result> { /// /// This function will return an error if unable to bencode torrent. pub fn encode_torrent(torrent: &Torrent) -> Result, Error> { - match serde_bencode::to_bytes(torrent) { + match torrust_serde_bencode::to_bytes(torrent) { Ok(bencode_bytes) => Ok(bencode_bytes), Err(e) => { eprintln!("{e:?}"); @@ -100,10 +100,11 @@ struct ParsedInfoDictFromMetainfoFile { pub fn calculate_info_hash(bytes: &[u8]) -> Result { // Extract the info dictionary let metainfo: ParsedInfoDictFromMetainfoFile = - serde_bencode::from_bytes(bytes).map_err(|_| DecodeTorrentFileError::InvalidInfoDictionary)?; + torrust_serde_bencode::from_bytes(bytes).map_err(|_| DecodeTorrentFileError::InvalidInfoDictionary)?; // Bencode the info dictionary - let info_dict_bytes = serde_bencode::to_bytes(&metainfo.info).map_err(|_| DecodeTorrentFileError::CannotBencodeInfoDict)?; + let info_dict_bytes = + torrust_serde_bencode::to_bytes(&metainfo.info).map_err(|_| DecodeTorrentFileError::CannotBencodeInfoDict)?; // Calculate the SHA-1 hash of the bencoded info dictionary let mut hasher = Sha1::new(); diff --git a/tests/common/contexts/torrent/fixtures.rs b/tests/common/contexts/torrent/fixtures.rs index 223f8419..afa4650b 100644 --- a/tests/common/contexts/torrent/fixtures.rs +++ b/tests/common/contexts/torrent/fixtures.rs @@ -223,8 +223,8 @@ impl TestTorrentWithCustomInfoField { } } - pub fn encode(torrent: &Self) -> Result, serde_bencode::Error> { - match serde_bencode::to_bytes(torrent) { + pub fn encode(torrent: &Self) -> Result, torrust_serde_bencode::Error> { + match torrust_serde_bencode::to_bytes(torrent) { Ok(bencode_bytes) => Ok(bencode_bytes), Err(e) => { eprintln!("{e:?}");